Autogen_core: Model Context

news/2025/2/1 4:01:56 标签: 人工智能, agent

目录

    • 示例代码
    • 代码解释
    • 另一个例子

示例代码

from dataclasses import dataclass

from autogen_core import AgentId, MessageContext, RoutedAgent, SingleThreadedAgentRuntime, message_handler
from autogen_core.model_context import BufferedChatCompletionContext
from autogen_core.models import AssistantMessage, ChatCompletionClient, SystemMessage, UserMessage
from autogen_ext.models.openai import OpenAIChatCompletionClient
@dataclass
class Message:
    content: str
class SimpleAgentWithContext(RoutedAgent):
    def __init__(self, model_client: ChatCompletionClient) -> None:
        super().__init__("A simple agent")
        self._system_messages = [SystemMessage(content="You are a helpful AI assistant.")]
        self._model_client = model_client
        self._model_context = BufferedChatCompletionContext(buffer_size=5)

    @message_handler
    async def handle_user_message(self, message: Message, ctx: MessageContext) -> Message:
        # Prepare input to the chat completion model.
        user_message = UserMessage(content=message.content, source="user")
        # Add message to model context.
        await self._model_context.add_message(user_message)
        # Generate a response.
        response = await self._model_client.create(
            self._system_messages + (await self._model_context.get_messages()),
            cancellation_token=ctx.cancellation_token,
        )
        # Return with the model's response.
        assert isinstance(response.content, str)
        # Add message to model context.
        await self._model_context.add_message(AssistantMessage(content=response.content, source=self.metadata["type"]))
        return Message(content=response.content)
runtime = SingleThreadedAgentRuntime()

model_client = OpenAIChatCompletionClient(
                    model="GLM-4-Air-0111",
                    api_key = "your api key",
                    base_url="https://open.bigmodel.cn/api/paas/v4/",
                    model_capabilities={
                "vision": True,
                "function_calling": True,
                "json_output": True,
            }
                    )

await SimpleAgentWithContext.register(
    runtime,
    "simple_agent_context",
    lambda: SimpleAgentWithContext(model_client),
)
# Start the runtime processing messages.
runtime.start()
agent_id = AgentId("simple_agent_context", "default")

# First question.
message = Message("Hello, what are some fun things to do in Seattle?")
print(f"Question: {message.content}")
response = await runtime.send_message(message, agent_id)
print(f"Response: {response.content}")
print("-----")

# Second question.
message = Message("What was the first thing you mentioned?")
print(f"Question: {message.content}")
response = await runtime.send_message(message, agent_id)
print(f"Response: {response.content}")

# Stop the runtime processing messages.
await runtime.stop()
Question: Hello, what are some fun things to do in Seattle?
Response: Seattle is a vibrant city with a lot to offer! Here are some fun things to do, catering to different interests:

**For the Outdoorsy Type:**

*   **Visit Pike Place Market:** This iconic public market is a must-see. Watch fish mongers throw fish, sample local produce, and grab a bite to eat.
*   **Explore the Seattle Great Wheel:** Enjoy stunning views of Elliott Bay and the city skyline from this Ferris wheel on the waterfront.
*   **Take a walk or bike ride along the Seattle Waterfront:** Enjoy the fresh air, views of the Puget Sound, and attractions like the Seattle Aquarium.
*   **Hike the Chirico Trail at Discovery Park:** This 2.8-mile loop offers beautiful views of Puget Sound and the Olympic Mountains.
*   **Go kayaking or stand-up paddleboarding:** Rent a vessel and explore the calm waters of Lake Union or the more adventurous Puget Sound.
*   **Visit the Japanese Garden at the Seattle Arboretum:** Stroll through this serene 3.5-acre garden featuring winding paths, water features, and traditional Japanese landscaping.
*   **Take a day trip to one of the nearby mountains:** Mount Rainier, Mount Baker, and the Cascades offer incredible hiking, scenic drives, and wildlife viewing opportunities.

**For the Culture Enthusiast:**

