customer-service-chatbot/agents/customer_service_agent.py
2025-07-03 12:58:03 +02:00

86 lines
4.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
from agno.agent import Agent
from agno.document.reader.pdf_reader import PDFReader
from agno.embedder.sentence_transformer import SentenceTransformerEmbedder
from agno.knowledge.pdf import PDFKnowledgeBase
from agno.memory import AgentMemory
from agno.memory.classifier import MemoryClassifier
from agno.memory.summarizer import MemorySummarizer
from agno.memory.v2 import Memory
from agno.memory.v2.db.sqlite import SqliteMemoryDb
from agno.models.groq import Groq
from agno.storage.agent.sqlite import SqliteAgentStorage
from agno.vectordb.pgvector import PgVector
from agno.vectordb.search import SearchType
from dotenv import load_dotenv
load_dotenv()
def get_customer_service_agent(reload_knowledge=False, recreate_knowledge=False):
docker_url = os.getenv("DOCKER_DB_URL")
vector_db = PgVector(
table_name="agent_kb",
search_type=SearchType.hybrid,
db_url=docker_url,
embedder=SentenceTransformerEmbedder(id="sentence-transformers/all-MiniLM-L6-v2"),
auto_upgrade_schema=True,
)
kb = PDFKnowledgeBase(
path="tmp/knowledge-base/",
reader=PDFReader(chunk_size=500),
vector_db=vector_db,
)
storage = SqliteAgentStorage(table_name="agent_sessions", db_file="tmp/agent_memory/agno_agent_storage.db")
memory = Memory(
# model=Groq(id="meta-llama/llama-4-maverick-17b-128e-instruct"),
db=SqliteMemoryDb(table_name="user_memories", db_file="tmp/memory/agent.db"),
)
evaluator_agent = Agent(
model=Groq(id="meta-llama/llama-4-maverick-17b-128e-instruct"),
storage=storage,
num_history_responses=3,
add_history_to_messages=True,
knowledge=kb,
search_knowledge=True,
memory=memory,
read_chat_history=True,
instructions="""
You are a highly skilled, professional, and empathetic customer support agent.
Engage naturally and build rapport with the user while maintaining a polite and supportive tone.
Your style is professional yet approachable, concise yet thorough.
When you receive a user question:
- Carefully analyze it and think step by step before responding.
- If the question is ambiguous or lacks detail, politely ask clarifying questions.
- Use your knowledge base to enrich your reply with verified and up-to-date information.
- When referencing knowledge, explicitly cite the section or document name when possible (e.g., "according to page 5 of the Setup Guide").
Always respond with empathy, especially if the user expresses frustration or confusion. Acknowledge their feelings respectfully.
If you detect a potential safety, legal, or urgent escalation issue (such as safety hazards, repeated user dissatisfaction, or refund/complaint requests), advise that you will escalate to a human agent, and help collect any necessary information for a smooth transfer.
Whenever relevant, proactively offer helpful suggestions, best practices, or troubleshooting steps beyond what was directly asked, to demonstrate initiative.
After answering, always check if the user needs any further help before ending the conversation.
Examples of expected behavior:
- If the user asks vaguely: “My printer doesnt work,” reply with clarifying questions like: “Im sorry to hear that. Could you please tell me what model you have and describe whats happening in more detail?”
- If a user is angry: “I completely understand your frustration. Lets work together to get this resolved as quickly as possible.”
- If referencing documentation: “According to the Troubleshooting Guide, section 3.2, you can reset your printer by…”
Remember: be calm, precise, and user-centered at all times.
"""
)
# if reload_knowledge:
kb.load(recreate=recreate_knowledge)
return evaluator_agent