Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(projects): add support for the PHOENIX_PROJECT_NAME param #2515

Merged
merged 4 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/phoenix/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
The directory in which to save, load, and export datasets. This directory must
be accessible by both the Phoenix server and the notebook environment.
"""
ENV_PHOENIX_PROJECT_NAME = "PHOENIX_PROJECT_NAME"
"""
The project name to use when logging traces and evals. defaults to 'default'.
"""


def _get_temp_path() -> Path:
Expand Down Expand Up @@ -113,3 +117,7 @@ def get_env_host() -> str:

def get_env_collector_endpoint() -> Optional[str]:
return os.getenv(ENV_PHOENIX_COLLECTOR_ENDPOINT)


def get_env_project_name() -> str:
return os.getenv(ENV_PHOENIX_PROJECT_NAME) or "default"
9 changes: 8 additions & 1 deletion src/phoenix/session/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
from requests import Session

import phoenix as px
from phoenix.config import get_env_collector_endpoint, get_env_host, get_env_port
from phoenix.config import (
get_env_collector_endpoint,
get_env_host,
get_env_port,
get_env_project_name,
)
from phoenix.session.data_extractor import TraceDataExtractor
from phoenix.trace import Evaluations
from phoenix.trace.dsl import SpanQuery
Expand Down Expand Up @@ -59,6 +64,7 @@ def query_spans(
root_spans_only: Optional[bool] = None,
project_name: Optional[str] = None,
) -> Optional[Union[pd.DataFrame, List[pd.DataFrame]]]:
project_name = project_name or get_env_project_name()
if not queries:
queries = (SpanQuery(),)
if self._use_active_session_if_available and (session := px.active_session()):
Expand Down Expand Up @@ -102,6 +108,7 @@ def get_evaluations(
self,
project_name: Optional[str] = None,
) -> List[Evaluations]:
project_name = project_name or get_env_project_name()
if self._use_active_session_if_available and (session := px.active_session()):
return session.get_evaluations(project_name=project_name)
response = self._session.get(
Expand Down
51 changes: 46 additions & 5 deletions src/phoenix/session/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
ENV_PHOENIX_COLLECTOR_ENDPOINT,
get_env_host,
get_env_port,
get_env_project_name,
get_exported_files,
)
from phoenix.core.model_schema_adapter import create_model_from_datasets
from phoenix.core.project import DEFAULT_PROJECT_NAME
from phoenix.core.traces import Traces
from phoenix.datasets.dataset import EMPTY_DATASET, Dataset
from phoenix.pointcloud.umap_parameters import get_umap_parameters
Expand Down Expand Up @@ -331,8 +331,34 @@ def query_spans(
root_spans_only: Optional[bool] = None,
project_name: Optional[str] = None,
) -> Optional[Union[pd.DataFrame, List[pd.DataFrame]]]:
"""
Queries the spans in the project based on the provided parameters.

Parameters
----------
queries : *SpanQuery
Variable-length argument list of SpanQuery objects representing
the queries to be executed.

start_time : datetime, optional
datetime representing the start time of the query.

stop_time : datetime, optional
datetime representing the stop time of the query.

root_spans_only : boolean, optional
whether to include only root spans in the results.

project_name : string, optional
name of the project to query. Defaults to the project name set
in the environment variable `PHOENIX_PROJECT_NAME` or 'default' if not set.

Returns:
results : DataFrame
DataFrame or list of DataFrames containing the query results.
"""
if not (traces := self.traces) or not (
project := traces.get_project(project_name or DEFAULT_PROJECT_NAME)
project := traces.get_project(project_name or get_env_project_name())
):
return None
if not queries:
Expand Down Expand Up @@ -362,9 +388,24 @@ def get_evaluations(
self,
project_name: Optional[str] = None,
) -> List[Evaluations]:
if not (traces := self.traces) or not (
project := traces.get_project(project_name or DEFAULT_PROJECT_NAME)
):
"""
Get the evaluations for a project.

Parameters
----------
project_name : str, optional
The name of the project. If not provided, the project name set
in the environment variable `PHOENIX_PROJECT_NAME` will be used.
Otherwise, 'default' will be used.

Returns
-------
evaluations : List[Evaluations]
A list of evaluations for the specified project.

"""
project_name = project_name or get_env_project_name()
if not (traces := self.traces) or not (project := traces.get_project(project_name)):
return []
return project.export_evaluations()

Expand Down
7 changes: 6 additions & 1 deletion src/phoenix/trace/langchain/instrumentor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
from typing import Any

from openinference.instrumentation.langchain import LangChainInstrumentor as Instrumentor
from openinference.semconv.resource import ResourceAttributes
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

from phoenix.config import get_env_project_name
from phoenix.trace.exporter import _OpenInferenceExporter
from phoenix.trace.tracer import _show_deprecation_warnings

Expand All @@ -26,6 +29,8 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__()

def instrument(self) -> None:
tracer_provider = trace_sdk.TracerProvider()
tracer_provider = trace_sdk.TracerProvider(
resource=Resource({ResourceAttributes.PROJECT_NAME: get_env_project_name()})
)
tracer_provider.add_span_processor(SimpleSpanProcessor(_OpenInferenceExporter()))
super().instrument(skip_dep_check=True, tracer_provider=tracer_provider)
7 changes: 6 additions & 1 deletion src/phoenix/trace/llama_index/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
from importlib.util import find_spec
from typing import Any

from openinference.semconv.resource import ResourceAttributes
from opentelemetry import trace as trace_api
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

from phoenix.config import get_env_project_name
from phoenix.trace.errors import IncompatibleLibraryVersionError
from phoenix.trace.exporter import _OpenInferenceExporter
from phoenix.trace.tracer import _show_deprecation_warnings
Expand Down Expand Up @@ -72,6 +75,8 @@ class OpenInferenceTraceCallbackHandler(_OpenInferenceTraceCallbackHandler):

def __init__(self, *args: Any, **kwargs: Any) -> None:
_show_deprecation_warnings(self, *args, **kwargs)
tracer_provider = trace_sdk.TracerProvider()
tracer_provider = trace_sdk.TracerProvider(
resource=Resource({ResourceAttributes.PROJECT_NAME: get_env_project_name()})
)
tracer_provider.add_span_processor(SimpleSpanProcessor(_OpenInferenceExporter()))
super().__init__(trace_api.get_tracer(__name__, __version__, tracer_provider))
7 changes: 6 additions & 1 deletion src/phoenix/trace/openai/instrumentor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
from typing import Any

from openinference.instrumentation.openai import OpenAIInstrumentor as Instrumentor
from openinference.semconv.resource import ResourceAttributes
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

from phoenix.config import get_env_project_name
from phoenix.trace.exporter import _OpenInferenceExporter
from phoenix.trace.tracer import _show_deprecation_warnings

Expand All @@ -21,6 +24,8 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__()

def instrument(self) -> None:
tracer_provider = trace_sdk.TracerProvider()
tracer_provider = trace_sdk.TracerProvider(
resource=Resource({ResourceAttributes.PROJECT_NAME: get_env_project_name()})
)
tracer_provider.add_span_processor(SimpleSpanProcessor(_OpenInferenceExporter()))
super().instrument(skip_dep_check=True, tracer_provider=tracer_provider)
Loading