*   **Explore the Museum of Pop Culture (MoPOP):** This museum dedicated to contemporary popular culture is a fun and interactive experience, with exhibits on music, science fiction, and fantasy.
*   **Visit the Seattle Art Museum (SAM):** Admire a diverse collection of art from around the world, including Native American art and contemporary works.
*   **See a show at the 5th Avenue Theatre or the Paramount Theatre:** Catch a Broadway show or a concert in one of these historic venues.
*   **Explore the historic Pioneer Square district:** This area features Victorian-era architecture, art galleries, and unique shops.
*   **Visit the Ballard Locks (Hiram M. Chittenden Locks):** Watch boats pass between Puget Sound and the Ship Canal, and enjoy the surrounding park.
*   **Attend a festival or event:** Seattle hosts numerous festivals throughout the year, including the Seattle International Film Festival, Bumbershoot, and the Capitol Hill Block Party.

**For the Foodie:**

*   **Enjoy a coffee at a local cafe:** Seattle is the birthplace of Starbucks, but you'll find many independent coffee shops offering delicious brews.
*   **Try some fresh seafood:** From salmon to Dungeness crab, Seattle is known for its fresh seafood. Canlis, The Pink Door, and Ivar's Acres of Clams are great options.
*   **Explore the diverse culinary scene:** Seattle has a wide range of cuisines, from Vietnamese pho to Ethiopian injera. Explore the International District for a variety of options.
*   **Visit a food truck pod:** Food truck pods are a great way to sample a variety of cuisines in one location. South Lake Union and Capitol Hill have several pods.

**For the Music Lover:**

*   **Visit the Experience Music Project (now part of MoPOP):** This museum celebrates the history of music, with a focus on Seattle's grunge scene.
*   **See a live music show:** Seattle has a thriving music scene, with venues like The Crocodile, Neumos, and The Showbox hosting shows almost every night.
*   **Explore the Music Row district on 5th Avenue:** This area is home to several music venues, record stores, and other music-related businesses.

**Unique Seattle Experiences:**

*   **Ride the monorail:** This historic monorail connects downtown Seattle to the Seattle Center.
*   **Visit the Seattle Center:** This park, built for the 1962 World's Fair, features the Space Needle, the International Fountain, and other attractions.
*   **Take a Bill Speidel's Underground Tour:** Explore the hidden passages and history beneath the city streets.
*   **Go on a whale watching tour:** Several companies offer tours to see orcas and other marine wildlife in Puget Sound.

**Tips for Planning Your Trip:**

*   **Best time to visit:** Spring and fall offer pleasant weather and fewer crowds. Summer can be warm and sunny, but also crowded and expensive.
*   **Getting around:** Seattle has a good public transportation system, including buses, light rail, and streetcars. Ride-sharing services are also readily available.
*   **Book accommodations and activities in advance:** Especially during peak season.

This is just a starting point, and there's much more to discover in Seattle! To help me narrow down the options further, tell me:

*   **When are you planning to visit?**
*   **How long will you be in Seattle?**
*   **What are your interests (e.g., outdoors, food, art, music)?**
*   **Who
-----
Question: What was the first thing you mentioned?
Response: The first thing I mentioned in my response about fun things to do in Seattle was **Visit Pike Place Market**.

代码解释

这段代码使用了一个名为 autogen_core 的库,该库通常用于创建和运行基于AI的聊天代理。代码的目的是创建一个简单的代理,它能够上下文感知地回答用户的问题。下面是代码的逻辑和功能的详细解释:

  1. 导入必要的类和模块

    • dataclass 用于定义简单的数据结构。
    • autogen_core 中的 AgentIdMessageContextRoutedAgentSingleThreadedAgentRuntimemessage_handler 用于创建和管理代理。
    • BufferedChatCompletionContext 用于管理聊天上下文。
    • AssistantMessageChatCompletionClientSystemMessageUserMessage 用于构建和发送消息。
    • OpenAIChatCompletionClient 是一个用于与OpenAI模型进行交互的客户端。
  2. 定义 Message 数据类

    • 这是一个简单的数据结构,用于存储消息的内容。
  3. 创建 SimpleAgentWithContext

    • 这个类继承自 RoutedAgent,代表一个能够处理消息的代理。
    • 构造函数初始化代理的名称、系统消息列表、模型客户端和模型上下文。
    • handle_user_message 方法是一个异步消息处理器,它接收用户消息,将其添加到模型上下文中,并使用模型客户端生成响应。
    • 生成的响应被添加回模型上下文,并返回给用户。
  4. 初始化运行时环境和模型客户端

    • runtime 是一个 SingleThreadedAgentRuntime 实例,用于管理代理的运行。
    • model_client 是一个 OpenAIChatCompletionClient 实例,配置了特定的模型名称、API密钥和基本URL。
  5. 注册和启动代理

    • SimpleAgentWithContext 被注册到运行时环境中,并指定了一个构造函数。
    • 运行时环境被启动以开始处理消息。
  6. 发送和接收消息

    • 通过 runtime.send_message 方法发送两个问题给代理,并打印出问题和相应的回答。
    • 第一个问题是关于在Seattle的有趣活动。
    • 第二个问题是关于代理提到的第一个活动。
  7. 停止运行时环境

    • 一旦消息处理完成,运行时环境被停止。

