Modellkontext#
Ein Modellkontext unterstützt die Speicherung und den Abruf von Chat Completion-Nachrichten. Er wird immer zusammen mit einem Modellclient verwendet, um LLM-basierte Antworten zu generieren.
Zum Beispiel ist BufferedChatCompletionContext ein Most-Recent-Used (MRU)-Kontext, der die neuesten buffer_size Nachrichten speichert. Dies ist nützlich, um bei vielen LLMs ein Überlaufen des Kontexts zu vermeiden.
Lassen Sie uns ein Beispiel betrachten, das BufferedChatCompletionContext verwendet.
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)
Versuchen wir nun, nach der ersten Frage weitere Folgefragen zu stellen.
model_client = OpenAIChatCompletionClient(
model="gpt-4o-mini",
# api_key="sk-...", # Optional if you have an OPENAI_API_KEY set in the environment.
)
runtime = SingleThreadedAgentRuntime()
await SimpleAgentWithContext.register(
runtime,
"simple_agent_context",
lambda: SimpleAgentWithContext(model_client=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()
await model_client.close()
Question: Hello, what are some fun things to do in Seattle?
Response: Seattle offers a variety of fun activities and attractions. Here are some highlights:
1. **Pike Place Market**: Visit this iconic market to explore local vendors, fresh produce, artisanal products, and watch the famous fish throwing.
2. **Space Needle**: Take a trip to the observation deck for stunning panoramic views of the city, Puget Sound, and the surrounding mountains.
3. **Chihuly Garden and Glass**: Marvel at the stunning glass art installations created by artist Dale Chihuly, located right next to the Space Needle.
4. **Seattle Waterfront**: Enjoy a stroll along the waterfront, visit the Seattle Aquarium, and take a ferry ride to nearby islands like Bainbridge Island.
5. **Museum of Pop Culture (MoPOP)**: Explore exhibits on music, science fiction, and pop culture in this architecturally striking building.
6. **Seattle Art Museum (SAM)**: Discover an extensive collection of art from around the world, including contemporary and Native American art.
7. **Gas Works Park**: Relax in this unique park that features remnants of an old gasification plant, offering great views of the Seattle skyline and Lake Union.
8. **Discovery Park**: Enjoy nature trails, beaches, and beautiful views of the Puget Sound and the Olympic Mountains in this large urban park.
9. **Ballard Locks**: Watch boats navigate the locks and see fish swimming upstream during the salmon migration season.
10. **Fremont Troll**: Check out this quirky public art installation under a bridge in the Fremont neighborhood.
11. **Underground Tour**: Take an entertaining guided tour through the underground passages of Pioneer Square to learn about Seattle's history.
12. **Brewery Tours**: Seattle is known for its craft beer scene. Visit local breweries for tastings and tours.
13. **Seattle Center**: Explore the cultural complex that includes the Space Needle, MoPOP, and various festivals and events throughout the year.
These are just a few options, and Seattle has something for everyone, whether you're into outdoor activities, culture, history, or food!
-----
Question: What was the first thing you mentioned?
Response: The first thing I mentioned was **Pike Place Market**. It's an iconic market in Seattle known for its local vendors, fresh produce, artisanal products, and the famous fish throwing by the fishmongers. It's a vibrant place full of sights, sounds, and delicious food.
Aus der zweiten Antwort können Sie sehen, dass der Agent nun seine eigenen vorherigen Antworten wieder abrufen kann.