-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.py
49 lines (38 loc) · 1.43 KB
/
serve.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
#!/usr/bin/env python
from typing import List
from fastapi import FastAPI
from langchain.prompts import ChatPromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.schema import BaseOutputParser
from langserve import add_routes
# 1. Chain definition
class OutputParser(BaseOutputParser[List[str]]):
"""Parse the output of an LLM call to a string."""
def parse(self, text: str) -> List[str]:
"""Parse the output of an LLM call."""
return "Selected cat from the list [\"tiger\", \"lion\" , \"leopard\" , \"snow leopard\" , \"jaguar\"] is: "+ text
template = """You are a expert of big cats who ONLY selects one item from this comma separated list:
LIST:["tiger", "lion" , "leopard" , "snow leopard" , "jaguar"]
A user will pass you a query expressed in natural language, and you must select one cat from LIST.
ONLY return a string from LIST, no additional text."""
human_template = "{text}"
chat_prompt = ChatPromptTemplate.from_messages([
("system", template),
("human", human_template),
])
category_chain = chat_prompt | ChatOpenAI() | OutputParser()
# 2. App definition
app = FastAPI(
title="LangChain Server",
version="1.0",
description="A simple api server using Langchain's Runnable interfaces",
)
# 3. Adding chain route
add_routes(
app,
category_chain,
path="/category_chain",
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="localhost", port=8000)