Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
fa8e1ef
feat: add fetch_scores method to Langfuse client
RichardKruemmel Aug 1, 2024
a298b09
feat: add fetch_scores method to Langfuse client
RichardKruemmel Aug 1, 2024
5f1e761
feat: update fetch_scores method in Langfuse client
RichardKruemmel Aug 1, 2024
e092a83
feat: update fetch_scores method in Langfuse client
RichardKruemmel Aug 1, 2024
4983e43
feat: add list method to ScoreClient
RichardKruemmel Aug 1, 2024
39f96a0
fix: fix assertion order in test_fetch_scores
RichardKruemmel Aug 1, 2024
7ec1368
feat: add fetch_sessions method to Langfuse client
RichardKruemmel Aug 1, 2024
c99055d
feat: add test_fetch_sessions to Langfuse client
RichardKruemmel Aug 1, 2024
62baa1a
feat: refactor fetch_sessions method in Langfuse client
RichardKruemmel Aug 1, 2024
c5c2d5d
feat: add FetchPromptsResponse class and fetch_prompts method to Lang…
RichardKruemmel Aug 1, 2024
30073ed
feat: add test_fetch_prompts to Langfuse client
RichardKruemmel Aug 1, 2024
1997259
refactor: remove unnecessary blank lines in Langfuse client
RichardKruemmel Aug 1, 2024
fe7c4c6
refactor: remove unnecessary blank lines in Langfuse client
RichardKruemmel Aug 1, 2024
42694c4
refactor: remove unnecessary blank lines in Langfuse client
RichardKruemmel Aug 1, 2024
b53ac5e
refactor: update Langfuse client's get_prompts method parameters
RichardKruemmel Aug 1, 2024
9a410ae
refactor: remove unnecessary blank lines in Langfuse client
RichardKruemmel Aug 1, 2024
50ad9d5
refactor: add user_id and name parameters to Langfuse client's fetch_…
RichardKruemmel Aug 1, 2024
71452dc
refactor: update Langfuse client's list method parameters
RichardKruemmel Aug 1, 2024
c13f56a
refactor: create multiple versions of a prompt in Langfuse client
RichardKruemmel Aug 1, 2024
400ffea
refactor: update Langfuse client's fetch_scores method parameters
RichardKruemmel Aug 1, 2024
a0b41da
refactor: update Langfuse client's fetch_scores method parameters
RichardKruemmel Aug 1, 2024
a88b648
refactor: update Langfuse client's fetch_scores method parameters
RichardKruemmel Aug 1, 2024
c5c216c
refactor: Update Langfuse client's fetch_scores method parameters
RichardKruemmel Aug 1, 2024
49cb6c6
refactor: Update Langfuse client's fetch_scores method parameters
RichardKruemmel Aug 1, 2024
e8da5e1
refactor: Update Langfuse client's fetch_scores method parameters
RichardKruemmel Aug 1, 2024
7c58106
refactor: Update Langfuse client's fetch_scores method parameters
RichardKruemmel Aug 1, 2024
2fb6325
refactor: Update Langfuse client's fetch_scores method parameters
RichardKruemmel Aug 1, 2024
410bd7a
refactor: Update Langfuse client's fetch_prompts method to fetch a sp…
RichardKruemmel Aug 1, 2024
e4a342e
refactor: Update Langfuse client's fetch_prompts method to fetch a sp…
RichardKruemmel Aug 1, 2024
4f74d31
refactor: Add methods to Langfuse client for retrieving datasets and …
RichardKruemmel Aug 2, 2024
4b04131
refactor: Add methods to Langfuse client for retrieving datasets and …
RichardKruemmel Aug 2, 2024
807ec4a
refactor: Update Langfuse client's fetch_scores method parameters
RichardKruemmel Aug 2, 2024
55a7bc3
refactor: Add tests for retrieving dataset items and datasets
RichardKruemmel Aug 2, 2024
3c1dc9b
refactor: Update Langfuse client's fetch_datasets method to handle pa…
RichardKruemmel Aug 2, 2024
2ecaecc
refactor: Update Langfuse client's fetch_datasets method to handle pa…
RichardKruemmel Aug 2, 2024
412ea05
refactor: Update Langfuse client's fetch_datasets method to handle pa…
RichardKruemmel Aug 2, 2024
d1509d8
refactor: Update Langfuse client's fetch_score method to fetch a spec…
RichardKruemmel Aug 2, 2024
cd90b20
refactor: Update Langfuse client's fetch_score method to fetch a spec…
RichardKruemmel Aug 2, 2024
49bde62
Refactor Langfuse client's fetch_score method to return specific scor…
RichardKruemmel Aug 2, 2024
6b9f363
Merge branch 'main' into richard/lfe-1312
RichardKruemmel Aug 20, 2024
616fa42
refactor: simplify test_fetch_score_by_id function
RichardKruemmel Aug 20, 2024
601fb72
Merge branch 'main' into richard/lfe-1312
RichardKruemmel Aug 20, 2024
fc51422
Merge branch 'main' into richard/lfe-1312
RichardKruemmel Aug 27, 2024
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
206 changes: 206 additions & 0 deletions langfuse/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import urllib.parse
import warnings
from dataclasses import dataclass
from langfuse.api.core.request_options import RequestOptions


