Agent class

An agent that can run prompts through an AI model and return responses.

This class provides a unified interface for interacting with different AI model providers and handling both string and typed responses.

Constructors

Agent.new(String model, {String? embeddingModel, String? apiKey, Uri? baseUrl, String? systemPrompt, JsonSchema? outputSchema, dynamic outputFromJson(Map<String, dynamic> json)?, Iterable<Tool>? tools, ToolCallingMode? toolCallingMode, double? temperature})
Factory constructor to create an Agent for a specific model.
factory
Agent.provider(Provider provider, {String? systemPrompt, JsonSchema? outputSchema, dynamic outputFromJson(Map<String, dynamic> json)?, Iterable<Tool>? tools, ToolCallingMode? toolCallingMode, double? temperature})
Creates a new Agent with the given provider.

Properties

caps Set<ProviderCaps>
The capabilities of this agent's model.
no setter
hashCode int
The hash code for this object.
no setterinherited
model String
Returns the model used by this agent in the format: providerName:generativeModelName, e.g. openai:gpt-4o google:gemini-2.0-flash openrouter:gpt-4o
getter/setter pair
outputFromJson → dynamic Function(Map<String, dynamic> json)?
Function to convert JSON output to a typed object.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

createEmbedding(String text, {EmbeddingType type = EmbeddingType.document}) Future<Float64List>
Generates vector embeddings for the given text.
listModels() Future<Iterable<ModelInfo>>
Lists all available models from this provider.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
run(String prompt, {Iterable<Message> messages = const [], Iterable<Part> attachments = const []}) Future<AgentResponse>
Executes the given prompt using the model and returns the complete response.
runFor<T>(String prompt, {Iterable<Message> messages = const [], Iterable<Part> attachments = const []}) Future<AgentResponseFor<T>>
Runs the given prompt through the model and returns a typed response.
runStream(String prompt, {Iterable<Message> messages = const [], Iterable<Part> attachments = const []}) Stream<AgentResponse>
Runs the given prompt through the model and returns the response as a stream.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

environment Map<String, String>
A map of environment variables that can be used by providers.
final
providers Map<String, ProviderFactory>
Returns a map of all available providers.
no setter

Static Methods

cosineSimilarity(Float64List a, Float64List b) double
Computes cosine similarity between two vectors.
dotProduct(Float64List a, Float64List b) double
Computes the dot product (scalar product) of two vectors.
findTopMatches<T>({required Map<T, Float64List> embeddingMap, required Float64List queryEmbedding, int limit = 1}) Iterable<T>
Returns the limit items with highest cosine similarity to queryEmbedding.
providerFor(String model, {String? embeddingModel, String? apiKey, Uri? baseUrl, double? temperature}) Provider
Resolves the Provider for the given model string.
runPrompt(DotPrompt prompt, {String? systemPrompt, JsonSchema? outputSchema, dynamic outputFromJson(Map<String, dynamic> json)?, Iterable<Tool>? tools, Map<String, dynamic> input = const {}, Iterable<Message> messages = const [], Iterable<Part> attachments = const []}) Future<AgentResponse>
Executes a given DotPrompt and returns the complete response.
runPromptFor<T>(DotPrompt prompt, {String? systemPrompt, JsonSchema? outputSchema, dynamic outputFromJson(Map<String, dynamic> json)?, Iterable<Tool>? tools, Map<String, dynamic> input = const {}, Iterable<Message> messages = const [], Iterable<Part> attachments = const []}) Future<AgentResponseFor<T>>
Executes a DotPrompt and returns a typed response.
runPromptStream(DotPrompt prompt, {String? systemPrompt, JsonSchema? outputSchema, dynamic outputFromJson(Map<String, dynamic> json)?, Iterable<Tool>? tools, Map<String, dynamic> input = const {}, Iterable<Message> messages = const [], Iterable<Part> attachments = const []}) Stream<AgentResponse>
Executes a given DotPrompt using the specified parameters and returns the response as a Stream of AgentResponse.