feat: implement streaming support for chat and enhance safety review process
- Updated .env.example to include API key placeholder and configuration instructions. - Refactored main.py to support streaming responses from the LLM, improving user experience during chat interactions. - Enhanced LLMClient to include methods for streaming chat and collecting responses. - Modified safety review process to pass static analysis warnings to the LLM for better code safety evaluation. - Improved UI components in chat_view.py to handle streaming messages effectively.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""
|
||||
聊天视图组件
|
||||
处理普通对话的 UI 展示
|
||||
处理普通对话的 UI 展示 - 支持流式消息
|
||||
"""
|
||||
|
||||
import tkinter as tk
|
||||
@@ -16,6 +16,7 @@ class ChatView:
|
||||
- 消息显示区域
|
||||
- 输入框
|
||||
- 发送按钮
|
||||
- 流式消息支持
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
@@ -33,6 +34,10 @@ class ChatView:
|
||||
self.parent = parent
|
||||
self.on_send = on_send
|
||||
|
||||
# 流式消息状态
|
||||
self._stream_active = False
|
||||
self._stream_tag = None
|
||||
|
||||
self._create_widgets()
|
||||
|
||||
def _create_widgets(self):
|
||||
@@ -71,6 +76,7 @@ class ChatView:
|
||||
self.message_area.tag_configure('assistant', foreground='#81c784', font=('Microsoft YaHei UI', 11))
|
||||
self.message_area.tag_configure('system', foreground='#ffb74d', font=('Microsoft YaHei UI', 10, 'italic'))
|
||||
self.message_area.tag_configure('error', foreground='#ef5350', font=('Microsoft YaHei UI', 10))
|
||||
self.message_area.tag_configure('streaming', foreground='#81c784', font=('Microsoft YaHei UI', 11))
|
||||
|
||||
# 输入区域框架
|
||||
input_frame = tk.Frame(self.frame, bg='#1e1e1e')
|
||||
@@ -147,6 +153,55 @@ class ChatView:
|
||||
self.message_area.see(tk.END)
|
||||
self.message_area.config(state=tk.DISABLED)
|
||||
|
||||
def start_stream_message(self, tag: str = 'assistant'):
|
||||
"""
|
||||
开始流式消息
|
||||
|
||||
Args:
|
||||
tag: 消息类型
|
||||
"""
|
||||
self._stream_active = True
|
||||
self._stream_tag = tag
|
||||
|
||||
self.message_area.config(state=tk.NORMAL)
|
||||
|
||||
# 添加前缀
|
||||
prefix_map = {
|
||||
'user': '[你] ',
|
||||
'assistant': '[助手] ',
|
||||
'system': '[系统] ',
|
||||
'error': '[错误] '
|
||||
}
|
||||
prefix = prefix_map.get(tag, '')
|
||||
|
||||
self.message_area.insert(tk.END, "\n" + prefix, tag)
|
||||
self.message_area.see(tk.END)
|
||||
# 保持 NORMAL 状态以便追加内容
|
||||
|
||||
def append_stream_chunk(self, chunk: str):
|
||||
"""
|
||||
追加流式消息片段
|
||||
|
||||
Args:
|
||||
chunk: 消息片段
|
||||
"""
|
||||
if not self._stream_active:
|
||||
return
|
||||
|
||||
self.message_area.insert(tk.END, chunk, self._stream_tag)
|
||||
self.message_area.see(tk.END)
|
||||
# 强制更新 UI
|
||||
self.message_area.update_idletasks()
|
||||
|
||||
def end_stream_message(self):
|
||||
"""结束流式消息"""
|
||||
if self._stream_active:
|
||||
self.message_area.insert(tk.END, "\n")
|
||||
self.message_area.see(tk.END)
|
||||
self.message_area.config(state=tk.DISABLED)
|
||||
self._stream_active = False
|
||||
self._stream_tag = None
|
||||
|
||||
def clear_messages(self):
|
||||
"""清空消息区域"""
|
||||
self.message_area.config(state=tk.NORMAL)
|
||||
|
||||
Reference in New Issue
Block a user