Skip to content

trulens.core.utils.keys

trulens.core.utils.keys

API keys and configuration

Setting keys

To check whether appropriate api keys have been set:

from trulens.core.utils.keys import check_keys

check_keys(
    "OPENAI_API_KEY",
    "HUGGINGFACE_API_KEY"
)

Alternatively you can set using check_or_set_keys:

from trulens.core.utils.keys import check_or_set_keys

check_or_set_keys(
    OPENAI_API_KEY="to fill in",
    HUGGINGFACE_API_KEY="to fill in"
)

This line checks that you have the requisite api keys set before continuing the notebook. They do not need to be provided, however, right on this line. There are several ways to make sure this check passes:

  • Explicit -- Explicitly provide key values to check_keys.

  • Python -- Define variables before this check like this:

OPENAI_API_KEY="something"
  • Environment -- Set them in your environment variable. They should be visible when you execute:
import os
print(os.environ)
  • .env -- Set them in a .env file in the same folder as the example notebook or one of its parent folders. An example of a .env file is found in trulens/trulens/env.example .

  • Endpoint class For some keys, set them as arguments to trulens endpoint class that manages the endpoint. For example, with openai, do this ahead of the check_keys check:

from trulens.providers.openai import OpenAIEndpoint
openai_endpoint = OpenAIEndpoint(api_key="something")
  • Provider class For some keys, set them as arguments to trulens feedback collection ("provider") class that makes use of the relevant endpoint. For example, with openai, do this ahead of the check_keys check:
from trulens.providers.openai import OpenAI
openai_feedbacks = OpenAI(api_key="something")

In the last two cases, please note that the settings are global. Even if you create multiple OpenAI or OpenAIEndpoint objects, they will share the configuration of keys (and other openai attributes).

Other API attributes

Some providers may require additional configuration attributes beyond api key. For example, openai usage via azure require special keys. To set those, you should use the 3rd party class method of configuration. For example with openai:

import openai

openai.api_type = "azure"
openai.api_key = "..."
openai.api_base = "https://example-endpoint.openai.azure.com"
openai.api_version = "2023-05-15"  # subject to change
# See https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/switching-endpoints .

Our example notebooks will only check that the api_key is set but will make use of the configured openai object as needed to compute feedback.

Functions

redact_value

redact_value(
    v: Union[str, Any], k: Optional[str] = None
) -> Union[str, Any]

Determine whether the given value v should be redacted and redact it if so. If its key k (in a dict/json-like) is given, uses the key name to determine whether redaction is appropriate. If key k is not given, only redacts if v is a string and identical to one of the keys ingested using setup_keys.

get_config_file

get_config_file() -> Optional[Path]

Looks for a .env file in current folder or its parents. Returns Path of found .env or None if not found.

_check_key

_check_key(
    k: str,
    v: Optional[str] = None,
    silent: bool = False,
    warn: bool = False,
) -> bool

Check that the given k is an env var with a value that indicates a valid api key or secret. If v is provided, checks that instead. If value indicates the key is not set, raises an informative error telling the user options on how to set that key. If silent is set, nothing will be printed. If warn is set, will log warning to logger and not throw an exception. Returns True if key is set. Silent disable warning logging.

_relative_path

_relative_path(path: Path, relative_to: Path) -> str

Get the path path relative to path relative_to even if relative_to is not a prefix of path. Iteratively takes the parent of relative_to in that case until it becomes a prefix. Each parent is indicated by '..'.

_collect_keys

_collect_keys(
    *args: str, **kwargs: Dict[str, str]
) -> Dict[str, str]

Collect values for keys from all of the currently supported sources. This includes:

  • Using env variables.

  • Using python variables.

  • Explicitly passed to check_or_set_keys.

  • Using vars defined in a .env file in current folder or one of its parents.

  • With initialization of trulens Endpoint class that handles a 3rd party api.

check_keys

check_keys(*keys: str) -> None

Check that all keys named in *args are set as env vars. Will fail with a message on how to set missing key if one is missing. If all are provided somewhere, they will be set in the env var as the canonical location where we should expect them subsequently.

Example
from trulens.core.utils.keys import check_keys

check_keys(
    "OPENAI_API_KEY",
    "HUGGINGFACE_API_KEY"
)

check_or_set_keys

check_or_set_keys(
    *args: str, **kwargs: Dict[str, str]
) -> None

Check various sources of api configuration values like secret keys and set env variables for each of them. We use env variables as the canonical storage of these keys, regardless of how they were specified. Values can also be specified explicitly to this method. Example:

from trulens.core.utils.keys import check_or_set_keys

check_or_set_keys(
    OPENAI_API_KEY="to fill in",
    HUGGINGFACE_API_KEY="to fill in"
)