-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwith-data.py
101 lines (81 loc) · 3.37 KB
/
with-data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from phi.agent import Agent, RunResponse
from phi.knowledge.json import JSONKnowledgeBase
from phi.vectordb.pgvector import PgVector
from flask import Flask, Response, request
from dotenv import load_dotenv
# from toolkit import CoinGeckoToolkit
from coingeckoTool import CoinGeckoTools
# Load environment variables from .env file
load_dotenv()
knowledge_base = JSONKnowledgeBase(
path="./data/data.json",
# Table name: ai.json_documents
vector_db=PgVector(
table_name="json_documents",
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
),
num_documents=10,
)
# Register the tools with the Agent
agent = Agent(
knowledge=knowledge_base,
search_knowledge=True,
# tools=[CoinGeckoToolkit()],
tools=[CoinGeckoTools()],
show_tool_calls=True,
# debug_mode=True,
instructions=[
"First you have to find the correct `id` of the coin user is trying to search",
"look for unnecessay words that are part of the question and not part of the coin name, discard those words and find the accurate word for the coin",
"Sanitize the user input before searching for the id in the knowledge, remove unnecessay words, spaces with hipiens",
"Look throughly for the exact coin `id` in the knowledge exactly matching the user input",
"The user input should match the the id, symbol and name in the knowledge, query for all the fields and find out which one matches the most",
"The id should match exactly with the user input",
"If exact not found, find the related results",
"Dont ask for the users confirmation, search using those related results you have found",
"also detect the currency in the user input, convert the currency in lowercase and pass it to the CoinGeckoToolkit functions",
"Search for the token price by passing the id as a argument in the CoinGeckoToolkit functions",
"Execute the actions for those results using the CoinGeckoToolkit functions",
],
)
agent.knowledge.load(recreate=False)
# Example usage
# agent.print_response("What is the price of baby-Trump in EUR?", stream=True)
# agent.print_response("What is the price of Trump in EUR?", stream=True)
# agent.print_response("What is the price of Pepe in INR?", stream=True)
# agent.print_response("What is the price of MAGA Trump in USD?", stream=True)
# agent.print_response("What is the price of Bitcoin in USD?", stream=True)
# agent.print_response("Tell me the market cap for bitcoin", stream=True)
# agent.print_response("Tell me about bitcoin", stream=True)
# agent.print_response("Tell me about pepe coin", stream=True)
# agent.print_response(
# "Tell me about latest trump coin that is created by Donuld trump", stream=True
# )
# flask app
app = Flask(__name__)
@app.route("/")
def index():
return Response(
"Hello",
content_type="text/html",
)
@app.route("/stream")
def home():
question = request.args.get("question")
def generate():
response: RunResponse = (
agent.run(
question,
stream=True,
),
)
(gen,) = response
while True:
try:
response = next(gen)
yield str(response.content)
except StopIteration:
break
return generate(), {"Content-Type": "text/plain"}
if __name__ == "__main__":
app.run(debug=True)