Dspy.LM behaviour (dspy v0.1.0)

View Source

Behaviour for language model clients.

Defines the interface for interacting with different language model providers like OpenAI, Anthropic, local models, etc.

Summary

Callbacks

Generate a completion from the language model.

Check if the language model supports a specific feature.

Functions

Create an assistant message.

Generate a completion using the configured language model.

Generate a completion using a specific language model.

Generate text from a simple prompt string.

Create a chat message.

Check if a language model supports a feature.

Create a system message.

Create a user message.

Types

message()

@type message() :: %{role: String.t(), content: String.t()}

request()

@type request() :: %{
  messages: [message()],
  max_tokens: pos_integer() | nil,
  temperature: float() | nil,
  stop: [String.t()] | nil,
  tools: [map()] | nil
}

response()

@type response() :: %{
  choices: [%{message: message(), finish_reason: String.t() | nil}],
  usage:
    %{
      prompt_tokens: pos_integer(),
      completion_tokens: pos_integer(),
      total_tokens: pos_integer()
    }
    | nil
}

t()

@type t() :: struct()

Callbacks

generate(lm, request)

@callback generate(lm :: t(), request :: request()) :: {:ok, response()} | {:error, any()}

Generate a completion from the language model.

supports?(lm, feature)

(optional)
@callback supports?(lm :: t(), feature :: atom()) :: boolean()

Check if the language model supports a specific feature.

Functions

assistant_message(content)

Create an assistant message.

generate(request)

Generate a completion using the configured language model.

generate(lm, request)

Generate a completion using a specific language model.

generate_text(prompt, opts \\ [])

Generate text from a simple prompt string.

message(role, content)

Create a chat message.

supports?(lm, feature)

Check if a language model supports a feature.

system_message(content)

Create a system message.

user_message(content)

Create a user message.