44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
|
|
'''
|
|||
|
|
非聊天模型类,继承自 LLM_Model_Base
|
|||
|
|
|
|||
|
|
author: DrGraph
|
|||
|
|
date: 2025-11-20
|
|||
|
|
'''
|
|||
|
|
from loguru import logger
|
|||
|
|
from langchain_openai import OpenAI
|
|||
|
|
from langchain_core.messages import AIMessage
|
|||
|
|
from DrGraph.utils.Constant import Constant
|
|||
|
|
from LLM.llm_model_base import LLM_Model_Base
|
|||
|
|
|
|||
|
|
|
|||
|
|
class NonChat_LLM(LLM_Model_Base):
|
|||
|
|
'''
|
|||
|
|
非聊天模型类,继承自 LLM_Model_Base,调用这个非聊天模型OpenAI
|
|||
|
|
- 语言模型名称, 缺省为"gpt-4o-mini"
|
|||
|
|
- 温度,缺省为0.7
|
|||
|
|
- 语言模型名称 = "非聊天模型", 在人机界面中显示
|
|||
|
|
'''
|
|||
|
|
def __init__(self, model_name: str = "gpt-4o-mini", temperature: float = 0.7):
|
|||
|
|
super().__init__(model_name, temperature)
|
|||
|
|
self.name = '非聊天模型'
|
|||
|
|
self.mode = Constant.LLM_MODE_NONCHAT
|
|||
|
|
self.llmModel = OpenAI(
|
|||
|
|
model_name=self.model_name,
|
|||
|
|
temperature=self.temperature,
|
|||
|
|
)
|
|||
|
|
# 返回消息格式,以便在chatbot中显示
|
|||
|
|
def invoke(self, prompt: str):
|
|||
|
|
'''
|
|||
|
|
调用非聊天模型,返回消息格式,以便在chatbot中显示
|
|||
|
|
prompt: 用户输入,为字符串类型
|
|||
|
|
return: 助手回复,为字符串类型
|
|||
|
|
'''
|
|||
|
|
logger.info(f"{self.name} >>> 1.1 用户输入: {type(prompt)}")
|
|||
|
|
try:
|
|||
|
|
response = self.llmModel.invoke(prompt)
|
|||
|
|
logger.info(f"{self.name} >>> 1.2 助手回复: {type(response)}")
|
|||
|
|
except Exception as e:
|
|||
|
|
logger.error(e)
|
|||
|
|
return response
|
|||
|
|
|
|||
|
|
|