Atualizar app/app.py
This commit is contained in:
+86
@@ -1,3 +1,89 @@
|
|||||||
|
import json
|
||||||
|
import requests
|
||||||
|
from flask import Flask, Response, request, jsonify, session, redirect, url_for
|
||||||
|
from authlib.integrations.flask_client import OAuth
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
|
from config import Config
|
||||||
|
from chat_store import ChatStore
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
app.config.from_object(Config)
|
||||||
|
app.secret_key = app.config["SECRET_KEY"]
|
||||||
|
|
||||||
|
store = ChatStore()
|
||||||
|
|
||||||
|
oauth = OAuth(app)
|
||||||
|
google = oauth.register(
|
||||||
|
name="google",
|
||||||
|
client_id=app.config["GOOGLE_CLIENT_ID"],
|
||||||
|
client_secret=app.config["GOOGLE_CLIENT_SECRET"],
|
||||||
|
access_token_url="https://oauth2.googleapis.com/token",
|
||||||
|
authorize_url="https://accounts.google.com/o/oauth2/v2/auth",
|
||||||
|
api_base_url="https://www.googleapis.com/oauth2/v2/",
|
||||||
|
client_kwargs={"scope": "openid email profile"},
|
||||||
|
)
|
||||||
|
|
||||||
|
def login_required(f):
|
||||||
|
@wraps(f)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
if "user" not in session:
|
||||||
|
return redirect(url_for("login"))
|
||||||
|
return f(*args, **kwargs)
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
@app.route("/login")
|
||||||
|
def login():
|
||||||
|
return google.authorize_redirect(app.config["GOOGLE_REDIRECT_URI"])
|
||||||
|
|
||||||
|
@app.route("/oauth/callback")
|
||||||
|
def oauth_callback():
|
||||||
|
token = google.authorize_access_token()
|
||||||
|
user_info = google.get("userinfo").json()
|
||||||
|
session["user"] = user_info
|
||||||
|
return redirect("/")
|
||||||
|
|
||||||
|
@app.route("/api/new_conversation")
|
||||||
|
@login_required
|
||||||
|
def new_conversation():
|
||||||
|
cid = store.new_conversation()
|
||||||
|
return jsonify({"conversation_id": cid})
|
||||||
|
|
||||||
|
@app.route("/api/history/<cid>")
|
||||||
|
@login_required
|
||||||
|
def history(cid):
|
||||||
|
return jsonify(store.get_messages(cid))
|
||||||
|
|
||||||
|
@app.route("/api/stream")
|
||||||
|
@login_required
|
||||||
|
def stream():
|
||||||
|
cid = request.args.get("conversation_id")
|
||||||
|
model = request.args.get("model", "llama3.2")
|
||||||
|
prompt = request.args.get("prompt")
|
||||||
|
|
||||||
|
store.add_message(cid, "user", prompt)
|
||||||
|
|
||||||
|
def generate():
|
||||||
|
url = f"{app.config['OLLAMA_BASE_URL']}/api/generate"
|
||||||
|
payload = {"model": model, "prompt": prompt, "stream": True}
|
||||||
|
|
||||||
|
with requests.post(url, json=payload, stream=True) as r:
|
||||||
|
for line in r.iter_lines():
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
data = json.loads(line.decode())
|
||||||
|
token = data.get("response", "")
|
||||||
|
store.add_message(cid, "assistant", token)
|
||||||
|
yield f"data: {json.dumps({'token': token})}\n\n"
|
||||||
|
except:
|
||||||
|
continue
|
||||||
|
|
||||||
|
return Response(generate(), mimetype="text/event-stream")
|
||||||
|
|
||||||
|
|
||||||
|
# ***
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
|
|||||||
Reference in New Issue
Block a user