InstructorLite.Adapters.OpenAI (instructor_lite v1.1.0)

View Source

OpenAI adapter.

This adapter is implemented using responses endpoint and structured outputs.

Params

params argument should be shaped as a Create model response request body.

Example

InstructorLite.instruct(%{
    input: [%{role: "user", content: "John is 25yo"}],
    model: "gpt-4o-mini",
    service_tier: "default"
  },
  response_model: %{name: :string, age: :integer},
  adapter: InstructorLite.Adapters.OpenAI,
  adapter_context: [api_key: Application.fetch_env!(:instructor_lite, :openai_key)]
)
{:ok, %{name: "John", age: 25}}

Summary

Functions

Parse API response in search of plain text output.

Updates params with prompt based on json_schema and notes.

Parse chat completion endpoint response.

Updates params with prompt for retrying a request.

Make request to OpenAI API.

Functions

find_output(response, opts)

Parse API response in search of plain text output.

Can return:

  • {:ok, text_output} on success.
  • {:error, :refusal, reason} on refusal.
  • {:error, :unexpected_response, response} if response is of unexpected shape.

initial_prompt(params, opts)

Updates params with prompt based on json_schema and notes.

It uses instructions parameter for system prompt.

Also specifies default gpt-4o-mini model if not provided by a user.

parse_response(response, opts)

Parse chat completion endpoint response.

Can return:

  • {:ok, parsed_json} on success.
  • {:error, :refusal, reason} on refusal.
  • {: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.

If the initial request was made with conversation state (enabled by default), it will drop previous chat messages from the request and specify previous_response_id instead. If conversation state is disabled, it will append new messages to the previous input the same way chat completions-based adapters do.

send_request(params, opts)

Make request to OpenAI API.

Options

  • :api_key (String.t/0) - Required. OpenAI 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://api.openai.com/v1/responses".