-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_api.py
241 lines (188 loc) · 7.29 KB
/
run_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import json
import os
from typing import List
from io import BytesIO
import uvicorn
from dotenv import load_dotenv
from fastapi import FastAPI, Query, UploadFile, File, Body, HTTPException, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from datetime import datetime
from supabase import create_client
import PyPDF2
from server.api import BlackSpaceAPI
# Load environment variables
load_dotenv()
# Access environment variables
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
CORS_ORIGINS = ["http://localhost:3000", "https://blackspace-ai.vercel.app"]
CORS_METHODS = ["GET", "POST"]
# Initialize FastAPI app
app = FastAPI()
# Configure CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=CORS_ORIGINS,
allow_credentials=True,
allow_methods=CORS_METHODS,
allow_headers=["*"],
)
from fastapi import Header, HTTPException, Depends
class AuthenticatedResponse(BaseModel):
message: str
def get_user_from_key(authorization: str) -> None:
if not authorization:
raise HTTPException(status_code=401, detail="Unauthorized")
response = supabase_client.table("users").select("*").eq("key", authorization).execute()
return response.data[0]
@app.get("/")
async def say_hello():
return {"message": "Hello World"}
class MessageList(BaseModel):
session_id: str
human_say: str
@app.post("/chat/{chat_id}")
async def chat_with_sales_agent(chat_id, session_id: str = Body(None), human_say: str = Body(...), stream: bool = Query(False), file: UploadFile = File(None)):
sales_api = None
user = get_user_from_key(chat_id)
if user is None:
raise HTTPException(status_code=401, detail="Unauthorized")
print(user["id"])
if session_id is None:
new_session_payload = {
"user_id": user["id"],
"created_at": datetime.now().isoformat(),
"updated_at": datetime.now().isoformat()
}
new_session = supabase_client.table("sessions").insert(new_session_payload).execute()
session_id = new_session.data[0]["id"]
conversations = supabase_client.table("conversations").select("*").eq("session_id", session_id).limit(20).execute()
conversations_history = []
for conversation in conversations.data:
conversations_history.append(conversation["text"])
sales_api = BlackSpaceAPI(
config_path=user["config"],
verbose=True,
product_catalog=user["products"],
model_name=os.getenv("GPT_MODEL", "gpt-3.5-turbo-0613"),
use_tools=os.getenv("USE_TOOLS_IN_API", "True").lower()
in ["true", "1", "t"],
conversation_history=conversations_history
)
extracted_text = ""
if file and file.filename.endswith(".pdf"):
file_content = await file.read()
pdf_reader = PyPDF2.PdfFileReader(BytesIO(file_content))
num_pages = pdf_reader.numPages
for page_num in range(num_pages):
page = pdf_reader.getPage(page_num)
extracted_text += page.extractText()
human_input = "User: " + human_say + extracted_text + " <END_OF_TURN>"
new_user_conversation = {
"session_id": session_id,
"text": human_input,
"type": "human",
"created_at": datetime.now().isoformat(),
"updated_at": datetime.now().isoformat()
}
supabase_client.table("conversations").insert(new_user_conversation).execute()
if stream:
async def stream_response():
stream_gen = sales_api.do_stream(conversations_history, human_say + extracted_text)
async for message in stream_gen:
data = {"token": message}
yield json.dumps(data).encode("utf-8") + b"\n"
return StreamingResponse(stream_response())
else:
response = await sales_api.do(human_say + extracted_text)
new_ai_conversation_payload = {
"session_id": session_id,
"text": response["reply"],
"type": "ai",
"created_at": datetime.now().isoformat(),
"updated_at": datetime.now().isoformat()
}
supabase_client.table("conversations").insert(new_ai_conversation_payload).execute()
response["session_id"] = session_id
return response
supabase_url = os.getenv("SUPABASE_URL")
supabase_key = os.getenv("SUPABASE_KEY")
supabase_client = create_client(supabase_url, supabase_key)
class ConversationCreate(BaseModel):
session_id: int
text: str
type: str
class UserCreate(BaseModel):
name: str
config: dict
products: str
class SessionCreate(BaseModel):
user_id: int
# Conversation API Endpoints
@app.post("/conversations/", status_code=status.HTTP_201_CREATED)
def create_conversation(conversation_data: ConversationCreate):
data = {
"session_id": conversation_data.session_id,
"text": conversation_data.text,
"type": conversation_data.type,
"created_at": datetime.now().isoformat(),
"updated_at": datetime.now().isoformat()
}
try:
response = supabase_client.table("conversations").insert(data).execute()
return response
except Exception as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
@app.get("/conversations/{session_id}/")
def read_conversation(session_id: int):
try:
response = supabase_client.table("conversations").select("*").eq("session_id", session_id).execute()
return response
except Exception as e:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e))
# User API Endpoints
@app.post("/users/", status_code=status.HTTP_201_CREATED)
def create_user(user_data: UserCreate):
data = {
"name": user_data.name,
"config": user_data.config,
"products": user_data.products,
"created_at": datetime.now().isoformat(),
"updated_at": datetime.now().isoformat()
}
try:
response = supabase_client.table("users").insert(data).execute()
return response
except Exception as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
@app.get("/users/{user_id}/")
def read_user(user_id: int):
try:
response = supabase_client.table("users").select("*").eq("id", user_id).execute()
return response
except Exception as e:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e))
# Session API Endpoints
@app.post("/sessions/", status_code=status.HTTP_201_CREATED)
def create_session(session_data: SessionCreate):
data = {
"user_id": session_data.user_id,
"created_at": datetime.now().isoformat(),
"updated_at": datetime.now().isoformat()
}
try:
response = supabase_client.table("sessions").insert(data).execute()
return response
except Exception as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
@app.get("/sessions/{session_id}/")
def read_session(session_id: int):
try:
response = supabase_client.table("sessions").select("*").eq("id", session_id).execute()
return response
except Exception as e:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e))
# Main entry point
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)