使用 Python 和 Flask 构建一个简单的聊天机器人
import requests
import flask
import re
import openai
headers = {
'authority': 'free-api.cveoy.top',
'accept': '*/*',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'no-cache',
'content-type': 'application/json',
'dnt': '1',
'origin': 'https://www.cveoy.com',
'pragma': 'no-cache',
'referer': 'https://www.cveoy.com/',
'sec-ch-ua': 'Google Chrome';v='111', 'Not(A:Brand';v='8', 'Chromium';v='111',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': 'Windows',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'cross-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
}
json_data = {
'prompt': '测试2',
}
openai.api_key='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'
def chat(content):
json_data['prompt'] = content
response = requests.post('https://free-api.cveoy.top/v3/completions', headers=headers, json=json_data, stream=True)
for chunk in response.iter_content(chunk_size=512):
yield chunk
#flask
app = flask.Flask(__name__)
@app.route('/', methods=['GET'])
def returnchat():
content = flask.request.args.get('content')
return flask.Response(chat(content), mimetype='text/plain')
if __name__ == '__main__':
app.run(
host='0.0.0.0',
port= 9000,
debug=False
)
原文地址: https://www.cveoy.top/t/topic/nMrN 著作权归作者所有。请勿转载和采集!