feat(api): add endpoint to create group of dedicated agents (#79)

Signed-off-by: mudler <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto
2025-03-22 18:44:22 +01:00
committed by GitHub
parent d689bb4331
commit c1ac7b675a
7 changed files with 136 additions and 57 deletions

View File

@@ -9,9 +9,11 @@ import (
"strings"
"time"
"github.com/mudler/LocalAgent/pkg/llm"
"github.com/mudler/LocalAgent/pkg/xlog"
"github.com/mudler/LocalAgent/services"
"github.com/mudler/LocalAgent/webui/types"
"github.com/sashabaranov/go-openai/jsonschema"
"github.com/mudler/LocalAgent/core/action"
"github.com/mudler/LocalAgent/core/agent"
@@ -396,3 +398,71 @@ func (a *App) Responses(pool *state.AgentPool) func(c *fiber.Ctx) error {
return c.JSON(response)
}
}
type AgentRole struct {
Name string `json:"name"`
Description string `json:"description"`
SystemPrompt string `json:"system_prompt"`
}
func (a *App) CreateGroup(pool *state.AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
var request struct {
Descript string `json:"description"`
}
if err := c.BodyParser(&request); err != nil {
return errorJSONMessage(c, err.Error())
}
var results struct {
Agents []AgentRole `json:"agents"`
}
xlog.Debug("Generating group", "description", request.Descript)
client := llm.NewClient(a.config.LLMAPIKey, a.config.LLMAPIURL, "10m")
err := llm.GenerateTypedJSON(c.Context(), client, request.Descript, a.config.LLMModel, jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"agents": {
Type: jsonschema.Array,
Items: &jsonschema.Definition{
Type: jsonschema.Object,
Required: []string{"name", "description", "system_prompt"},
Properties: map[string]jsonschema.Definition{
"name": {
Type: jsonschema.String,
Description: "The name of the agent",
},
"description": {
Type: jsonschema.String,
Description: "The description of the agent",
},
"system_prompt": {
Type: jsonschema.String,
Description: "The system prompt for the agent",
},
},
},
},
},
}, &results)
if err != nil {
return errorJSONMessage(c, err.Error())
}
for _, agent := range results.Agents {
xlog.Info("Creating agent", "name", agent.Name, "description", agent.Description)
config := state.AgentConfig{
Name: agent.Name,
Description: agent.Description,
SystemPrompt: agent.SystemPrompt,
}
if err := pool.CreateAgent(agent.Name, &config); err != nil {
return errorJSONMessage(c, err.Error())
}
}
return c.JSON(results)
}
}

View File

@@ -6,6 +6,9 @@ type Config struct {
DefaultChunkSize int
Pool *state.AgentPool
ApiKeys []string
LLMAPIURL string
LLMAPIKey string
LLMModel string
}
type Option func(*Config)
@@ -16,6 +19,24 @@ func WithDefaultChunkSize(size int) Option {
}
}
func WithLLMModel(model string) Option {
return func(c *Config) {
c.LLMModel = model
}
}
func WithLLMAPIUrl(url string) Option {
return func(c *Config) {
c.LLMAPIURL = url
}
}
func WithLLMAPIKey(key string) Option {
return func(c *Config) {
c.LLMAPIKey = key
}
}
func WithPool(pool *state.AgentPool) Option {
return func(c *Config) {
c.Pool = pool

View File

@@ -141,6 +141,8 @@ func (app *App) registerRoutes(pool *state.AgentPool, webapp *fiber.App) {
webapp.Post("/action/:name/run", app.ExecuteAction(pool))
webapp.Get("/actions", app.ListActions())
webapp.Post("/api/agent/group/create", app.CreateGroup(pool))
webapp.Post("/settings/import", app.ImportAgent(pool))
webapp.Get("/settings/export/:name", app.ExportAgent(pool))
}