完成的功能

  • 这段代码创建了一个能够理解上下文的聊天代理,它可以接收用户的问题,并根据之前的对话历史生成相关的回答。
  • 该代理使用了一个远程的AI模型(通过 OpenAIChatCompletionClient)来生成回答。
  • 通过将用户的每个问题和代理的回答存储在上下文中,代理能够理解对话的流程并提供更相关的回答。

请注意,为了使代码能够运行,需要确保 autogen_coreautogen_ext 库是可用的,并且 OpenAIChatCompletionClient 能够与指定的模型进行通信。此外,代码中使用了异步编程,这意味着它需要在支持异步操作的Python环境中运行。

另一个例子

from dataclasses import dataclass
from autogen_core import AgentId, MessageContext, RoutedAgent, SingleThreadedAgentRuntime, message_handler
from autogen_core.model_context import BufferedChatCompletionContext
from autogen_core.models import AssistantMessage, ChatCompletionClient, SystemMessage, UserMessage
from autogen_ext.models.openai import OpenAIChatCompletionClient

@dataclass
class Message:
    content: str

class CodeExplainerAgent(RoutedAgent):
    def __init__(self, model_client: ChatCompletionClient) -> None:
        super().__init__("A code explainer agent")
        self._system_messages = [SystemMessage(content="You are an AI assistant specialized in explaining Python code.")]
        self._model_client = model_client
        self._model_context = BufferedChatCompletionContext(buffer_size=5)

    @message_handler
    async def handle_user_message(self, message: Message, ctx: MessageContext) -> Message:
        user_message = UserMessage(content=message.content, source="user")
        await self._model_context.add_message(user_message)
        
        response = await self._model_client.create(
            self._system_messages + (await self._model_context.get_messages()),
            cancellation_token=ctx.cancellation_token,
        )
        
        assert isinstance(response.content, str)
        await self._model_context.add_message(AssistantMessage(content=response.content, source=self.metadata["type"]))
        return Message(content=response.content)

runtime = SingleThreadedAgentRuntime()

model_client = OpenAIChatCompletionClient(
    model="GLM-4-Air-0111",
    api_key="your api key",
    base_url="https://open.bigmodel.cn/api/paas/v4/",
    model_capabilities={
        "vision": True,
        "function_calling": True,
        "json_output": True,
    }
)

await CodeExplainerAgent.register(
    runtime,
    "code_explainer_agent",
    lambda: CodeExplainerAgent(model_client),
)

runtime.start()
agent_id = AgentId("code_explainer_agent", "default")

# 代码解释示例
message = Message("解释以下代码:\n```python\nimport numpy as np\nA = np.array([[1, 2], [3, 4]])\nprint(A.T)\n```")
print(f"Question: {message.content}")
response = await runtime.send_message(message, agent_id)
print(f"Response: {response.content}")
print("-----")

# 继续问上下文相关问题
message = Message("代码中的 `.T` 是什么意思?")
print(f"Question: {message.content}")
response = await runtime.send_message(message, agent_id)
print(f"Response: {response.content}")

await runtime.stop()

Question: 解释以下代码:
```python
import numpy as np
A = np.array([[1, 2], [3, 4]])
print(A.T)
```
Response: 这段代码首先导入了NumPy库,并将其命名为`np`。NumPy是一个用于科学计算的Python库,它提供了一个强大的N维数组对象和一系列用于快速操作数组的函数。

接着,代码创建了一个2x2的NumPy数组`A`,其元素为[[1, 2], [3, 4]]。

