Skip to content

API

The API class is used to access and update prompts and chains logged to the Comet platform.

You can use an instance of the API() class to quickly and easily access all of your logged information at Comet, including prompts inputs, outputs, metadata.

Example calls:

  • API.get_llm_trace_by_name(trace_name): gets a trace by name
  • API.get_llm_trace_by_key(trace_key): gets a trace by key

API.init

__init__(api_key: Optional[str] = None)

API class for accessing and updating prompt information.

Args:

  • api_key: Optional. Your private COMET_API_KEY.

Note: api_key may be defined in the environment variable (COMET_API_KEY) or in a .comet.config file.

Example:

from comet_llm import API
api = API(api_key="<Your Comet API Key>")

API.get_llm_trace_by_key

get_llm_trace_by_key(trace_key: str) -> llm_trace_api.LLMTraceAPI

Get a LLMTraceAPI by key.

Args:

  • trace_key: str, key of the prompt or chain

Returns: An LLMTraceAPI object that can be used to get or update trace data

API.get_llm_trace_by_name

get_llm_trace_by_name(
    workspace: str,
    project_name: str,
    trace_name: str
) -> llm_trace_api.LLMTraceAPI

Get a LLMTraceAPI object by name.

Args:

  • workspace: str, name of the workspace
  • project_name: str, name of the project
  • trace_name: str, name of the prompt or chain

Returns: An LLMTraceAPI object that can be used to get or update trace data

API.query

query(workspace: str, project_name: str,
    query: str) -> List[llm_trace_api.LLMTraceAPI]

Fetch LLM Trace based on a query. Currently it is only possible to use trace metadata or details fields to filter the traces.

Args:

  • workspace: str, name of the workspace
  • project_name: str, name of the project
  • query: str, name of the prompt or chain

Returns: A list of LLMTraceAPI objects

Notes:

The query object takes the form of (QUERY_VARIABLE OPERATOR VALUE) with:

  • QUERY_VARIABLE is either TraceMetadata(""), Duration(), Timestamp(), UserFeedback().
  • OPERATOR is any standard mathematical operators <=, >=, !=, <, >.

It is also possible to add multiple query conditions using &.

If you are querying nested parameters, you should flatted the parameter name using the . operator.

To query the duration, you can use Duration().

Example:

from comet_llm import API
from comet_llm.query_dsl import TraceMetadata, Duration, Timestamp, UserFeedback

api = API(api_key = "<Your Comet API key>")

# Find all traces where the metadata field `token` is greater than 50
api.query("workspace", "project", TraceMetadata("token") > 50)

# Find all traces where the duration field is between 1 second and 2 seconds
api.query("workspace", "project", (Duration() > 1) & (Duration() <= 2))

# Find all traces based on the timestamp
api.query("workspace", "project", Timestamp() > datetime(2023, 9, 10))

# Find all traces based on positive user feedback
api.query("workspace", "project", UserFeedback() == 1)
Apr. 25, 2024