Skip to content

Commit e957edf

Browse files
demo: Add FastAPI Example (#14)
1 parent 9641bf3 commit e957edf

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

demo/fastapi_langchain/__init__.py

Whitespace-only changes.

demo/fastapi_langchain/local.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uvicorn main:app --port 6000 --reload

demo/fastapi_langchain/main.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
3+
from fastapi import FastAPI
4+
from starlette.middleware.gzip import GZipMiddleware
5+
from starlette_exporter import PrometheusMiddleware, handle_metrics
6+
7+
from xpuls.mlmonitor.langchain.instrument import LangchainTelemetry
8+
9+
service_name = "xpuls AI demo service"
10+
app = FastAPI(
11+
title=service_name,
12+
description="",
13+
version="0.0.1"
14+
)
15+
app.add_middleware(GZipMiddleware, minimum_size=1000)
16+
app.add_middleware(PrometheusMiddleware, app_name=service_name, group_paths=True)
17+
app.add_route("/metrics", handle_metrics) # Metrics are published at this endpoint
18+
19+
# Initialise xpuls.ai
20+
default_labels = {"service": "xpulsai-demo-service", "namespace": "mlops",
21+
"agent_name": "not_found"}
22+
23+
os.environ["XPULSAI_TRACING_ENABLED"] = "true" # ENABLE THIS ONLY IF ADVANCED xpuls.ai MONITORING IS REQUIRED
24+
25+
# xpuls_host_url is an optional parameter and required if `XPULSAI_TRACING_ENABLED` is enabled
26+
LangchainTelemetry(default_labels=default_labels, xpuls_host_url='https://api.xpuls.ai').auto_instrument()
27+
28+
@app.get('/healthcheck')
29+
def health_check():
30+
return "ping!"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fastapi
2+
starlette-exporter
3+
starlette
4+
uvicorn

0 commit comments

Comments
 (0)