Skip to content

Commit

Permalink
Merge pull request #22 from curieo-org/phoenix-tracing
Browse files Browse the repository at this point in the history
phoenix tracing added for llm
  • Loading branch information
raahulrahl authored Mar 27, 2024
2 parents 834914c + 48fb13d commit db9cf7b
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 15 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=
2 changes: 1 addition & 1 deletion backend/app/api/endpoints/search_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
@version(1, 0)
async def get_search_results(
query: str = "",
routecategory: RouteCategory = RouteCategory.NS
routecategory: RouteCategory = RouteCategory.PBW
) -> JSONResponse:
if trace_transaction := sentry_sdk.Hub.current.scope.transaction:
trace_transaction.set_tag("title", 'api_get_search_results')
Expand Down
5 changes: 4 additions & 1 deletion backend/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@
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"

# Phoenix Configuration
PHOENIX_API_ENDPOINT: str = config("PHOENIX_API_ENDPOINT", default="http://127.0.0.1:6007/v1/traces")

#AI models
ROUTER_MODEL: str = "gpt-3.5-turbo"
SQL_GENERATION_MODEL: str = "codellama/CodeLlama-13b-Instruct-hf"
RESPONSE_SYNTHESIZER_MODEL: str = "NousResearch/Nous-Hermes-llama-2-7b"
RESPONSE_SYNTHESIZER_MODEL: str = "NousResearch/Nous-Hermes-llama-2-7b"
5 changes: 4 additions & 1 deletion backend/app/database/nebula_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ def result_to_dict(self, result: ResultSet) -> dict[str, list]:
for col_num in range(result.col_size()):
col_name = columns[col_num]
col_list = result.column_values(col_name)
result_dict[col_name] = [x.cast() for x in col_list]
if len(col_list) > 0:
result_dict[col_name] = [x.cast() for x in col_list]

assert len(result_dict) > 0

return result_dict

Expand Down
9 changes: 2 additions & 7 deletions backend/app/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import app.services.tracing # noqa

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 +12,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 +25,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
2 changes: 1 addition & 1 deletion backend/app/router/orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, config):
async def query_and_get_answer(
self,
search_text: str,
routecategory: RouteCategory = RouteCategory.NS
routecategory: RouteCategory = RouteCategory.PBW
) -> dict[str, str]:
# search router call
logger.info(
Expand Down
24 changes: 22 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,17 @@ 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()


setup_tracing()
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 db9cf7b

Please sign in to comment.