删除无用的AI调用逻辑
This commit is contained in:
@@ -6,7 +6,7 @@ Created Date: 2024-01-21
|
||||
Last Modified: 2024-03-24
|
||||
Modified by: MrCrawL
|
||||
"""
|
||||
from utils.ai.dify_news_analyze import dify_news_title_analyze
|
||||
from plugins.global_news.dify_news_analyze import dify_news_title_analyze
|
||||
from utils.markdown_to_image import convert_md_str_to_image
|
||||
|
||||
'''Existing problem: text with hyperlink won't be saved'''
|
||||
@@ -15,8 +15,6 @@ import requests
|
||||
from time import localtime, sleep
|
||||
from lxml import etree
|
||||
from loguru import logger
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
# 请求配置
|
||||
HEADERS = {
|
||||
|
||||
@@ -11,7 +11,6 @@ import requests
|
||||
from lxml import etree
|
||||
|
||||
from base import func_english_news
|
||||
from utils.ai.dify_news_analyze import dify_news_title_analyze
|
||||
|
||||
|
||||
class News(object):
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
[GlobalNews]
|
||||
enable = true
|
||||
command = ["全球新闻", "国际新闻", "环球新闻", "政经新闻", "政治经济新闻"]
|
||||
command-format = """
|
||||
🌍全球新闻指令:
|
||||
全球新闻 - 获取最新的全球政治经济新闻
|
||||
"""
|
||||
"""
|
||||
|
||||
|
||||
authorization = "Bearer app-rhhKkbvHd2IAQoGX7xTzXZJj" # 请替换为真实的Authorization token
|
||||
url = 'http://192.168.2.240/v1/chat-messages'
|
||||
@@ -1,6 +1,8 @@
|
||||
import asyncio
|
||||
import json
|
||||
import threading
|
||||
import time # 添加这一行
|
||||
import aiohttp
|
||||
from typing import Dict, Any, List, Optional, Tuple
|
||||
|
||||
from base.plugin_common.message_plugin_interface import MessagePluginInterface
|
||||
@@ -8,7 +10,6 @@ from base.plugin_common.plugin_interface import PluginStatus
|
||||
from utils.decorator.plugin_decorators import plugin_stats_decorator
|
||||
from utils.robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
|
||||
from utils.decorator.points_decorator import plugin_points_cost
|
||||
from utils.ai.dify_news_analyze import dify_news_title_analyze
|
||||
from utils.markdown_to_image import convert_md_str_to_image
|
||||
from wechat_ipad import WechatAPIClient
|
||||
|
||||
@@ -61,7 +62,8 @@ class GlobalNewsPlugin(MessagePluginInterface):
|
||||
self.command_format = self._config.get("GlobalNews", {}).get("command-format",
|
||||
"全球新闻 - 获取最新的全球政治经济新闻")
|
||||
self.enable = self._config.get("GlobalNews", {}).get("enable", True)
|
||||
|
||||
self._key = self._config.get("GlobalNews", {}).get("authorization", "")
|
||||
self._url = self._config.get("GlobalNews", {}).get("url", "")
|
||||
self.LOG.info(f"[{self.name}] 插件初始化完成,指令:{self._commands}")
|
||||
return True
|
||||
|
||||
@@ -171,7 +173,7 @@ class GlobalNewsPlugin(MessagePluginInterface):
|
||||
|
||||
# 使用AI分析新闻
|
||||
markdown_news = await self._run_in_executor(
|
||||
dify_news_title_analyze, news_titles
|
||||
self.dify_news_title_analyze, news_titles
|
||||
)
|
||||
|
||||
# 转换为图片
|
||||
@@ -188,3 +190,62 @@ class GlobalNewsPlugin(MessagePluginInterface):
|
||||
"""在线程池中运行同步函数"""
|
||||
loop = asyncio.get_event_loop()
|
||||
return await loop.run_in_executor(None, func, *args)
|
||||
|
||||
async def dify_news_title_analyze(self, content: str) -> str:
|
||||
"""异步分析新闻标题
|
||||
Args:
|
||||
content: 新闻标题内容
|
||||
Returns:
|
||||
str: 分析后的内容
|
||||
"""
|
||||
# 设置Authorization和URL
|
||||
data = {
|
||||
"response_mode": "blocking",
|
||||
"conversation_id": "",
|
||||
"inputs": {},
|
||||
"query": content,
|
||||
"user": "a-bot-global_news"
|
||||
}
|
||||
|
||||
# 设置请求头
|
||||
headers = {
|
||||
"Content-Type": "application/json; charset=utf-8",
|
||||
"Authorization": self._key
|
||||
}
|
||||
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(self._url, headers=headers, json=data) as response:
|
||||
if response.status != 200:
|
||||
self.LOG.error(f"新闻分析请求失败: {response.status}")
|
||||
return None
|
||||
|
||||
response_data = await response.json()
|
||||
self.LOG.debug(f"新闻分析响应: {response_data}")
|
||||
return self.extract_content(response_data)
|
||||
|
||||
except Exception as e:
|
||||
self.LOG.error(f"新闻分析请求出错: {e}")
|
||||
return None
|
||||
|
||||
def extract_content(self, data):
|
||||
"""解析API响应内容
|
||||
Args:
|
||||
data: API返回的响应数据,可以是字典或字符串
|
||||
Returns:
|
||||
str: 提取的answer内容
|
||||
"""
|
||||
try:
|
||||
# 如果是字符串,尝试解析为字典
|
||||
if isinstance(data, str):
|
||||
data = json.loads(data)
|
||||
# 如果是字典,直接获取answer
|
||||
if isinstance(data, dict):
|
||||
answer = data.get('answer', '')
|
||||
if answer:
|
||||
return answer
|
||||
|
||||
return None
|
||||
except Exception as e:
|
||||
self.LOG.error(f"解析响应失败: {str(e)}")
|
||||
return None
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
#
|
||||
# curl -X POST 'http://192.168.2.240/v1/chat-messages' \
|
||||
# --header 'Authorization: Bearer {api_key}' \
|
||||
# --header 'Content-Type: application/json' \
|
||||
# --data-raw '{
|
||||
# "inputs": {},
|
||||
# "query": "What are the specs of the iPhone 13 Pro Max?",
|
||||
# "response_mode": "streaming",
|
||||
# "conversation_id": "",
|
||||
# "user": "abc-123",
|
||||
# "files": [
|
||||
# {
|
||||
# "type": "image",
|
||||
# "transfer_method": "remote_url",
|
||||
# "url": "https://cloud.dify.ai/logo/logo-site.png"
|
||||
# }
|
||||
# ]
|
||||
# }'
|
||||
import json
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
def dify_news_title_analyze(content):
|
||||
# 设置Authorization和URL
|
||||
authorization = "Bearer app-rhhKkbvHd2IAQoGX7xTzXZJj" # 请替换为真实的Authorization token
|
||||
url = 'http://192.168.2.240/v1/chat-messages'
|
||||
|
||||
data = {
|
||||
"response_mode": "blocking",
|
||||
"conversation_id": "",
|
||||
"inputs": {},
|
||||
"query": content,
|
||||
"user": "a-bot"
|
||||
}
|
||||
|
||||
# 设置请求头
|
||||
headers = {
|
||||
"Content-Type": "application/json; charset=utf-8",
|
||||
"Authorization": authorization
|
||||
}
|
||||
|
||||
# 发送POST请求
|
||||
response = requests.post(url, headers=headers, data=json.dumps(data), )
|
||||
response.encoding = 'utf-8'
|
||||
|
||||
# 输出响应内容
|
||||
print(response.status_code)
|
||||
print(response.json())
|
||||
return extract_content(response.json())
|
||||
|
||||
|
||||
def extract_content(data):
|
||||
"""解析API响应内容
|
||||
Args:
|
||||
data: API返回的响应数据,可以是字典或字符串
|
||||
Returns:
|
||||
str: 提取的answer内容
|
||||
"""
|
||||
try:
|
||||
# 如果是字符串,尝试解析为字典
|
||||
if isinstance(data, str):
|
||||
data = json.dumps(data)
|
||||
# 如果是字典,直接获取answer
|
||||
if isinstance(data, dict):
|
||||
answer = data.get('answer', '')
|
||||
if answer:
|
||||
return answer
|
||||
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"解析响应失败: {str(e)}")
|
||||
return None
|
||||
Reference in New Issue
Block a user