This repository has been archived by the owner on May 18, 2023. It is now read-only.
forked from seanpixel/Teenage-AGI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
183 lines (147 loc) · 6.89 KB
/
api.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from pydantic import BaseModel
import uvicorn
import os
from dotenv import load_dotenv
import logging
from typing import Dict, Any
from replacement_chains import Agent
import json
CANNED_RESPONSES=False
# Set up logging
logging.basicConfig(
level=logging.INFO, # Set the logging level (e.g., DEBUG, INFO, WARNING, ERROR, CRITICAL)
format="%(asctime)s [%(levelname)s] %(message)s", # Set the log message format
)
logger = logging.getLogger(__name__)
from dotenv import load_dotenv
# def establish_connection():
# AGENT_NAME = os.getenv("AGENT_NAME") or "my-agent"
#
# agent = Agento(AGENT_NAME)
# agent.createIndex()
# return agent
load_dotenv()
app = FastAPI(debug=True)
class Payload(BaseModel):
payload: Dict[str, Any]
class ImageResponse(BaseModel):
success: bool
message: str
@app.get("/")
async def root():
return {"message": "Hello, World, I am alive!"}
@app.post("/variate-diet-assumption", response_model=dict)
async def variate_diet_assumption(request_data: Payload) -> dict:
json_payload = request_data.payload
agent_instance =Agent()
agent_instance.set_user_session(json_payload["user_id"], json_payload["session_id"])
output = agent_instance.update_agent_preferences(str(json_payload['variate_assumption']))
stripped_string_dict = {"response": output}
# Return a JSON response with the new dictionary
return JSONResponse(content=stripped_string_dict)
@app.post("/variate-food-goal", response_model=dict)
async def variate_food_goal(request_data: Payload) -> dict:
json_payload = request_data.payload
factors_dict = {factor['name']: factor['amount'] for factor in json_payload['factors']}
agent = Agent()
agent.set_user_session(json_payload["user_id"], json_payload["session_id"])
output = agent.goal_optimization(str(json_payload['variate_goal']), model_speed="slow")
start = output.find('{')
end = output.rfind('}')
if start != -1 and end != -1:
stripped_string_output = output[start:end + 1]
print(stripped_string_output)
else:
print("No JSON data found in string.")
stripped_string_dict = {"response": stripped_string_output}
# Return a JSON response with the new dictionary
return JSONResponse(content=stripped_string_dict)
@app.post("/recipe-request", response_model=dict)
async def recipe_request(request_data: Payload) -> dict:
if CANNED_RESPONSES:
with open('fixtures/recipe_response.json', 'r') as f:
json_data = json.load(f)
stripped_string_dict = {"response": json_data}
return JSONResponse(content=stripped_string_dict)
json_payload = request_data.payload
factors_dict = {factor['name']: factor['amount'] for factor in json_payload['factors']}
agent = Agent()
agent.set_user_session(json_payload["user_id"], json_payload["session_id"])
output = agent.recipe_generation(factors_dict, model_speed="slow")
return JSONResponse(content={"response":{"recipes": json.loads(output)}});
@app.post("/restaurant-request", response_model=dict)
async def restaurant_request(request_data: Payload) -> dict:
json_payload = request_data.payload
factors_dict = {factor['name']: factor['amount'] for factor in json_payload['factors']}
agent = Agent()
agent.set_user_session(json_payload["user_id"], json_payload["session_id"])
output = agent.restaurant_generation(factors_dict, model_speed="slow")
return JSONResponse(content={"response":{"restaurants": output}});
@app.post("/delivery-request", response_model=dict)
async def delivery_request(request_data: Payload) -> dict:
json_payload = request_data.payload
factors_dict = {factor['name']: factor['amount'] for factor in json_payload['factors']}
agent = Agent()
agent.set_user_session(json_payload["user_id"], json_payload["session_id"])
output = await agent.delivery_generation(factors_dict, zipcode=json_payload["zipcode"], model_speed="slow")
print("HERE IS THE OUTPUT", output)
return JSONResponse(content={"response": {"url": output}})
@app.post("/solution-request", response_model=dict)
async def solution_request(request_data: Payload) -> dict:
json_payload = request_data.payload
factors_dict = {factor['name']: factor['amount'] for factor in json_payload['factors']}
agent = Agent()
agent.set_user_session(json_payload["user_id"], json_payload["session_id"])
output = agent.solution_generation(factors_dict, model_speed=json_payload["model_speed"])
start = output.find('{')
end = output.rfind('}')
if start != -1 and end != -1:
stripped_string_output = output[start:end + 1]
print(stripped_string_output)
else:
print("No JSON data found in string.")
stripped_string_dict = {"response": stripped_string_output}
# Return a JSON response with the new dictionary
return JSONResponse(content=stripped_string_dict)
@app.post("/generate-diet-goal", response_model=dict)
async def generate_diet_goal(request_data: Payload) -> dict:
if CANNED_RESPONSES:
with open('fixtures/goal_response.json', 'r') as f:
json_data = json.load(f)
stripped_string_dict = {"response": json_data}
return JSONResponse(content=stripped_string_dict)
json_payload = request_data.payload
agent = Agent()
agent.set_user_session(json_payload["user_id"], json_payload["session_id"])
output = agent.goal_generation({}, model_speed= json_payload["model_speed"])
return JSONResponse(content={"response":json.loads(output)})
@app.post("/generate-diet-sub-goal", response_model=dict)
async def generate_diet_sub_goal(request_data: Payload) -> dict:
if CANNED_RESPONSES:
with open('fixtures/subgoal_response.json', 'r') as f:
json_data = json.load(f)
stripped_string_dict = {"response": json_data}
# Return a JSON response with the new dictionary
return JSONResponse(content=stripped_string_dict)
json_payload = request_data.payload
agent = Agent()
agent.set_user_session(json_payload["user_id"], json_payload["session_id"])
output = agent.sub_goal_generation(factors=json_payload["factors"], model_speed= json_payload["model_speed"])
return JSONResponse(content={"response":json.loads(output)})
@app.post("/voice-input", response_model=dict)
async def voice_input(request_data: Payload) -> dict:
json_payload = request_data.payload
agent = Agent()
agent.set_user_session(json_payload["user_id"], json_payload["session_id"])
output = agent.voice_input(query=json_payload["query"], model_speed= json_payload["model_speed"])
return JSONResponse(content={"response":json.loads(output)})
@app.get("/health")
def health_check():
return {"status": "OK"}
def start_api_server():
# agent = establish_connection()
uvicorn.run(app, host="0.0.0.0", port=8000)
if __name__ == "__main__":
start_api_server()