-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
255 lines (209 loc) · 8.6 KB
/
Copy pathmain.py
File metadata and controls
255 lines (209 loc) · 8.6 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import openai
import os
from dotenv import find_dotenv, load_dotenv
import time
import logging
from datetime import datetime
import requests
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("CUSTOM_ENV_NAME"),
# )
news_api_key = os.environ.get("NEWS_API_KEY")
client = openai.OpenAI()
model = "gpt-3.5-turbo-16k"
def get_news(topic):
url = (
f"https://newsapi.org/v2/everything?q={topic}&apiKey={news_api_key}&pageSize=5"
)
try:
response = requests.get(url)
if response.status_code == 200:
news = json.dumps(response.json(), indent=4)
news_json = json.loads(news)
data = news_json
# Access all the fiels == loop through
status = data["status"]
total_results = data["totalResults"]
articles = data["articles"]
final_news = []
# Loop through articles
for article in articles:
source_name = article["source"]["name"]
author = article["author"]
title = article["title"]
description = article["description"]
url = article["url"]
content = article["content"]
title_description = f"""
Title: {title},
Author: {author},
Source: {source_name},
Description: {description},
URL: {url}
"""
final_news.append(title_description)
return final_news
else:
return []
except requests.exceptions.RequestException as e:
print("Error occured during API Request", e)
class AssistantManager:
thread_id = None
assistant_id = "asst_2VcPmVpAnjFWBdGzPVbtoiXF"
def __init__(self, model: str = model):
self.client = client
self.model = model
self.assistant = None
self.thread = None
self.run = None
self.summary = None
# Retrieve existing assistant and thread if IDs are already set
if AssistantManager.assistant_id:
self.assistant = self.client.beta.assistants.retrieve(
assistant_id=AssistantManager.assistant_id
)
# storing the existing thread if there is one
if AssistantManager.thread_id:
self.thread = self.client.beta.threads.retrieve(
thread_id=AssistantManager.thread_id
)
def create_assistant(self, name, instructions, tools):
if not self.assistant:
assistant_obj = self.client.beta.assistants.create(
name=name, instructions=instructions, tools=tools, model=self.model
)
AssistantManager.assistant_id = assistant_obj.id
self.assistant = assistant_obj
print(f"AssisID:::: {self.assistant.id}")
def create_thread(self):
if not self.thread:
thread_obj = self.client.beta.threads.create()
AssistantManager.thread_id = thread_obj.id
self.thread = thread_obj
print(f"ThreadID::: {self.thread.id}")
def add_message_to_thread(self, role, content):
if self.thread:
self.client.beta.threads.messages.create(
thread_id=self.thread.id, role=role, content=content
)
def run_assistant(self, instructions):
if self.thread and self.assistant:
# running the assistant and assisigning to run
self.run = self.client.beta.threads.runs.create(
thread_id=self.thread.id,
assistant_id=self.assistant.id,
instructions=instructions,
)
def process_message(self):
if self.thread:
messages = self.client.beta.threads.messages.list(thread_id=self.thread.id)
summary = []
last_message = messages.data[0]
role = last_message.role
response = last_message.content[0].text.value
summary.append(response)
self.summary = "\n".join(summary)
print(f"SUMMARY-----> {role.capitalize()}: ==> {response}")
# for msg in messages:
# role = msg.role
# content = msg.content[0].text.value
# print(f"SUMMARY-----> {role.capitalize()}: ==> {content}")
def call_required_functions(self, required_actions):
if not self.run:
return
tool_outputs = []
for action in required_actions["tool_calls"]:
func_name = action["function"]["name"]
arguments = json.loads(action["function"]["arguments"])
if func_name == "get_news":
output = get_news(topic=arguments["topic"])
print(f"STUFFFFF;;;;{output}")
final_str = ""
for item in output:
final_str += "".join(item)
tool_outputs.append({"tool_call_id": action["id"], "output": final_str})
else:
raise ValueError(f"Unknown function: {func_name}")
print("Submitting outputs back to the Assistant...")
self.client.beta.threads.runs.submit_tool_outputs(
thread_id=self.thread.id, run_id=self.run.id, tool_outputs=tool_outputs
)
# for streamlit
def get_summary(self):
return self.summary
def wait_for_completion(self):
if self.thread and self.run:
while True:
time.sleep(5)
run_status = self.client.beta.threads.runs.retrieve(
thread_id=self.thread.id, run_id=self.run.id
)
print(f"RUN STATUS:: {run_status.model_dump_json(indent=4)}")
if run_status.status == "completed":
self.process_message()
break
elif run_status.status == "requires_action":
print("FUNCTION CALLING NOW...")
self.call_required_functions(
required_actions=run_status.required_action.submit_tool_outputs.model_dump()
)
# Run the steps
def run_steps(self):
run_steps = self.client.beta.threads.runs.steps.list(
thread_id=self.thread.id, run_id=self.run.id
)
print(f"Run-Steps::: {run_steps}")
return run_steps.data
def main():
# news = get_news("bitcoin")
# print(news[0])
manager = AssistantManager()
# Streamlit interface
st.title("News Summarizer")
with st.form(key="user_input_form"):
instructions = st.text_input("Enter topic:")
submit_button = st.form_submit_button(label="Run Assistant")
if submit_button:
manager.create_assistant(
name="News Summarizer",
instructions="You are a personal article summarizer Assistant who knows how to take a list of article's titles and descriptions and then write a short summary of all the news articles",
tools=[
{
"type": "function",
"function": {
"name": "get_news",
"description": "Get the list of articles/news for the given topic",
"parameters": {
"type": "object",
"properties": {
"topic": {
"type": "string",
"description": "The topic for the news, e.g. bitcoin",
}
},
"required": ["topic"],
},
},
}
],
)
manager.create_thread()
# Add the message and run the assistant
manager.add_message_to_thread(
role="user", content=f"summarize the news on this topic {instructions}?"
)
manager.run_assistant(instructions="Summarize the news")
# Wait for completions and process messages
manager.wait_for_completion()
summary = manager.get_summary()
st.write(summary)
st.text("Run Steps:")
st.code(manager.run_steps(), line_numbers=True)
if __name__ == "__main__":
main()