-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
86 lines (72 loc) · 2.27 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
import together
import os
import sys
import time
from dotenv import load_dotenv
from rich.console import Console
from rich.markdown import Markdown
import colorama
import sys
import time
RED = "\x1b[1;31;40m"
GREEN = " \x1b[32m"
YELLOW = "\x1b[33m"
console = Console()
load_dotenv()
api_key = os.getenv("API_KEY")
client = together.Together(api_key=api_key)
def clear():
if os.name == "nt":
os.system("cls")
else:
os.system("clear")
def hook(tp, *args) -> None:
if tp is KeyboardInterrupt:
print(colorama.Fore.RED)
exit()
def colourized_input(text: str, color: str) -> (str | None):
while True:
try:
sys.excepthook = hook
inp = input(text + color).strip()
print(colorama.Fore.RESET, end="", flush=True)
if not inp:
raise ValueError("Input cannot be empty")
return inp
except ValueError as e:
print(f"{color}{e}{colorama.Fore.RESET}")
except Exception as e:
print(f"Unexpected error: {e}")
break
def typing_effect(text, delay=0.05):
for char in text:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(delay)
print()
def render_markdown_typing_effect(markdown_text, delay=0.05):
console = Console()
markdown = Markdown(markdown_text)
with console.capture() as capture:
console.print(markdown)
rendered_text = capture.get()
typing_effect(rendered_text, delay)
def Main_Loop() -> None:
i = 0
while i < 15:
message = str(colourized_input(
f"{RED}>>>>>>>>>>>> ", colorama.Fore.GREEN))
if message == "exit":
break
if message == "clear":
clear()
message = str(colourized_input(
f"{RED}>>> ", colorama.Fore.GREEN))
completion = client.chat.completions.create(model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo", messages=[{"role": "user", "content": message}])
# formatted_message = format_message(
# completion.choices[0].message.content)
# typing_effect(0.05, formatted_message,
# style="bold blue")
render_markdown_typing_effect(completion.choices[0].message.content, delay=0.005)
i += 1
Main_Loop()