model_generate.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # !/usr/bin/ python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. @Project : lq-agent-api
  5. @File :model_generate.py
  6. @IDE :PyCharm
  7. @Author :
  8. @Date :2025/7/14 14:22
  9. '''
  10. from typing import Dict, Optional
  11. from langchain_core.prompts import HumanMessagePromptTemplate
  12. from langchain_core.prompts import ChatPromptTemplate
  13. from foundation.utils.utils import get_models
  14. from foundation.utils.yaml_utils import system_prompt_config
  15. class TestGenerateModelClient:
  16. """
  17. 主要是生成式模型
  18. """
  19. def __init__(self):
  20. # 获取部署的模型列表
  21. llm, chat, embed = get_models()
  22. self.llm = llm
  23. self.chat = chat
  24. # 固定系统提示词
  25. self.system_prompt = system_prompt_config["system_prompt"]
  26. def get_prompt_template(self):
  27. """
  28. 构造普通Prompt提示词模板
  29. """
  30. human_template = """
  31. {system_message}
  32. 用户的问题为:
  33. {question}
  34. 答案为:
  35. """
  36. human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
  37. chat_prompt_template = ChatPromptTemplate.from_messages([human_message_prompt])
  38. return chat_prompt_template
  39. def get_model_generate_invoke(self, trace_id, task_prompt_info: dict, input_query, context=None):
  40. """
  41. 模型生成链
  42. """
  43. # Step 1: 定义系统提示词模板 system_prompt
  44. # Step 2: 构建完整的 prompt 模板
  45. prompt_template = ChatPromptTemplate.from_messages([
  46. ("system", self.system_prompt), #task_prompt_info["task_prompt"]
  47. ("human", "{input}")
  48. ])
  49. # Step 3: 初始化模型
  50. # Step 4: 使用模板格式化输入
  51. messages = prompt_template.invoke({"input": input_query})
  52. # Step 5: 流式调用模型
  53. response = self.llm.invoke(messages)
  54. return response.content
  55. def get_model_generate_stream(self, trace_id, task_prompt_info: dict, input_query, context=None):
  56. """
  57. 模型生成链
  58. """
  59. # Step 1: 定义系统提示词模板 system_prompt
  60. # Step 2: 构建完整的 prompt 模板
  61. prompt_template = ChatPromptTemplate.from_messages([
  62. ("system", self.system_prompt), #task_prompt_info["task_prompt"]
  63. ("human", "{input}")
  64. ])
  65. # Step 3: 初始化模型
  66. # Step 4: 使用模板格式化输入
  67. messages = prompt_template.invoke({"input": input_query})
  68. # Step 5: 流式调用模型
  69. response = self.llm.stream(messages)
  70. # Step 6: 逐 token 输出(打字机效果)
  71. for chunk in response:
  72. yield chunk.content
  73. def get_input_context(
  74. self,
  75. trace_id: str,
  76. task_prompt_info: dict,
  77. input_query: str,
  78. context: Optional[str] = None
  79. ) -> str:
  80. #server_logger.info(f"task_prompt_info: {task_prompt_info}")
  81. """构建问题和上下文"""
  82. context = context or "无"
  83. task_prompt_info_str = task_prompt_info["task_prompt"]
  84. # 针对场景优化的上下文提示
  85. base_context_prompt = """
  86. 日志链路跟踪ID:{trace_id}
  87. 任务信息:{task_prompt_info_str}
  88. 相关上下文数据:{context}
  89. 户问题:{input}
  90. """
  91. return base_context_prompt.format(
  92. trace_id=trace_id,
  93. task_prompt_info_str=task_prompt_info_str,
  94. context=context,
  95. input=input_query
  96. )
  97. #
  98. test_generate_model_client = TestGenerateModelClient()