Class SingleFunctionCall
- Namespace
- LMKit.FunctionCalling
- Assembly
- LM-Kit.NET.dll
The SingleFunctionCall class provides an interface to execute a single method
based on a natural language prompt, leveraging language model inference for function selection
and parameter extraction. It supports dynamic method invocation, function registration,
and parameter validation. The class allows configuration to force function selection even when
no suitable function is identified.
public class SingleFunctionCall : IDisposable
- Inheritance
-
SingleFunctionCall
- Implements
- Inherited Members
Examples
Example: Define and call functions via natural language
using LMKit.Model;
using LMKit.FunctionCalling;
using LMKit.Agents.Tools;
using System;
using System.ComponentModel;
// Define a class with LMFunction-decorated methods
public class Calculator
{
[LMFunction("Add", "Adds two numbers together")]
public int Add(
[Description("First number")] int a,
[Description("Second number")] int b)
{
return a + b;
}
[LMFunction("Multiply", "Multiplies two numbers")]
public int Multiply(
[Description("First number")] int a,
[Description("Second number")] int b)
{
return a * b;
}
}
// Use the function caller
LM model = LM.LoadFromModelID("llama-3.2-1b");
using var functionCall = new SingleFunctionCall(model);
functionCall.ImportFunctions<Calculator>();
// Submit natural language prompts
var result1 = functionCall.Submit("What is 5 plus 3?");
Console.WriteLine($"Function: {result1.FunctionName}, Result: {result1.Result}");
var result2 = functionCall.Submit("Multiply 7 by 6");
Console.WriteLine($"Function: {result2.FunctionName}, Result: {result2.Result}");
Example: Using BeforeMethodInvoke for validation
using LMKit.Model;
using LMKit.FunctionCalling;
using LMKit.FunctionCalling.Events;
using System;
LM model = LM.LoadFromModelID("llama-3.2-1b");
using var functionCall = new SingleFunctionCall(model);
functionCall.ImportFunctions<Calculator>();
// Add validation before function execution
functionCall.BeforeMethodInvoke += (sender, args) =>
{
Console.WriteLine($"About to call: {args.FunctionName}");
Console.WriteLine($"Parameters: {string.Join(", ", args.Parameters)}");
// Optionally cancel the invocation
// args.Cancel = true;
};
var result = functionCall.Submit("Add 10 and 20");
Remarks
Key Features
- Register C# methods as callable functions via ImportFunctions<T>() or ImportFunctions(object)
- Natural language function selection using LLM inference
- Automatic parameter extraction from prompts
- Optional automatic function invocation via InvokeFunctions
- Force function selection mode via ForceFunctionSelection
- Pre-invocation event for validation via BeforeMethodInvoke
Function Registration
Methods must be decorated with LMFunctionAttribute to be recognized.
Parameters should have DescriptionAttribute for better inference.
Only primitive types and strings are supported as parameters.
Usage Pattern
- Create SingleFunctionCall with a language model
- Import functions from a class with LMFunction-decorated methods
- Submit natural language prompts to invoke functions
Constructors
- SingleFunctionCall(LM)
Initializes a new instance of the
SingleFunctionCallclass with the specified language model.
Properties
- ForceFunctionSelection
Gets or sets a value indicating whether a function must be selected even if none seems suitable. When set to
true, the engine will always select a function. When set tofalse, the engine can detect that no function is suitable for the instruction. The default value isfalse.
- InvokeFunctions
Gets or sets a value indicating whether the registered functions should be invoked automatically. The default value is
true.
Methods
- ImportFunctions(object)
Registers all methods in the specified object instance that are marked with the LMFunctionAttribute.
- ImportFunctions<T>()
Registers all methods in the specified type
Tthat are marked with the LMFunctionAttribute.
- Submit(string, CancellationToken)
Synchronously submits a natural language prompt and returns the result of the function invocation.
- SubmitAsync(string, CancellationToken)
Asynchronously submits a natural language prompt and returns the result of the function invocation.
Events
- BeforeMethodInvoke
Occurs before the invocation of an identified method, allowing subscribers to cancel the invocation.