Skip to content

Chat Agents Example

This example demonstrates how to create a chat system with automatic conversation summarization. It shows how to combine a chat agent for interaction with a summary agent that maintains a concise record of the conversation.

Overview

The Chat Agents example shows how to: - Create a friendly chat interface - Implement automatic conversation summarization - Manage conversation history - Chain multiple agents for enhanced functionality

Implementation

Here's the complete implementation:

from yosrai import Agent, Action, ContextFunctions
import agents_chat_prompts

# Create chat agent
chat_agent = Agent(
    agent_code='chat_agent',
    agent_name='Chat Agent',
    instructions_template='You are AI Friend, you should chat friendly and reply to the user input, do not exceed {{ reply_words }} words in your reply',
    prompt_template='{{ query }}',
    outputs={'output': ContextFunctions.DEFAULT, 'messages': ContextFunctions.APPEND_MESSAGES}
)

# Create summary agent
summary_agent = Agent(
    agent_code='summary_agent',
    agent_name='Summary Agent',
    instructions_template='Your main job is to summarize the conversation so far, do not exceed {{ summary_words }} words in your summary',
    prompt_template='''
    {% if messages|length > 2 %}
    {% if summary=='' %}
    Create a summary of the Conversation Messages, You have to summarize and return
    summary, Summary output in maximum of {{ summary_words }} words

    Conversation Messages:
    {% for msg in messages %}
    {{ msg.role }}: {{ msg.content }}
    {% endfor %}
    {% else %}
    Extend the following Current Summary by taking into account the following New Messages,
    You have to summarize and return summary, Summary output in maximum of {{ summary_words }} words

    Summary:
    {{ summary }}

    New Messages:
    {% for msg in messages %}
    {{ msg.role }}: {{ msg.content }}
    {% endfor %}
    {% endif %}
    {% endif %}
    ''',
    outputs={'summary': ContextFunctions.DEFAULT, 'messages': ContextFunctions.LATEST}
)

# Set up the workflow
action = Action('My Action')
action.Context(query='', reply_words=30, summary_words=50, summary='')
action.add_agent(chat_agent)
action.add_agent(summary_agent)
action.add_link(chat_agent, summary_agent)
action.add_link(summary_agent, "END")

Workflow Visualization

The workflow shows how the chat and summary agents work together:

---
title: Chat and Summarize
---
graph LR
START((Start)) --> chat_agent[Chat Agent]
chat_agent(Chat Agent) --> summary_agent(Summary Agent)
summary_agent(Summary Agent) --> END((End))

Key Components

Chat Agent Configuration

The chat agent is configured to: - Provide friendly, conversational responses - Maintain a word limit for responses - Append messages to conversation history

chat_agent = Agent(
    agent_code='chat_agent',
    agent_name='Chat Agent',
    instructions_template='You are AI Friend...',
    prompt_template='{{ query }}',
    outputs={'output': ContextFunctions.DEFAULT, 'messages': ContextFunctions.APPEND_MESSAGES}
)

Summary Agent Configuration

The summary agent is designed to: - Monitor conversation length - Create initial summaries - Update existing summaries with new content - Maintain concise summary length

summary_agent = Agent(
    agent_code='summary_agent',
    agent_name='Summary Agent',
    instructions_template='Your main job is to summarize...',
    prompt_template='{% if messages|length > 2 %}...',
    outputs={'summary': ContextFunctions.DEFAULT, 'messages': ContextFunctions.LATEST}
)

Context Management

The context is initialized with parameters for both agents:

action.Context(query='', reply_words=30, summary_words=50, summary='')

Workflow Definition

The chat system workflow is defined by: 1. Adding both agents to the action 2. Creating a link from chat to summary agent 3. Creating a link from summary agent to END

action.add_agent(chat_agent)
action.add_agent(summary_agent)
action.add_link(chat_agent, summary_agent)
action.add_link(summary_agent, "END")

Example Interaction

Here's a sample interaction:

User: "Hi! Can you tell me about yourself?"

Chat Agent: "Hello! I'm an AI friend here to chat with you. I enjoy friendly conversations and can discuss various topics while keeping things concise and engaging."

Summary Agent: "Initial interaction where user asks about the AI's identity. AI responds with a friendly introduction, emphasizing its role as a conversational partner."

User: "What are your hobbies?"

Chat Agent: "I enjoy learning from conversations, exploring new ideas, and helping others. While I don't have physical hobbies, I love intellectual discussions and creative exchanges!"

Summary Agent: "Conversation covers introductions and discussion about AI's interests, focusing on intellectual and conversational activities. The interaction maintains a friendly and engaging tone."

Best Practices

When implementing the Chat Agents pattern:

  1. Conversation Flow: Maintain natural dialogue progression
  2. Summary Relevance: Keep summaries focused on key points
  3. Word Limits: Set appropriate limits for both chat and summary
  4. Context Preservation: Ensure important details are retained in summaries
  5. Response Style: Maintain consistent tone and personality

Use Cases

This pattern is ideal for: - Long-running chat applications - Customer service systems - Educational chatbots - Meeting summarization - Documentation assistants

Next Steps

After implementing the Chat Agents pattern, you can: 1. Add sentiment analysis 2. Implement topic categorization 3. Add memory management for long conversations 4. Enhance summary generation with key points extraction 5. Add multi-language support