File size: 12,450 Bytes
ea51a4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import os
import json
import requests
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from flask import Flask, request, jsonify, send_from_directory
from openai import OpenAI
from duckduckgo_search import DDGS
from functions import FUNCTIONS_GROUP_1, FUNCTIONS_GROUP_2, get_function_descriptions

app = Flask(__name__)
API_KEY = os.getenv("OPENAI_API_KEY")
BASE_URL = os.getenv("OPENAI_BASE_URL")
emailkey = os.getenv("EMAIL_KEY")
client = OpenAI(api_key=API_KEY, base_url=BASE_URL)

def search_duckduckgo(keywords):
    search_term = " ".join(keywords)
    with DDGS() as ddgs:
        return list(ddgs.text(keywords=search_term, region="cn-zh", safesearch="on", max_results=5))

def search_papers(query):
    url = f"https://api.crossref.org/works?query={query}"
    response = requests.get(url)
    if response.status_code == 200:
        data = response.json()
        papers = data['message']['items']
        processed_papers = []
        for paper in papers:
            processed_paper = {
                "标题": paper.get('title', [''])[0],
                "作者": ", ".join([f"{author.get('given', '')} {author.get('family', '')}" for author in paper.get('author', [])]),
                "DOI": paper.get('DOI', ''),
                "ISBN": ", ".join(paper.get('ISBN', [])),
                "摘要": paper.get('abstract', '').replace('<p>', '').replace('</p>', '').replace('<italic>', '').replace('</italic>', '')
            }
            processed_papers.append(processed_paper)
        return processed_papers
    else:
        return []

def send_email(to, subject, content):
    try:
        with smtplib.SMTP('106.15.184.28', 8025) as smtp:
            smtp.login("jwt", emailkey)
            message = MIMEMultipart()
            message['From'] = "Me <[email protected]>"
            message['To'] = to
            message['Subject'] = subject
            message.attach(MIMEText(content, 'html'))
            smtp.sendmail("[email protected]", to, message.as_string())
        return True
    except Exception as e:
        print(f"发送邮件时出错: {str(e)}")
        return False

def get_openai_response(messages, model="gpt-4o-mini", functions=None, function_call=None):
    try:
        response = client.chat.completions.create(
            model=model,
            messages=messages,
            functions=functions,
            function_call=function_call
        )
        return response.choices[0].message
    except Exception as e:
        print(f"调用OpenAI API时出错: {str(e)}")
        return None

def process_function_call(function_name, function_args):
    if function_name == "search_duckduckgo":
        keywords = function_args.get('keywords', [])
        if not keywords:
            return "搜索关键词为空,无法执行搜索。"
        return json.dumps(search_duckduckgo(keywords), ensure_ascii=False)
    elif function_name == "search_papers":
        query = function_args.get('query', '')
        if not query:
            return "搜索查询为空,无法执行论文搜索。"
        return json.dumps(search_papers(query), ensure_ascii=False)
    elif function_name == "send_email":
        to = function_args.get('to', '')
        subject = function_args.get('subject', '')
        content = function_args.get('content', '')
        if not to or not subject or not content:
            return "邮件信息不完整,无法发送邮件。"
        success = send_email(to, subject, content)
        return json.dumps({
            "success": success,
            "message": "邮件发送成功" if success else "邮件发送失败",
            "to": to,
            "subject": subject,
            "content": content,
            "is_email": True  # 添加这个标记来识别邮件发送功能
        }, ensure_ascii=False)
    else:
        return "未知的函数调用。"

@app.route('/')
def index():
    return send_from_directory('.', 'index.html')

# @app.route('/chat', methods=['POST'])
# def chat():
#     data = request.json
#     question = data['question']
#     history = data.get('history', [])
#     messages = history + [{"role": "user", "content": question}]
    
#     status_log = []
    
#     # 次级模型1: 处理搜索相关函数
#     status_log.append("次级模型1:正在判断是否需要选调第一组函数")
#     sub_model_1_response = get_openai_response(messages, model="gpt-4o-mini", functions=FUNCTIONS_GROUP_1, function_call="auto")
    
#     # 次级模型2: 处理邮件发送相关函数
#     status_log.append("次级模型2:正在判断是否需要选调第二组函数")
#     sub_model_2_response = get_openai_response(messages, model="gpt-4o-mini", functions=FUNCTIONS_GROUP_2, function_call="auto")
    
#     function_call_1 = sub_model_1_response.function_call if sub_model_1_response and sub_model_1_response.function_call else None
#     function_call_2 = sub_model_2_response.function_call if sub_model_2_response and sub_model_2_response.function_call else None
    
