123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- from typing import List
- from applications.config import Chunk
- from applications.api import fetch_deepseek_completion
- class ChatClassifier:
- @staticmethod
- def generate_summary_prompt(query, search_results):
- """
- 生成总结的prompt。
- :param query: 问题
- :param search_results: 搜索结果列表,每个元素包含 'content', 'contentSummary', 'score'
- :return: 生成的总结prompt
- """
- # 为了让AI更好地理解,我们将使用以下格式构建prompt:
- prompt = f"问题: {query}\n\n请结合以下搜索结果,生成一个总结:\n"
- # 先生成基于相似度加权的summary
- weighted_summaries = []
- weighted_contents = []
- for result in search_results:
- content = result['content']
- content_summary = result['contentSummary']
- score = result['score']
- # 加权内容摘要和内容
- weighted_summaries.append((content_summary, score))
- weighted_contents.append((content, score))
- # 为了生成更准确的总结,基于相似度加权内容和摘要
- weighted_summaries.sort(key=lambda x: x[1], reverse=True) # 按相似度降序排列
- weighted_contents.sort(key=lambda x: x[1], reverse=True) # 按相似度降序排列
- # 将加权的摘要和内容加入到prompt中
- prompt += "\n-- 加权内容摘要 --\n"
- for summary, score in weighted_summaries:
- prompt += f"摘要: {summary} | 相似度: {score:.2f}\n"
- prompt += "\n-- 加权内容 --\n"
- for content, score in weighted_contents:
- prompt += f"内容: {content} | 相似度: {score:.2f}\n"
- # 最后请求AI进行总结
- prompt += "\n基于上述内容,请帮我生成一个简洁的总结。"
- return prompt
- async def chat_with_deepseek(self, query, search_results):
- prompt = self.generate_summary_prompt(query, search_results)
- response = await fetch_deepseek_completion(
- model="DeepSeek-V3", prompt=prompt
- )
- return response
|