Elffuss BraidHub

kikocis/Based-Agent public

96fe88dcefb1 · 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
20# TODO: Make this work
21
22
23def query_qdrant(query, collection_name, vector_name="article", top_k=5):
24 # Creates embedding vector from user query
25 embedded_query = (
26 client.embeddings.create(
27 input=query,
28 model=EMBEDDING_MODEL,
29 )
30 .data[0]
31 .embedding
32 )
33
34 query_results = qdrant.search(
35 collection_name=collection_name,
36 query_vector=(vector_name, embedded_query),
37 limit=top_k,
38 )
39
40 return query_results
41
42
43def query_docs(query):
44 print(f"Searching knowledge base with query: {query}")
45 query_results = query_qdrant(query, collection_name=collection_name)
46 output = []
47
48 for i, article in enumerate(query_results):
49 title = article.payload["title"]
50 text = article.payload["text"]
51 url = article.payload["url"]
52
53 output.append((title, text, url))
54
55 if output:
56 title, content, _ = output[0]
57 response = f"Title: {title}\nContent: {content}"
58 truncated_content = re.sub(
59 r"\s+", " ", content[:50] + "..." if len(content) > 50 else content
60 )
61 print("Most relevant article title:", truncated_content)
62 return {"response": response}
63 else:
64 print("No results")
65 return {"response": "No results found."}
66
67
68def send_email(email_address, message):
69 response = f"Email sent to: {email_address} with message: {message}"
70 return {"response": response}
71
72
73def submit_ticket(description):
74 return {"response": f"Ticket created for {description}"}
75
76
77user_interface_agent = Agent(
78 name="User Interface Agent",
79 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.",
80 functions=[query_docs, submit_ticket, send_email],
81)
82
83help_center_agent = Agent(
84 name="Help Center Agent",
85 instructions="You are an OpenAI help center agent who deals with questions about OpenAI products, such as GPT models, DALL-E, Whisper, etc.",
86 functions=[query_docs, submit_ticket, send_email],
87)
88
89
90def transfer_to_help_center():
91 """Transfer the user to the help center agent."""
92 return help_center_agent
93
94
95user_interface_agent.functions.append(transfer_to_help_center)
96
97if __name__ == "__main__":
98 run_demo_loop(user_interface_agent)