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:
@@ -5,7 +5,7 @@ LLM 软规则审查器
|
||||
|
||||
import os
|
||||
import json
|
||||
from typing import Optional
|
||||
from typing import Optional, List
|
||||
from dataclasses import dataclass
|
||||
from dotenv import load_dotenv
|
||||
|
||||
@@ -36,7 +36,8 @@ class LLMReviewer:
|
||||
self,
|
||||
user_input: str,
|
||||
execution_plan: str,
|
||||
code: str
|
||||
code: str,
|
||||
warnings: Optional[List[str]] = None
|
||||
) -> LLMReviewResult:
|
||||
"""
|
||||
审查代码安全性
|
||||
@@ -45,6 +46,7 @@ class LLMReviewer:
|
||||
user_input: 用户原始需求
|
||||
execution_plan: 执行计划
|
||||
code: 待审查的代码
|
||||
warnings: 静态检查产生的警告列表
|
||||
|
||||
Returns:
|
||||
LLMReviewResult: 审查结果
|
||||
@@ -52,20 +54,26 @@ class LLMReviewer:
|
||||
try:
|
||||
client = get_client()
|
||||
|
||||
# 构建警告信息
|
||||
warning_text = ""
|
||||
if warnings and len(warnings) > 0:
|
||||
warning_text = "\n\n【静态检查警告】请重点审查以下内容:\n" + "\n".join(f"- {w}" for w in warnings)
|
||||
|
||||
messages = [
|
||||
{"role": "system", "content": SAFETY_REVIEW_SYSTEM},
|
||||
{"role": "user", "content": SAFETY_REVIEW_USER.format(
|
||||
user_input=user_input,
|
||||
execution_plan=execution_plan,
|
||||
code=code
|
||||
)}
|
||||
) + warning_text}
|
||||
]
|
||||
|
||||
response = client.chat(
|
||||
messages=messages,
|
||||
model=self.model_name,
|
||||
temperature=0.1,
|
||||
max_tokens=512
|
||||
max_tokens=512,
|
||||
timeout=120
|
||||
)
|
||||
|
||||
return self._parse_response(response)
|
||||
@@ -124,9 +132,9 @@ class LLMReviewer:
|
||||
def review_code_safety(
|
||||
user_input: str,
|
||||
execution_plan: str,
|
||||
code: str
|
||||
code: str,
|
||||
warnings: Optional[List[str]] = None
|
||||
) -> LLMReviewResult:
|
||||
"""便捷函数:审查代码安全性"""
|
||||
reviewer = LLMReviewer()
|
||||
return reviewer.review(user_input, execution_plan, code)
|
||||
|
||||
return reviewer.review(user_input, execution_plan, code, warnings)
|
||||
|
||||
Reference in New Issue
Block a user