File size: 1,688 Bytes
e17df50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Chains
from langchain_core.pydantic_v1 import BaseModel, Field

# To serve the app
from fastapi import FastAPI
from langchain_core.messages import BaseMessage
from langserve import add_routes, CustomUserType

import dotenv
dotenv.load_dotenv()

from ingredients import script_db, woo_db, full_chain, compound_chain, agent_executor



## Type specifications (with unusual class-scope fields)

class StrInput(BaseModel):
    input: str

class Input(BaseModel):
    input: str
    chat_history: list[BaseMessage] = Field(
        ...,
        extra = dict(widget = dict(type = 'chat', input = 'location')),
    )

class Output(BaseModel):
    output: str



## App definition
# NOTE: The chat playground type has a web page issue (flashes and becomes white, hence non-interactable; this was supposedly solved in an issue late last year)

app = FastAPI(
    title = 'Star Wars Expert',
    version = '1.0',
    description = 'A Star Wars expert chatbot',
)


# Basic retriever versions

# add_routes(app, script_db.as_retriever())
# add_routes(app, woo_db.as_retriever())


# History-aware retriever version
# add_routes(app, full_chain.with_types(input_type = StrInput, output_type = Output), playground_type = 'default')


# Agent version

# add_routes(app, agent_executor, playground_type = 'chat')
# add_routes(app, agent_executor.with_types(input_type = StrInput, output_type = Output))


# Non-agent chain-logic version

add_routes(app, compound_chain.with_types(input_type = StrInput))
# add_routes(app, compound_chain.with_types(input_type = Input), playground_type = 'chat')



if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, host = 'localhost', port = 8000)