# !/usr/bin/ python # -*- coding: utf-8 -*- ''' @Project : lq-agent-api @File :intent.py @IDE :PyCharm @Author : @Date :2025/7/14 12:04 ''' import os import sys sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from logger.loggering import server_logger from utils.utils import get_models from langchain_core.prompts import SystemMessagePromptTemplate from langchain_core.prompts import HumanMessagePromptTemplate from langchain_core.prompts import ChatPromptTemplate from langchain_core.prompts import FewShotChatMessagePromptTemplate from utils.yaml_utils import fixed_question_intent_config class FixedIntentIdentifyClient: def __init__(self): """ 创建意图识别类 """ # 获取部署的模型列表 llm, chat, embed = get_models() self.llm_recognition = chat # 加载 意图识别系统配置信息 self.intent_prompt = fixed_question_intent_config def recognize_intent(self, input): """ 意图识别 输入:用户输入的问题 输出:识别出的意图,可选项: """ # 准备few-shot样例 examples = self.intent_prompt["fixed_problem_answer"] #server_logger.info(f"加载prompt配置.examples: {examples}") # 定义样本模板 examples_prompt = ChatPromptTemplate.from_messages( [ ("human", "{inn}"), ("ai", "{out}"), ] ) few_shot_prompt = FewShotChatMessagePromptTemplate(example_prompt=examples_prompt, examples=examples) final_prompt = ChatPromptTemplate.from_messages( [ ('system', self.intent_prompt["system_prompt"]), few_shot_prompt, ('human', '{input}'), ] ) chain = final_prompt | self.llm_recognition result = chain.invoke(input=input) # 容错处理 if hasattr(result, 'content'): # 如果 result 有 content 属性,使用它 return result.content else: # 否则,直接返回 result return result fixed_intent_identify_client = FixedIntentIdentifyClient() if __name__ == '__main__': #input = "你好" #input = "我要查询信息" #result=cattle_farm_query input = "查询最近步数情况" #result=cattle_farm_query input = "这套系统给我带来了什么好处" #result=cattle_farm_warning_plan input = "建设数字化后给我带来了多少效益" #result=cattle_farm_warning_task_execute #input = "查询11基本信息" #input = "查询16号的信息,返回json" # input = "查询16号的信息,返回markdown" # input = "编号为0014的数据,返回JSON格式数据" # input = "查询0013的信息" # input = "查询0013的数据" # input = """ result = fixed_intent_identify_client.recognize_intent(input) server_logger.info(f"input={input} ,result={result}")