# README
Swarm
An ergonomic, lightweight multi-agent orchestration in Go (inspired by openai/swarm).
Install
go get -u github.com/feiskyer/swarm-go
Examples
Basic
package main
import (
"fmt"
"reflect"
"github.com/feiskyer/swarm-go"
)
func main() {
// Create a new agent
agent := swarm.NewAgent("Assistant")
agent.WithModel("gpt-4o").
WithInstructions("You are a helpful assistant.")
// Example function that the agent can call
weatherFunc := swarm.NewAgentFunction(
"getWeather",
"Get the current weather for a given location. Requires a location parameter.",
func(args map[string]interface{}) (interface{}, error) {
location, ok := args["location"].(string)
if !ok {
return nil, fmt.Errorf("location not provided")
}
return fmt.Sprintf("The weather in %s is sunny", location), nil
},
[]swarm.Parameter{{Name: "location", Type: reflect.TypeOf("string")}},
)
// Add function to agent
agent.AddFunction(weatherFunc)
// Run the demo loop
swarm.RunDemoLoop(agent, nil, false, false)
}
Streaming
package main
import (
"fmt"
"reflect"
"github.com/feiskyer/swarm-go"
)
func main() {
// Create a new agent
agent := swarm.NewAgent("Assistant")
agent.WithModel("gpt-4o").
WithInstructions("You are a helpful assistant.")
// Example function that the agent can call
weatherFunc := func(args map[string]interface{}) (interface{}, error) {
location, ok := args["location"].(string)
if !ok {
return nil, fmt.Errorf("location not provided")
}
return fmt.Sprintf("The weather in %s is sunny", location), nil
}
// Add function to agent
agent.AddFunction(swarm.NewAgentFunction(
"getWeather",
"Get the current weather for a given location. Requires a location parameter.",
weatherFunc,
[]swarm.Parameter{{Name: "location", Type: reflect.TypeOf("string")}},
))
// Run the demo loop
swarm.RunDemoLoop(agent, nil, true, false)
}
Agent Handoff
package main
import (
"context"
"fmt"
"os"
"github.com/feiskyer/swarm-go"
)
func main() {
client := swarm.NewSwarm(swarm.NewOpenAIClient(os.Getenv("OPENAI_API_KEY")))
englishAgent := swarm.NewAgent("English Agent").WithInstructions("You only speak English.")
spanishAgent := swarm.NewAgent("Spanish Agent").WithInstructions("You only speak Spanish.")
transferToSpanishAgent := swarm.NewAgentFunction(
"transferToSpanishAgent",
"Transfer spanish speaking users immediately.",
func(args map[string]interface{}) (interface{}, error) {
return spanishAgent, nil
},
[]swarm.Parameter{},
)
englishAgent.AddFunction(transferToSpanishAgent)
messages := []map[string]interface{}{
{
"role": "user",
"content": "Hola. ¿Como estás?",
},
}
response, err := client.Run(context.TODO(), englishAgent, messages, nil, "gpt-4o", false, true, 10, true)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(response.Messages[len(response.Messages)-1]["content"])
}
Use Azure OpenAI
package main
import (
"context"
"fmt"
"os"
"github.com/feiskyer/swarm-go"
)
func main() {
azureApiKey := os.Getenv("AZURE_OPENAI_API_KEY")
azureApiBase := os.Getenv("AZURE_OPENAI_API_BASE")
client := swarm.NewSwarm(swarm.NewAzureOpenAIClient(azureApiKey, azureApiBase))
englishAgent := swarm.NewAgent("English Agent").WithInstructions("You only speak English.")
spanishAgent := swarm.NewAgent("Spanish Agent").WithInstructions("You only speak Spanish.")
transferToSpanishAgent := swarm.NewAgentFunction(
"transferToSpanishAgent",
"Transfer spanish speaking users immediately.",
func(args map[string]interface{}) (interface{}, error) {
return spanishAgent, nil
},
[]swarm.Parameter{},
)
englishAgent.AddFunction(transferToSpanishAgent)
messages := []map[string]interface{}{
{
"role": "user",
"content": "Hola. ¿Como estás?",
},
}
response, err := client.Run(context.TODO(), englishAgent, messages, nil, "gpt-4o", false, true, 10, true)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(response.Messages[len(response.Messages)-1]["content"])
}
Contribution
The project is opensourced at github feiskyer/swarm-go with MIT License.
If you would like to contribute to the project, please follow these guidelines:
- Fork the repository and clone it to your local machine.
- Create a new branch for your changes.
- Make your changes and commit them with a descriptive commit message.
- Push your changes to your forked repository.
- Open a pull request to the main repository.
# Packages
No description provided by the author
# Functions
DebugPrint prints debug information if debug is enabled.
FunctionToJSON converts a Go function to OpenAI function format.
LoadWorkflow loads a workflow from a YAML file.
MergeFields merges source fields into target map recursively.
NewAgent creates a new Agent with default values.
NewAgentFunction creates a new AgentFunction from a function and description.
NewAzureOpenAIClient creates a new OpenAI client wrapper for Azure.
NewDefaultSwarm creates a new Swarm instance with the default OpenAI client inferred from the environment variables.
NewOpenAIClient creates a new OpenAI client wrapper.
NewOpenAIClientWithBaseURL creates a new OpenAI client wrapper with a custom base URL.
NewSwarm creates a new Swarm instance with the provided OpenAI client.
RunDemoLoop starts an interactive CLI session.
# Constants
ContextVariablesName is the name of the key used to store context variables.
# Variables
ErrEmptyMessages indicates that the messages array is empty.
Common errors.
Common errors.
Common errors.
Common errors.
Common errors.
ErrInvalidToolCall indicates an invalid tool call.
# Structs
Agent represents an AI agent with its configuration and capabilities.
Function represents a function call, including its name and arguments.
Parameter represents a function parameter with its metadata.
Response encapsulates the complete response from an agent interaction.
Result encapsulates the return value from an agent function.
SimpleAgentFunction is a helper struct to create AgentFunction from a simple function.
StreamResponse represents a streaming response chunk.
Swarm orchestrates interactions between agents and OpenAI.
ToolCall represents a call to a specific tool/function.
Workflow represents a sequence of steps to be executed Workflow represents a workflow configuration.
WorkflowStep represents a single step in a workflow.
# Interfaces
AgentFunction represents a callable function that can be used by an agent.
OpenAIClient defines the interface for OpenAI API interactions.