Spaces:
Sleeping
Sleeping
fix: missing definition of chat
Browse files- modules/ai_model.py +31 -2
modules/ai_model.py
CHANGED
|
@@ -140,7 +140,7 @@ class AIModel:
|
|
| 140 |
else: # text
|
| 141 |
return input_type, None, raw_input
|
| 142 |
|
| 143 |
-
def run_inference(self, input_type: str, formatted_input: Union[str, Image.Image], prompt: str) -> str:
|
| 144 |
|
| 145 |
try:
|
| 146 |
if len(prompt) > 500:
|
|
@@ -176,7 +176,7 @@ class AIModel:
|
|
| 176 |
**inputs,
|
| 177 |
max_new_tokens=256,
|
| 178 |
do_sample=True,
|
| 179 |
-
temperature=
|
| 180 |
top_p=0.9,
|
| 181 |
pad_token_id=self.processor.tokenizer.eos_token_id,
|
| 182 |
use_cache=True
|
|
@@ -198,6 +198,35 @@ class AIModel:
|
|
| 198 |
except Exception as e:
|
| 199 |
log.error(f"❌ 模型推理失败: {e}", exc_info=True)
|
| 200 |
return "抱歉,处理您的请求时遇到技术问题。"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
def _build_limited_prompt(self, processed_text: str, context: str = "") -> str:
|
| 203 |
"""构建长度受限的prompt - 新增辅助方法"""
|
|
|
|
| 140 |
else: # text
|
| 141 |
return input_type, None, raw_input
|
| 142 |
|
| 143 |
+
def run_inference(self, input_type: str, formatted_input: Union[str, Image.Image], prompt: str,temperature: float = 0.7) -> str:
|
| 144 |
|
| 145 |
try:
|
| 146 |
if len(prompt) > 500:
|
|
|
|
| 176 |
**inputs,
|
| 177 |
max_new_tokens=256,
|
| 178 |
do_sample=True,
|
| 179 |
+
temperature=temperature,
|
| 180 |
top_p=0.9,
|
| 181 |
pad_token_id=self.processor.tokenizer.eos_token_id,
|
| 182 |
use_cache=True
|
|
|
|
| 198 |
except Exception as e:
|
| 199 |
log.error(f"❌ 模型推理失败: {e}", exc_info=True)
|
| 200 |
return "抱歉,处理您的请求时遇到技术问题。"
|
| 201 |
+
|
| 202 |
+
def chat_completion(self, model: str, messages: list, **kwargs) -> str:
|
| 203 |
+
|
| 204 |
+
if not self.is_available():
|
| 205 |
+
log.error("模型未就绪,无法执行 chat_completion")
|
| 206 |
+
# 对于需要JSON输出的场景,返回一个表示错误的有效JSON字符串
|
| 207 |
+
if kwargs.get("response_format", {}).get("type") == "json_object":
|
| 208 |
+
return '{"error": "Model not available"}'
|
| 209 |
+
return "抱歉,AI 模型当前不可用。"
|
| 210 |
+
|
| 211 |
+
full_prompt = "\n".join([msg.get("content", "") for msg in messages])
|
| 212 |
+
|
| 213 |
+
temperature = kwargs.get("temperature", 0.7)
|
| 214 |
+
|
| 215 |
+
if kwargs.get("response_format", {}).get("type") == "json_object":
|
| 216 |
+
# 在 prompt 末尾添加指令,强制模型输出 JSON
|
| 217 |
+
full_prompt += "\n\n请注意:你的回答必须是一个严格的、不含任何额外解释和代码块标记的 JSON 对象。"
|
| 218 |
+
# 对于JSON生成任务,使用较低的 temperature 以获得更稳定、确定性的结构
|
| 219 |
+
temperature = 0.1
|
| 220 |
+
|
| 221 |
+
log.debug(f"▶️ 执行 chat_completion (适配器), temperature={temperature}, prompt='{full_prompt[:100]}...'")
|
| 222 |
+
|
| 223 |
+
return self.run_inference(
|
| 224 |
+
input_type="text",
|
| 225 |
+
formatted_input=None,
|
| 226 |
+
prompt=full_prompt,
|
| 227 |
+
temperature=temperature # 将处理后的 temperature 传递下去
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
|
| 231 |
def _build_limited_prompt(self, processed_text: str, context: str = "") -> str:
|
| 232 |
"""构建长度受限的prompt - 新增辅助方法"""
|