This repository was archived by the owner on Aug 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_cli.py
More file actions
executable file
·298 lines (238 loc) · 9.24 KB
/
query_cli.py
File metadata and controls
executable file
·298 lines (238 loc) · 9.24 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""
CLI tool để truy vấn hệ thống hỏi đáp sản phẩm sử dụng Typer và Rich.
"""
import os
import time
from enum import Enum
from typing import Optional
import typer
from rich import print as rprint
from rich.console import Console
from rich.markdown import Markdown
from rich.panel import Panel
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.prompt import Prompt
from rich.table import Table
from src import LangchainPipeline, VectorStore
__author__ = "Lâm Quang Trí"
__copyright__ = "Copyright 2025, Lâm Quang Trí"
__credits__ = ["Lâm Quang Trí"]
__maintainer__ = "Lâm Quang Trí"
__email__ = "quangtri.lam.9@gmail.com"
__status__ = "Development"
# Khởi tạo Typer và Rich Console
app = typer.Typer(
help="Hệ thống hỏi đáp sản phẩm điện tử",
add_completion=False,
)
console = Console()
class OutputFormat(str, Enum):
"""Định dạng hiển thị kết quả."""
TEXT = "text"
MARKDOWN = "markdown"
JSON = "json"
class Mode(str, Enum):
"""Chế độ hiển thị kết quả."""
BLOCK = "block"
STREAM = "stream" # Chế độ mặc định
def get_qa_pipeline():
"""Khởi tạo pipeline hỏi đáp."""
with console.status("[bold green]Đang khởi tạo hệ thống hỏi đáp..."):
vector_store = VectorStore()
qa_pipeline = LangchainPipeline(vector_store=vector_store)
return qa_pipeline
def interactive_mode(
qa_pipeline: LangchainPipeline,
output_format: OutputFormat = OutputFormat.TEXT,
show_sources: bool = False,
mode: Mode = Mode.STREAM,
) -> None:
"""Chạy chế độ tương tác để nhập nhiều câu hỏi."""
rprint(
Panel.fit(
"[bold cyan]Hệ thống hỏi đáp sản phẩm[/bold cyan]\n"
"[italic]Gõ 'exit' để thoát, 'help' để xem trợ giúp[/italic]",
border_style="blue",
)
)
history = []
while True:
query = Prompt.ask("\n[bold green]Câu hỏi của bạn")
if query.lower() == "exit":
break
if query.lower() == "help":
show_help()
continue
if query.lower() == "clear":
os.system("cls" if os.name == "nt" else "clear")
continue
if query.lower() == "history":
show_history(history)
continue
if not query.strip():
continue
start_time = time.time()
if mode == Mode.STREAM:
# Hiển thị kết quả theo chế độ streaming
answer = display_streaming_answer(query, qa_pipeline, output_format)
else:
# Hiển thị kết quả theo chế độ block
with Progress(
SpinnerColumn(),
TextColumn("[bold green]Đang xử lý câu hỏi..."),
transient=True,
) as progress:
progress.add_task("processing", total=None)
answer = qa_pipeline.answer_question(query)
end_time = time.time()
history.append({"question": query, "answer": answer})
# Hiển thị thời gian xử lý
console.print(
f"\n[bold]Thời gian xử lý:[/bold] {end_time - start_time:.2f} giây"
)
# Nếu không phải streaming, hiển thị kết quả
if mode != Mode.STREAM:
display_answer(query, answer, output_format, end_time - start_time)
def show_help():
"""Hiển thị trợ giúp cho chế độ tương tác."""
help_table = Table(title="Lệnh Có Sẵn")
help_table.add_column("Lệnh", style="cyan")
help_table.add_column("Mô tả", style="green")
help_table.add_row("exit", "Thoát khỏi chương trình")
help_table.add_row("help", "Hiển thị menu trợ giúp này")
help_table.add_row("clear", "Xóa màn hình")
help_table.add_row("history", "Hiển thị lịch sử câu hỏi và trả lời")
console.print(help_table)
def show_history(history):
"""Hiển thị lịch sử câu hỏi và trả lời."""
if not history:
rprint("[italic yellow]Chưa có lịch sử câu hỏi.[/italic yellow]")
return
history_table = Table(title="Lịch Sử Hỏi Đáp")
history_table.add_column("STT", style="cyan", no_wrap=True)
history_table.add_column("Câu Hỏi", style="green")
history_table.add_column("Trả Lời", style="blue")
for i, item in enumerate(history, 1):
history_table.add_row(
str(i),
item["question"][:50] + ("..." if len(item["question"]) > 50 else ""),
item["answer"][:50] + ("..." if len(item["answer"]) > 50 else ""),
)
console.print(history_table)
def display_streaming_answer(query, qa_pipeline, output_format):
"""Hiển thị câu trả lời theo chế độ streaming."""
console.print(f"\n[bold green]Q:[/bold green] {query}")
console.print("\n[bold blue]A:[/bold blue] ", end="")
# Lưu câu trả lời đầy đủ để trả về
full_answer = ""
try:
# Phục vụ stream từng phần
for chunk in qa_pipeline.answer_question_stream(query):
console.print(chunk, end="", highlight=False)
full_answer += chunk
except Exception as e:
error_msg = f"Lỗi khi xử lý streaming: {e}"
console.print(f"\n[bold red]{error_msg}[/bold red]")
return error_msg
console.print() # Xuống dòng sau khi hoàn thành
return full_answer
def display_answer(query, answer, output_format, time_taken=None):
"""Hiển thị câu trả lời theo định dạng được chọn."""
if output_format == OutputFormat.TEXT:
rprint(
Panel(
f"[bold green]Q:[/bold green] {query}\n\n[bold blue]A:[/bold blue] {answer}",
title="Kết quả",
border_style="green",
)
)
elif output_format == OutputFormat.MARKDOWN:
rprint(
Panel(
f"[bold green]Câu hỏi:[/bold green]\n{query}\n\n[bold blue]Trả lời:[/bold blue]",
title="Kết quả",
border_style="green",
)
)
console.print(Markdown(answer))
elif output_format == OutputFormat.JSON:
import json
result = {
"question": query,
"answer": answer,
"time_taken": f"{time_taken:.2f}s" if time_taken else "N/A",
}
rprint(
Panel(
json.dumps(result, ensure_ascii=False, indent=2),
title="JSON Result",
border_style="green",
)
)
@app.command()
def query(
query: Optional[str] = typer.Option(
None, "--query", "-q", help="Câu hỏi cần truy vấn"
),
interactive: bool = typer.Option(
False, "--interactive", "-i", help="Chạy ở chế độ tương tác"
),
output_format: OutputFormat = typer.Option(
OutputFormat.TEXT, "--format", "-f", help="Định dạng hiển thị đầu ra"
),
show_sources: bool = typer.Option(
False, "--sources", "-s", help="Hiển thị thông tin nguồn dữ liệu (nếu có)"
),
mode: Mode = typer.Option(
Mode.STREAM,
"--mode",
"-m",
help="Chế độ hiển thị: stream (mặc định) hoặc block",
),
):
"""Truy vấn hệ thống hỏi đáp sản phẩm."""
# Khởi tạo pipeline
qa_pipeline = get_qa_pipeline()
if interactive:
interactive_mode(qa_pipeline, output_format, show_sources, mode)
elif query:
start_time = time.time()
if mode == Mode.STREAM:
# Hiển thị kết quả theo chế độ streaming
answer = display_streaming_answer(query, qa_pipeline, output_format)
else:
# Hiển thị kết quả theo chế độ block
answer = qa_pipeline.answer_question(query)
end_time = time.time()
time_taken = end_time - start_time
display_answer(query, answer, output_format, time_taken)
end_time = time.time()
console.print(
f"\n[bold]Thời gian xử lý:[/bold] {end_time - start_time:.2f} giây"
)
else:
typer.echo("Vui lòng cung cấp một câu hỏi hoặc sử dụng chế độ tương tác.")
raise typer.Exit(1)
@app.command()
def info():
"""Hiển thị thông tin về hệ thống hỏi đáp."""
rprint(
Panel.fit(
"[bold cyan]Thông tin hệ thống hỏi đáp sản phẩm điện tử[/bold cyan]\n\n"
"Hệ thống sử dụng [bold]LangChain[/bold] và [bold]Qdrant[/bold] để cung cấp câu trả lời "
"chính xác dựa trên dữ liệu sản phẩm.\n\n"
"[italic]Được phát triển như một dự án cuối kỳ.[/italic]",
border_style="blue",
title="Thông tin",
)
)
@app.command()
def version():
"""Hiển thị phiên bản của hệ thống."""
console.print("[bold cyan]Hệ thống hỏi đáp sản phẩm[/bold cyan] v1.0.0")
@app.callback()
def main():
"""Chương trình dòng lệnh để truy vấn hệ thống hỏi đáp sản phẩm điện tử."""
pass
if __name__ == "__main__":
app()