-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathstarter.py
More file actions
47 lines (36 loc) · 1.37 KB
/
starter.py
File metadata and controls
47 lines (36 loc) · 1.37 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
from contextlib import asynccontextmanager
from typing import List
from uuid import uuid4
import uvicorn
from activities import TranslateParams
from fastapi import FastAPI, HTTPException
from langchain_interceptor import LangChainContextPropagationInterceptor
from temporalio.client import Client
from temporalio.envconfig import ClientConfig
from workflow import LangChainWorkflow, TranslateWorkflowParams
@asynccontextmanager
async def lifespan(app: FastAPI):
config = ClientConfig.load_client_connect_config()
config.setdefault("target_host", "localhost:7233")
client = await Client.connect(
**config,
interceptors=[LangChainContextPropagationInterceptor()],
)
yield
app = FastAPI(lifespan=lifespan)
@app.post("/translate")
async def translate(phrase: str, language1: str, language2: str, language3: str):
languages = [language1, language2, language3]
client = app.state.temporal_client
try:
result = await client.execute_workflow(
LangChainWorkflow.run,
TranslateWorkflowParams(phrase, languages),
id=f"langchain-translation-{uuid4()}",
task_queue="langchain-task-queue",
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
return {"translations": result}
if __name__ == "__main__":
uvicorn.run(app, host="localhost", port=8000)