2025-02-18 00:07:19 +08:00
|
|
|
from config.logger import setup_logging
|
2025-02-11 23:13:18 +08:00
|
|
|
import openai
|
|
|
|
|
from core.providers.llm.base import LLMProviderBase
|
|
|
|
|
|
2025-02-18 00:07:19 +08:00
|
|
|
TAG = __name__
|
|
|
|
|
logger = setup_logging()
|
2025-02-11 23:13:18 +08:00
|
|
|
|
2025-02-14 00:54:59 +08:00
|
|
|
|
2025-02-11 23:13:18 +08:00
|
|
|
class LLMProvider(LLMProviderBase):
|
|
|
|
|
def __init__(self, config):
|
|
|
|
|
self.model_name = config.get("model_name")
|
|
|
|
|
self.api_key = config.get("api_key")
|
|
|
|
|
if 'base_url' in config:
|
|
|
|
|
self.base_url = config.get("base_url")
|
|
|
|
|
else:
|
|
|
|
|
self.base_url = config.get("url")
|
2025-02-14 00:54:59 +08:00
|
|
|
if "你" in self.api_key:
|
2025-02-18 00:07:19 +08:00
|
|
|
logger.bind(tag=TAG).error("你还没配置LLM的密钥,请在配置文件中配置密钥,否则无法正常工作")
|
2025-02-11 23:13:18 +08:00
|
|
|
self.client = openai.OpenAI(api_key=self.api_key, base_url=self.base_url)
|
|
|
|
|
|
|
|
|
|
def response(self, session_id, dialogue):
|
|
|
|
|
try:
|
|
|
|
|
responses = self.client.chat.completions.create(
|
|
|
|
|
model=self.model_name,
|
|
|
|
|
messages=dialogue,
|
|
|
|
|
stream=True
|
|
|
|
|
)
|
2025-02-24 16:16:06 +08:00
|
|
|
|
|
|
|
|
is_active = True
|
2025-02-11 23:13:18 +08:00
|
|
|
for chunk in responses:
|
2025-02-24 16:16:06 +08:00
|
|
|
try:
|
|
|
|
|
# 检查是否存在有效的choice且content不为空
|
|
|
|
|
delta = chunk.choices[0].delta if getattr(chunk, 'choices', None) else None
|
|
|
|
|
content = delta.content if hasattr(delta, 'content') else ''
|
|
|
|
|
except IndexError:
|
|
|
|
|
content = ''
|
|
|
|
|
if content:
|
|
|
|
|
# 处理标签跨多个chunk的情况
|
|
|
|
|
if '<think>' in content:
|
|
|
|
|
is_active = False
|
|
|
|
|
content = content.split('<think>')[0]
|
|
|
|
|
if '</think>' in content:
|
|
|
|
|
is_active = True
|
|
|
|
|
content = content.split('</think>')[-1]
|
|
|
|
|
if is_active:
|
2025-02-11 23:13:18 +08:00
|
|
|
yield content
|
2025-02-24 16:16:06 +08:00
|
|
|
|
2025-02-11 23:13:18 +08:00
|
|
|
except Exception as e:
|
2025-02-18 00:07:19 +08:00
|
|
|
logger.bind(tag=TAG).error(f"Error in response generation: {e}")
|