-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
102 lines (93 loc) · 2.98 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
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
import os
from rich import print
from huggingface_hub import InferenceClient
# Get your HuggingFace Access Token from https://huggingface.co/settings/tokens
# Then run this: export CLIGPTTOKEN="hf_xxxxxxYOUR-KEY-HERExxxxxx"
apiKey = os.getenv('CLIGPTTOKEN')
client = InferenceClient(api_key=apiKey)
msg = ""
latestAImsg = ""
# Prompt given to the AI
prompt = "You are a helpful AI assistant."
# AI model to use (must be on HuggingFace)
aiModel = "mistralai/Mistral-7B-Instruct-v0.2"
if prompt == "":
messages = []
else:
messages = [
{
"role": "system",
"content": str(prompt)
}
]
print("Type \"/help\" for commands.")
running = True
while running:
print("[bold]Me[/bold]: ", end="")
msg = input("")
doReply = True
if msg[0] == "/":
if msg in ["/quit", "/q", "/exit", "/stop"]:
print("[bold]System[/bold]: Exiting")
doReply = True
quit()
elif msg in ["/clear", "/c", "/forget", "/new"]:
os.system("clear")
print("[bold]System[/bold]: Started new chat")
doReply = False
if prompt == "":
messages = []
else:
messages = [
{
"role": "system",
"content": str(prompt)
}
]
elif msg == "/echo":
msg = latestAImsg
doReply = True
elif msg == "/prompt":
prompt = str(input("Enter AI prompt for future chats:\n"))
doReply = False
elif msg in ["/regenerate", "/regen", "/re", "/r"]:
doReply = True
elif msg == "/help":
doReply = False
print("""
Commands:
/quit, /q - Exit the program
/clear, /c - Clear chat history
/regenerate, /r - Regenerate last message
/echo - Repeat what the AI said back to it
/prompt - Set prompt for future chats (does not persist after quit)
/help - Show this help text
""")
else:
print("[bold red]Error[/bold red]: Unknown command. Run /help for more info. Messages starting with / are not passed to the AI.")
doReply = False
if doReply:
msgDict = {
"role": "user",
"content": str(msg)
}
messages.append(msgDict)
stream = client.chat.completions.create(
model=aiModel,
messages=messages,
max_tokens=12000,
stream=True
)
print("[bold]AI[/bold]: ", end="")
reply = ""
for chunk in stream:
replyAppend = chunk.choices[0].delta.content
print(replyAppend, end="")
reply += replyAppend
print()
msgDict = {
"role": "assistant",
"content": str(reply)
}
latestAImsg = str(reply)
messages.append(msgDict)