init commit
This commit is contained in:
commit
a6c6d6e336
|
|
@ -0,0 +1,11 @@
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
*.pyo
|
||||||
|
.env
|
||||||
|
|
||||||
|
# IDE
|
||||||
|
.vscode/
|
||||||
|
.idea/
|
||||||
|
|
||||||
|
# OS
|
||||||
|
.DS_Store" >> .gitignore
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
from flask import Flask
|
||||||
|
from app.api.v1.controllers import api_v1
|
||||||
|
from app.api.v1.zhipu_controller import zhipu_controller
|
||||||
|
from app.config import Config
|
||||||
|
|
||||||
|
def create_app():
|
||||||
|
app = Flask(__name__)
|
||||||
|
app.config.from_object(Config)
|
||||||
|
|
||||||
|
# Register Blueprints
|
||||||
|
app.register_blueprint(api_v1, url_prefix='/api/v1')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Register Zhipu-related routes under /api/v1/zhipu
|
||||||
|
app.register_blueprint(zhipu_controller, url_prefix='/api/v1')
|
||||||
|
|
||||||
|
return app
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
from flask import Blueprint, jsonify
|
||||||
|
|
||||||
|
api_v1 = Blueprint('api_v1', __name__)
|
||||||
|
|
||||||
|
@api_v1.route('/ping', methods=['GET'])
|
||||||
|
def ping():
|
||||||
|
return jsonify({'message': 'pong!'})
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
from flask import Blueprint, request, jsonify, Response
|
||||||
|
from app.services.zhipu_service import ZhipuService
|
||||||
|
|
||||||
|
zhipu_controller = Blueprint('zhipu_controller', __name__)
|
||||||
|
zhipu_service = ZhipuService()
|
||||||
|
|
||||||
|
|
||||||
|
@zhipu_controller.route('/zhipu/stream', methods=['POST'])
|
||||||
|
def stream_sse():
|
||||||
|
data = request.json
|
||||||
|
message = data.get('message', '')
|
||||||
|
|
||||||
|
def event_stream():
|
||||||
|
for chunk in zhipu_service.talk_to_zhipu_sse(message):
|
||||||
|
if chunk:
|
||||||
|
yield chunk
|
||||||
|
# yield f"data: {chunk}\n\n"
|
||||||
|
|
||||||
|
return Response(event_stream(), content_type='text/event-stream')
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
import os
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
SECRET_KEY = os.environ.get('SECRET_KEY') or 'supersecretkey'
|
||||||
|
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///app.db'
|
||||||
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
from zhipuai import ZhipuAI
|
||||||
|
|
||||||
|
class ZhipuService:
|
||||||
|
def __init__(self):
|
||||||
|
self.model_name = "glm-4"
|
||||||
|
self.app_secret_key = "d54f764a1d67c17d857bd3983b772016.GRjowY0fyiMNurLc"
|
||||||
|
|
||||||
|
def talk_to_zhipu(self, message):
|
||||||
|
client = ZhipuAI(api_key=self.app_secret_key) # 请填写您自己的APIKey
|
||||||
|
response = client.chat.completions.create(
|
||||||
|
model=self.model_name, # 填写需要调用的模型名称
|
||||||
|
messages=[
|
||||||
|
{"role": "user", "content": message},
|
||||||
|
],
|
||||||
|
stream=True, # 流式输出
|
||||||
|
temperature= 0.01, #随机度,越大越发散,0.01则是一个比较确定和稳定的输出
|
||||||
|
top_p= 0.1, #选择前 10% 概率的 tokens 作为候选,也是影响随机程度
|
||||||
|
)
|
||||||
|
accum_resp = ""
|
||||||
|
for chunk in response:
|
||||||
|
print(chunk.choices[0].delta.content)
|
||||||
|
accum_resp = accum_resp + chunk.choices[0].delta.content
|
||||||
|
print(accum_resp)
|
||||||
|
return accum_resp
|
||||||
|
|
||||||
|
def talk_to_zhipu_sse(self, message):
|
||||||
|
client = ZhipuAI(api_key=self.app_secret_key) # 请填写您自己的APIKey
|
||||||
|
response = client.chat.completions.create(
|
||||||
|
model=self.model_name, # 填写需要调用的模型名称
|
||||||
|
messages=[
|
||||||
|
{"role": "user", "content": message},
|
||||||
|
],
|
||||||
|
stream=True, # 流式输出
|
||||||
|
temperature= 0.01, #随机度,越大越发散,0.01则是一个比较确定和稳定的输出
|
||||||
|
top_p= 0.1, #选择前 10% 概率的 tokens 作为候选,也是影响随机程度
|
||||||
|
)
|
||||||
|
|
||||||
|
for chunk in response:
|
||||||
|
print(chunk.choices[0].delta.content)
|
||||||
|
yield chunk.choices[0].delta.content
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
from app import create_app
|
||||||
|
|
||||||
|
app = create_app()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run()
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
alembic==1.13.3
|
||||||
|
blinker==1.8.2
|
||||||
|
click==8.1.7
|
||||||
|
Flask==3.0.3
|
||||||
|
Flask-Migrate==4.0.7
|
||||||
|
Flask-SQLAlchemy==3.1.1
|
||||||
|
importlib_metadata==8.5.0
|
||||||
|
importlib_resources==6.4.5
|
||||||
|
itsdangerous==2.2.0
|
||||||
|
Jinja2==3.1.4
|
||||||
|
Mako==1.3.5
|
||||||
|
MarkupSafe==2.1.5
|
||||||
|
SQLAlchemy==2.0.35
|
||||||
|
typing_extensions==4.12.2
|
||||||
|
Werkzeug==3.0.4
|
||||||
|
zipp==3.20.2
|
||||||
|
zhipuai==2.0.1
|
||||||
Loading…
Reference in New Issue