File tree 1 file changed +40
-0
lines changed
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ from fastapi import FastAPI
2
+ from langchain .prompts import ChatPromptTemplate
3
+ from langchain .chains import LLMChain
4
+ from langserve import add_routes
5
+ import uvicorn
6
+ from langchain_community .llms import Ollama
7
+
8
+ # Setup FastAPI app
9
+ app = FastAPI (
10
+ title = "Langchain Server" ,
11
+ version = "1.0" , # version should be a string
12
+ description = "A simple API server"
13
+ )
14
+
15
+ # Initialize Ollama model
16
+ llm = Ollama (model = "llama3.1" )
17
+
18
+ # Create prompts
19
+ prompt1 = ChatPromptTemplate .from_template ("write me an essay about {topic} with 100 words" )
20
+ prompt2 = ChatPromptTemplate .from_template ("write a poem about {topic} in 100 words" )
21
+
22
+ # Create LLMChains
23
+ chain1 = LLMChain (llm = llm , prompt = prompt1 )
24
+ chain2 = LLMChain (llm = llm , prompt = prompt2 )
25
+
26
+ # Add routes
27
+ add_routes (
28
+ app ,
29
+ chain1 ,
30
+ path = "/essay"
31
+ )
32
+ add_routes (
33
+ app ,
34
+ chain2 ,
35
+ path = "/poem"
36
+ )
37
+
38
+ # Run the app
39
+ if __name__ == "__main__" :
40
+ uvicorn .run (app , host = "127.0.0.1" , port = 8090 )
You can’t perform that action at this time.
0 commit comments