-
Notifications
You must be signed in to change notification settings - Fork 1
/
streaming.py
55 lines (45 loc) · 1.5 KB
/
streaming.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# from gpt_index import SimpleDirectoryReader, GPTListIndex,readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
import asyncio
from types import FunctionType
from llama_index import ServiceContext, GPTVectorStoreIndex, LLMPredictor, PromptHelper, SimpleDirectoryReader, load_index_from_storage
import sys
import os
import time
# from llama_index.response.schema import StreamingResponse
from fastapi.responses import StreamingResponse
import uvicorn
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import uvicorn
from create_agent import create_agent
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class Item(BaseModel):
input_text: str
agent = create_agent()
async def astreamer(generator):
try:
for i in generator:
yield (i)
await asyncio.sleep(.1)
except asyncio.CancelledError as e:
print('cancelled')
@app.post("/question_answering")
async def create_item(item: Item):
input_sentence = item.input_text
print("INPUT: ", input_sentence)
response = agent.chat(input_sentence)
return StreamingResponse(astreamer(response.response), media_type="text/event-stream")
@app.get("/")
@app.get("/health_check")
async def health_check():
return "ok"
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)