-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_terminal.py
More file actions
62 lines (51 loc) · 1.78 KB
/
main_terminal.py
File metadata and controls
62 lines (51 loc) · 1.78 KB
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
import json
from pyreact.boot import bootstrap, read_terminal_and_invoke
from pyreact.core.core import component
from components import Root
import os
import dotenv
import dspy
import asyncio
@component
def Boot():
dotenv.load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
lm_default = dspy.LM("openai/gpt-4o", api_key=api_key)
lm_fast = dspy.LM("openai/gpt-4o-mini", api_key=api_key)
models = {
"default": lm_default,
"fast": lm_fast,
"reasoning": lm_default,
}
return [Root(key="root", models=models)]
async def main():
def _on_console(text: str) -> None:
if text.startswith("__MESSAGE__:"):
message_json = text[12:] # Remove "__MESSAGE__:"
message_data = json.loads(message_json)
sender_colors = {
"user": "\x1b[34m", # Blue
"system": "\x1b[90m", # Gray
"assistant": "\x1b[32m", # Green
}
type_colors = {
"chat": "",
"info": "\x1b[36m", # Cyan
"warning": "\x1b[33m", # Yellow
"error": "\x1b[31m", # Red
}
sender = message_data["sender"]
message_type = message_data["message_type"]
text = message_data["text"]
color = sender_colors.get(sender, "") + type_colors.get(message_type, "")
reset = "\x1b[0m"
formatted_text = f"{color}[{sender.upper()}] {text}{reset}\n"
print(formatted_text, end="", flush=True)
myapp = bootstrap(Boot, fps=20)
myapp.attach_web_bridge(
on_console=_on_console,
target_loop=asyncio.get_running_loop(),
)
await read_terminal_and_invoke(myapp, prompt="> ", wait=True)
if __name__ == "__main__":
asyncio.run(main())