-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb.py
More file actions
77 lines (60 loc) · 2.96 KB
/
b.py
File metadata and controls
77 lines (60 loc) · 2.96 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
import requests
import re, json
from openai import OpenAI
prompt = 'i like a place with high skyscrappers and culture and some food and i also like places with beaches and nice and warm weather and my budegt perday is 152'
def askAI():
response = requests.post('https://fumes-api.onrender.com/llama3',
json={
'prompt': [
{"role": "system", "content": "You have to analyse the user prompt and suggest them more than 5 countries based on their preferences. you only have to suggest them countries based on their preferences and write the standard Name for the countries. You Have to Follow a specific format to suggest them countries in all cases no exception. The format is: [country Name1, Country Name2, Country Name3, ...., country Name N]"},
{"role": "user", "content": f"{prompt}"}
],
"temperature":0.5,
"topP":0.3,
"lengthPenality":0.3,
"maxTokens": 2000
}, stream=True)
text = ''
buffer = ''
for chunk in response.iter_content(chunk_size=1024):
if chunk:
print(chunk.decode('utf-8'))
buffer += chunk.decode('utf-8')
try:
data = json.loads(buffer)
content = data.get('choices', [{}])[0].get('delta', {}).get('content', '')
text += content
buffer = '' # Clear the buffer once we've successfully parsed it
except json.JSONDecodeError:
# If a JSONDecodeError is raised, it means that the buffer does not contain a complete JSON object
# So we just continue accumulating chunks in the buffer
pass
return text
def extract_data(text):
matches = re.findall(r'\[([^]]*)\]', text)
countries = [country.strip() for country in matches[0].split(',')]
return countries
def ask():
import pickle
api_key =None
with open('OpenAI_API.bin', 'rb') as f:
api_key = pickle.load(f)
client = OpenAI(api_key=api_key)
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You have to analyse the user prompt and suggest them more than 5 countries based on their preferences. you only have to suggest them countries based on their preferences and write the standand Name for the countries. You Have to Follow a specific format to suggest them countries in all cases no exception. The format is: [country Name1, Country Name2, Country Name3, ...., country Name N]"},
{"role": "user", "content": f"{prompt}"}
])
text = response.choices[0].message.content
print('\n GPT:\n', text)
matches = re.findall(r'\[([^]]*)\]', text)
if matches:
countries = [country.strip() for country in matches[0].split(',')]
else:
countries = []
print(countries)
text = askAI()
print('Llama3:\n', text)
#data = extract_data(text)
#print('Llama3:\n', text, '\nExtracted Data:\n', data)