-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.py
More file actions
58 lines (41 loc) · 1.25 KB
/
serve.py
File metadata and controls
58 lines (41 loc) · 1.25 KB
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
# Implementing everything we built into a single chain.
from fastapi import FastAPI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_groq import ChatGroq
import os
from dotenv import load_dotenv
from langserve import add_routes
#Add routes helps us to create APIs. It is based on FastAPI
load_dotenv()
#Setting up the model.
groq_api_key = os.getenv("GROQ_API_KEY")
model = ChatGroq(model="Gemma2-9b-It", groq_api_key = groq_api_key)
#Creating the prompts.
system_template = "Translate the following into {language} in a conversational style. Don't keep it formal."
human_template = "{text}"
chat_prompt = ChatPromptTemplate.from_messages(
[
("system", system_template),
("human", human_template),
]
)
#Setting up Parser
parser = StrOutputParser()
#Setting up the chain.
chain = chat_prompt | model | parser
# App Definition
app = FastAPI(title='Langchain Translation using Groq',
version='1.0.0',
description="A simple API server using langchain rumble interfaces."
)
#Adding Routes
add_routes(
app,
chain,
path='/chain'
)
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8000)
#