hyf-backend/th_agenter/llm/llm_model_chat.py

30 lines
1.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from langchain_openai import ChatOpenAI
from loguru import logger
from DrGraph.utils.Constant import Constant
from LLM.llm_model_base import LLM_Model_Base
class Chat_LLM(LLM_Model_Base):
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_CHAT
self.llmModel = ChatOpenAI(
model_name=self.model_name,
temperature=self.temperature,
)
# 返回消息格式以便在chatbot中显示
def invoke(self, prompt: str):
prompt_template_value = self.buildPromptTemplateValue(
prompt=prompt,
methodType=Constant.LLM_PROMPT_TEMPLATE_METHOD_INVOKE,
valueType=Constant.LLM_PROMPT_VALUE_MESSAGES)
try:
response = self.llmModel.invoke(prompt_template_value)
logger.info(f"{self.name} >>> 2. 助手回复: {type(response)}\n{response}")
# response = {"role": "assistant", "content": response.content}
except Exception as e:
logger.error(e)
return response