jack 11 сар өмнө
parent
commit
bc8ad82b3a
3 өөрчлөгдсөн 41 нэмэгдсэн , 15 устгасан
  1. 2 2
      api_ollama.py
  2. 9 2
      config.json
  3. 30 11
      main.py

+ 2 - 2
api_ollama.py

@@ -3,7 +3,7 @@ import time
 from ollama import Client as oClient
 
 class OllamaChat(object):
-    def call_ollama(self, host, text, prompt_words, model='llava:13b', temperature=0.4):
+    def call_ollama(self, host, role, text, prompt_words, model='llava:13b', temperature=0.4):
         messages = text + '\n\n' + prompt_words
         self.client = oClient(host=host)
         self.model = model
@@ -14,7 +14,7 @@ class OllamaChat(object):
         try:
             response_iter = self.client.chat(model=self.model,
                                              messages=[
-                                                 {'role': 'system', 'content': '你是一个新闻报播员'},
+                                                 {'role': 'system', 'content': role},
                                                  {'role': 'user', 'content': self.messages}
                                              ],
                                              options={"temperature": self.temperature},

+ 9 - 2
config.json

@@ -1,4 +1,9 @@
 {
+  "example": {
+    "target_url_list": [],
+    "prompt_words": "",
+    "role": ""
+  },
   "web3": {
     "target_url_list": [
       "https://wublock123.com",
@@ -11,7 +16,8 @@
       "https://foresightnews.pro/news",
       "https://www.web3sj.com/news/"
     ],
-    "prompt_words": "给你一个或多个网页的源代码, 里面是未清洗的网页源代码,你可以无视网页源代码的部分,关注内容就行,重复的话就不用说了,中间不需要有空行 请用中文回答"
+    "prompt_words": "给你一个或多个网页的源代码, 里面是未清洗的网页源代码,你可以无视网页源代码的部分,关注内容就行,重复的话就不用说了,中间不需要有空行 请用中文回答",
+    "role": "你是一个新闻报播员, 用简短的话, 描述一下这些网页的内容"
   },
   "A": {
     "target_url_list": [
@@ -31,6 +37,7 @@
       "https://www.jiemian.com/",
       "https://www.caixin.com/"
     ],
-    "prompt_words": "给你一个或多个网页的源代码, 里面是未清洗的网页源代码,你可以无视网页源代码的部分,关注内容就行,重复的话就不用说了,帮我总结一下这些网站的内容, 请用中文回答"
+    "prompt_words": "给你一个或多个网页的源代码, 里面是未清洗的网页源代码,你可以无视网页源代码的部分,关注内容就行,重复的话就不用说了,帮我总结一下这些网站的内容, 请用中文回答",
+    "role": ""
   }
 }

+ 30 - 11
main.py

@@ -14,6 +14,29 @@ key = 'web3'
 
 
 class AINEWS:
+    def create_config_if_not_exists():
+        # 获取当前文件的目录路径
+        current_dir = os.path.dirname(os.path.abspath(__file__))
+        
+        # 构建 config.json 文件的完整路径
+        config_path = os.path.join(current_dir, 'config.json')
+        
+        # 检查 config.json 文件是否存在
+        if not os.path.exists(config_path):
+            # 如果不存在,创建并写入默认的 JSON 数据
+            default_config = {
+                "example": {
+                    "target_url_list": [],
+                    "prompt_words": "",
+                    "role": ""
+                }
+            }
+            
+            # 写入 JSON 数据到 config.json 文件
+            with open(config_path, 'w', encoding='utf-8') as f:
+                json.dump(default_config, f, indent=4)
+            
+            print(f"Created {config_path} with default configuration.")
     def save_to_txt(self, url_to_text):
         current_file_path = os.path.dirname(__file__)
         save_file_path = os.path.join(current_file_path, 'save_txt')
@@ -32,10 +55,10 @@ class AINEWS:
 
         if not config:
             print('config.json is not exist!')
-            exit()
+            exit(0)
 
         k = config[key]
-        return k['target_url_list'], k['prompt_words']
+        return k['target_url_list'], k['prompt_words'], k['role']
 
     @staticmethod
     async def scroll_to_percentage(page):
@@ -97,7 +120,7 @@ class AINEWS:
 
             return url_to_text
 
-    def main(self, target_url_list, prompt_words):
+    def main(self, target_url_list, prompt_words, role):
         url_to_text = asyncio.run(self.get_htmls(target_url_list))
 
         # 创建消息bot实例
@@ -105,18 +128,12 @@ class AINEWS:
 
         self.save_to_txt(url_to_text)
 
-        role = '你是一个新闻报播员'
-
-        prompt_words += ''
-
         O = OllamaChat()
         for k, v in url_to_text.items():
             response_context = O.call_ollama('http://127.0.0.1:11434', role, v, prompt_words)
             message = f'{k}\n{response_context}\n'
-
             # 发送消息
             bot.send_message(message)
-            
 
         # K = KIMI()
         # response_context = K.call_kimi(prompt_words)
@@ -130,8 +147,10 @@ class AINEWS:
         #     message = f'{k}\n{response_context}\n'
         #     print(message)
 
+
 if __name__ == "__main__":
     ainews = AINEWS()
-    target_url_list, prompt_words = ainews.load_config(key)
-    ainews.main(target_url_list, prompt_words)
+    ainews.create_config_if_not_exists()
+    target_url_list, prompt_words, role = ainews.load_config(key)
+    ainews.main(target_url_list, prompt_words, role)
     print('done!')