加入了新的模型,使用更强的模型玩
This commit is contained in:
121
base/func_claude.py
Normal file
121
base/func_claude.py
Normal file
@@ -0,0 +1,121 @@
|
||||
import requests
|
||||
import json
|
||||
import logging
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class Claude():
|
||||
def __init__(self, conf: dict) -> None:
|
||||
self.key = conf.get("key")
|
||||
self.api = conf.get("api")
|
||||
prompt = conf.get("prompt")
|
||||
self.model = conf.get("model", "windsurf/claude-3-5-sonnet")
|
||||
self.LOG = logging.getLogger("Claude")
|
||||
self.conversation_list = {}
|
||||
self.system_content_msg = {"role": "system", "content": prompt}
|
||||
|
||||
def __repr__(self):
|
||||
return 'Claude'
|
||||
|
||||
def get_answer(self, question: str, wxid: str) -> str:
|
||||
# 设置请求头
|
||||
self.updateMessage(wxid, question, "user")
|
||||
rsp = ""
|
||||
try:
|
||||
headers = {
|
||||
"Content-Type": "application/json; charset=utf-8",
|
||||
"Authorization": self.key
|
||||
}
|
||||
# 设置请求的payload
|
||||
data = {
|
||||
"model": self.model,
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": f"{question}"
|
||||
}
|
||||
|
||||
]
|
||||
}
|
||||
# 发送POST请求
|
||||
response = requests.post(self.api, headers=headers, data=json.dumps(data), )
|
||||
response.encoding = 'utf-8'
|
||||
|
||||
# 输出响应内容
|
||||
print(response.status_code)
|
||||
# print(response.text)
|
||||
rsp = extract_content(response.text)
|
||||
self.updateMessage(wxid, rsp, "assistant")
|
||||
except Exception as e0:
|
||||
self.LOG.error(f"发生未知错误:{str(e0)}")
|
||||
return rsp
|
||||
|
||||
def updateMessage(self, wxid: str, question: str, role: str) -> None:
|
||||
now_time = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
||||
|
||||
time_mk = "当需要回答时间时请直接参考回复:"
|
||||
# 初始化聊天记录,组装系统信息
|
||||
if wxid not in self.conversation_list.keys():
|
||||
question_ = [
|
||||
self.system_content_msg,
|
||||
{"role": "system", "content": "" + time_mk + now_time}
|
||||
]
|
||||
self.conversation_list[wxid] = question_
|
||||
|
||||
# 当前问题
|
||||
content_question_ = {"role": role, "content": question}
|
||||
self.conversation_list[wxid].append(content_question_)
|
||||
|
||||
for cont in self.conversation_list[wxid]:
|
||||
if cont["role"] != "system":
|
||||
continue
|
||||
if cont["content"].startswith(time_mk):
|
||||
cont["content"] = time_mk + now_time
|
||||
|
||||
# 只存储10条记录,超过滚动清除
|
||||
i = len(self.conversation_list[wxid])
|
||||
if i > 10:
|
||||
print("滚动清除微信记录:" + wxid)
|
||||
# 删除多余的记录,倒着删,且跳过第一个的系统消息
|
||||
del self.conversation_list[wxid][1]
|
||||
|
||||
@staticmethod
|
||||
def value_check(conf: dict) -> bool:
|
||||
if conf:
|
||||
if conf.get("key") and conf.get("api") and conf.get("prompt"):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
# 解析JSON
|
||||
def extract_content(data_string):
|
||||
try:
|
||||
data = json.loads(data_string)
|
||||
# 提取content字段
|
||||
content = data["choices"][0]["message"].get("content", "")
|
||||
return content
|
||||
except json.JSONDecodeError:
|
||||
print("Invalid JSON")
|
||||
return None
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from configuration import Config
|
||||
|
||||
config = Config().CLAUDE
|
||||
if not config:
|
||||
exit(0)
|
||||
|
||||
chat = Claude(config)
|
||||
|
||||
while True:
|
||||
q = input(">>> ")
|
||||
try:
|
||||
time_start = datetime.now() # 记录开始时间
|
||||
print(chat.get_answer(q, "Jyunere"))
|
||||
time_end = datetime.now() # 记录结束时间
|
||||
|
||||
print(f"{round((time_end - time_start).total_seconds(), 2)}s") # 计算的时间差为程序的执行时间,单位为秒/s
|
||||
except Exception as e:
|
||||
print(e)
|
||||
26
config.yaml
26
config.yaml
@@ -36,27 +36,23 @@ logging:
|
||||
|
||||
root:
|
||||
level: INFO
|
||||
handlers: [console, info_file_handler, error_file_handler]
|
||||
handlers: [ console, info_file_handler, error_file_handler ]
|
||||
|
||||
groups:
|
||||
enable: [45317011307@chatroom,49571962306@chatroom,43687793133@chatroom] # 允许响应的群 roomId,大概长这样:2xxxxxxxxx3@chatroom
|
||||
enable: [ 45317011307@chatroom,49571962306@chatroom,43687793133@chatroom ] # 允许响应的群 roomId,大概长这样:2xxxxxxxxx3@chatroom
|
||||
|
||||
news:
|
||||
receivers: [45317011307@chatroom,49571962306@chatroom,43687793133@chatroom] # 定时新闻接收人(roomid 或者 wxid)
|
||||
receivers: [ 45317011307@chatroom,49571962306@chatroom,43687793133@chatroom ] # 定时新闻接收人(roomid 或者 wxid)
|
||||
|
||||
report_reminder:
|
||||
receivers: [] # 定时日报周报月报提醒(roomid 或者 wxid)
|
||||
receivers: [ ] # 定时日报周报月报提醒(roomid 或者 wxid)
|
||||
|
||||
chatgpt: # -----chatgpt配置这行不填-----
|
||||
key: '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'# 填写你 ChatGPT 的 key
|
||||
key: # 填写你 ChatGPT 的 key
|
||||
api: https://api.openai.com/v1 # 如果你不知道这是干嘛的,就不要改
|
||||
model: gpt-3.5-turbo
|
||||
proxy: # 如果你在国内,你可能需要魔法,大概长这样:http://域名或者IP地址:端口号
|
||||
prompt: '你是一个信息归纳分析工程师,你叫小牛,你根据提问会搜索相关资料。经过信息精炼之后返回内容。
|
||||
请回复时以以下格式进行返回:
|
||||
- 问题:回顾提问内容
|
||||
- 问题评价:分析问题的提出角度,如(财经、彩票、房产、股票、家居、教育、科技、社会、时尚、时政、体育、星座、游戏、娱乐)等
|
||||
- 总结:经过200个字以内的优化返回' # 根据需要对角色进行设定
|
||||
prompt: 你是智能聊天机器人,你叫 wcferry # 根据需要对角色进行设定
|
||||
|
||||
chatglm: # -----chatglm配置这行不填-----
|
||||
key: sk-012345678901234567890123456789012345678901234567 # 这个应该不用动
|
||||
@@ -91,3 +87,13 @@ bard: # -----bard配置这行不填-----
|
||||
zhipu: # -----zhipu配置这行不填-----
|
||||
api_key: #api key
|
||||
model: # 模型类型
|
||||
|
||||
claude:
|
||||
key: 46a5674a-e978-491b-a810-5d54605f2c36
|
||||
api: http://127.0.0.1:8080/v1/chat/completions # 如果你不知道这是干嘛的,就不要改
|
||||
model: windsurf/claude-3-5-sonnet
|
||||
prompt: '你是一个信息归纳分析工程师,你根据提问会搜索相关资料。经过信息精炼之后返回内容。
|
||||
请回复时以以下格式进行返回:
|
||||
- 问题:回顾提问内容
|
||||
- 问题评价:分析问题的提出角度,如(财经、彩票、房产、股票、家居、教育、科技、社会、时尚、时政、体育、星座、游戏、娱乐)等
|
||||
- 总结:经过200个字以内的优化返回' # 根据需要对角色进行设定
|
||||
@@ -37,3 +37,4 @@ class Config(object):
|
||||
self.CHATGLM = yconfig.get("chatglm", {})
|
||||
self.BardAssistant = yconfig.get("bard", {})
|
||||
self.ZhiPu = yconfig.get("zhipu", {})
|
||||
self.CLAUDE = yconfig.get("claude", {})
|
||||
|
||||
@@ -10,12 +10,13 @@ class ChatType(IntEnum):
|
||||
CHATGLM = 4 # ChatGLM
|
||||
BardAssistant = 5 # Google Bard
|
||||
ZhiPu = 6 # ZhiPu
|
||||
CLAUDE = 7 # CLAUDE
|
||||
|
||||
@staticmethod
|
||||
def is_in_chat_types(chat_type: int) -> bool:
|
||||
if chat_type in [ChatType.TIGER_BOT.value, ChatType.CHATGPT.value,
|
||||
ChatType.XINGHUO_WEB.value, ChatType.CHATGLM.value,
|
||||
ChatType.BardAssistant.value, ChatType.ZhiPu.value]:
|
||||
ChatType.BardAssistant.value, ChatType.ZhiPu.value, ChatType.CLAUDE.value]:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import redis
|
||||
from datetime import datetime
|
||||
import re
|
||||
|
||||
|
||||
from wcferry import WxMsg
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import pymysql
|
||||
from datetime import datetime, timedelta
|
||||
import redis
|
||||
|
||||
# 配置数据库连接
|
||||
db_config = {
|
||||
@@ -9,6 +10,9 @@ db_config = {
|
||||
'database': 'message_archive'
|
||||
}
|
||||
|
||||
# 连接到Redis
|
||||
r = redis.Redis(host='192.168.2.32', port=6379, db=0)
|
||||
|
||||
|
||||
def archive_message(group_id, timestamp_str, sender, content, message_type, attachment_url=None):
|
||||
# 连接到数据库
|
||||
@@ -41,13 +45,21 @@ def get_messages(group_id, all_contacts: dict):
|
||||
connection = pymysql.connect(**db_config)
|
||||
|
||||
try:
|
||||
with connection.cursor() as cursor:
|
||||
# 获取redis 中的上次总结时间,本次从上次开始算,弱没有,则从8小时之前开始计算
|
||||
# 生成Redis key
|
||||
key = f"{group_id}:summary_time"
|
||||
last_summary_time = r.get(key)
|
||||
if last_summary_time is None:
|
||||
# 获取当前时间并计算8小时前的时间
|
||||
current_time = datetime.now()
|
||||
eight_hours_ago = current_time - timedelta(hours=8)
|
||||
|
||||
# 转换为数据库中存储的时间格式 (假设timestamp是DATETIME类型)
|
||||
eight_hours_ago_str = eight_hours_ago.strftime('%Y-%m-%d %H:%M:%S')
|
||||
last_summary_time = eight_hours_ago.strftime('%Y-%m-%d %H:%M:%S')
|
||||
current_date = current_time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
r.set(key, current_date)
|
||||
|
||||
with connection.cursor() as cursor:
|
||||
|
||||
# 执行查询,获取最近8小时的消息
|
||||
query = """
|
||||
@@ -55,7 +67,7 @@ def get_messages(group_id, all_contacts: dict):
|
||||
FROM messages
|
||||
WHERE timestamp >= %s AND message_type =1 and group_id = %s
|
||||
"""
|
||||
cursor.execute(query, (eight_hours_ago_str, group_id))
|
||||
cursor.execute(query, (last_summary_time, group_id))
|
||||
|
||||
# 提取结果并组成带逗号的字符串
|
||||
result = []
|
||||
|
||||
5
robot.py
5
robot.py
@@ -20,6 +20,7 @@ from base.func_chengyu import cy
|
||||
from base.func_news import News
|
||||
from base.func_tigerbot import TigerBot
|
||||
from base.func_xinghuo_web import XinghuoWeb
|
||||
from base.func_claude import Claude
|
||||
from configuration import Config
|
||||
from constants import ChatType
|
||||
from job_mgmt import Job
|
||||
@@ -57,6 +58,8 @@ class Robot(Job):
|
||||
self.chat = BardAssistant(self.config.BardAssistant)
|
||||
elif chat_type == ChatType.ZhiPu.value and ZhiPu.value_check(self.config.ZhiPu):
|
||||
self.chat = ZhiPu(self.config.ZhiPu)
|
||||
elif chat_type == ChatType.CLAUDE.value and Claude.value_check(self.config.CLAUDE):
|
||||
self.chat = Claude(self.config.CLAUDE)
|
||||
else:
|
||||
self.LOG.warning("未配置模型")
|
||||
self.chat = None
|
||||
@@ -73,6 +76,8 @@ class Robot(Job):
|
||||
self.chat = BardAssistant(self.config.BardAssistant)
|
||||
elif ZhiPu.value_check(self.config.ZhiPu):
|
||||
self.chat = ZhiPu(self.config.ZhiPu)
|
||||
elif Claude.value_check(self.config.CLAUDE):
|
||||
self.chat = Claude(self.config.CLAUDE)
|
||||
else:
|
||||
self.LOG.warning("未配置模型")
|
||||
self.chat = None
|
||||
|
||||
Reference in New Issue
Block a user