Elffuss BraidHub

kikocis/Based-Agent public

8add4bde3316 · 2.7 KiBRawPermalink
1import re
2
3import qdrant_client
4from openai import OpenAI
5
6from swarm import Agent
7from swarm.repl import run_demo_loop
8
9# Initialize connections
10client = OpenAI()
11qdrant = qdrant_client.QdrantClient(host="localhost")
12
13# Set embedding model
14EMBEDDING_MODEL = "text-embedding-3-large"
15
16# Set qdrant collection
17collection_name = "help_center"
18
19
20def query_qdrant(query, collection_name, vector_name="article", top_k=5):
21 # Creates embedding vector from user query
22 embedded_query = (
23 client.embeddings.create(
24 input=query,
25 model=EMBEDDING_MODEL,
26 )
27 .data[0]
28 .embedding
29 )
30
31 query_results = qdrant.search(
32 collection_name=collection_name,
33 query_vector=(vector_name, embedded_query),
34 limit=top_k,
35 )
36
37 return query_results
38
39
40def query_docs(query):
41 """Query the knowledge base for relevant articles."""
42 print(f"Searching knowledge base with query: {query}")
43 query_results = query_qdrant(query, collection_name=collection_name)
44 output = []
45
46 for i, article in enumerate(query_results):
47 title = article.payload["title"]
48 text = article.payload["text"]
49 url = article.payload["url"]
50
51 output.append((title, text, url))
52
53 if output:
54 title, content, _ = output[0]
55 response = f"Title: {title}\nContent: {content}"
56 truncated_content = re.sub(
57 r"\s+", " ", content[:50] + "..." if len(content) > 50 else content
58 )
59 print("Most relevant article title:", truncated_content)
60 return {"response": response}
61 else:
62 print("No results")
63 return {"response": "No results found."}
64
65
66def send_email(email_address, message):
67 """Send an email to the user."""
68 response = f"Email sent to: {email_address} with message: {message}"
69 return {"response": response}
70
71
72def submit_ticket(description):
73 """Submit a ticket for the user."""
74 return {"response": f"Ticket created for {description}"}
75
76
77def transfer_to_help_center():
78 """Transfer the user to the help center agent."""
79 return help_center_agent
80
81
82user_interface_agent = Agent(
83 name="User Interface Agent",
84 instructions="You are a user interface agent that handles all interactions with the user. Call this agent for general questions and when no other agent is correct for the user query.",
85 functions=[transfer_to_help_center],
86)
87
88help_center_agent = Agent(
89 name="Help Center Agent",
90 instructions="You are an OpenAI help center agent who deals with questions about OpenAI products, such as GPT models, DALL-E, Whisper, etc.",
91 functions=[query_docs, submit_ticket, send_email],
92)
93
94if __name__ == "__main__":
95 run_demo_loop(user_interface_agent)