streaming experiments

This commit is contained in:
Alex
2023-05-18 23:52:59 +01:00
parent e49dd0cc6a
commit ff2e79fe7b
3 changed files with 70 additions and 35 deletions

View File

@@ -9,7 +9,7 @@ import dotenv
import requests
from celery import Celery
from celery.result import AsyncResult
from flask import Flask, request, render_template, send_from_directory, jsonify
from flask import Flask, request, render_template, send_from_directory, jsonify, Response
from langchain import FAISS
from langchain import VectorDBQA, HuggingFaceHub, Cohere, OpenAI
from langchain.chains import LLMChain, ConversationalRetrievalChain
@@ -120,6 +120,21 @@ def home():
embeddings_choice=settings.EMBEDDINGS_NAME)
def complete_stream(input):
import time
for i in range(10):
data = json.dumps({"answer": i})
#data = {"answer": str(i)}
yield f"data: {data}\n\n"
time.sleep(0.05)
# send data.type = "end" to indicate that the stream has ended as json
data = json.dumps({"type": "end"})
yield f"data: {data}\n\n"
@app.route("/stream", methods=['POST', 'GET'])
def stream():
return Response(complete_stream("hi"), mimetype='text/event-stream')
@app.route("/api/answer", methods=["POST"])
def api_answer():
data = request.get_json()