01_AutoGen初识

42 阅读1分钟

什么是AutoGen

AutoGen是微软推出的一个AI框架,主要功能是可以通过创建多个AI智能体(agents)来协同工作,共同完成任务

地址链接

microsoft.github.io/autogen/sta…

示例代码

import asyncio

from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.ui import Console
from autogen_core.models import ModelFamily, ModelInfo
from autogen_ext.models.openai import OpenAIChatCompletionClient

# Define a model client. You can use other model client that implements
# the `ChatCompletionClient` interface.
model_client = OpenAIChatCompletionClient(
    model="kimi-k2-turbo-preview",
    # api_key="YOUR_API_KEY",
    api_key="******",
    base_url="https://api.moonshot.cn/v1",
    model_info=ModelInfo(
        vision=False,
        function_calling=True,
        json_output=True,
        structured_output=True,
        family=ModelFamily.UNKNOWN,
    ),
)


# Define a simple function tool that the agent can use.
# For this example, we use a fake weather tool for demonstration purposes.
async def get_weather(city: str) -> str:
    """Get the weather for a given city."""
    return f"The weather in {city} is 73 degrees and Sunny."


# Define an AssistantAgent with the model, tool, system message, and reflection enabled.
# The system message instructs the agent via natural language.
agent = AssistantAgent(
    name="weather_agent",
    model_client=model_client,
    tools=[get_weather],
    system_message="你是一名专业的天气预测专家",
    reflect_on_tool_use=True,
    model_client_stream=True,  # Enable streaming tokens from the model client.
)


# Run the agent and stream the messages to the console.
async def main1() -> None:
    await Console(agent.run_stream(task="广州今天天气怎么样"))
    # Close the connection to the model client.
    await model_client.close()



if __name__ == "__main__":
    asyncio.run(main1())

上面是我们参考官网的models自己写的基础代码

官网示例代码如下

image.png

可以得出结论 在我们自己的OpenAiChatCompletionClient(OpenAI聊天补全客户端)方法里需要传入要用的model,api_key,base_url。 如果我们的model非谷歌或者gem模型 想用自定义国产一类的模型的话 需要额外传入model_info 来描述自定义的国产模型