最后,代码使用`.T`属性来获取数组`A`的转置,并将其打印出来。`.T`属性是NumPy数组的一个特性,它返回数组的转置视图。对于二维数组,这意味着行和列将互换。

当运行这段代码时,输出将是:

```
[[1 3]
 [2 4]]
```

这显示了原始数组`A`的转置,其中第一行[1, 2]变成了第一列,第二行[3, 4]变成了第二列。
-----
Question: 代码中的 `.T` 是什么意思?
Response: 在 NumPy 中,`.T` 属性用于获取数组的转置。转置是一个矩阵操作,它将矩阵的行和列进行互换。例如,如果一个矩阵 `A` 的形状是 `(m, n)`,那么它的转置 `A.T` 的形状将是 `(n, m)`。

在您的代码示例中:

```python
import numpy as np
A = np.array([[1, 2], [3, 4]])
print(A.T)
```

`A` 是一个 2x2 的矩阵:

```
A = [[1, 2],
     [3, 4]]
```

应用 `.T` 属性后,我们得到 `A` 的转置:

```
A.T = [[1, 3],
       [2, 4]]
```

这里,原来的行变成了列,原来的列变成了行。

对于更高维度的数组,`.T` 同样适用,并且可以结合 `axes` 参数进行更复杂的转置操作,但那超出了您当前代码的范围。对于大多数矩阵操作,简单地使用 `.T` 就足够了。

参考链接:https://microsoft.github.io/autogen/stable/user-guide/core-user-guide/components/model-context.html


http://www.niftyadmin.cn/n/5839000.html

相关文章

JavaScript - Web APIs(下)

日期对象 目标:掌握日期对象,可以让网页显示日期 日期对象:用来表示时间的对象 作用:可以得到当前系统时间 学习路径: 实例化 日期对象方法 时间戳 实例化 目标:能够实例化日期对象 在代码中发…

[Java]泛型(一)泛型类

1. 什么是泛型类? 泛型类是指类中使用了占位符类型(类型参数)的类。通过使用泛型类,你可以编写可以处理多种数据类型的代码,而无需为每种类型编写单独的类。泛型类使得代码更具通用性和可重用性,同时可以保…

基于 Redis GEO 实现条件分页查询用户附近的场馆列表

🎯 本文档详细介绍了如何使用Redis GEO模块实现场馆位置的存储与查询,以支持“附近场馆”搜索功能。首先,通过微信小程序获取用户当前位置,并将该位置信息与场馆的经纬度数据一同存储至Redis中。利用Redis GEO高效的地理空间索引能…

goframe 多语言国际化解决方案

项目背景 本项目采用基于JSON配置的多语言国际化(i18n)解决方案,支持多种语言的无缝切换和本地化。 目录结构 manifest/ └── i18n/├── zh.json # 简体中文├── zh-tw.json # 繁体中文├── en.json # 英语├…

Vue3.0教程003:setup语法糖

文章目录 3.1 OptionsAPI与CompositionAPIOptions API的弊端Composition API的优势 3.2 拉开序幕的setup3.3 setup语法糖 3.1 OptionsAPI与CompositionAPI vue2的API设计是Options风格的vue3的API设计是Composition(组合)风格的 Options API的弊端 Opt…

【Python】深入探索Python元类:动态生成类与对象的艺术

《Python OpenCV从菜鸟到高手》带你进入图像处理与计算机视觉的大门! 解锁Python编程的无限可能:《奇妙的Python》带你漫游代码世界 元类是Python中一个高级且强大的特性,允许开发者在类的创建过程中插入自定义逻辑,从而动态生成类和对象。本文将全面介绍Python中的元类概…

【漫话机器学习系列】070.汉明损失(Hamming Loss)

汉明损失(Hamming Loss) 汉明损失是多标签分类问题中的一种评价指标,用于衡量预测结果与实际标签之间的差异。它定义为预测错误的标签比例,即错误标签的个数占总标签数量的比值。 在多标签分类中,每个样本可以属于多…

基于springboot+vue的扶贫助农系统的设计与实现

开发语言:Java框架:springbootJDK版本:JDK1.8服务器:tomcat7数据库:mysql 5.7(一定要5.7版本)数据库工具:Navicat11开发软件:eclipse/myeclipse/ideaMaven包:…