-
Notifications
You must be signed in to change notification settings - Fork 1
/
ask-ai.py
executable file
·48 lines (39 loc) · 1.49 KB
/
ask-ai.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
import openai
import os
import argparse
# Set up OpenAI API credentials
openai.api_key = os.environ["OPENAI_API_KEY"]
def query(args):
response = openai.ChatCompletion.create(
model=args.model,
messages=[
{"role": "user", "content": args.q },
],
)
return response["choices"][0]["message"]["content"]
def chat(messages, args):
response = openai.ChatCompletion.create(
model=args.model,
messages=messages,
)
return response["choices"][0]["message"]["content"]
if __name__ == "__main__":
argparse = argparse.ArgumentParser()
argparse.add_argument('--q', type=str, default=None, help='query to ask the AI')
# add flag for chat mode
argparse.add_argument('--c', action='store_true', help='chat mode')
argparse.add_argument('--model', type=str, default='gpt-3.5-turbo', help='openai model to use, default "gpt-3.5-turbo" or use "gpt-4-1106-preview"')
args = argparse.parse_args()
if args.c:
messages = []
while True:
inp = input("You: ")
if inp == "exit":
break
messages.append({"role": "user", "content": inp})
response = chat(messages,args)
print("AI: " + response)
messages.append({"role": "assistant", "content": response})
elif args.q:
response = query(args)
print(response)