View Source LangChain.FunctionParam (LangChain v0.2.0)

Define a function parameter as a struct. Used to generate the expected JSONSchema data for describing one or more arguments being passed to a LangChain.Function.

Note: This is not intended to be a fully compliant implementation of JSONSchema types. This is intended to be a convenience for working with the most common situations when working with an LLM that understands JSONSchema.

Supports:

  • simple values - string, integer, number, boolean
  • enum values - enum: ["alpha", "beta"]. The values can be strings, integers, etc.
  • array values - type: :array couples with item_type: "string" to express it is an array of.
    • item_type is optional. When omitted, it can be a mixed array.
    • item_type: "object" allows for creating an array of objects. Use object_properties: [...] to describe the structure of the objects.
  • objects - Define the object's expected values or supported structure using object_properties.

The function to_parameters_schema/1 is used to convert a list of FunctionParam structs into a JSONSchema formatted data map.

Examples

Basic string field.

FunctionParam.new!(%{name: "name", type: :string})

Basic string field with description

FunctionParam.new!(%{name: "name",
                     type: :string,
                     description: "User's name"})

Basic required string field with description

FunctionParam.new!(%{name: "name", type: :string, required: true})

Required boolean field with description

FunctionParam.new!(%{name: "active", type: :boolean, required: true})

A string Enum field. The field's value can only be one of the values specified by the enum.

FunctionParam.new!(%{name: "color",
                     type: :string,
                     enum: ["red", "yellow", "blue"],
                     description: "The specified primary color})

Array of strings field example. Defines a set of tags that an LLM may assign.

FunctionParam.new!(%{name: "tags", type: :array, item_type: "string")

A field that represents an object with nested fields.

FunctionParam.new!(%{name: "person",
                     type: :object,
                     object_properties: [
                       FunctionParam.new!(%{name: "name", type: :string, required: true}),
                       FunctionParam.new!(%{name: "age", type: :integer}),
                     ]
                  })

Summary

Functions

Build a new FunctionParam struct.

Build a new FunctionParam struct and return it or raise an error if invalid.

Return the list of required property names.

Transform a FunctionParam to a JSONSchema compatible definition that is added to the passed in data map.

Transform a list of FunctionParam structs into a map expressing the structure in a JSONSchema compatible way.

Types

@type t() :: %LangChain.FunctionParam{
  description: term(),
  enum: term(),
  item_type: term(),
  name: term(),
  object_properties: term(),
  required: term(),
  type: term()
}

Functions

@spec new(attrs :: map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

Build a new FunctionParam struct.

@spec new!(attrs :: map()) :: t() | no_return()

Build a new FunctionParam struct and return it or raise an error if invalid.

Link to this function

required_properties(params)

View Source
@spec required_properties(params :: [t()]) :: [String.t()]

Return the list of required property names.

Link to this function

to_json_schema(data, param)

View Source
@spec to_json_schema(data :: map(), t()) :: map() | no_return()

Transform a FunctionParam to a JSONSchema compatible definition that is added to the passed in data map.

Link to this function

to_parameters_schema(params)

View Source
@spec to_parameters_schema([t()]) :: %{required(String.t()) => any()}

Transform a list of FunctionParam structs into a map expressing the structure in a JSONSchema compatible way.