Skip to content

Commit

Permalink
feat: refactor startup process and instrument httpx
Browse files Browse the repository at this point in the history
  • Loading branch information
CNSeniorious000 committed Jun 14, 2024
1 parent 84adb8e commit 19e3ec8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies = [
"zhipuai~=2.1.0.20240521",
"anthropic~=0.28.0",
"dashscope~=1.19.2",
"logfire[fastapi,system-metrics]~=0.42.0",
"logfire[fastapi,system-metrics,httpx]~=0.42.0",
]

[tool.pdm]
Expand Down
25 changes: 25 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from contextlib import suppress

with suppress(ModuleNotFoundError):
from dotenv import load_dotenv

load_dotenv(override=True)

from .utils.config import env

if env.logfire_token:
import logfire

logfire.configure()
logfire.info("app started", **env.model_dump())
logfire.instrument_httpx()
logfire.instrument_openai()
logfire.instrument_anthropic()

from .entry import app

logfire.instrument_fastapi(app)

from .utils.load import generate_pyi

generate_pyi()
14 changes: 1 addition & 13 deletions src/entry.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.responses import PlainTextResponse
from starlette.middleware.cors import CORSMiddleware
from starlette.staticfiles import StaticFiles

from .routes.prompts import prompts_router
from .routes.run import run_router
from .utils.config import env
from .utils.load import generate_pyi
from .utils.time import now

app = FastAPI(title="Promplate Demo", description="<https://github.com/promplate/demo>", on_startup=[generate_pyi, load_dotenv])
app = FastAPI(title="Promplate Demo", description="<https://github.com/promplate/demo>")
app.add_middleware(CORSMiddleware, allow_origins="*", allow_credentials=True, allow_methods="*", allow_headers="*")
app.include_router(prompts_router, prefix="/prompts")
app.include_router(run_router)
Expand All @@ -22,12 +19,3 @@ async def greet():


app.mount("/", StaticFiles(directory="frontend/dist", html=True, check_dir=False))

if env.logfire_token:
import logfire

logfire.configure()
logfire.info("app started", **env.model_dump())
logfire.instrument_openai()
logfire.instrument_anthropic()
logfire.instrument_fastapi(app)

0 comments on commit 19e3ec8

Please sign in to comment.