|
|
@@ -79,53 +79,57 @@ class OllamaService:
|
|
|
# 示例帮助模型理解格式
|
|
|
example = '{"entities": [{"name": "成都市", "type": "LOC", "charStart": 10, "charEnd": 13}, {"name": "2024年5月", "type": "DATE", "charStart": 0, "charEnd": 7}]}'
|
|
|
|
|
|
- # /no_think 指令用于禁用 Qwen3 的思考模式
|
|
|
- prompt = f"""/no_think
|
|
|
-你是一个命名实体识别(NER)专家。请从以下文本中提取命名实体。
|
|
|
+ # 简洁直接的 prompt,要求模型只输出 JSON
|
|
|
+ prompt = f"""请从以下文本中提取命名实体,直接输出JSON,不要解释。
|
|
|
|
|
|
-【任务要求】
|
|
|
-1. 只输出JSON格式,不要输出任何解释或思考过程
|
|
|
-2. 实体类型: {types_desc}
|
|
|
-3. charStart和charEnd是实体在文本中的字符位置索引(从0开始)
|
|
|
+实体类型: {types_desc}
|
|
|
|
|
|
-【输出格式】
|
|
|
+输出格式示例:
|
|
|
{example}
|
|
|
|
|
|
-【待处理文本】
|
|
|
+文本:
|
|
|
{text}
|
|
|
|
|
|
-【JSON输出】"""
|
|
|
+请直接输出JSON:"""
|
|
|
return prompt
|
|
|
|
|
|
async def _call_ollama(self, prompt: str, disable_thinking: bool = True) -> Optional[str]:
|
|
|
"""
|
|
|
- 调用 Ollama API
|
|
|
+ 调用 Ollama Chat API
|
|
|
|
|
|
Args:
|
|
|
prompt: 输入提示词
|
|
|
disable_thinking: 是否禁用思考模式(适用于 Qwen3 等支持思考的模型)
|
|
|
"""
|
|
|
- url = f"{self.base_url}/api/generate"
|
|
|
+ # 使用 /api/chat 接口,think 参数仅在此接口生效
|
|
|
+ url = f"{self.base_url}/api/chat"
|
|
|
payload = {
|
|
|
"model": self.model,
|
|
|
- "prompt": prompt,
|
|
|
+ "messages": [
|
|
|
+ {
|
|
|
+ "role": "user",
|
|
|
+ "content": prompt
|
|
|
+ }
|
|
|
+ ],
|
|
|
"stream": False,
|
|
|
"options": {
|
|
|
"temperature": 0.1, # 低温度,更确定性的输出
|
|
|
- "num_predict": 20480, # 最大输出 token
|
|
|
}
|
|
|
}
|
|
|
|
|
|
# Qwen3 思考模式:禁用思考,直接输出 JSON 结果
|
|
|
- # 思考模式会导致 token 用于推理过程,无法输出最终结果
|
|
|
- payload["think"] = False
|
|
|
+ # think 参数仅在 /api/chat 接口中生效
|
|
|
+ if disable_thinking:
|
|
|
+ payload["think"] = False
|
|
|
|
|
|
try:
|
|
|
async with httpx.AsyncClient(timeout=self.timeout) as client:
|
|
|
response = await client.post(url, json=payload)
|
|
|
response.raise_for_status()
|
|
|
result = response.json()
|
|
|
- return result.get("response", "")
|
|
|
+ # chat 接口返回格式: {"message": {"role": "assistant", "content": "..."}}
|
|
|
+ message = result.get("message", {})
|
|
|
+ return message.get("content", "")
|
|
|
except httpx.TimeoutException:
|
|
|
logger.error(f"Ollama 请求超时: timeout={self.timeout}s")
|
|
|
return None
|