Contexto De Modelo
Un contexto de modelo permite el almacenamiento y recuperación de mensajes de finalización de chat. Siempre se usa junto con un cliente de modelo para generar respuestas basadas en LLM.
Por ejemplo, BufferedChatCompletionContext
es un contexto de tipo MRU (más recientemente usado) que almacena el número más reciente de mensajes definido por buffer_size
. Esto es útil para evitar el desbordamiento de contexto en muchos LLMs.
Veamos un ejemplo que utiliza BufferedChatCompletionContext
.
from dataclasses import dataclass
from saptiva_agents import LLAMA_MODEL
from saptiva_agents.core import AgentId, MessageContext, RoutedAgent, SingleThreadedAgentRuntime, message_handler, BufferedChatCompletionContext
from saptiva_agents.models import AssistantMessage, SystemMessage, UserMessage
from saptiva_agents.base import SaptivaAIChatCompletionClient
@dataclass
class Message:
content: str
class SimpleAgentWithContext(RoutedAgent):
def __init__(self, model_client: SaptivaAIChatCompletionClient) -> 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:
# Preparar entrada para el modelo de finalización de chat.
user_message = UserMessage(content=message.content, source="user")
# Agregar mensaje al contexto del modelo.
await self._model_context.add_message(user_message)
# Generar una respuesta.
response = await self._model_client.create(
self._system_messages + (await self._model_context.get_messages()),
cancellation_token=ctx.cancellation_token,
)
# Retornar la respuesta del modelo.
assert isinstance(response.content, str)
# Agregar respuesta al contexto del modelo.
await self._model_context.add_message(AssistantMessage(content=response.content, source=self.metadata["type"]))
return Message(content=response.content)
Ahora intentemos hacer preguntas de seguimiento después de la primera.
model_client = SaptivaAIChatCompletionClient(
model=LLAMA_MODEL,
api_key="TU_SAPTIVA_API_KEY",
)
runtime = SingleThreadedAgentRuntime()
await SimpleAgentWithContext.register(
runtime,
"simple_agent_context",
lambda: SimpleAgentWithContext(model_client=model_client),
)
# Iniciar el procesamiento de mensajes del runtime.
runtime.start()
agent_id = AgentId("simple_agent_context", "default")
# Primera pregunta.
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("-----")
# Segunda pregunta.
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}")
# Detener el procesamiento de mensajes del runtime.
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.
Como puedes ver en la segunda respuesta, ahora el agente puede recordar sus propias respuestas anteriores.
Última actualización