wangzhibo
2026-08-30 c8515170fd399b4d2dfaebb59128d4674e58915e
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
import json
from config.logger import setup_logging
import requests
from core.providers.llm.base import LLMProviderBase
from core.utils.util import check_model_key
 
TAG = __name__
logger = setup_logging()
 
 
class LLMProvider(LLMProviderBase):
    def __init__(self, config):
        self.api_key = config["api_key"]
        self.base_url = config.get("base_url")
        self.detail = config.get("detail", False)
        self.variables = config.get("variables", {})
        model_key_msg = check_model_key("FastGPTLLM", self.api_key)
        if model_key_msg:
            logger.bind(tag=TAG).error(model_key_msg)
 
    def response(self, session_id, dialogue, **kwargs):
        # 取最后一条用户消息
        last_msg = next(m for m in reversed(dialogue) if m["role"] == "user")
 
        # 发起流式请求
        with requests.post(
            f"{self.base_url}/chat/completions",
            headers={"Authorization": f"Bearer {self.api_key}"},
            json={
                "stream": True,
                "chatId": session_id,
                "detail": self.detail,
                "variables": self.variables,
                "messages": [{"role": "user", "content": last_msg["content"]}],
            },
            stream=True,
        ) as r:
            for line in r.iter_lines():
                if line:
                    try:
                        if line.startswith(b"data: "):
                            if line[6:].decode("utf-8") == "[DONE]":
                                break
 
                            data = json.loads(line[6:])
                            if "choices" in data and len(data["choices"]) > 0:
                                delta = data["choices"][0].get("delta", {})
                                if (
                                    delta
                                    and "content" in delta
                                    and delta["content"] is not None
                                ):
                                    content = delta["content"]
                                    if "<think>" in content:
                                        continue
                                    if "</think>" in content:
                                        continue
                                    yield content
 
                    except json.JSONDecodeError as e:
                        continue
                    except Exception as e:
                        continue
 
    def response_with_functions(self, session_id, dialogue, functions=None):
        logger.bind(tag=TAG).error(
            f"fastgpt暂未实现完整的工具调用(function call),建议使用其他意图识别"
        )