#     final_function_call = None
#     response = None
#     if function_call_1 and function_call_2:
#         # 裁决模型: 决定使用哪个函数调用
#         status_log.append("裁决模型:正在决定使用哪个函数调用")
#         arbitration_messages = messages + [
#             {"role": "system", "content": "两个次级模型都建议使用函数。请决定使用哪个函数更合适。"},
#             {"role": "assistant", "content": f"次级模型1建议使用函数:{function_call_1.name}"},
#             {"role": "assistant", "content": f"次级模型2建议使用函数:{function_call_2.name}"}
#         ]
#         arbitration_response = get_openai_response(arbitration_messages, model="gpt-4o-mini")
#         if "模型1" in arbitration_response.content or function_call_1.name in arbitration_response.content:
#             final_function_call = function_call_1
#             status_log.append(f"裁决模型:决定使用函数 {function_call_1.name}")
#         else:
#             final_function_call = function_call_2
#             status_log.append(f"裁决模型:决定使用函数 {function_call_2.name}")
#     elif function_call_1:
#         final_function_call = function_call_1
#         status_log.append(f"次级模型1:决定使用函数 {function_call_1.name}")
#     elif function_call_2:
#         final_function_call = function_call_2
    #      status_log.append(f"次级模型2:决定使用函数 {function_call_2.name}")
    #  else:
    #      status_log.append("次级模型:判断不需要进行搜索或发送邮件")
    
    # if final_function_call:
    #     function_name = final_function_call.name
    #     function_args = json.loads(final_function_call.arguments)
    #     status_log.append(f"正在执行函数 {function_name}")
    #     result = process_function_call(function_name, function_args)
    #     status_log.append(f"函数 {function_name} 执行完成")
        
    #     # 检查是否为邮件发送功能
    #     result_dict = json.loads(result)
    #     if result_dict.get("is_email", False):
    #         response = f"邮件{'已成功' if result_dict['success'] else '未能成功'}发送到 {result_dict['to']}。\n\n主题:{result_dict['subject']}\n\n内容:\n{result_dict['content']}"
    #     else:
    #         messages.append({
    #             "role": "function",
    #             "name": function_name,
    #             "content": result
    #         })
    
    # # 只有在没有邮件发送结果时才调用主模型
    # if not response:
    #     status_log.append("主模型:正在生成回答")
    #     final_response = get_openai_response(messages, model="gpt-4o-mini")
    #     response = final_response.content if final_response else "Error occurred"
    #     status_log.append("主模型:回答生成完成")
    
    # return jsonify({
    #     "response": response,
    #     "status_log": status_log
    # })

@app.route('/chat', methods=['POST'])
def chat():
    data = request.json
    question = data['question']
    history = data.get('history', [])
    messages = history + [{"role": "user", "content": question}]
    
    status_log = []
    
    # 次级模型1: 处理搜索相关函数
    status_log.append("次级模型1:正在判断是否需要选调第一组函数")
    sub_model_1_response = get_openai_response(messages, model="gpt-4o-mini", functions=FUNCTIONS_GROUP_1, function_call="auto")
    
    # 次级模型2: 处理邮件发送相关函数
    status_log.append("次级模型2:正在判断是否需要选调第二组函数")
    sub_model_2_response = get_openai_response(messages, model="gpt-4o-mini", functions=FUNCTIONS_GROUP_2, function_call="auto")
    
    function_call_1 = sub_model_1_response.function_call if sub_model_1_response and sub_model_1_response.function_call else None
    function_call_2 = sub_model_2_response.function_call if sub_model_2_response and sub_model_2_response.function_call else None
    
    if not function_call_1:
        status_log.append("次级模型1:判断不需要选调第一组函数")
    if not function_call_2:
        status_log.append("次级模型2:判断不需要选调第二组函数")
    
    final_function_call = None
    response = None
    if function_call_1 and function_call_2:
        # 裁决模型: 决定使用哪个函数调用
        status_log.append("裁决模型:正在决定使用哪个函数调用")
        arbitration_messages = messages + [
            {"role": "system", "content": "两个次级模型都建议使用函数。请决定使用哪个函数更合适。"},
            {"role": "assistant", "content": f"次级模型1建议使用函数:{function_call_1.name}"},
            {"role": "assistant", "content": f"次级模型2建议使用函数:{function_call_2.name}"}
        ]
        arbitration_response = get_openai_response(arbitration_messages, model="gpt-4o-mini")
        if "模型1" in arbitration_response.content or function_call_1.name in arbitration_response.content:
            final_function_call = function_call_1
            status_log.append(f"裁决模型:决定使用函数 {function_call_1.name}")
        else:
            final_function_call = function_call_2
            status_log.append(f"裁决模型:决定使用函数 {function_call_2.name}")
    elif function_call_1:
        final_function_call = function_call_1
        status_log.append(f"次级模型1:决定使用函数 {function_call_1.name}")
    elif function_call_2:
        final_function_call = function_call_2
        status_log.append(f"次级模型2:决定使用函数 {function_call_2.name}")
    else:
        status_log.append("所有次级模型:判断不需要进行任何函数调用")
    
    if final_function_call:
        function_name = final_function_call.name
        function_args = json.loads(final_function_call.arguments)
        status_log.append(f"正在执行函数 {function_name}")
        result = process_function_call(function_name, function_args)
        status_log.append(f"函数 {function_name} 执行完成")
        
        # 检查是否为邮件发送功能
        result_dict = json.loads(result)
        if result_dict.get("is_email", False):
            response = f"邮件{'已成功' if result_dict['success'] else '未能成功'}发送到 {result_dict['to']}。\n\n主题:{result_dict['subject']}\n\n内容:\n{result_dict['content']}"
        else:
            messages.append({
                "role": "function",
                "name": function_name,
                "content": result
            })
    
    # 只有在没有邮件发送结果时才调用主模型
    if not response:
        status_log.append("主模型:正在生成回答")
        final_response = get_openai_response(messages, model="gpt-4o-mini")
        response = final_response.content if final_response else "Error occurred"
        status_log.append("主模型:回答生成完成")
    
    return jsonify({
        "response": response,
        "status_log": status_log
    })

@app.route('/settings', methods=['POST'])
def update_settings():
    data = request.json
    max_history = data.get('max_history', 10)
    return jsonify({"status": "success", "max_history": max_history})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860, debug=True)