通过云服务器代理转发实现
具体代码如下
# !/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: [email protected]
@file: server.py
@time: 2023/11/3 17:16
@desc: 异步转发服务端,支持上下文
"""
import asyncio
import aiohttp
from quart import Quart, request, jsonify
app = Quart(__name__)
def get_response():
return {
"code": -1,
"msg": "",
"data": ""
}
async def get_data(query,repeat,system="你是一个智能机器人"):
"""
"""
users = [{"role": "user", "content": "{}".format(user)} for user in query.split("|")]
messages = [{"role": "system", "content": "{}".format(system)}]
messages.extend(users)
# print("message",messages)
url = "https://api.openai.com/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer 这里填写购买的GPT4 key"
}
data = {
"model": "gpt-4",
"messages": messages
}
answer_lis = []
print("repeat",repeat)
for i in range(repeat):
async with aiohttp.ClientSession(headers=headers) as session:
async with session.post(url, json=data) as response:
response_text = await response.json()
answer_lis.append({str(i+1):{
"query":query,
"answer":response_text
}})
return answer_lis
@app.route('/conn', methods=["POST"])
async def conn():
result = get_response()
query = (await request.form).get("query")
print("query", query)
if query:
system = (await request.form).get("system",None)
print("system",system)
try:
repeat = (await request.form).get("repeat",1)
if repeat:
repeat = int(repeat)
if repeat<=1:
repeat = 1
res = await get_data(query,repeat,system)
except Exception as e:
print(e,e.__traceback__.tb_lineno)
code = 1
msg = "gpt error"
else:
code = 0
msg = "success"
result["data"] = res
else:
code = -1
msg = "query empty"
result["code"] = code
result["msg"] = msg
return jsonify(result)
if __name__ == '__main__':
asyncio.run(app.run_task(host='0.0.0.0', port=5000))
# !/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: [email protected]
@file: client.py
@time: 2023/11/3 17:17
@desc:
"""
import requests
# 转发服务接口url
url = "http://服务器外网ip地址:5000/conn"
# 请求参数 query:提问的问题 上下文语句通过 | 分割 ,repeat 重复请求接口次数
data = {"query": "你是gpt4吗|我爱你","repeat":3}
# 下边是这个返回值格式样例
print(requests.post(url, data=data).json())
{
"code": 0,#0:正常状态,非0存在问题
"data": {#gpt接口返回的数据
"answer": {
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "是的,我是OpenAI的语言模型GPT-4。",
"role": "assistant"
}
}
],
"created": 1698990351,
"id": "chatcmpl-8GhI7thqOFj3qbrY3rCbcuZ84tq9i",
"model": "gpt-4-0613",
"object": "chat.completion",
"usage": {
"completion_tokens": 17,
"prompt_tokens": 44,
"total_tokens": 61
}
},
"query": "你是gpt4吗" # 提问的问题
},
"msg": "success" # 状态,原因 code不等于0时会在这里提示异常原因
}