-
Notifications
You must be signed in to change notification settings - Fork 0
/
ask_openai.py
67 lines (56 loc) · 1.63 KB
/
ask_openai.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
#!/usr/bin/env python3
# ask ChatGPT in the Terminal
my_api_key = 'sk-***'
# models: https://openai.com/api
models = {
'4o': 'gpt-4o',
'o1pre': 'o1-preview'
}
openai_model = models['o1pre']
from os.path import basename
from prompt_toolkit import prompt
from sys import argv, exit
from openai import OpenAI
from rich.console import Console
from rich.markdown import Markdown
# question
myname = basename(argv.pop(0))
question = '\x20'.join(argv)
while not question:
try: question = prompt('Q: ')
except KeyboardInterrupt:
print()
exit('interrupted')
# api args
client = OpenAI(api_key=my_api_key)
customized = 'You are a scientist, give formal answers. \
Tell me, if you are not sure. It is very important that your \
answer is correct!' # begging helps
temp = 0.2 # will make it more focused and deterministic
# call api
try:
if openai_model == 'o1-preview':
completion = client.chat.completions.create(
model=openai_model,
messages=[
{'role': 'user', 'content': question}
]
)
else:
completion = client.chat.completions.create(
model=openai_model,
temperature=temp,
messages=[
{'role': 'system', 'content': customized},
{'role': 'user', 'content': question}
]
)
real_model = str(completion.model)
answer = str(completion.choices[0].message.content)
# formatting
console = Console()
markdown = Markdown(answer)
print(); print('Model: ', real_model); print(); console.print(markdown); print()
exit(0)
except Exception as e:
exit(str(e))