-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
85 lines (66 loc) · 2.39 KB
/
main.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
import os
from openai import OpenAI
from typing import Union
from fastapi import FastAPI, HTTPException
from time import sleep
from dotenv import load_dotenv
# Import functions
from gpt_api import types, functions
# Initialize FastAPI
app = FastAPI()
# Load environment variables
load_dotenv()
# Initialize OpenAI client
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
if OPENAI_API_KEY is None:
raise HTTPException("OPENAI_API_KEY environment variable is not set")
client = OpenAI(api_key=OPENAI_API_KEY)
# Create or load assistant
assistant_id = functions.create_assistant(
client
) # this function comes from 'functions.py'
# Root route
@app.get("/")
async def read_root():
return {"message": "🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀"}
# Start conversation thread
@app.get("/start")
async def start_conversation():
print("Starting a new conversation...") # Debugging line
thread = client.beta.threads.create()
print(f"New thread created with ID: {thread.id}") # Debugging line
return {"thread_id": thread.id}
# Generate response
@app.post("/chat")
async def chat(dialogueSnippet: types.DialogueSnippet):
dialogue = dialogueSnippet.dict()
thread_id = dialogue.get("thread_id")
user_input = dialogue.get("message", "")
if not thread_id:
print("Error: Missing thread_id") # Debugging line
return {"error": "Missing thread_id"}, 400
print(
f"Received message: {user_input} for thread ID: {thread_id}"
) # Debugging line
# Add the user's message to the thread
client.beta.threads.messages.create(
thread_id=thread_id, role="user", content=user_input
)
# Run the Assistant
run = client.beta.threads.runs.create(
thread_id=thread_id, assistant_id=assistant_id
)
# Check if the Run requires action (function call)
while True:
run_status = client.beta.threads.runs.retrieve(
thread_id=thread_id, run_id=run.id
)
print(f"Run status: {run_status.status}")
if run_status.status == "completed":
break
sleep(1) # Wait for a second before checking again
# Retrieve and return the latest message from the assistant
messages = client.beta.threads.messages.list(thread_id=thread_id)
response = messages.data[0].content[0].text.value
print(f"Assistant response: {response}") # Debugging line
return {"response": response}