优化ai服务适配
This commit is contained in:
21
ai-server.py
21
ai-server.py
@@ -1,26 +1,29 @@
|
||||
from flask import Flask, request
|
||||
from zhipuai import ZhipuAI
|
||||
from openai import OpenAI
|
||||
from waitress import serve
|
||||
|
||||
app = Flask(__name__)
|
||||
client = ZhipuAI(api_key="d1e97306540d12bb2f834be961fcacb1.SNBShlCxWYJCx0qZ")
|
||||
client = OpenAI(base_url="https://open.bigmodel.cn/api/paas/v4", api_key="d1e97306540d12bb2f834be961fcacb1.SNBShlCxWYJCx0qZ")
|
||||
|
||||
|
||||
@app.route('/ai_text', methods=['POST'])
|
||||
def ai_text():
|
||||
input = request.get_data(as_text=True)
|
||||
_input = request.get_data(as_text=True)
|
||||
# 如果input值为空,则返回空字符串
|
||||
if not input:
|
||||
if not _input:
|
||||
return ''
|
||||
print(f'input: {_input}')
|
||||
# app.logger.info(f'input: {input}')
|
||||
completion = client.chat.completions.create(
|
||||
model="glm-4-flash",
|
||||
messages=[
|
||||
{"role": "user", "content": input},
|
||||
{"role": "user", "content": _input},
|
||||
],
|
||||
stream=False
|
||||
)
|
||||
return completion.choices[0].message.content
|
||||
_output = completion.choices[0].message.content
|
||||
# app.logger.info(f'output: {output}')
|
||||
return _output
|
||||
|
||||
|
||||
# app.run(host='0.0.0.0', port=6721)
|
||||
from waitress import serve
|
||||
|
||||
serve(app, host='0.0.0.0', port=6721)
|
||||
|
||||
Reference in New Issue
Block a user