jack 11 月之前
当前提交
b503bfb5b6
共有 8 个文件被更改,包括 318 次插入0 次删除
  1. 53 0
      .gitignore
  2. 33 0
      api_deepseek.py
  3. 30 0
      api_kimi.py
  4. 55 0
      api_ollama.py
  5. 89 0
      main.py
  6. 17 0
      requirements.txt
  7. 40 0
      send_matrix.py
  8. 1 0
      send_to_email.py

+ 53 - 0
.gitignore

@@ -0,0 +1,53 @@
+.DS_Store
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+env/
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+*.egg-info/
+.installed.cfg
+*.egg
+.idea/*
+xml_files/
+
+# PyInstaller
+#  Usually these files are written by a python script from a template
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*,cover
+
+*.mo
+*.pot
+
+*.log

+ 33 - 0
api_deepseek.py

@@ -0,0 +1,33 @@
+# -*- coding: utf-8 -*-
+
+from openai import OpenAI
+
+'''
+deepseek-chat
+'''
+
+
+class DeepSeek(object):
+    def call_deepseek(self, message):
+        try:
+            print('call deepseek')
+            client = OpenAI(
+                api_key="sk-20afb9967a124b63a6f67dcb69f17d74",
+                base_url="https://api.deepseek.com",
+            )
+
+            completion = client.chat.completions.create(
+                model="deepseek-chat",
+                messages=[
+                    {"role": "system", "content": "You are a helpful assistant"},
+                    {"role": "user", "content": f"{message}"},
+                ],
+                stream=False,
+                temperature=0.3,
+            )
+
+            result = completion.choices[0].message.content
+
+            return result
+        except Exception as e:
+            print(e)

+ 30 - 0
api_kimi.py

@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+
+from openai import OpenAI
+
+'''
+moonshot-v1-8k
+moonshot-v1-32k
+moonshot-v1-128k
+'''
+
+class KIMI(object):
+    def call_kimi(self, message):
+        try:
+            print('call kimi')
+            client = OpenAI(
+                api_key="sk-Fz9tRF8naXReN2H7zcB1AEtnpOmhonFPJgxlVvQHpql54Ymu",
+                base_url="https://api.moonshot.cn/v1",
+            )
+
+            completion = client.chat.completions.create(
+                model="moonshot-v1-128k",
+                messages=[{"role": "user", "content": f"{message}"}],
+                temperature=0.3,
+            )
+
+            result = completion.choices[0].message.content
+
+            return result
+        except Exception as e:
+            print(e)

+ 55 - 0
api_ollama.py

@@ -0,0 +1,55 @@
+# -*- coding: utf-8 -*-
+import time
+import subprocess
+import sys
+import timeit
+from ollama import Client as oClient
+
+'''
+可选模型:
+qwen:1.8b
+qwen2:1.5b
+qwen2.5:3b
+gemma2:2b
+'''
+model_list = [
+    'qwen:1.8b',
+    'qwen2:1.5b',
+    'qwen2.5:3b',
+    'gemma2:2b'
+]
+
+
+class ChatBot:
+    def __init__(self, host, messages, model='qwen:1.8b', temperature=0.4):
+        self.client = oClient(host=host)
+        self.model = model
+        self.messages = messages
+        self.temperature = temperature
+
+    def start_chat(self):
+        print(f'use model: {self.model}')
+        try:
+            response_iter = self.client.chat(model=self.model,
+                                             messages=[
+                                                 {'role': 'user', 'content': '你是一个新闻整理专员'},
+                                                 {'role': 'user', 'content': self.messages}
+                                             ],
+                                             options={"temperature": self.temperature},
+                                             stream=False)
+            return response_iter['message']['content']
+        except Exception as e:
+            print(f"\n发生错误: {e}")
+
+
+# if __name__ == "__main__":
+#     for m in model_list:
+#         C = ChatBot('http://erhe.top:27381', 'hello,你好呀', m)
+#         start_time = time.time()
+#
+#         response_context = C.start_chat()
+#         print(response_context)
+#
+#         end_time = time.time()
+#         run_time = end_time - start_time
+#         print(f"程序运行时间:{run_time} 秒\n")

+ 89 - 0
main.py

@@ -0,0 +1,89 @@
+# -*- coding: utf-8 -*-
+import re
+from playwright.async_api import async_playwright
+import asyncio
+from bs4 import BeautifulSoup
+
+from api_ollama import *
+from api_kimi import *
+from api_deepseek import *
+
+from send_to_email import *
+
+
+class AINEWS:
+    async def get_htmls(self, urls):
+        htmls = []
+        async with async_playwright() as p:
+            # 启动浏览器
+            browser = await p.chromium.launch(headless=True)
+            # 创建浏览器上下文
+            context = await browser.new_context()
+
+            async def get_html(url):
+                try:
+                    print(f'正在打开: {url}')
+                    # 在上下文中打开新页面
+                    page = await context.new_page()
+                    # 导航到指定网址
+                    await page.goto(url)
+                    # 获取渲染后的 HTML
+                    html = await page.content()
+                    # 关闭页面
+                    await page.close()
+                    return html
+                except Exception as e:
+                    print(f"Error fetching {url}: {e}")
+                    return ""
+
+            # 使用 asyncio.gather 同时获取所有网站的 HTML
+            tasks = [get_html(url) for url in urls]
+            htmls_list = await asyncio.gather(*tasks)
+
+            # 使用 BeautifulSoup 格式化每个 HTML 内容
+            formatted_htmls = []
+            for html in htmls_list:
+                soup = BeautifulSoup(html, 'html.parser')
+                formatted_html = soup.get_text()
+                cleaned_text = re.sub(r'[\n\t\r]+', ' ', formatted_html)
+                cleaned_text = re.sub(r'\s+', ' ', cleaned_text).strip()
+                formatted_htmls.append(cleaned_text)
+
+            # 将所有格式化后的 HTML 内容合并到一个字符串中
+            text = "\n".join(formatted_htmls)
+
+            # 关闭上下文和浏览器
+            await context.close()
+            await browser.close()
+
+            return text
+
+    def main(self):
+        urls = ["https://www.smzdm.com/jingxuan/", "https://faxian.smzdm.com/"]
+        text = asyncio.run(self.get_htmls(urls))
+
+        # print(text)
+
+        prompt_words = '''
+            给你几个个网页的源代码, 里面是未清洗的网页源代码
+            你可以无视网页源代码的部分,关注内容就行,重复的话就不用说了
+            帮我总结一下内容, 请用中文回答
+        '''
+        prompt_words += text
+
+        # C = ChatBot('http://erhe.top:27381', prompt_words, 'qwen2.5:3b')
+        # response_context = C.start_chat()
+        # print(response_context)
+
+        # K = KIMI()
+        # response_context = K.call_kimi(prompt_words)
+        # print(response_context)
+
+        D = DeepSeek()
+        response_context = D.call_deepseek(prompt_words)
+        print(response_context)
+
+
+if __name__ == "__main__":
+    ainews = AINEWS()
+    ainews.main()

+ 17 - 0
requirements.txt

@@ -0,0 +1,17 @@
+annotated-types==0.7.0
+anyio==4.8.0
+certifi==2024.12.14
+exceptiongroup==1.2.2
+gotify==0.6.0
+greenlet==3.1.1
+h11==0.14.0
+httpcore==1.0.7
+httpx==0.27.2
+idna==3.10
+ollama==0.4.5
+playwright==1.49.1
+pydantic==2.10.5
+pydantic_core==2.27.2
+pyee==12.0.0
+sniffio==1.3.1
+typing_extensions==4.12.2

+ 40 - 0
send_matrix.py

@@ -0,0 +1,40 @@
+# -*- coding: utf-8 -*-
+# pip install matrix_client
+
+from matrix_client.client import MatrixClient
+from matrix_client.api import MatrixHttpApi
+
+class MatrixBot:
+    def __init__(self, user, password):
+        self.base_url = "https://matrix.erhe.top"
+        self.user = user
+        self.password = password
+        self.client = MatrixClient("https://matrix.erhe.top")
+        self.token = self.login()
+        self.to = "!ddrrTpQmepfgivMxeW:chat.abeginner.cn"
+
+    def login(self):
+        self.token = self.client.login(username=self.user, password=self.password)
+        return self.token
+
+    def send_message(self, message):
+        if self.token:
+            try:
+                api = MatrixHttpApi(self.base_url, token=self.token)
+                api.send_message(self.to, message)
+            except Exception as e:
+                print(e)
+                api = MatrixHttpApi(self.base_url, token=self.token)
+                api.send_message(self.to, e)
+
+        else:
+            print("Bot is not logged in. Please login first.")
+
+# if __name__ == '__main__':
+# 测试调用
+# user = "bot1"
+# pw = "aaaAAA111!!!"
+# message = "123987456"
+#
+# bot = MatrixBot(user, pw)
+# bot.send_message(message)

+ 1 - 0
send_to_email.py

@@ -0,0 +1 @@
+# -*- coding: utf-8 -*-