functions

package
v1.40.1-0...-c5b9f45 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	JSONBNF = `` /* 504-byte string literal not displayed */

)

Variables

This section is empty.

Functions

func CleanupLLMResult

func CleanupLLMResult(llmresult string, functionConfig FunctionsConfig) string

func ParseFunctionCallArgs

func ParseFunctionCallArgs(functionArguments string, functionConfig FunctionsConfig) string

func ParseJSON

func ParseJSON(s string) ([]map[string]any, error)

ParseJSON is a function that parses a JSON string that might contain multiple JSON objects and syntax errors in between by shifting the offset This for e.g. allow to parse { "foo": "bar" } invalid { "baz": "qux" } into [ { "foo": "bar" }, { "baz": "qux" } ] Credits to Michael Yang (https://github.com/mxyng) for the original implementation This is a slightly reworked version, improved for readability and error handling

func ParseTextContent

func ParseTextContent(llmresult string, functionConfig FunctionsConfig) string

Types

type Argument

type Argument struct {
	Type       string                 `json:"type"`
	Properties map[string]interface{} `json:"properties"`
}

type FuncCallResults

type FuncCallResults struct {
	Name      string
	Arguments string
}

func ParseFunctionCall

func ParseFunctionCall(llmresult string, functionConfig FunctionsConfig) []FuncCallResults

type Function

type Function struct {
	Name        string                 `json:"name"`
	Description string                 `json:"description"`
	Strict      bool                   `json:"strict"`
	Parameters  map[string]interface{} `json:"parameters"`
}

type FunctionName

type FunctionName struct {
	Const string `json:"const"`
}

type Functions

type Functions []Function

func (Functions) Select

func (f Functions) Select(name string) Functions

Select returns a list of functions containing the function with the given name

func (Functions) ToJSONStructure

func (f Functions) ToJSONStructure(name, args string) JSONFunctionStructure

ToJSONStructure converts a list of functions to a JSON structure that can be parsed to a grammar This allows the LLM to return a response of the type: { "name": "function_name", "arguments": { "arg1": "value1", "arg2": "value2" } }

type FunctionsConfig

type FunctionsConfig struct {
	// DisableNoAction disables the "no action" tool
	// By default we inject a tool that does nothing and is used to return an answer from the LLM
	DisableNoAction bool `yaml:"disable_no_action"`

	// Grammar is the configuration for the grammar
	GrammarConfig GrammarConfig `yaml:"grammar"`

	// NoActionFunctionName is the name of the function that does nothing. It defaults to "answer"
	NoActionFunctionName string `yaml:"no_action_function_name"`

	// NoActionDescriptionName is the name of the function that returns the description of the no action function
	NoActionDescriptionName string `yaml:"no_action_description_name"`

	// ResponseRegex is a named regex to extract the function name and arguments from the response
	ResponseRegex []string `yaml:"response_regex"`

	// JSONRegexMatch is a regex to extract the JSON object from the response
	JSONRegexMatch []string `yaml:"json_regex_match"`

	// ArgumentRegex is a named regex to extract the arguments from the response. Use ArgumentRegexKey and ArgumentRegexValue to set the names of the named regex for key and value of the arguments.
	ArgumentRegex []string `yaml:"argument_regex"`
	// ArgumentRegex named regex names for key and value extractions. default: key and value
	ArgumentRegexKey   string `yaml:"argument_regex_key_name"`   // default: key
	ArgumentRegexValue string `yaml:"argument_regex_value_name"` // default: value

	// ReplaceFunctionResults allow to replace strings in the results before parsing them
	ReplaceFunctionResults []ReplaceResult `yaml:"replace_function_results"`

	// ReplaceLLMResult allow to replace strings in the results before parsing them
	ReplaceLLMResult []ReplaceResult `yaml:"replace_llm_results"`

	// CaptureLLMResult is a regex to extract a string from the LLM response
	// that is used as return string when using tools.
	// This is useful for e.g. if the LLM outputs a reasoning and we want to get the reasoning as a string back
	CaptureLLMResult []string `yaml:"capture_llm_results"`

	// FunctionName enable the LLM to return { "name": "function_name", "arguments": { "arg1": "value1", "arg2": "value2" } }
	// instead of { "function": "function_name", "arguments": { "arg1": "value1", "arg2": "value2" } }.
	// This might be useful for certain models trained with the function name as the first token.
	FunctionNameKey      string `yaml:"function_name_key"`
	FunctionArgumentsKey string `yaml:"function_arguments_key"`
}

FunctionsConfig is the configuration for the tool/function call. It includes setting to map the function name and arguments from the response and, for instance, also if processing the requests with BNF grammars.

func (FunctionsConfig) GrammarOptions

func (g FunctionsConfig) GrammarOptions() []func(o *grammars.GrammarOption)

type GrammarConfig

type GrammarConfig struct {
	// ParallelCalls enables the LLM to return multiple function calls in the same response
	ParallelCalls bool `yaml:"parallel_calls"`

	DisableParallelNewLines bool `yaml:"disable_parallel_new_lines"`

	// MixedMode enables the LLM to return strings and not only JSON objects
	// This is useful for models to not constraining returning only JSON and also messages back to the user
	MixedMode bool `yaml:"mixed_mode"`

	// NoMixedFreeString disables the mixed mode for free strings
	// In this way if the LLM selects a free string, it won't be mixed necessarily with JSON objects.
	// For example, if enabled the LLM or returns a JSON object or a free string, but not a mix of both
	// If disabled(default): the LLM can return a JSON object surrounded by free strings (e.g. `this is the JSON result: { "bar": "baz" } for your question`). This forces the LLM to return at least a JSON object, but its not going to be strict
	NoMixedFreeString bool `yaml:"no_mixed_free_string"`

	// NoGrammar disables the grammar parsing and parses the responses directly from the LLM
	NoGrammar bool `yaml:"disable"`

	// Prefix is the suffix to append to the grammar when being generated
	// This is useful when models prepend a tag before returning JSON
	Prefix string `yaml:"prefix"`

	// ExpectStringsAfterJSON enables mixed string suffix
	ExpectStringsAfterJSON bool `yaml:"expect_strings_after_json"`

	// PropOrder selects what order to print properties
	// for instance name,arguments will make print { "name": "foo", "arguments": { "bar": "baz" } }
	// instead of { "arguments": { "bar": "baz" }, "name": "foo" }
	PropOrder string `yaml:"properties_order"`

	// SchemaType can be configured to use a specific schema type to force the grammar
	// available : json, llama3.1
	SchemaType string `yaml:"schema_type"`

	GrammarTriggers []GrammarTrigger `yaml:"triggers"`
}

type GrammarTrigger

type GrammarTrigger struct {
	// Trigger is the string that triggers the grammar
	Word string `yaml:"word"`
}

type Item

type Item struct {
	Type       string                 `json:"type"`
	Properties map[string]interface{} `json:"properties"`
}

type JSONFunctionStructure

type JSONFunctionStructure struct {
	OneOf []Item                 `json:"oneOf,omitempty"`
	AnyOf []Item                 `json:"anyOf,omitempty"`
	Defs  map[string]interface{} `json:"$defs,omitempty"`
}

func (JSONFunctionStructure) Grammar

func (j JSONFunctionStructure) Grammar(options ...func(*grammars.GrammarOption)) (string, error)

type ReplaceResult

type ReplaceResult struct {
	Key   string `yaml:"key"`
	Value string `yaml:"value"`
}

type SchemaConverter

type SchemaConverter interface {
	GrammarFromBytes([]byte, ...func(*grammars.GrammarOption)) (string, error)
}

func NewSchemaConverter

func NewSchemaConverter(opt grammars.GrammarOption) SchemaConverter

type Tool

type Tool struct {
	Type     string   `json:"type"`
	Function Function `json:"function,omitempty"`
}

type Tools

type Tools []Tool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL