Spaces:
Build error
Build error
Create open.py
Browse files
open.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from groq_response import groq_response
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import os
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
client = OpenAI(api_key=os.getenv('OPENA_AI_API_KEY'))
|
| 10 |
+
prompt = st.chat_input("Type your question...")
|
| 11 |
+
my_assistant = client.beta.assistants.retrieve("asst_whBs56nCEId4lym1lgt392vS")
|
| 12 |
+
ass_id = "asst_whBs56nCEId4lym1lgt392vS"
|
| 13 |
+
|
| 14 |
+
def retrival_openai(prompt, instructions):
|
| 15 |
+
thread = client.beta.threads.create()
|
| 16 |
+
message = client.beta.threads.messages.create(
|
| 17 |
+
thread_id=thread.id,
|
| 18 |
+
role="user",
|
| 19 |
+
content=prompt
|
| 20 |
+
)
|
| 21 |
+
run = client.beta.threads.runs.create_and_poll(
|
| 22 |
+
thread_id=thread.id,
|
| 23 |
+
assistant_id=ass_id,
|
| 24 |
+
instructions=instructions
|
| 25 |
+
)
|
| 26 |
+
if run.status == 'completed':
|
| 27 |
+
messages = client.beta.threads.messages.list(
|
| 28 |
+
thread_id=thread.id
|
| 29 |
+
)
|
| 30 |
+
assistant_messages = [message for message in messages.data if message.role == 'assistant']
|
| 31 |
+
for message in assistant_messages:
|
| 32 |
+
for content_block in message.content:
|
| 33 |
+
# Check if there's a method or property to get 'value'
|
| 34 |
+
if hasattr(content_block.text, 'value'):
|
| 35 |
+
return content_block.text.value
|
| 36 |
+
else:
|
| 37 |
+
# Otherwise print the object to debug
|
| 38 |
+
#st.write(messages)
|
| 39 |
+
#st.write(content_block.text)
|
| 40 |
+
return None
|
| 41 |
+
else:
|
| 42 |
+
return None
|
| 43 |
+
|
| 44 |
+
|