Management Commands

Initialize 3rd party providers.

smarter.apps.provider.management.commands.initialize_providers.ANTHROPIC_VERSION = '2023-06-01'

The Anthropic API version that the models api requires, in its anthropic-version header.

class smarter.apps.provider.management.commands.initialize_providers.Command(stdout=None, stderr=None, no_color=False, force_color=False)[source]

Bases: SmarterCommand

Django manage.py initialize_providers.py command.

Used to create/update the principal Providers that are preloaded on all platforms. Creates/updates the API key Secret, the Provider, and the ProviderModels for each provider. The following providers are initialized:

  • Anthropic

  • Cohere

  • Fireworks

  • Google Service Account

  • Google AI

  • Google Maps

  • Meta AI

  • Mistral

  • OpenAI

  • TogetherIA

This is called indirectly by initialize_platform and runs during automated deployment.

echo_banner(name)[source]

Echo a banner to the console.

handle(*args, **options)[source]

Initialize all built-in providers.

Whether a provider’s stored logo is the given image.

Every deploy runs this command. Saving the logo again uploads it, and saves the provider.

Parameters:
  • provider (Provider) – The provider.

  • logo (bytes) – The logo’s bytes.

Returns:

True if the provider has a logo, and its content is logo.

Return type:

bool

initialize_anthropic()[source]

Initialize Anthropic provider and its models.

Initialize the Brave Search api key Secret.

ImageSearchPlugins, such as safe_image_search, and the Brave WebsearchPlugin samples, such as research_assistant, read their api key from this Secret. As with Tavily, a missing key is not stored. An ImageSearchPlugin without it returns no images, and logs how to create it.

initialize_cohere()[source]

Initialize Cohere provider and its models.

initialize_fireworks()[source]

Initialize Fireworks provider and its models.

initialize_generic_provider(api_key_env_var, name, provider_configuration, logo_filename, provider_api_url, auth_headers=<function bearer_auth_headers>, models_params=None)[source]

Initialize a provider and its models.

  • create/update api key secret

  • create/update provider

  • create/update provider models

Parameters:
  • auth_headers (Callable[[str], dict[str, str]]) – returns the headers that authenticate to the provider’s models api, given its key.

  • models_params (dict[str, str] | None) – query parameters for the models api, e.g. a page size.

initialize_google_maps()[source]

Initialize Google Maps provider.

initialize_google_service_account()[source]

Initialize Google service account credentials.

A Google service account is a special identity that allows an application, rather than a person, to securely authenticate with Google APIs and services. It uses cryptographic credentials to prove its identity and obtain temporary access tokens without requiring a user to log in. The service account can only perform the actions that have been explicitly granted to it through Google Cloud IAM permissions.

Example service account JSON structure:

service_account_json = {
"type": "service_account",
"project_id": "smarter-sh",
"private_key_id": "65fc3e5bd38b1234567890676f6d6abcdefghijk",
"private_key": "PRIVATE-KEY-DATA",
"client_email": "smarter-gemini-initializer@smarter-sh.iam.gserviceaccount.com",
"client_id": "104887368144732193269",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/smarter-gemini-initializer%40smarter-sh.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}

service_account_b64 = base64.b64encode(json.dumps(service_account_json).encode("utf-8")).decode("ascii")
print(service_account_b64)
initialize_googleai()[source]

Initialize Google AI provider and its models.

initialize_metaai()[source]

Initialize Meta AI provider and its models.

initialize_mistral()[source]

Initialize Mistral provider and its models.

initialize_openai()[source]

Initialize OpenAI provider and its models.

initialize_tavily()[source]

Initialize the Tavily web search api key Secret.

WebsearchPlugins whose search provider is Tavily, such as the sample smarter_project_websearch, read their api key from this Secret. Unlike the provider api keys, a missing key is not stored: the Secret is skipped, so that applying the plugin reports the missing Secret.

initialize_togetheria()[source]

Initialize TogetherAI provider and its models.

log_missing_api_keys()[source]

Log one banner that lists the API keys that are not set, and how to set them.

missing_api_key(env_var, used_for, url)[source]

Record an API key that is not set, for the summary that handle() logs.

Parameters:
  • env_var (str) – The environment variable, without the optional SMARTER_ prefix.

  • used_for (str) – What the key is used for.

  • url (str) – Where to get the key.

missing_api_keys: list[tuple[str, str, str]]

their environment variable, what they are used for, and where to get one.

Type:

The API keys that are not set

user_profile: UserProfile
smarter.apps.provider.management.commands.initialize_providers.anthropic_auth_headers(api_key)[source]

The headers that authenticate to Anthropic’s models api, which rejects a request without anthropic-version.

Parameters:

api_key (str) – the Anthropic api key.

Return type:

dict[str, str]

Returns:

the x-api-key and anthropic-version headers.

smarter.apps.provider.management.commands.initialize_providers.bearer_auth_headers(api_key)[source]

The headers that authenticate to an OpenAI compatible models api.

Parameters:

api_key (str) – the provider’s api key.

Return type:

dict[str, str]

Returns:

an Authorization header with the key as a Bearer token.

smarter.apps.provider.management.commands.initialize_providers.googleai_auth_headers(api_key)[source]

The headers that authenticate to the Gemini api, which treats a Bearer token as an OAuth 2 token.

Parameters:

api_key (str) – the Gemini api key.

Return type:

dict[str, str]

Returns:

the x-goog-api-key header.