-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgpt_api.py
129 lines (95 loc) · 4.57 KB
/
gpt_api.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
import requests
import os
import json
from flask import session
from dotenv import load_dotenv
def fetch_tarot_reading(selected_cards):
load_dotenv()
api_key = os.getenv('OPENAI_KEY')
print(api_key)
url = 'https://api.openai.com/v1/engines/text-davinci-003/completions'
headers = {
'Content-Type': 'application/json',
'Authorization': f"Bearer {api_key}"
}
prompt = f"Generated cards: {selected_cards} Reply with a ~200 character paragraph: go over the meaning of each card provided and emphasise what its position is (eg past, present, future, etc). Offer some basic insight into how this spread can answer the question that was asked earlier (if a question was asked). \Do not refer to cards spatially but in relation to the position field provided in JSON. Always refere to the user as 'you' and the tarot reader as 'I'."
data = {
'prompt': prompt,
'max_tokens': 350
}
if "log" not in session:
session["log"] = {}
session["log"]["reading"] = []
session["log"]["reading"].append(prompt)
session.modified = True
try:
response = requests.post(url, headers=headers, json=data)
response_data = response.json()
if 'choices' in response_data and response_data['choices']:
message = response_data['choices'][0].get('text', '').strip()
session["log"]["reading"].append(f"TarotGPT: {message}")
session.modified = True
return message
else:
return "Error: Invalid response from OpenAI API" + json.dumps(response_data)
except requests.exceptions.RequestException as e:
print('Error:', e)
return None
def remove_tarot_gpt_prefix(input_string):
prefix = "TarotGPT: "
if input_string.startswith(prefix):
return input_string[len(prefix):]
else:
return input_string
def chat(message_type, message, card_name):
if card_name == None:
card_name = ""
chat_system_prompts = {
"reading" : f"You are TarotGPT, a tarot reader. If the user hasn't already asked a question, enquire whether the user would like to ask the tarot any specific questions. \
If the user has no more questions, invite the user to press the shuffle cards button. This will provide you a tarot card spread. Once you have received the spread, keep it in memory as no other tarot spread can be generated.\
User messages will always end in with the following symbol: '🜑'. \
Never end your own messages with this symbol ('🜑'). \
ONLY reply as TarotGPT, but never start the reply with the text: \"TarotGPT\".",
"card_detail": f"You are a tarot card expert. You know alot about tarot cards, but you are not a tarot card. \
Answer the user's questions about the meaning of a specific card. " + card_name + " is the card that the user is asking about.\
When possible, casually include an emoji that is relevant to the tarot card without mentioning the word Emoji."
}
system_message = chat_system_prompts.get(message_type, None)
counter = session.get('counter', 0)
counter += 1
session['counter'] = counter
if "log" not in session:
session["log"] = {}
log_type = message_type
if log_type == "card_detail":
log_type = card_name
if log_type not in session["log"]:
session["log"][log_type] = []
session["log"][log_type].append(system_message)
session["log"][log_type].append(f"User: {message}")
load_dotenv()
api_key = os.getenv('OPENAI_KEY')
print(api_key)
url = 'https://api.openai.com/v1/engines/text-davinci-003/completions'
headers = {
'Content-Type': 'application/json',
'Authorization': f"Bearer {api_key}"
}
data = {
'prompt': ' '.join(session["log"][log_type]),
'max_tokens': 1000
}
print(data)
try:
response = requests.post(url, headers=headers, json=data)
response_data = response.json()
if 'choices' in response_data and response_data['choices']:
message = response_data['choices'][0].get('text', '').strip()
message = remove_tarot_gpt_prefix(message)
session["log"][log_type].append(f"TarotGPT: {message}")
return f"{message}"
else:
return "Error: Invalid response from OpenAI API" + json.dumps(response_data)
except requests.exceptions.RequestException as e:
print('Error:', e)
return None