test_intent.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # !/usr/bin/ python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. @Project : xiwu-agent-api
  5. @File :intent.py
  6. @IDE :PyCharm
  7. @Author :LINGMIN
  8. @Date :2025/7/14 12:04
  9. '''
  10. import os
  11. import sys
  12. sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
  13. from foundation.observability.logger.loggering import server_logger
  14. from foundation.ai.models import get_models
  15. from langchain_core.prompts import SystemMessagePromptTemplate
  16. from langchain_core.prompts import HumanMessagePromptTemplate
  17. from langchain_core.prompts import ChatPromptTemplate
  18. from langchain_core.prompts import FewShotChatMessagePromptTemplate
  19. from foundation.utils import yaml_utils
  20. from foundation.infrastructure.config import config_handler
  21. class TestIntentIdentifyClient:
  22. def __init__(self):
  23. """
  24. 创建意图识别类
  25. """
  26. # 获取部署的模型列表
  27. llm, chat, embed = get_models()
  28. self.llm_recognition = chat
  29. # 加载 意图识别系统配置信息
  30. self.intent_prompt = yaml_utils.get_intent_prompt()
  31. def recognize_intent(self , trace_id: str , config: dict , input: str):
  32. """
  33. 意图识别
  34. 输入:用户输入的问题
  35. 输出:识别出的意图,可选项:
  36. """
  37. session_id = config["session_id"]
  38. history = "无"
  39. # 根据历史记录和用户问题进行识别意图
  40. return self.recognize_intent_history(input=input , history=history)
  41. def recognize_intent_history(self , input: str , history="无"):
  42. """
  43. 意图识别
  44. 输入:用户输入的问题
  45. 输出:识别出的意图,可选项:
  46. """
  47. # 准备few-shot样例
  48. examples = self.intent_prompt["intent_examples"]
  49. #server_logger.info(f"加载prompt配置.examples: {examples}")
  50. system_prompt = self.intent_prompt["system_prompt"]
  51. system_prompt = system_prompt.format(history=history)
  52. server_logger.info(f"增加用户历史记录,用于意图识别,prompt配置.system_prompt: {system_prompt}")
  53. # 定义样本模板
  54. examples_prompt = ChatPromptTemplate.from_messages(
  55. [
  56. ("human", "{inn}"),
  57. ("ai", "{out}"),
  58. ]
  59. )
  60. few_shot_prompt = FewShotChatMessagePromptTemplate(example_prompt=examples_prompt,
  61. examples=examples)
  62. final_prompt = ChatPromptTemplate.from_messages(
  63. [
  64. ('system', system_prompt),
  65. few_shot_prompt,
  66. ('human', '{input}'),
  67. ]
  68. )
  69. chain = final_prompt | self.llm_recognition
  70. server_logger.info(f"意图识别输入input: {input}")
  71. result = chain.invoke(input={"input": input})
  72. # 容错处理
  73. if hasattr(result, 'content'):
  74. # 如果 result 有 content 属性,使用它
  75. return result.content
  76. else:
  77. # 否则,直接返回 result
  78. return result
  79. intent_identify_client = TestIntentIdentifyClient()
  80. if __name__ == '__main__':
  81. input = "你好"
  82. input = "查询课程"
  83. input = "操作"
  84. result = intent_identify_client.recognize_intent_history(history="" , input=input)
  85. server_logger.info(f"result={result}")