-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
297 lines (260 loc) · 11.7 KB
/
utils.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import json
from duckduckgo_search import DDGS
from bs4 import BeautifulSoup
import requests
def retrieve_web_data(search_terms, max_results=3):
results = []
with DDGS() as search_engine:
for result in search_engine.text(search_terms, safesearch="Off", max_results=max_results):
title = result["title"]
url = result["href"]
result_text = f' *Title*: {title} *Body*: {result["body"]}\n*Scraped_text*: {extract_site_content(url)}'
results.append(result_text)
return '\n'.join(results)
def extract_site_content(web_url):
try:
response = requests.get(web_url, timeout=3)
soup = BeautifulSoup(response.text, "lxml")
raw_text = soup.get_text()
cleaned_text = ''.join(line.strip() for line in raw_text.split('\n'))
return cleaned_text[:256]
except Exception as e:
print(e)
return "Error: Requested site couldn't be viewed. Please inform in your response that the informations may not be up to date or correct."
def extract_json(content):
# Initialize counters for curly braces
open_brace_count = 0
close_brace_count = 0
json_start_index = None
json_end_index = None
# Iterate over the content by index and character
for index, char in enumerate(content):
if char == '{':
open_brace_count += 1
# Mark the start of JSON content
if json_start_index is None:
json_start_index = index
elif char == '}':
close_brace_count += 1
# If the counts match, we've found the end of the JSON content
if open_brace_count == close_brace_count:
json_end_index = index + 1 # Include the closing brace
break
# If we found a start and end, extract and parse the JSON
if json_start_index is not None and json_end_index is not None:
json_str = content[json_start_index:json_end_index]
try:
json_data = json.loads(json_str)
return json_data
except json.JSONDecodeError as e:
raise ValueError("Invalid JSON content") from e
else:
raise ValueError("No JSON content found")
import requests
import os
from openai import OpenAI
import codecs
import json
import yaml
class OpenAIBackend:
def __init__(self, model="gpt-4o-mini"):
self.model = model
self.load_api_key()
self.client = OpenAI(
# This is the default and can be omitted
api_key=self.api_key,
)
self.conversation_history = []
def load_api_key(self):
api_key_path = os.path.join(os.path.expanduser("~"), ".openai_api_key")
try:
with open(api_key_path, 'r') as file:
self.api_key = file.read().strip()
except FileNotFoundError:
raise FileNotFoundError(f"OpenAI API key file not found. Please create a file named .openai_api_key in your home directory ({os.path.expanduser('~')}) containing your OpenAI API key.")
def communicate(self, prompt, reset=True):
f = codecs.open("conversation_history.txt", "a", "utf-8")
if reset:
self.conversation_history = []
self.conversation_history.append({"role": "user", "content": prompt})
f.write("="*10+"\n")
for item in self.conversation_history:
f.write(item['role']+":\n"+str(item['content'])+"\n")
try:
response = self.client.chat.completions.create(
model=self.model,
messages=self.conversation_history
)
assistant_message = response.choices[0].message.content.strip()
f.write("-"*10+"\n"+"response:\n")
f.write(assistant_message+"\n")
f.close()
self.conversation_history.append({"role": "assistant", "content": assistant_message})
# Extract JSON if present
if "```json" in assistant_message:
json_start = assistant_message.index("```json") + 7
json_end = assistant_message.rindex("```", json_start)
json_str = assistant_message[json_start:json_end].strip()
try:
assistant_message = json.loads(json_str)
except json.JSONDecodeError:
print("Warning: Failed to parse JSON. Returning original message.")
print("-=-=-=-=\nJsonstring:\n")
print(json_str)
print("-=-=-=-=")
# Extract YAML if present
elif "```yaml" in assistant_message or "```yml" in assistant_message:
yaml_start = assistant_message.index(
"```yaml") + 7 if "```yaml" in assistant_message else assistant_message.index("```yml") + 6
yaml_end = assistant_message.rindex("```", yaml_start)
yaml_str = assistant_message[yaml_start:yaml_end].strip()
try:
assistant_message = yaml.safe_load(yaml_str) # Parse YAML string into a dictionary
except yaml.YAMLError:
print("Warning: Failed to parse YAML. Returning original message.")
print("-=-=-=-=\nYamlstring:\n")
print(yaml_str)
print("-=-=-=-=")
return assistant_message
except Exception as e:
print(f"Error communicating with OpenAI API: {str(e)}")
return "Error: Unable to get response from OpenAI API"
import codecs
import requests
import json
import yaml # Import PyYAML module
class llm_chatter:
def __init__(self, host='http://127.0.0.1:5000/v1/chat/completions'):
self.host = host
self.msg_history = []
self.headers = {
"Content-Type": "application/json"
}
def communicate(self, prompt, greedy=True, reset=True, max_tokens=4096):
f = codecs.open("conversation_history.txt", "a", "utf-8")
if reset:
self.msg_history = []
self.msg_history.append({"role": "user", "content": prompt})
f.write("=" * 10 + "\n")
for item in self.msg_history:
f.write(item['role'] + ":\n" + str(item['content']) + "\n")
data = {
"mode": "instruct",
"max_tokens": max_tokens,
#"instruction_template":template,
"messages": self.msg_history,
'temperature': 0.001,
}
if greedy:
data['temperature'] = 0
response = requests.post(self.host, headers=self.headers, json=data, verify=False)
answer = response.json()['choices'][0]['message']['content']
assistant_message = answer
f.write("-" * 10 + "\n" + "response:\n")
f.write(answer + "\n")
f.close()
self.msg_history.append({"role": "assistant", "content": answer})
# Extract JSON if present
if "```json" in assistant_message:
json_start = assistant_message.index("```json") + 7
json_end = assistant_message.rindex("```", json_start)
json_str = assistant_message[json_start:json_end].strip()
try:
assistant_message = json.loads(json_str)
except json.JSONDecodeError:
print("Warning: Failed to parse JSON. Returning original message.")
print("-=-=-=-=\nJsonstring:\n")
print(json_str)
print("-=-=-=-=")
# Extract YAML if present
elif "```yaml" in assistant_message or "```yml" in assistant_message:
yaml_start = assistant_message.index("```yaml") + 7 if "```yaml" in assistant_message else assistant_message.index("```yml") + 6
yaml_end = assistant_message.rindex("```", yaml_start)
yaml_str = assistant_message[yaml_start:yaml_end].strip()
try:
assistant_message = yaml.safe_load(yaml_str) # Parse YAML string into a dictionary
except yaml.YAMLError:
print("Warning: Failed to parse YAML. Returning original message.")
print("-=-=-=-=\nYamlstring:\n")
print(yaml_str)
print("-=-=-=-=")
return assistant_message
class ollama_chatter:
def __init__(self, host='http://host.docker.internal:11434'):
self.host = host
self.msg_history = []
self.headers = {
"Content-Type": "application/json"
}
def communicate(self, prompt, greedy=True, reset=True, max_tokens=4096):
f = codecs.open("conversation_history.txt", "a", "utf-8")
if reset:
self.msg_history = []
self.msg_history.append({"role": "user", "content": prompt})
f.write("=" * 10 + "\n")
for item in self.msg_history:
f.write(item['role'] + ":\n" + str(item['content']) + "\n")
# Configure the request data
data = {
"model": "qwen2.5:32b-instruct-q4_K_S", # This can be made configurable # qwen2.5-coder:14b-instruct-q5_K_M
"messages": self.msg_history,
"stream": False,
"options": {
"num_predict": max_tokens, # This corresponds to max_tokens
"temperature": 0.001 if not greedy else 0,
}
}
# Make the API call
response = requests.post(f"{self.host}/api/chat", headers=self.headers, json=data)
# Extract the response content
# Note: Ollama's response format might need adjustment depending on the actual response structure
answer = json.loads(response.content.decode())['message']['content']
f.write("-" * 10 + "\n" + "response:\n")
f.write(answer + "\n")
f.close()
self.msg_history.append({"role": "assistant", "content": answer})
# Process special formats (JSON/YAML)
assistant_message = answer
# Extract JSON if present
if "```json" in assistant_message:
json_start = assistant_message.index("```json") + 7
json_end = assistant_message.rindex("```", json_start)
json_str = assistant_message[json_start:json_end].strip()
try:
assistant_message = json.loads(json_str)
except json.JSONDecodeError:
print("Warning: Failed to parse JSON. Returning original message.")
print("-=-=-=-=\nJsonstring:\n")
print(json_str)
print("-=-=-=-=")
# Extract YAML if present
elif "```yaml" in assistant_message or "```yml" in assistant_message:
yaml_start = assistant_message.index("```yaml") + 7 if "```yaml" in assistant_message else assistant_message.index("```yml") + 6
yaml_end = assistant_message.rindex("```", yaml_start)
yaml_str = assistant_message[yaml_start:yaml_end].strip()
try:
assistant_message = yaml.safe_load(yaml_str)
except yaml.YAMLError:
print("Warning: Failed to parse YAML. Returning original message.")
print("-=-=-=-=\nYamlstring:\n")
print(yaml_str)
print("-=-=-=-=")
return assistant_message
class llm_completion:
def __init__(self,host='http://127.0.0.1:5000/v1/completions'):
self.host = host
self.headers = {
"Content-Type": "application/json"
}
def complete(self,prompt,greedy=False,max_tokens=2048):
data = {
"max_tokens": max_tokens,
"prompt": prompt,
"temperature": 1,
"top_p": 0.9,
}
if greedy:
data['temperature'] = 0
response = requests.post(self.host, headers=self.headers, json=data, verify=False)
answer = response.json()['choices'][0]['text']
return answer