-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
134 lines (105 loc) ยท 4.31 KB
/
cli.py
File metadata and controls
134 lines (105 loc) ยท 4.31 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from dotenv import load_dotenv
import asyncio
import os
from agent import (
VersatileAgent,
OutputMode,
OutputEvent
)
# ํ๊ฒฝ ๋ณ์ ๋ก๋
load_dotenv()
class CLIInterface:
"""CLI ์ถ๋ ฅ ์ฒ๋ฆฌ ํด๋์ค"""
def __init__(self, agent: VersatileAgent):
self.agent = agent
async def process_stream(self, stream_generator, newline_at_end: bool = True):
"""์คํธ๋ฆผ ์ด๋ฒคํธ๋ฅผ CLI์ ์ถ๋ ฅ"""
async for event in stream_generator:
cli_string = event.to_cli_string()
if event.event_type == "stream":
print(cli_string, end='', flush=True)
else:
print(cli_string, end='', flush=True)
if newline_at_end:
print()
async def chat(self, user_message: str, session_id: str = "default"):
"""์ผ๋ฐ ๋ํ"""
stream = self.agent.chat_stream(user_message, session_id)
await self.process_stream(stream)
async def tool(self, user_message: str, session_id: str = "default"):
"""๋๊ตฌ ์ฌ์ฉ ๋ชจ๋"""
stream = self.agent.tool_stream(user_message, session_id)
await self.process_stream(stream)
async def think(self, user_message: str, session_id: str = "default"):
"""์ฌ๊ณ ๊ณผ์ ๋ชจ๋"""
stream = self.agent.think_and_answer_stream(user_message, session_id)
await self.process_stream(stream)
def clear_history(self, session_id: str = "default"):
"""๋ํ ๊ธฐ๋ก ์ด๊ธฐํ"""
self.agent.clear_history(session_id)
event = OutputEvent(OutputMode.LOG, "tag", "conversation history cleared.", is_start=True)
print(event.to_cli_string())
event = OutputEvent(OutputMode.LOG, "tag", is_start=False)
print(event.to_cli_string())
async def main():
"""๋ฉ์ธ ์คํ ํจ์"""
# ๋ชจ๋ธ ๋ก๋
model_path = os.getenv("MODEL_PATH", './models/llama-3-Korean-Bllossom-8B/Q8_0.gguf')
print(f"Loading model from {model_path}...")
agent = VersatileAgent(model_path)
print("Model loaded successfully!")
cli = CLIInterface(agent)
print()
print("=" * 40)
print("๋ฒ์ฌํ์ผ (Versatile)")
print()
print("commands: 'quit', 'clear'")
print()
print("qna: '<prompt>'")
print("tool mode: '@tool <prompt>'")
print("think mode: '@think <prompt>'")
print("=" * 40)
print()
session_id = "default"
while True:
try:
user_input = input("you: ").strip()
if not user_input:
continue
if user_input.lower() in ['quit', 'exit']:
event = OutputEvent(OutputMode.LOG, "tag", "close versatile", is_start=True)
print(event.to_cli_string(), end='')
event = OutputEvent(OutputMode.LOG, "tag", is_start=False)
print(event.to_cli_string())
break
if user_input.lower() == 'clear':
cli.clear_history(session_id)
continue
print()
if user_input.lower().startswith('@think '):
event = OutputEvent(OutputMode.LOG, "tag", "think mode enabled.", is_start=True)
print(event.to_cli_string(), end='')
event = OutputEvent(OutputMode.LOG, "tag", is_start=False)
print(event.to_cli_string())
print()
await cli.think(user_input[7:], session_id)
continue
if user_input.lower().startswith('@tool '):
event = OutputEvent(OutputMode.LOG, "tag", "tool mode enabled.", is_start=True)
print(event.to_cli_string(), end='')
event = OutputEvent(OutputMode.LOG, "tag", is_start=False)
print(event.to_cli_string())
print()
await cli.tool(user_input[6:], session_id)
print()
continue
await cli.chat(user_input, session_id)
print()
except KeyboardInterrupt:
print("\n\nInterrupted by user")
break
except Exception as e:
print(f"\n[ERROR] {str(e)}")
continue
if __name__ == "__main__":
asyncio.run(main())