-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
106 lines (89 loc) · 4 KB
/
app.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
import openai
import os
from dotenv import find_dotenv, load_dotenv
import time
import logging
from datetime import datetime
import json
import streamlit as st
load_dotenv()
# openai.api_key = os.environ.get("OPENAI_API_KEY")
# defaults to getting the key using os.environ.get("OPENAI_API_KEY")
# if you saved the key under a different environment variable name, you can do something like:
# client = OpenAI(
# api_key=os.environ.get("OPENAI_API_KEY"),
# )
client = openai.OpenAI()
model = "gpt-4-1106-preview" # "gpt-3.5-turbo-16k"
# Step 1. Upload a file to OpenaI embeddings ===
filepath = "./cryptocurrency.pdf"
file_object = client.files.create(file=open(filepath, "rb"), purpose="assistants")
# Step 2 - Create an assistant
# assistant = client.beta.assistants.create(
# name="Studdy Buddy",
# instructions="""You are a helpful study assistant who knows a lot about understanding research papers.
# Your role is to summarize papers, clarify terminology within context, and extract key figures and data.
# Cross-reference information for additional insights and answer related questions comprehensively.
# Analyze the papers, noting strengths and limitations.
# Respond to queries effectively, incorporating feedback to enhance your accuracy.
# Handle data securely and update your knowledge base with the latest research.
# Adhere to ethical standards, respect intellectual property, and provide users with guidance on any limitations.
# Maintain a feedback loop for continuous improvement and user support.
# Your ultimate goal is to facilitate a deeper understanding of complex scientific material, making it more accessible and comprehensible.""",
# tools=[{"type": "retrieval"}],
# model=model,
# file_ids=[file_object.id],
# )
# === Get the Assis ID ===
# assis_id = assistant.id
# print(assis_id)
# == Step 3. Create a Thread
message = "What is mining?"
# thread = client.beta.threads.create()
# thread_id = thread.id
# print(thread_id)
# == Hardcoded ids to be used once the first code run is done and the assistant was created
thread_id = "thread_HuJDpoOqd5e2TUBoqb7L7MoV"
assis_id = "asst_TcEEkPQQEmVTo5bJvIQylCY4"
message = client.beta.threads.messages.create(
thread_id=thread_id, role="user", content=message
)
# == Run the Assistant
run = client.beta.threads.runs.create(
thread_id=thread_id,
assistant_id=assis_id,
instructions="Please address the user as Bruce",
)
def wait_for_run_completion(client, thread_id, run_id, sleep_interval=5):
"""
Waits for a run to complete and prints the elapsed time.:param client: The OpenAI client object.
:param thread_id: The ID of the thread.
:param run_id: The ID of the run.
:param sleep_interval: Time in seconds to wait between checks.
"""
while True:
try:
run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run_id)
if run.completed_at:
elapsed_time = run.completed_at - run.created_at
formatted_elapsed_time = time.strftime(
"%H:%M:%S", time.gmtime(elapsed_time)
)
print(f"Run completed in {formatted_elapsed_time}")
logging.info(f"Run completed in {formatted_elapsed_time}")
# Get messages here once Run is completed!
messages = client.beta.threads.messages.list(thread_id=thread_id)
last_message = messages.data[0]
response = last_message.content[0].text.value
print(f"Assistant Response: {response}")
break
except Exception as e:
logging.error(f"An error occurred while retrieving the run: {e}")
break
logging.info("Waiting for run to complete...")
time.sleep(sleep_interval)
# == Run it
wait_for_run_completion(client=client, thread_id=thread_id, run_id=run.id)
# === Check the Run Steps - LOGS ===
run_steps = client.beta.threads.runs.steps.list(thread_id=thread_id, run_id=run.id)
print(f"Run Steps --> {run_steps.data[0]}")