Search documentation of instructor_lite

# InstructorLite.Adapters.Gemini (instructor_lite v1.0.0)

View Source

Gemini adapter.

This adapter is implemented using Text generation endpoint configured for structured output

## Params

`params` argument should be shaped as a `models.GenerateBody` request body

## Example

    InstructorLite.instruct(
      %{contents: [%{role: "user", parts: [%{text: "John is 25yo"}]}]},
      response_model: %{name: :string, age: :integer},
      json_schema: %{
        type: "object",
        required: [:age, :name],
        properties: %{name: %{type: "string"}, age: %{type: "integer"}}
      },
      adapter: InstructorLite.Adapters.Gemini,
      adapter_context: [
        model: "gemini-1.5-flash-8b",
        api_key: Application.fetch_env!(:instructor_lite, :gemini_key)
      ]
    )
    {:ok, %{name: "John", age: 25}}

#### Specifying model

Note how, unlike other adapters, the Gemini adapter expects `model` under `adapter_context`.

#### JSON Schema

Gemini's idea of JSON Schema is quite different from other major models, so `InstructorLite.JSONSchema` won't help you even for simple cases. Luckily, the Gemini API provides detailed errors for invalid schemas.

# Summary

## Functions

initial_prompt(params, opts)

Puts `systemInstruction` and updates `generationConfig` in `params` with prompt based on `json_schema` and `notes`.

parse_response(response, opts)

Parse text generation endpoint response.

retry_prompt(params, resp_params, errors, response, opts)

Updates `params` with prompt for retrying a request.

send_request(params, opts)

Make request to Gemini API

# Functions

# initial_prompt(params, opts)

Puts `systemInstruction` and updates `generationConfig` in `params` with prompt based on `json_schema` and `notes`.

# parse_response(response, opts)

Parse text generation endpoint response.

Can return:

- `{:ok, parsed_json}` on success.
- `{:error, :refusal, prompt_feedback}` if request was blocked.
- `{:error, :unexpected_response, response}` if response is of unexpected shape.

# retry_prompt(params, resp_params, errors, response, opts)

Updates `params` with prompt for retrying a request.

# send_request(params, opts)

Make request to Gemini API

## Options

- `:api_key` (`String.t/0`) - Required. Gemini API key

- `:http_client` (`atom/0`) - Any module that follows `Req.post/2` interface The default value is `Req`.

- `:http_options` (`keyword/0`) - Options passed to `http_client.post/2` The default value is `[receive_timeout: 60000]`.

- `:url` (`String.t/0`) - API endpoint to use for sending requests The default value is `"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent"`.

- `:model` (`String.t/0`) - Gemini model The default value is `"gemini-1.5-flash-8b"`.