View Source InstructorLite.Adapter behaviour (instructor_lite v0.3.0)

Behaviour for implementing adapter modules.

The role of an adapter module is to encapsulate all logic for a particular LLM, which can be quite specific. As a result, most of the details live in adapter modules and main InstructorLite interface is very broad.

Implement your own!

While built-in adapters are fairly flexible, users are encouraged to write their own adapters to establish a better control over prompt building, http clients, etc.

Summary

Types

Map of adapter-specific values, such as messages, prompt, model name, or temperature. These parameters are typically sent to the LLM in request body.

Parsed response content that can be cast to a changeset.

Raw content of a successful response.

Callbacks

Update params with model-specific prompt.

Parse API response.

Update params with model-specific prompt for a retry. This usually involves appending errors from a changeset.

Make request to model API.

Types

params()

@type params() :: map()

Map of adapter-specific values, such as messages, prompt, model name, or temperature. These parameters are typically sent to the LLM in request body.

parsed_response()

@type parsed_response() :: map()

Parsed response content that can be cast to a changeset.

response()

@type response() :: any()

Raw content of a successful response.

Callbacks

initial_prompt(params, opts)

@callback initial_prompt(params(), InstructorLite.opts()) :: params()

Update params with model-specific prompt.

parse_response(response, opts)

@callback parse_response(response(), InstructorLite.opts()) ::
  {:ok, parsed_response()} | {:error, any()} | {:error, reason :: atom(), any()}

Parse API response.

On success, it returns an ok tuple with parsed json object ready to be cast to a changeset.

On error, it may return a 3-element error tuple with error reason. In the worst-case scenario, it returns the original input wrapped in an error tuple.

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

@callback retry_prompt(
  params(),
  parsed_response(),
  errors :: String.t(),
  response(),
  InstructorLite.opts()
) :: params()

Update params with model-specific prompt for a retry. This usually involves appending errors from a changeset.

send_request(params, opts)

@callback send_request(params(), InstructorLite.opts()) ::
  {:ok, response()} | {:error, any()}

Make request to model API.