autogen_ext.models.semantic_kernel#
- class SKChatCompletionAdapter(sk_client: ChatCompletionClientBase, kernel: Kernel | None = None, prompt_settings: PromptExecutionSettings | None = None, model_info: ModelInfo | None = None, service_id: str | None = None)[源代码]#
-
SKChatCompletionAdapter 是一个适配器,允许使用 Semantic Kernel 模型客户端作为 Autogen ChatCompletion 客户端。 这使得能够无缝地将 Semantic Kernel 连接器(例如,Azure OpenAI、Google Gemini、Ollama 等)集成到依赖 ChatCompletionClient 接口的 Autogen 代理中。
通过利用此适配器,您可以
传入 Kernel 和任何受支持的 Semantic Kernel ChatCompletionClientBase 连接器。
在聊天完成期间提供工具(通过 Autogen Tool 或 ToolSchema)用于函数调用。
流式传输响应或在单个请求中检索它们。
- 提供提示设置以通过构造函数全局控制聊天完成行为
或通过 extra_create_args 字典按请求控制。
可以安装的扩展列表
semantic-kernel-anthropic: 安装此扩展以使用 Anthropic 模型。
semantic-kernel-google: 安装此扩展以使用 Google Gemini 模型。
semantic-kernel-ollama: 安装此扩展以使用 Ollama 模型。
semantic-kernel-mistralai: 安装此扩展以使用 MistralAI 模型。
semantic-kernel-aws: 安装此扩展以使用 AWS 模型。
semantic-kernel-hugging-face: 安装此扩展以使用 Hugging Face 模型。
- 参数:
sk_client (ChatCompletionClientBase) – 要包装的 Semantic Kernel 客户端(例如,AzureChatCompletion、GoogleAIChatCompletion、OllamaChatCompletion)。
kernel (Optional[Kernel]) – 用于执行请求的 Semantic Kernel 实例。 如果未提供,则必须在每个请求的 extra_create_args 中传入一个。
prompt_settings (Optional[PromptExecutionSettings]) – 要使用的默认提示执行设置。 可以按请求覆盖。
model_info (Optional[ModelInfo]) – 有关模型功能的信息。
service_id (Optional[str]) – 可选服务标识符。
示例
具有函数调用的 Anthropic 模型
pip install "autogen-ext[semantic-kernel-anthropic]"
import asyncio import os from autogen_agentchat.agents import AssistantAgent from autogen_agentchat.ui import Console from autogen_core.models import ModelFamily, UserMessage from autogen_ext.models.semantic_kernel import SKChatCompletionAdapter from semantic_kernel import Kernel from semantic_kernel.connectors.ai.anthropic import AnthropicChatCompletion, AnthropicChatPromptExecutionSettings from semantic_kernel.memory.null_memory import NullMemory async def get_weather(city: str) -> str: """Get the weather for a city.""" return f"The weather in {city} is 75 degrees." async def main() -> None: sk_client = AnthropicChatCompletion( ai_model_id="claude-3-5-sonnet-20241022", api_key=os.environ["ANTHROPIC_API_KEY"], service_id="my-service-id", # Optional; for targeting specific services within Semantic Kernel ) settings = AnthropicChatPromptExecutionSettings( temperature=0.2, ) model_client = SKChatCompletionAdapter( sk_client, kernel=Kernel(memory=NullMemory()), prompt_settings=settings, model_info={ "function_calling": True, "json_output": True, "vision": True, "family": ModelFamily.CLAUDE_3_5_SONNET, "structured_output": True, }, ) # Call the model directly. response = await model_client.create([UserMessage(content="What is the capital of France?", source="test")]) print(response) # Create an assistant agent with the model client. assistant = AssistantAgent( "assistant", model_client=model_client, system_message="You are a helpful assistant.", tools=[get_weather] ) # Call the assistant with a task. await Console(assistant.run_stream(task="What is the weather in Paris and London?")) asyncio.run(main())
具有函数调用的 Google Gemini 模型
pip install "autogen-ext[semantic-kernel-google]"
import asyncio import os from autogen_agentchat.agents import AssistantAgent from autogen_agentchat.ui import Console from autogen_core.models import UserMessage, ModelFamily from autogen_ext.models.semantic_kernel import SKChatCompletionAdapter from semantic_kernel import Kernel from semantic_kernel.connectors.ai.google.google_ai import ( GoogleAIChatCompletion, GoogleAIChatPromptExecutionSettings, ) from semantic_kernel.memory.null_memory import NullMemory def get_weather(city: str) -> str: """Get the weather for a city.""" return f"The weather in {city} is 75 degrees." async def main() -> None: sk_client = GoogleAIChatCompletion( gemini_model_id="gemini-2.0-flash", api_key=os.environ["GEMINI_API_KEY"], ) settings = GoogleAIChatPromptExecutionSettings( temperature=0.2, ) kernel = Kernel(memory=NullMemory()) model_client = SKChatCompletionAdapter( sk_client, kernel=kernel, prompt_settings=settings, model_info={ "family": ModelFamily.GEMINI_2_0_FLASH, "function_calling": True, "json_output": True, "vision": True, "structured_output": True, }, ) # Call the model directly. model_result = await model_client.create( messages=[UserMessage(content="What is the capital of France?", source="User")] ) print(model_result) # Create an assistant agent with the model client. assistant = AssistantAgent( "assistant", model_client=model_client, tools=[get_weather], system_message="You are a helpful assistant." ) # Call the assistant with a task. stream = assistant.run_stream(task="What is the weather in Paris and London?") await Console(stream) asyncio.run(main())
Ollama 模型
pip install "autogen-ext[semantic-kernel-ollama]"
import asyncio from autogen_agentchat.agents import AssistantAgent from autogen_core.models import UserMessage from autogen_ext.models.semantic_kernel import SKChatCompletionAdapter from semantic_kernel import Kernel from semantic_kernel.connectors.ai.ollama import OllamaChatCompletion, OllamaChatPromptExecutionSettings from semantic_kernel.memory.null_memory import NullMemory async def main() -> None: sk_client = OllamaChatCompletion( host="http://localhost:11434", ai_model_id="llama3.2:latest", ) ollama_settings = OllamaChatPromptExecutionSettings( options={"temperature": 0.5}, ) model_client = SKChatCompletionAdapter( sk_client, kernel=Kernel(memory=NullMemory()), prompt_settings=ollama_settings ) # Call the model directly. model_result = await model_client.create( messages=[UserMessage(content="What is the capital of France?", source="User")] ) print(model_result) # Create an assistant agent with the model client. assistant = AssistantAgent("assistant", model_client=model_client) # Call the assistant with a task. result = await assistant.run(task="What is the capital of France?") print(result) asyncio.run(main())
- actual_usage() RequestUsage [源代码]#
- count_tokens(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = []) int [源代码]#
- async create(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = [], json_output: bool | type[BaseModel] | None = None, extra_create_args: Mapping[str, Any] = {}, cancellation_token: CancellationToken | None = None) CreateResult [source]#
使用 Semantic Kernel 客户端创建一个聊天补全。
extra_create_args 字典可以包含两个特殊键
- “kernel” (可选)
semantic_kernel.Kernel
的一个实例,用于执行请求。 如果在构造函数或 extra_create_args 中未提供,则会引发 ValueError。
- “prompt_execution_settings” (可选)
一个
PromptExecutionSettings
子类的实例,对应于底层的 Semantic Kernel 客户端(例如,AzureChatPromptExecutionSettings, GoogleAIChatPromptExecutionSettings)。如果未提供,将使用适配器的默认提示设置。
- 参数:
messages – 要发送的 LLM 消息列表。
tools – 聊天期间可能调用的工具。
json_output – 模型是否应返回 JSON。
extra_create_args – 控制聊天补全行为的附加参数。
cancellation_token – 允许取消请求的令牌。
- 返回值:
CreateResult – 聊天补全的结果。
- async create_stream(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = [], json_output: bool | type[BaseModel] | None = None, extra_create_args: Mapping[str, Any] = {}, cancellation_token: CancellationToken | None = None) AsyncGenerator[str | CreateResult, None] [source]#
使用 Semantic Kernel 客户端创建一个流式聊天补全。
extra_create_args 字典可以包含两个特殊键
- “kernel” (可选)
semantic_kernel.Kernel
的一个实例,用于执行请求。 如果在构造函数或 extra_create_args 中未提供,则会引发 ValueError。
- “prompt_execution_settings” (可选)
一个
PromptExecutionSettings
子类的实例,对应于底层的 Semantic Kernel 客户端(例如,AzureChatPromptExecutionSettings, GoogleAIChatPromptExecutionSettings)。如果未提供,将使用适配器的默认提示设置。
- 参数:
messages – 要发送的 LLM 消息列表。
tools – 聊天期间可能调用的工具。
json_output – 模型是否应返回 JSON。
extra_create_args – 控制聊天补全行为的附加参数。
cancellation_token – 允许取消请求的令牌。
- 产生:
Union[str, CreateResult] – 响应的字符串块或包含函数调用的 CreateResult。
- remaining_tokens(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = []) int [source]#
- total_usage() RequestUsage [source]#