浏览代码

v1.0
服务上线

罗俊辉 1 年之前
父节点
当前提交
aaa0c62c42
共有 5 个文件被更改,包括 122 次插入38 次删除
  1. 4 4
      applications/ai.py
  2. 28 23
      applications/pipeline.py
  3. 8 6
      deal/matchArticle_deal.py
  4. 6 5
      task.py
  5. 76 0
      test/prompt_dev.py

+ 4 - 4
applications/ai.py

@@ -71,7 +71,7 @@ def tencent_ai(prompt):
     client = hunyuan_client.HunyuanClient(cred, "ap-beijing", clientProfile)
     req = models.ChatCompletionsRequest()
     params = {
-        "Model": "hunyuan-pro",
+        "Model": "hunyuan-lite",
         "Messages": [
             {
                 "Role": "user",
@@ -99,9 +99,9 @@ def tly_ai(prompt):
     TLY = TavilyClient(api_key="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")
     response = TLY.search(
         query=prompt,
-        search_depth="advanced",
+        # search_depth="advanced",
         include_images=True,
-        include_answer=True,
-        include_raw_content=True
+        # include_answer=True,
+        # include_raw_content=True
     )
     return response

+ 28 - 23
applications/pipeline.py

@@ -4,7 +4,7 @@ prompt pipeline
 """
 import json
 
-from applications.ai import kimi_ai, tly_ai
+from applications.ai import kimi_ai, tly_ai, tencent_ai
 
 
 # 第一步先把问题裂变
@@ -15,15 +15,12 @@ def question_fission(query):
     :return:
     """
     prompt = f"""
-        # 角色
         - 你是一个提问裂变器,能够将用户提供的搜索query裂变成4个不同方向的提问,以便获取更加全面的搜索结果。
         
-        ## 任务
-        - 根据用户提供的搜索query:{query},生成5个在主题一致但方向有所差别的提问。
+        - 根据搜索query:'{query}',生成5个在主题一致但方向有所差别的提问。
         - 确保生成的提问简洁明了,直接输出5个问句。
         - 以JSON格式输出5个问句,每个问句可以分别引用。
-        
-        ## 技能
+        你拥有以下技能
         - 技能1:语义理解与转换
           1. 理解用户提供的搜索query的核心主题和意图。
           2. 在保持主题一致的前提下,生成不同方向的提问。
@@ -36,24 +33,23 @@ def question_fission(query):
           1. 能够处理任何主题的搜索query。
           2. 生成的提问适用于各种搜索引擎和信息检索场景。
         
-        ## 限制
+        限制
         - 每次生成的提问数量固定为5个。
         - 不添加额外的说明或解释,直接输出5个问句。
         - 不考虑特定的关键词或排除某些关键词。
         - 以JSON格式输出问句,每个问句可以分别引用。
         
-        ## 输出格式模板
-        ```json
-        {
-            "question1": "问句1",
-            "question2": "问句2",
-            "question3": "问句3",
-            "question4": "问句4",
-            "question5": "问句5"
-        }
+        输出格式模板
+        key, value分别是
+        "question1": "问句1",
+        "question2": "问句2",
+        "question3": "问句3",
+        "question4": "问句4",
+        "question5": "问句5"
     """
+    # print(prompt)
     question_dict = kimi_ai(prompt=prompt)
-    return question_dict
+    return json.loads(question_dict.replace("'", '"'))
 
 
 # 第二步搜索内容
@@ -67,6 +63,20 @@ def search_materials(question):
     return response
 
 
+def generate_text(question):
+    """
+    使用kimi生成文章
+    :param question:
+    :return:
+    """
+    prompt = f"""
+    通过给到你的这个问题:'{question}',
+    生成一篇小文章,文章需要有逻辑,有参考意义
+    """
+    text = tencent_ai(prompt)
+    return text
+
+
 # 第三步,清洗,筛选,总结, 生成文章
 def summary_articles(materials):
     """
@@ -99,14 +109,9 @@ def summary_articles(materials):
     ## OutputFormat:
     返回json格式,如下
     {
-        "title": 总结上述材料的标题,
+    "title": 总结上述材料的标题,
         "text":  编写的文章
     }
     """
     response = kimi_ai(prompt=prompt)
     return img_list, response['title'], response['text']
-
-
-
-
-

+ 8 - 6
deal/matchArticle_deal.py

@@ -10,7 +10,7 @@ from uuid import uuid4
 
 from applications.config import db_config
 from applications.functions import whisper
-from applications.pipeline import question_fission, search_materials, summary_articles
+from applications.pipeline import question_fission, search_materials, summary_articles, generate_text
 
 
 class MatchArticlesTask(object):
@@ -71,11 +71,13 @@ class MatchArticlesTask(object):
             task_id, title, text = task_tuple
             # 先用视频标题作为query, 后续可逐步优化
             question_dict = question_fission(title)
-            material_list = []
+            material_dict = {}
             for question_key in question_dict:
-                material = search_materials(question=question_dict[question_key])
-                material_list.append(material)
-            material_result = json.dumps(material_list, ensure_ascii=False)
+                question = question_dict[question_key]
+                material = generate_text(question)
+                material_dict[question] = material_dict
+
+            material_result = json.dumps(material_dict, ensure_ascii=False)
             update_sql = f"""
             UPDATE {db_config}
             SET materials = '{material_result}', status_code = 2
@@ -214,7 +216,7 @@ class MatchArticlesV2(object):
         :return:
         """
         select_sql = f"""
-        SELECT video_id, cover, images, ai_text, ai_title, status_code
+        SELECT video_id, cover, img_list, ai_text, ai_title, status_code
         FROM {db_config}
         WHERE task_id = '{self.task_id}';
         """

+ 6 - 5
task.py

@@ -73,15 +73,16 @@ async def main():
     TMC = TaskMySQLClient()
     await TMC.init_pool()
     MAT = MatchArticlesTask(mysql_client=TMC)
-    await MAT.whisper_task()
-    await asyncio.sleep(120)
+    # await MAT.whisper_task()
+    # await asyncio.sleep(120)
     await MAT.materials_task()
-    await asyncio.sleep(120)
-    await MAT.ai_task()
-    await asyncio.sleep(120)
+    # await asyncio.sleep(120)
+    # await MAT.ai_task()
+    # await asyncio.sleep(120)
 
 
 if __name__ == '__main__':
     while True:
         asyncio.run(main())
+        print("执行完成,等待120s")
         time.sleep(120)

+ 76 - 0
test/prompt_dev.py

@@ -0,0 +1,76 @@
+import json
+from applications.ai import kimi_ai, tly_ai, tencent_ai
+
+
+def question_fission(query):
+    """
+    问题裂变
+    :param query
+    :return:
+    """
+    prompt = f"""
+        - 你是一个提问裂变器,能够将用户提供的搜索query裂变成4个不同方向的提问,以便获取更加全面的搜索结果。
+
+        - 根据搜索query:'{query}',生成5个在主题一致但方向有所差别的提问。
+        - 确保生成的提问简洁明了,直接输出5个问句。
+        - 以JSON格式输出5个问句,每个问句可以分别引用。
+        你拥有以下技能
+        - 技能1:语义理解与转换
+          1. 理解用户提供的搜索query的核心主题和意图。
+          2. 在保持主题一致的前提下,生成不同方向的提问。
+
+        - 技能2:提问生成
+          1. 根据原始query,生成5个不同方向的提问。
+          2. 确保每个提问都能引导用户获得不同的搜索结果。
+
+        - 技能3:多领域适应
+          1. 能够处理任何主题的搜索query。
+          2. 生成的提问适用于各种搜索引擎和信息检索场景。
+
+        限制
+        - 每次生成的提问数量固定为5个。
+        - 不添加额外的说明或解释,直接输出5个问句。
+        - 不考虑特定的关键词或排除某些关键词。
+        - 以JSON格式输出问句,每个问句可以分别引用。
+        输出格式模板
+        key, value分别是
+        'question1': '问句1',
+        'question2': '问句2',
+        'question3': '问句3',
+        'question4': '问句4',
+        'question5': '问句5'
+        """
+    question_dict = kimi_ai(prompt=prompt)
+    return question_dict
+
+
+def generate_text(question):
+    """
+    使用kimi生成文章
+    :param question:
+    :return:
+    """
+    prompt = f"""
+    通过给到你的这个问题:'{question}',
+    生成一篇小文章,文章需要有逻辑,有参考意义
+    """
+    response = tencent_ai(prompt)
+    return response
+
+
+question_dict = {
+    "question1": "退休后的生活有哪些值得关注的方面?",
+    "question2": "退休人员如何规划自己的退休生活?",
+    "question3": "退休后如何保持身心健康?",
+    "question4": "退休人员有哪些适合的娱乐活动推荐?",
+    "question5": "退休后如何有效管理个人财务?"
+}
+
+material = {}
+for question_key in question_dict:
+    question = question_dict[question_key]
+    response = generate_text(question)
+    material[question] = response
+
+print(json.dumps(material, ensure_ascii=False, indent=4))
+