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

comet_llm.API ¶

API(api_key: Optional[str] = None)

API class for accessing and updating prompt information.

Parameters:

  • api_key (Optional[str], default: None ) –

    You private Comet API key

Example
1
2
3
4
5
import comet_llm

comet_llm.init()

api = comet_llm.API()

get_llm_trace_by_key ¶

get_llm_trace_by_key(trace_key: str) -> llm_trace_api.LLMTraceAPI

Get an API Trace object by key.

Parameters:

  • trace_key (str) –

    Key of the prompt or chain

get_llm_trace_by_name ¶

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

Get an API Trace object by name.

Parameters:

  • workspace (str) –

    Name of the workspace.

  • project_name (str) –

    Name of the project.

  • trace_name (str) –

    Name of the prompt or chain.

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.

Parameters:

  • workspace (str) –

    Name of the workspace

  • project_name (str) –

    Name of the project

  • query (str) –

    Query to use

Note

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

  • QUERY_VARIABLE is either TraceMetadata, Duration, Timestamp.
  • 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
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import comet_llm
from comet_llm.query_dsl import TraceMetadata, Duration, Timestamp, UserFeedback

comet_llm.init()
api = comet_llm.API()

# 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)
Jul. 25, 2024