Finish moving types
This commit is contained in:
committed by
Ettore Di Giacinto
parent
f0b8bfb4f4
commit
75a8d63e83
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/mudler/LocalAgent/core/types"
|
||||
"github.com/mudler/LocalAgent/pkg/xlog"
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
"github.com/traefik/yaegi/interp"
|
||||
@@ -79,24 +80,24 @@ func (a *CustomAction) Plannable() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (a *CustomAction) Run(ctx context.Context, params ActionParams) (ActionResult, error) {
|
||||
func (a *CustomAction) Run(ctx context.Context, params types.ActionParams) (types.ActionResult, error) {
|
||||
v, err := a.i.Eval(fmt.Sprintf("%s.Run", a.config["name"]))
|
||||
if err != nil {
|
||||
return ActionResult{}, err
|
||||
return types.ActionResult{}, err
|
||||
}
|
||||
|
||||
run := v.Interface().(func(map[string]interface{}) (string, map[string]interface{}, error))
|
||||
|
||||
res, meta, err := run(params)
|
||||
return ActionResult{Result: res, Metadata: meta}, err
|
||||
return types.ActionResult{Result: res, Metadata: meta}, err
|
||||
}
|
||||
|
||||
func (a *CustomAction) Definition() ActionDefinition {
|
||||
func (a *CustomAction) Definition() types.ActionDefinition {
|
||||
|
||||
v, err := a.i.Eval(fmt.Sprintf("%s.Definition", a.config["name"]))
|
||||
if err != nil {
|
||||
xlog.Error("Error getting custom action definition", "error", err)
|
||||
return ActionDefinition{}
|
||||
return types.ActionDefinition{}
|
||||
}
|
||||
|
||||
properties := v.Interface().(func() map[string][]string)
|
||||
@@ -104,7 +105,7 @@ func (a *CustomAction) Definition() ActionDefinition {
|
||||
v, err = a.i.Eval(fmt.Sprintf("%s.RequiredFields", a.config["name"]))
|
||||
if err != nil {
|
||||
xlog.Error("Error getting custom action definition", "error", err)
|
||||
return ActionDefinition{}
|
||||
return types.ActionDefinition{}
|
||||
}
|
||||
|
||||
requiredFields := v.Interface().(func() []string)
|
||||
@@ -121,8 +122,8 @@ func (a *CustomAction) Definition() ActionDefinition {
|
||||
Description: v[1],
|
||||
}
|
||||
}
|
||||
return ActionDefinition{
|
||||
Name: ActionDefinitionName(a.config["name"]),
|
||||
return types.ActionDefinition{
|
||||
Name: types.ActionDefinitionName(a.config["name"]),
|
||||
Description: a.config["description"],
|
||||
Properties: prop,
|
||||
Required: requiredFields(),
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
. "github.com/mudler/LocalAgent/core/action"
|
||||
"github.com/mudler/LocalAgent/core/types"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
@@ -63,7 +64,7 @@ return []string{"foo"}
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
definition := customAction.Definition()
|
||||
Expect(definition).To(Equal(ActionDefinition{
|
||||
Expect(definition).To(Equal(types.ActionDefinition{
|
||||
Properties: map[string]jsonschema.Definition{
|
||||
"foo": {
|
||||
Type: jsonschema.String,
|
||||
@@ -75,7 +76,7 @@ return []string{"foo"}
|
||||
Description: "A test action",
|
||||
}))
|
||||
|
||||
runResult, err := customAction.Run(context.Background(), ActionParams{
|
||||
runResult, err := customAction.Run(context.Background(), types.ActionParams{
|
||||
"Foo": "bar",
|
||||
})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
package action
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/sashabaranov/go-openai"
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
)
|
||||
|
||||
type ActionContext struct {
|
||||
context.Context
|
||||
cancelFunc context.CancelFunc
|
||||
}
|
||||
|
||||
func (ac *ActionContext) Cancel() {
|
||||
if ac.cancelFunc != nil {
|
||||
ac.cancelFunc()
|
||||
}
|
||||
}
|
||||
|
||||
func NewContext(ctx context.Context, cancel context.CancelFunc) *ActionContext {
|
||||
return &ActionContext{
|
||||
Context: ctx,
|
||||
cancelFunc: cancel,
|
||||
}
|
||||
}
|
||||
|
||||
type ActionParams map[string]interface{}
|
||||
|
||||
type ActionResult struct {
|
||||
Result string
|
||||
Metadata map[string]interface{}
|
||||
}
|
||||
|
||||
func (ap ActionParams) Read(s string) error {
|
||||
err := json.Unmarshal([]byte(s), &ap)
|
||||
return err
|
||||
}
|
||||
|
||||
func (ap ActionParams) String() string {
|
||||
b, _ := json.Marshal(ap)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func (ap ActionParams) Unmarshal(v interface{}) error {
|
||||
b, err := json.Marshal(ap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := json.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//type ActionDefinition openai.FunctionDefinition
|
||||
|
||||
type ActionDefinition struct {
|
||||
Properties map[string]jsonschema.Definition
|
||||
Required []string
|
||||
Name ActionDefinitionName
|
||||
Description string
|
||||
}
|
||||
|
||||
type ActionDefinitionName string
|
||||
|
||||
func (a ActionDefinitionName) Is(name string) bool {
|
||||
return string(a) == name
|
||||
}
|
||||
|
||||
func (a ActionDefinitionName) String() string {
|
||||
return string(a)
|
||||
}
|
||||
|
||||
func (a ActionDefinition) ToFunctionDefinition() openai.FunctionDefinition {
|
||||
return openai.FunctionDefinition{
|
||||
Name: a.Name.String(),
|
||||
Description: a.Description,
|
||||
Parameters: jsonschema.Definition{
|
||||
Type: jsonschema.Object,
|
||||
Properties: a.Properties,
|
||||
Required: a.Required,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package action
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/mudler/LocalAgent/core/types"
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
)
|
||||
|
||||
@@ -21,16 +22,16 @@ type IntentResponse struct {
|
||||
Reasoning string `json:"reasoning"`
|
||||
}
|
||||
|
||||
func (a *IntentAction) Run(context.Context, ActionParams) (ActionResult, error) {
|
||||
return ActionResult{}, nil
|
||||
func (a *IntentAction) Run(context.Context, types.ActionParams) (types.ActionResult, error) {
|
||||
return types.ActionResult{}, nil
|
||||
}
|
||||
|
||||
func (a *IntentAction) Plannable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (a *IntentAction) Definition() ActionDefinition {
|
||||
return ActionDefinition{
|
||||
func (a *IntentAction) Definition() types.ActionDefinition {
|
||||
return types.ActionDefinition{
|
||||
Name: "pick_tool",
|
||||
Description: "Pick a tool",
|
||||
Properties: map[string]jsonschema.Definition{
|
||||
|
||||
@@ -3,6 +3,7 @@ package action
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/mudler/LocalAgent/core/types"
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
)
|
||||
|
||||
@@ -18,16 +19,16 @@ type ConversationActionResponse struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func (a *ConversationAction) Run(context.Context, ActionParams) (ActionResult, error) {
|
||||
return ActionResult{}, nil
|
||||
func (a *ConversationAction) Run(context.Context, types.ActionParams) (types.ActionResult, error) {
|
||||
return types.ActionResult{}, nil
|
||||
}
|
||||
|
||||
func (a *ConversationAction) Plannable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (a *ConversationAction) Definition() ActionDefinition {
|
||||
return ActionDefinition{
|
||||
func (a *ConversationAction) Definition() types.ActionDefinition {
|
||||
return types.ActionDefinition{
|
||||
Name: ConversationActionName,
|
||||
Description: "Use this tool to initiate a new conversation or to notify something.",
|
||||
Properties: map[string]jsonschema.Definition{
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package action
|
||||
|
||||
import "context"
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/mudler/LocalAgent/core/types"
|
||||
)
|
||||
|
||||
// StopActionName is the name of the action
|
||||
// used by the LLM to stop any further action
|
||||
@@ -12,16 +16,16 @@ func NewStop() *StopAction {
|
||||
|
||||
type StopAction struct{}
|
||||
|
||||
func (a *StopAction) Run(context.Context, ActionParams) (ActionResult, error) {
|
||||
return ActionResult{}, nil
|
||||
func (a *StopAction) Run(context.Context, types.ActionParams) (types.ActionResult, error) {
|
||||
return types.ActionResult{}, nil
|
||||
}
|
||||
|
||||
func (a *StopAction) Plannable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (a *StopAction) Definition() ActionDefinition {
|
||||
return ActionDefinition{
|
||||
func (a *StopAction) Definition() types.ActionDefinition {
|
||||
return types.ActionDefinition{
|
||||
Name: StopActionName,
|
||||
Description: "Use this tool to stop any further action and stop the conversation. You must use this when it looks like there is a conclusion to the conversation or the topic diverged too much from the original conversation. For instance if the user offer his help and you already replied with a message, you can use this tool to stop the conversation.",
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package action
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/mudler/LocalAgent/core/types"
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
)
|
||||
|
||||
@@ -29,16 +30,16 @@ type PlanSubtask struct {
|
||||
Reasoning string `json:"reasoning"`
|
||||
}
|
||||
|
||||
func (a *PlanAction) Run(context.Context, ActionParams) (ActionResult, error) {
|
||||
return ActionResult{}, nil
|
||||
func (a *PlanAction) Run(context.Context, types.ActionParams) (types.ActionResult, error) {
|
||||
return types.ActionResult{}, nil
|
||||
}
|
||||
|
||||
func (a *PlanAction) Plannable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (a *PlanAction) Definition() ActionDefinition {
|
||||
return ActionDefinition{
|
||||
func (a *PlanAction) Definition() types.ActionDefinition {
|
||||
return types.ActionDefinition{
|
||||
Name: PlanActionName,
|
||||
Description: "The assistant for solving complex tasks that involves calling more functions in sequence, replies with the action.",
|
||||
Properties: map[string]jsonschema.Definition{
|
||||
|
||||
@@ -3,6 +3,7 @@ package action
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/mudler/LocalAgent/core/types"
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
)
|
||||
|
||||
@@ -19,16 +20,16 @@ type ReasoningResponse struct {
|
||||
Reasoning string `json:"reasoning"`
|
||||
}
|
||||
|
||||
func (a *ReasoningAction) Run(context.Context, ActionParams) (ActionResult, error) {
|
||||
return ActionResult{}, nil
|
||||
func (a *ReasoningAction) Run(context.Context, types.ActionParams) (types.ActionResult, error) {
|
||||
return types.ActionResult{}, nil
|
||||
}
|
||||
|
||||
func (a *ReasoningAction) Plannable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (a *ReasoningAction) Definition() ActionDefinition {
|
||||
return ActionDefinition{
|
||||
func (a *ReasoningAction) Definition() types.ActionDefinition {
|
||||
return types.ActionDefinition{
|
||||
Name: "pick_action",
|
||||
Description: "try to understand what's the best thing to do and pick an action with a reasoning",
|
||||
Properties: map[string]jsonschema.Definition{
|
||||
|
||||
@@ -3,6 +3,7 @@ package action
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/mudler/LocalAgent/core/types"
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
)
|
||||
|
||||
@@ -21,7 +22,7 @@ type ReplyResponse struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func (a *ReplyAction) Run(context.Context, ActionParams) (string, error) {
|
||||
func (a *ReplyAction) Run(context.Context, types.ActionParams) (string, error) {
|
||||
return "no-op", nil
|
||||
}
|
||||
|
||||
@@ -29,8 +30,8 @@ func (a *ReplyAction) Plannable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (a *ReplyAction) Definition() ActionDefinition {
|
||||
return ActionDefinition{
|
||||
func (a *ReplyAction) Definition() types.ActionDefinition {
|
||||
return types.ActionDefinition{
|
||||
Name: ReplyActionName,
|
||||
Description: "Use this tool to reply to the user once we have all the informations we need.",
|
||||
Properties: map[string]jsonschema.Definition{
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/mudler/LocalAgent/core/types"
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
)
|
||||
|
||||
@@ -33,16 +34,16 @@ type AgentInternalState struct {
|
||||
Goal string `json:"goal"`
|
||||
}
|
||||
|
||||
func (a *StateAction) Run(context.Context, ActionParams) (ActionResult, error) {
|
||||
return ActionResult{Result: "internal state has been updated"}, nil
|
||||
func (a *StateAction) Run(context.Context, types.ActionParams) (types.ActionResult, error) {
|
||||
return types.ActionResult{Result: "internal state has been updated"}, nil
|
||||
}
|
||||
|
||||
func (a *StateAction) Plannable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (a *StateAction) Definition() ActionDefinition {
|
||||
return ActionDefinition{
|
||||
func (a *StateAction) Definition() types.ActionDefinition {
|
||||
return types.ActionDefinition{
|
||||
Name: StateActionName,
|
||||
Description: "update the agent state (short memory) with the current state of the conversation.",
|
||||
Properties: map[string]jsonschema.Definition{
|
||||
|
||||
Reference in New Issue
Block a user