-
Notifications
You must be signed in to change notification settings - Fork 36
/
routes.py
169 lines (125 loc) · 4.58 KB
/
routes.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from flask_app.flask_app import app, db
from flask import request, jsonify,send_from_directory
# from flask import Flask, request, jsonify
from agents import *
from retrievers import *
from flask_app.db_models import Response,Query
from llama_index.core import Document
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.core import VectorStoreIndex
from llama_index.core import Settings
df = pd.read_csv('Housing.csv')
desc="Housing Dataset"
dict_ = make_data(df,desc)
doc = [str(dict_)]
def initiatlize_retrievers(_styling_instructions, _doc):
retrievers ={}
style_index = VectorStoreIndex.from_documents([Document(text=x) for x in _styling_instructions])
retrievers['style_index'] = style_index
retrievers['dataframe_index'] = VectorStoreIndex.from_documents([Document(text=x) for x in _doc])
return retrievers
retrievers = initiatlize_retrievers(styling_instructions,doc)
AVAILABLE_AGENTS = {
"data_viz_agent":data_viz_agent,
"sk_learn_agent":sk_learn_agent,
"statistical_analytics_agent":statistical_analytics_agent,
"preprocessing_agent":preprocessing_agent
}
ai_system = auto_analyst(agents=list(AVAILABLE_AGENTS.values()),retrievers=retrievers)
@app.route('/upload_dataframe', methods=['POST'])
def upload_dataframe():
data = request.get_json()
df = pd.read_csv(data['file'])
retrievers = initiatlize_retrievers(data['styling_instructions'], df)
return jsonify({"message": "Dataframe uploaded successfully"}), 200
# Get all queries
@app.route('/queries', methods=['GET'])
def get_queries():
queries = Query.query.order_by(Query.created_at.desc()).all()
return jsonify([query.to_json() for query in queries])
# Get query by id
@app.route('/queries/<int:id>', methods=['GET'])
def get_query(id):
query = Query.query.get_or_404(id)
return jsonify(query.to_json())
# Get all responses
@app.route('/responses', methods=['GET'])
def get_responses():
responses = Response.query.order_by(Response.created_at.desc()).all()
return jsonify([response.to_json() for response in responses])
# Get responses by query id
@app.route('/responses/query/<int:query_id>', methods=['GET'])
def get_responses_by_query(query_id):
responses = Response.query.filter_by(query_id=query_id).order_by(Response.created_at.desc()).all()
return jsonify([response.to_json() for response in responses])
# Chat with specific agent
@app.route('/chat/<agent_name>', methods=['POST'])
def chat_with_agent(agent_name):
data = request.json
query_text = data.get('query')
# Save query
query = Query(query=query_text)
db.session.add(query)
db.session.commit()
# Generate response using agent
if agent_name in AVAILABLE_AGENTS:
agent = AVAILABLE_AGENTS[agent_name]()
response_text = agent.generate_response(query_text)
# Save response
response = Response(
agent_name=agent_name,
query=query_text,
response=response_text
)
db.session.add(response)
db.session.commit()
return jsonify(response.to_json()), 201
else:
return jsonify({"error": "Agent not found"}), 404
# Chat with all agents
@app.route('/chat', methods=['POST'])
def chat_with_all():
"""
Example request body in Postman:
Send as JSON (raw) with Content-Type: application/json header
"""
data = request.json
query_text = data.get('query')
print(query_text)
# Save query
query = Query(query=query_text)
db.session.add(query)
db.session.commit()
responses = []
# Get response from each agent
response_text = ai_system(query_text)
# Save response
response = Response(
id=db.session.query(Response).count() + 1,
agent_name="ai_system",
query=query_text,
response=str(response_text),
created_at=db.func.now()
)
db.session.add(response)
db.session.commit()
return jsonify([response.to_json() for response in responses]), 201
@app.route('/health', methods=['GET'])
def health():
return jsonify({"message": "Hello World"}), 200
@app.route('/')
def index():
return jsonify({
"title": "Welcome to the API",
"message": "Hello World",
"colors": {
"primary": "#007bff",
"secondary": "#6c757d",
"success": "#28a745",
"danger": "#dc3545"
}
}), 200
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon')
print("WORKING")