File size: 3,581 Bytes
5a2b2d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from langchain_core.runnables import Runnable
from langchain_core.callbacks import BaseCallbackHandler
from fastapi import FastAPI, Request, Depends
from sse_starlette.sse import EventSourceResponse
from langserve.serialization import WellKnownLCSerializer
from typing import List
from sqlalchemy.orm import Session

import schemas
from chains import simple_chain
import crud, models, schemas
from database import SessionLocal, engine
from callbacks import LogResponseCallback


models.Base.metadata.create_all(bind=engine)

app = FastAPI()

# def get_db():
#     db = SessionLocal()
#     try:
#         yield db
#     finally:
#         db.close()


async def generate_stream(input_data: schemas.BaseModel, runnable: Runnable, callbacks: List[BaseCallbackHandler]=[]):
    for output in runnable.stream(input_data.dict(), config={"callbacks": callbacks}): 
        data = WellKnownLCSerializer().dumps(output).decode("utf-8")
        yield {'data': data, "event": "data"} 
    yield {"event": "end"}


@app.post("/simple/stream")
async def simple_stream(request: Request):
    data = await request.json()
    user_question = schemas.UserQuestion(**data['input'])
    return EventSourceResponse(generate_stream(user_question, simple_chain))


# @app.post("/formatted/stream")
# async def formatted_stream(request: Request):
#     # TODO: use the formatted_chain to implement the "/formatted/stream" endpoint.
#     raise NotImplemented


# def get_db():
#     db = SessionLocal()
#     try:
#         yield db
#     finally:
#         db.close()

# @app.post("/history/stream")
# async def history_stream(request: Request, db: Session = Depends(get_db)):  
#     # TODO: Let's implement the "/history/stream" endpoint. The endpoint should follow those steps:
#     # - The endpoint receives the request
#     # - The request is parsed into a user request
#     # - The user request is used to pull the chat history of the user
#     # - We add as part of the user history the current question by using add_message.
#     # - We create an instance of HistoryInput by using format_chat_history.
#     # - We use the history input within the history chain.
#     raise NotImplemented


# @app.post("/rag/stream")
# async def rag_stream(request: Request, db: Session = Depends(get_db)):  
#     # TODO: Let's implement the "/rag/stream" endpoint. The endpoint should follow those steps:
#     # - The endpoint receives the request
#     # - The request is parsed into a user request
#     # - The user request is used to pull the chat history of the user
#     # - We add as part of the user history the current question by using add_message.
#     # - We create an instance of HistoryInput by using format_chat_history.
#     # - We use the history input within the rag chain.
#     raise NotImplemented


# @app.post("/filtered_rag/stream")
# async def filtered_rag_stream(request: Request, db: Session = Depends(get_db)):  
#     # TODO: Let's implement the "/filtered_rag/stream" endpoint. The endpoint should follow those steps:
#     # - The endpoint receives the request
#     # - The request is parsed into a user request
#     # - The user request is used to pull the chat history of the user
#     # - We add as part of the user history the current question by using add_message.
#     # - We create an instance of HistoryInput by using format_chat_history.
#     # - We use the history input within the filtered rag chain.
#     raise NotImplemented
    


if __name__ == "__main__":
    import uvicorn
    uvicorn.run("main:app", host="localhost", reload=True,  port=8000)