from langfuse.api.resources.commons.types.dataset_run_with_items import (
DatasetRunWithItems,
)
from langfuse.api.resources.commons.types.observations_view import ObservationsView
from langfuse.api.resources.commons.types.score_source import ScoreSource
from langfuse.api.resources.commons.types.session import Session
from langfuse.api.resources.commons.types.trace_with_details import TraceWithDetails
from langfuse.api.resources.datasets.types.paginated_dataset_runs import (
Expand Down Expand Up @@ -126,6 +128,27 @@ class FetchSessionsResponse:
data: typing.List[Session]
meta: MetaResponse

@dataclass
class FetchScoreResponse:
"""Response object for fetch_score method."""

data: ScoreBody

@dataclass
class FetchScoresResponse:
"""Response object for fetch_scores method."""

data: typing.List[ScoreBody]
meta: MetaResponse


@dataclass
class FetchPromptsResponse:
"""Response object for fetch_prompts method."""

data: typing.List[Union[Prompt_Text, Prompt_Chat]]
meta: MetaResponse


class Langfuse(object):
"""Langfuse Python client.
Expand Down Expand Up @@ -368,6 +391,26 @@ def get_dataset(
except Exception as e:
self.log.exception(e)
raise e

def get_datasets(
self, *, page: Optional[int] = None, limit: Optional[int] = None, request_options: Optional[RequestOptions] = None
) -> typing.List[Dataset]:
"""Get all datasets.

Args:
page (Optional[int]): Page number of the datasets to return, starts at 1. Defaults to None.
limit (Optional[int]): Maximum number of datasets to return. Defaults to 50.
request_options (Optional[RequestOptions]): Request options. Defaults to None.

Returns:
List[Dataset]: The datasets.
"""
try:
self.log.debug("Getting datasets")
return self.client.datasets.list(page=page, limit=limit, request_options=request_options)
except Exception as e:
self.log.exception(e)
raise e

def get_dataset_item(self, id: str) -> "DatasetItemClient":
"""Get the dataset item with the given id."""
Expand Down Expand Up @@ -539,6 +582,38 @@ def create_dataset_item(
except Exception as e:
self.log.exception(e)
raise e

def get_dataset_items(
self,
dataset_name: typing.Optional[str] = None,
source_trace_id: typing.Optional[str] = None,
source_observation_id: typing.Optional[str] = None,
page: typing.Optional[int] = None,
limit: typing.Optional[int] = None,
request_options: typing.Optional[RequestOptions] = None,
) -> typing.List[DatasetItem]:
"""Get all dataset items.

Args:
dataset_name (Optional[str]): Name of the dataset.
source_trace_id (Optional[str]): Source trace id.
source_observation_id (Optional[str]): Source observation id.
page (Optional[int]): Page number of the dataset items to return, starts at 1. Defaults to None.
limit (Optional[int]): Maximum number of dataset items to return. Defaults to 50.
request_options (Optional[RequestOptions]): Request options. Defaults to None.

Returns:
List[DatasetItem]: The dataset items.
"""
try:
self.log.debug("Getting dataset items")
return self.client.dataset_items.list(
dataset_name=dataset_name, source_trace_id=source_trace_id, source_observation_id=source_observation_id, page=page, limit=limit, request_options=request_options
)
except Exception as e:
self.log.exception(e)
raise e


def fetch_trace(
self,
Expand Down Expand Up @@ -932,6 +1007,137 @@ def fetch_sessions(
except Exception as e:
self.log.exception(e)
raise e

def fetch_score(
self,
id: str,
) -> FetchScoreResponse:
"""Get a score in the current project with the given identifier.

Args:
id: The identifier of the score to fetch.

Returns:
FetchScoreResponse: The score with the given id on `data`.

Raises:
Exception: If the score with the given id could not be found within the authenticated project or if an error occurred during the request.
"""
try:
self.log.debug(f"Getting score {id}")
res = self.client.score.get_by_id(id)
return FetchScoreResponse(data=res)
except Exception as e:
self.log.exception(e)
raise e

def fetch_scores(
self,
*,
page: typing.Optional[int] = None,
limit: typing.Optional[int] = None,
user_id: typing.Optional[str] = None,
name: typing.Optional[str] = None,
from_timestamp: typing.Optional[dt.datetime] = None,
to_timestamp: typing.Optional[dt.datetime] = None,
source: typing.Optional[ScoreSource] = None,
operator: typing.Optional[str] = None,
value: typing.Optional[float] = None,
score_ids: typing.Optional[str] = None,
config_id: typing.Optional[str] = None,
data_type: typing.Optional[ScoreDataType] = None,
request_options: typing.Optional[RequestOptions] = None,

) -> FetchScoresResponse:
"""Get a list of scores in the current project.

Args:
page (Optional[int]): Page number of the scores to return. Defaults to None.
limit (Optional[int]): Maximum number of scores to return. Defaults to None.
user_id (Optional[str]): User identifier. Defaults to None.
name (Optional[str]): Name of the scores to return. Defaults to None.
from_timestamp (Optional[dt.datetime]): Retrieve only scores with a timestamp on or after this datetime. Defaults to None.
to_timestamp (Optional[dt.datetime]): Retrieve only scores with a timestamp before this datetime. Defaults to None.
source (Optional[ScoreSource]): Source of the scores. Defaults to None.
operator (Optional[str]): Operator of the scores. Defaults to None.
value (Optional[float]): Value of the scores. Defaults to None.
score_ids (Optional[str]): Score identifier. Defaults to None.
config_id (Optional[str]): Configuration identifier. Defaults to None.
data_type (Optional[ScoreDataType]): Data type of the scores. Defaults to None.
request_options (Optional[RequestOptions]): Type of the score. Defaults to None.

Returns:
FetchScoresResponse, list of scores on `data` and metadata on `meta`.

Raises:
Exception: If an error occurred during the request.
"""
try:
self.log.debug(
f"Getting scores... {page}, {limit}, {user_id}, {name}, {from_timestamp}, {to_timestamp}, {source}, {operator}, {value}, {score_ids}, {config_id}, {data_type}, {request_options}"
)
res = self.client.score.get(
page=page,
limit=limit,
user_id=user_id,
name=name,
from_timestamp=from_timestamp,
to_timestamp=to_timestamp,
source=source,
operator=operator,
value=value,
score_ids=score_ids,
config_id=config_id,
data_type=data_type,
request_options=request_options,
)
return FetchScoresResponse(data=res.data, meta=res.meta)
except Exception as e:
self.log.exception(e)
raise e

def fetch_prompts(
self,
*,
page: typing.Optional[int] = None,
limit: typing.Optional[int] = None,
name: typing.Optional[str] = None,
label: typing.Optional[str] = None,
tag: typing.Optional[str] = None,
request_options: typing.Optional[RequestOptions] = None,
) -> FetchPromptsResponse:
"""Get a list of prompts in the current project matching the given parameters.

Args:
page (Optional[int]): Page number of the prompts to return. Defaults to None.
limit (Optional[int]): Maximum number of prompts to return. Defaults to None.
name (Optional[str]): Name of the prompts to return. Defaults to None.
label (Optional[str]): Label of the prompts to return. Defaults to None.
tag (Optional[str]): Tag of the prompts to return. Defaults to None.
request_options (Optional[RequestOptions]): Type of the prompt. Defaults to None.

Returns:
FetchPromptsResponse, list of prompts on `data` and metadata on `meta`.

Raises:
Exception: If an error occurred during the request.
"""
try:
self.log.debug(
f"Getting prompts... {page}, {limit}, {name}, {label}, {tag}, {request_options}"
)
res = self.client.prompts.list(
page=page,
limit=limit,
name=name,
label=label,
tag=tag,
request_options=request_options,
)
return FetchPromptsResponse(data=res.data, meta=res.meta)
except Exception as e:
self.log.exception(e)
raise e

@overload
def get_prompt(
Expand Down
Loading