-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
64 lines (40 loc) · 1.76 KB
/
main.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
import openai
import config
import typer
from rich import print
from rich.table import Table
def main():
openai.api_key = config.api_key
print("🗣️ [bold green]ChatGPT API en Python[/bold green]")
table = Table("Command", "Description")
table.add_row("exit", "Exit from the app")
table.add_row("new", "Cut the conversation and create a new one")
print(table)
# Using role and content section, we will give it a context that can influence chatgpt's responses.
context = {"role": "system",
"content": "You are a very helpful assistant."}
messages = [context]
while True:
content = __prompt()
if content =="new":
print("☑️ 👍 [bold blue]We started a new chat[/bold blue]")
messages = [context]
content = __prompt()
messages.append({"role": "user", "content": content})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=messages)
# We will give it the response_content so that chatgpt can remember the answer from previous queries
response_content = response.choices[0].message.content
messages.append({"role": "assistant", "content": response_content})
print(f"[bold green] -> [/bold green] [green]{response_content}[/green]")
def __prompt() -> str:
prompt = typer.prompt("\nWhat do you want to talk about❓")
if prompt == "exit":
exit = typer.confirm("⛔ Wait, are you sure about that❓")
if exit:
print("👋 We look forward to see you again 🫂")
raise typer.Abort()
return __prompt()
return prompt
if __name__ == "__main__":
typer.run(main)