Add Counter action to count things (#38)
* add host * fix: make user name explicit in IRC * feat: Add counter action
This commit is contained in:
committed by
GitHub
parent
3763f320b9
commit
7486e68a17
@@ -7,8 +7,8 @@ services:
|
|||||||
# Image list (dockerhub): https://hub.docker.com/r/localai/localai
|
# Image list (dockerhub): https://hub.docker.com/r/localai/localai
|
||||||
image: localai/localai:latest-cpu
|
image: localai/localai:latest-cpu
|
||||||
command:
|
command:
|
||||||
- rombo-org_rombo-llm-v3.0-qwen-32b # minimum suggested model
|
# - rombo-org_rombo-llm-v3.0-qwen-32b # minimum suggested model
|
||||||
#- marco-o1 (smaller)
|
- marco-o1 # (smaller)
|
||||||
- granite-embedding-107m-multilingual
|
- granite-embedding-107m-multilingual
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "curl", "-f", "http://localhost:8080/readyz"]
|
test: ["CMD", "curl", "-f", "http://localhost:8080/readyz"]
|
||||||
@@ -67,5 +67,7 @@ services:
|
|||||||
- LOCALAGENT_STATE_DIR=/pool
|
- LOCALAGENT_STATE_DIR=/pool
|
||||||
- LOCALAGENT_TIMEOUT=5m
|
- LOCALAGENT_TIMEOUT=5m
|
||||||
- LOCALAGENT_ENABLE_CONVERSATIONS_LOGGING=false
|
- LOCALAGENT_ENABLE_CONVERSATIONS_LOGGING=false
|
||||||
|
extra_hosts:
|
||||||
|
- "host.docker.internal:host-gateway"
|
||||||
volumes:
|
volumes:
|
||||||
- ./volumes/localagent/:/pool
|
- ./volumes/localagent/:/pool
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ const (
|
|||||||
ActionBrowse = "browse"
|
ActionBrowse = "browse"
|
||||||
ActionSendMail = "send_mail"
|
ActionSendMail = "send_mail"
|
||||||
ActionGenerateImage = "generate_image"
|
ActionGenerateImage = "generate_image"
|
||||||
|
ActionCounter = "counter"
|
||||||
)
|
)
|
||||||
|
|
||||||
var AvailableActions = []string{
|
var AvailableActions = []string{
|
||||||
@@ -39,6 +40,7 @@ var AvailableActions = []string{
|
|||||||
ActionWikipedia,
|
ActionWikipedia,
|
||||||
ActionSendMail,
|
ActionSendMail,
|
||||||
ActionGenerateImage,
|
ActionGenerateImage,
|
||||||
|
ActionCounter,
|
||||||
}
|
}
|
||||||
|
|
||||||
func Actions(a *state.AgentConfig) func(ctx context.Context) []agent.Action {
|
func Actions(a *state.AgentConfig) func(ctx context.Context) []agent.Action {
|
||||||
@@ -80,6 +82,8 @@ func Actions(a *state.AgentConfig) func(ctx context.Context) []agent.Action {
|
|||||||
allActions = append(allActions, actions.NewBrowse(config))
|
allActions = append(allActions, actions.NewBrowse(config))
|
||||||
case ActionSendMail:
|
case ActionSendMail:
|
||||||
allActions = append(allActions, actions.NewSendMail(config))
|
allActions = append(allActions, actions.NewSendMail(config))
|
||||||
|
case ActionCounter:
|
||||||
|
allActions = append(allActions, actions.NewCounter(config))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
95
services/actions/counter.go
Normal file
95
services/actions/counter.go
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/mudler/LocalAgent/core/action"
|
||||||
|
"github.com/sashabaranov/go-openai/jsonschema"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CounterAction manages named counters that can be created, updated, and queried
|
||||||
|
type CounterAction struct {
|
||||||
|
counters map[string]int
|
||||||
|
mutex sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCounter creates a new counter action
|
||||||
|
func NewCounter(config map[string]string) *CounterAction {
|
||||||
|
return &CounterAction{
|
||||||
|
counters: make(map[string]int),
|
||||||
|
mutex: sync.RWMutex{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run executes the counter action
|
||||||
|
func (a *CounterAction) Run(ctx context.Context, params action.ActionParams) (action.ActionResult, error) {
|
||||||
|
// Parse parameters
|
||||||
|
request := struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Adjustment int `json:"adjustment"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
if err := params.Unmarshal(&request); err != nil {
|
||||||
|
return action.ActionResult{}, fmt.Errorf("invalid parameters: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if request.Name == "" {
|
||||||
|
return action.ActionResult{}, fmt.Errorf("counter name cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
a.mutex.Lock()
|
||||||
|
defer a.mutex.Unlock()
|
||||||
|
|
||||||
|
// Get current value or initialize if it doesn't exist
|
||||||
|
currentValue, exists := a.counters[request.Name]
|
||||||
|
|
||||||
|
// Update the counter
|
||||||
|
newValue := currentValue + request.Adjustment
|
||||||
|
a.counters[request.Name] = newValue
|
||||||
|
|
||||||
|
// Prepare the response message
|
||||||
|
var message string
|
||||||
|
if !exists && request.Adjustment == 0 {
|
||||||
|
message = fmt.Sprintf("Created counter '%s' with initial value 0", request.Name)
|
||||||
|
} else if !exists {
|
||||||
|
message = fmt.Sprintf("Created counter '%s' with initial value %d", request.Name, newValue)
|
||||||
|
} else if request.Adjustment > 0 {
|
||||||
|
message = fmt.Sprintf("Increased counter '%s' by %d to %d", request.Name, request.Adjustment, newValue)
|
||||||
|
} else if request.Adjustment < 0 {
|
||||||
|
message = fmt.Sprintf("Decreased counter '%s' by %d to %d", request.Name, -request.Adjustment, newValue)
|
||||||
|
} else {
|
||||||
|
message = fmt.Sprintf("Current value of counter '%s' is %d", request.Name, newValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
return action.ActionResult{
|
||||||
|
Result: message,
|
||||||
|
Metadata: map[string]any{
|
||||||
|
"counter_name": request.Name,
|
||||||
|
"counter_value": newValue,
|
||||||
|
"adjustment": request.Adjustment,
|
||||||
|
"is_new": !exists,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Definition returns the action definition
|
||||||
|
func (a *CounterAction) Definition() action.ActionDefinition {
|
||||||
|
return action.ActionDefinition{
|
||||||
|
Name: "counter",
|
||||||
|
Description: "Create, update, or query named counters. Specify a name and an adjustment value (positive to increase, negative to decrease, zero to query).",
|
||||||
|
Properties: map[string]jsonschema.Definition{
|
||||||
|
"name": {
|
||||||
|
Type: jsonschema.String,
|
||||||
|
Description: "The name of the counter to create, update, or query.",
|
||||||
|
},
|
||||||
|
"adjustment": {
|
||||||
|
Type: jsonschema.Integer,
|
||||||
|
Description: "The value to adjust the counter by. Positive to increase, negative to decrease, zero to query the current value.",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Required: []string{"name", "adjustment"},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -101,7 +101,7 @@ func (i *IRC) Start(a *agent.Agent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
xlog.Info("Recv message", "message", message, "sender", sender, "channel", channel)
|
xlog.Info("Recv message", "message", message, "sender", sender, "channel", channel)
|
||||||
cleanedMessage := "I am " + sender + ". " + cleanUpMessage(message, i.nickname)
|
cleanedMessage := "My name is " + sender + ". " + cleanUpMessage(message, i.nickname)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
res := a.Ask(
|
res := a.Ask(
|
||||||
|
|||||||
Reference in New Issue
Block a user