module AI::Google

Defined in:

ai/google/convert_json_schema_to_openapi_schema.cr
ai/google/convert_to_google_messages.cr
ai/google/convert_usage.cr
ai/google/get_model_path.cr
ai/google/google.cr
ai/google/google_error.cr
ai/google/google_generative_ai_embedding_model.cr
ai/google/google_generative_ai_image_model.cr
ai/google/google_generative_ai_image_settings.cr
ai/google/google_generative_ai_language_model.cr
ai/google/google_generative_ai_options.cr
ai/google/google_generative_ai_prompt.cr
ai/google/google_prepare_tools.cr
ai/google/google_provider.cr
ai/google/google_supported_file_url.cr
ai/google/map_finish_reason.cr
ai/google/version.cr

Constant Summary

API_KEY_ENV_VAR = "GOOGLE_GENERATIVE_AI_API_KEY"

Environment variable for API key.

DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com/v1beta"

Default base URL for Google Generative AI API.

DEFAULT_PROVIDER_NAME = "google.generative-ai"

Default provider name.

IMAGE_MODEL_IDS = ["imagen-4.0-generate-001", "imagen-4.0-ultra-generate-001", "imagen-4.0-fast-generate-001"]

Known image model IDs for Google Generative AI.

VALID_IMAGE_PROVIDER_OPTIONS = ["personGeneration", "aspectRatio"]

Valid provider options that can be passed to Google Image API.

VALID_LANGUAGE_MODEL_PROVIDER_OPTIONS = ["responseModalities", "thinkingConfig", "cachedContent", "structuredOutputs", "safetySettings", "threshold", "audioTimestamp", "labels", "mediaResolution", "imageConfig", "retrievalConfig"]

Valid provider option keys for filtering.

VERSION = "0.1.0"

Class Method Summary

Class Method Detail

def self.convert_json_schema_to_openapi_schema(json_schema : JSON::Any | Nil, is_root : Bool = true) : Hash(String, JSON::Any) | Nil #

Converts JSON Schema 7 to OpenAPI Schema 3.0.

This is needed because Google Generative AI uses OpenAPI schema format for structured outputs, while the AI SDK uses JSON Schema.


[View source]
def self.convert_to_google_messages(prompt : Provider::LanguageModel::Prompt, *, is_gemma_model : Bool = false, provider_options_name : String = "google") : ConvertToGoogleMessagesResult #

Converts SDK prompt messages to Google Generative AI format.


[View source]
def self.convert_usage(usage : UsageMetadata | Nil) : Provider::LanguageModel::Usage #

Converts Google Generative AI usage metadata to the unified Usage type.


[View source]
def self.create_google_generative_ai(base_url : String | Nil = nil, api_key : String | Nil = nil, headers : Hash(String, String) | Nil = nil, name : String | Nil = nil) : GoogleGenerativeAIProvider #

Creates a Google Generative AI provider instance.


[View source]
def self.get_model_path(model_id : String) : String #

Constructs the model path for API requests.

If the model_id contains a slash (e.g., "projects/my-project/..."), it's returned as-is. Otherwise, "models/" is prepended.


[View source]
def self.google : GoogleGenerativeAIProvider #

Default Google Generative AI provider instance.

Uses the GOOGLE_GENERATIVE_AI_API_KEY environment variable for authentication.


[View source]
def self.map_finish_reason(finish_reason : String | Nil, *, has_tool_calls : Bool) : FinishReason #

Maps Google Generative AI finish reasons to unified finish reasons.

The Google API returns various finish reasons that need to be mapped to the standardized AI SDK finish reasons.


[View source]
def self.prepare_tools(tools : Array(Tool) | Nil, tool_choice : Provider::LanguageModel::ToolChoice | Nil, model_id : String) : PrepareToolsResult #

Prepares tools for the Google Generative AI API.


[View source]
def self.supported_file_url?(url : URI) : Bool #

Checks if a URL is supported for direct file access by Google Generative AI.

Supported URLs:

  • Google Generative Language files API: https://generativelanguage.googleapis.com/v1beta/files/...
  • YouTube URLs (public or unlisted videos): youtube.com/watch?v=... or youtu.be/...

[View source]