Skip to content

Commit

Permalink
✨ phoenix tracing added for llm
Browse files Browse the repository at this point in the history
  • Loading branch information
rathijitpapon committed Mar 26, 2024
1 parent 3dae97c commit 700d54e
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 12 deletions.
7 changes: 6 additions & 1 deletion backend/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,10 @@ POSTHOG_API_KEY=
POSTHOG_HOST=

# QDRANT API Configuration
## need to be set before running the server
QDRANT_API_KEY=
QDRANT_TOP_K=
QDRANT_TOP_K=

# Phoenix Configuration
## default values set in the code for local development
PHOENIX_API_ENDPOINT=
5 changes: 4 additions & 1 deletion backend/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,7 @@
# Dspy Integration Configuration
CLINICAL_TRIAL_SQL_PROGRAM: str = "app/dspy_integration/dspy_programs/clinical_trials_sql_generation.json"
CLINICAL_TRIALS_RESPONSE_REFINEMENT_PROGRAM: str = "app/dspy_integration/dspy_programs/clinical_trials_response_refinement.json"
ORCHESRATOR_ROUTER_PROMPT_PROGRAM: str = "app/dspy_integration/dspy_programs/orchestrator_router_prompt.json"
ORCHESRATOR_ROUTER_PROMPT_PROGRAM: str = "app/dspy_integration/dspy_programs/orchestrator_router_prompt.json"

# Phoenix Configuration
PHOENIX_API_ENDPOINT: str = config("PHOENIX_API_ENDPOINT", default="http://127.0.0.1:6007/v1/traces")
11 changes: 4 additions & 7 deletions backend/app/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# setup tracing
from app.services.tracing import setup_tracing
setup_tracing()

from fastapi import FastAPI, HTTPException
from fastapi.responses import RedirectResponse

# from authx import AuthX, AuthXConfig
from app.database.redis import Redis

from app import config
Expand All @@ -11,9 +14,6 @@

from app.middleware.process_time import ProcessTimeHeaderMiddleware

from app.services.tracing import setup_tracing


def get_application() -> FastAPI:
application = FastAPI(
title=config.PROJECT_NAME, debug=config.DEBUG, version=config.VERSION
Expand All @@ -27,9 +27,6 @@ def redirect_to_docs() -> RedirectResponse: # pylint: disable=W0612
async def startup(): # pylint: disable=W0612
print()

# setup tracing
setup_tracing()

# connect to redis
cache = Redis()
await cache.connect()
Expand Down
21 changes: 19 additions & 2 deletions backend/app/services/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
from sentry_sdk.integrations.starlette import StarletteIntegration
from sentry_sdk.integrations.fastapi import FastApiIntegration

from app.config import SENTRY_DSN, SENTRY_ENABLE_TRACING
from openinference.instrumentation.llama_index import LlamaIndexInstrumentor
from opentelemetry import trace as trace_api
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

from app.config import SENTRY_DSN, SENTRY_ENABLE_TRACING, PHOENIX_API_ENDPOINT, ENVIRONMENT

def setup_tracing():
sentry_sdk.init(
Expand All @@ -24,4 +31,14 @@ def setup_tracing():
event_level=logging.WARNING
),
],
)
)


if ENVIRONMENT == 'production':
resource = Resource(attributes={})
tracer_provider = trace_sdk.TracerProvider(resource=resource)
span_exporter = OTLPSpanExporter(endpoint=PHOENIX_API_ENDPOINT)
span_processor = SimpleSpanProcessor(span_exporter=span_exporter)
tracer_provider.add_span_processor(span_processor=span_processor)
trace_api.set_tracer_provider(tracer_provider=tracer_provider)
LlamaIndexInstrumentor().instrument()
200 changes: 199 additions & 1 deletion backend/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ llama-index-llms-together = "^0.1.3"
llama-index-postprocessor-cohere-rerank = "^0.1.2"
together = "^0.2.11"
llama-index-vector-stores-qdrant = "^0.1.4"
openinference-semantic-conventions = "^0.1.5"
openinference-instrumentation-llama-index = "^1.2.1"
opentelemetry-exporter-otlp = "^1.23.0"

[tool.poetry.group.dev.dependencies]
pytest = "^8.1.1"
Expand Down

0 comments on commit 700d54e

Please sign in to comment.