This commit is contained in:
mudler
2024-03-31 17:20:06 +02:00
parent 8601956e53
commit aa62d9ef9e
6 changed files with 350 additions and 236 deletions

58
action/definition.go Normal file
View File

@@ -0,0 +1,58 @@
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() {
ac.cancelFunc()
}
func NewContext(ctx context.Context, cancel context.CancelFunc) *ActionContext {
return &ActionContext{
Context: ctx,
cancelFunc: cancel,
}
}
type ActionParams map[string]string
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)
}
//type ActionDefinition openai.FunctionDefinition
type ActionDefinition struct {
Properties map[string]jsonschema.Definition
Required []string
Name string
Description string
}
func (a ActionDefinition) ToFunctionDefinition() openai.FunctionDefinition {
return openai.FunctionDefinition{
Name: a.Name,
Description: a.Description,
Parameters: jsonschema.Definition{
Type: jsonschema.Object,
Properties: a.Properties,
Required: a.Required,
},
}
}

35
action/intention.go Normal file
View File

@@ -0,0 +1,35 @@
package action
import (
"github.com/sashabaranov/go-openai/jsonschema"
)
func NewIntention(s ...string) *IntentAction {
return &IntentAction{tools: s}
}
type IntentAction struct {
tools []string
}
func (a *IntentAction) Run(ActionParams) (string, error) {
return "no-op", nil
}
func (a *IntentAction) Definition() ActionDefinition {
return ActionDefinition{
Name: "intent",
Description: "detect user intent",
Properties: map[string]jsonschema.Definition{
"reasoning": {
Type: jsonschema.String,
Description: "The city and state, e.g. San Francisco, CA",
},
"tool": {
Type: jsonschema.String,
Enum: a.tools,
},
},
Required: []string{"tool", "reasoning"},
}
}