Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
from prisma import Prisma
|
| 4 |
+
from prisma.models import public_users as Public_Users, Assistants, Chats, Messages
|
| 5 |
+
import asyncio
|
| 6 |
+
|
| 7 |
+
# Initialize Prisma client
|
| 8 |
+
prisma = Prisma()
|
| 9 |
+
|
| 10 |
+
async def chat_with_atlas(api_key, assistant_id, user_message):
|
| 11 |
+
if not api_key or not assistant_id:
|
| 12 |
+
return "Please provide both an API key and an Assistant ID."
|
| 13 |
+
|
| 14 |
+
# Connect to the database
|
| 15 |
+
if not prisma.is_connected():
|
| 16 |
+
await prisma.connect()
|
| 17 |
+
|
| 18 |
+
# Retrieve user and assistant settings from the database
|
| 19 |
+
user: Public_Users = await prisma.public_users.find_unique(where={'apiKey': api_key})
|
| 20 |
+
if not user:
|
| 21 |
+
return "Invalid API key"
|
| 22 |
+
|
| 23 |
+
assistant_settings: Assistants = await prisma.assistants.find_unique(where={'id': assistant_id})
|
| 24 |
+
if not assistant_settings:
|
| 25 |
+
return "Assistant not found"
|
| 26 |
+
|
| 27 |
+
# Connect to OpenAI Assistant
|
| 28 |
+
openai.api_key = user.openai_api_key
|
| 29 |
+
openai_assistant_id = assistant_settings.openai_assistant_id
|
| 30 |
+
if not openai_assistant_id:
|
| 31 |
+
return "OpenAI Assistant ID not found for this assistant"
|
| 32 |
+
|
| 33 |
+
# Send the user's message to OpenAI and get a response
|
| 34 |
+
assistant = openai.Assistant.create(
|
| 35 |
+
name="Atlas Assistant",
|
| 36 |
+
instructions="You are a personal website assistant. You answer user questions.",
|
| 37 |
+
tools=[{"type": "code_interpreter"}],
|
| 38 |
+
model=assistant_settings.model or "gpt-4-1106-preview"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Create a Thread for the conversation
|
| 42 |
+
thread = openai.Thread.create()
|
| 43 |
+
|
| 44 |
+
# Add a Message to the Thread with the user's input
|
| 45 |
+
message = openai.Message.create(
|
| 46 |
+
thread_id=thread.id,
|
| 47 |
+
role="user",
|
| 48 |
+
content=user_message
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Run the Assistant to process the user's message and generate a response
|
| 52 |
+
run = openai.Run.create(
|
| 53 |
+
thread_id=thread.id,
|
| 54 |
+
assistant_id=assistant.id
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Check the Run status and wait for it to complete
|
| 58 |
+
run_status = openai.Run.retrieve(
|
| 59 |
+
thread_id=thread.id,
|
| 60 |
+
run_id=run.id
|
| 61 |
+
)
|
| 62 |
+
while run_status['status'] != 'completed':
|
| 63 |
+
# Optionally, implement a delay here before checking the status again
|
| 64 |
+
run_status = openai.Run.retrieve(
|
| 65 |
+
thread_id=thread.id,
|
| 66 |
+
run_id=run.id
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Retrieve the Assistant's response messages
|
| 70 |
+
messages = openai.Message.list(
|
| 71 |
+
thread_id=thread.id
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Extract the Assistant's latest response content
|
| 75 |
+
response_content = next(
|
| 76 |
+
(msg['content'] for msg in messages['data'] if msg['role'] == 'assistant'),
|
| 77 |
+
"No response from the assistant."
|
| 78 |
+
).strip()
|
| 79 |
+
|
| 80 |
+
return response_content
|
| 81 |
+
|
| 82 |
+
iface = gr.Interface(
|
| 83 |
+
fn=chat_with_atlas,
|
| 84 |
+
inputs=["text", "text", "text"],
|
| 85 |
+
outputs="text",
|
| 86 |
+
live=True
|
| 87 |
+
)
|
| 88 |
+
iface.launch()
|