-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_step_lcel.py
56 lines (44 loc) · 1.28 KB
/
2_step_lcel.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
56
#!/usr/bin/env python
from fastapi import FastAPI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI
from langserve import add_routes
from dotenv import load_dotenv
from langchain.chains import TransformChain
load_dotenv()
# 1. Create prompt template
system_template = "Translate the following into {language}:"
prompt_template = ChatPromptTemplate.from_messages([
('system', system_template),
('user', '{text}')
])
# 2. Create model
model = ChatOpenAI()
# 3. Create parser
parser = StrOutputParser()
# 4. Create uppercase transform function
def uppercase_transform(inputs: dict) -> dict:
return {"uppercase_text": inputs["text"].upper()}
uppercase_chain = TransformChain(
input_variables=["text"],
output_variables=["uppercase_text"],
transform=uppercase_transform
)
# 5. Create chain
chain = prompt_template | model | parser | uppercase_chain
# 6. App definition
app = FastAPI(
title="LangChain Server",
version="1.0",
description="A simple API server using LangChain's Runnable interfaces",
)
# 7. Adding chain route
add_routes(
app,
chain,
path="/chain",
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="localhost", port=8000)