Refactor entire system to be more contextual so that conversation flow can be more easily managed
This commit is contained in:
18
function.go
18
function.go
@@ -31,7 +31,7 @@ type Function struct {
|
||||
definition *jsonschema.Definition
|
||||
}
|
||||
|
||||
func (f *Function) Execute(ctx context.Context, input string) (string, error) {
|
||||
func (f *Function) Execute(ctx *Context, input string) (string, error) {
|
||||
if !f.fn.IsValid() {
|
||||
return "", fmt.Errorf("function %s is not implemented", f.Name)
|
||||
}
|
||||
@@ -46,7 +46,7 @@ func (f *Function) Execute(ctx context.Context, input string) (string, error) {
|
||||
}
|
||||
|
||||
// now we can call the function
|
||||
exec := func(ctx context.Context) (string, error) {
|
||||
exec := func(ctx *Context) (string, error) {
|
||||
out := f.fn.Call([]reflect.Value{reflect.ValueOf(ctx), p.Elem()})
|
||||
|
||||
if len(out) != 2 {
|
||||
@@ -62,7 +62,7 @@ func (f *Function) Execute(ctx context.Context, input string) (string, error) {
|
||||
|
||||
var cancel context.CancelFunc
|
||||
if f.Timeout > 0 {
|
||||
ctx, cancel = context.WithTimeout(ctx, f.Timeout)
|
||||
ctx, cancel = ctx.WithTimeout(f.Timeout)
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
@@ -90,3 +90,15 @@ type FunctionCall struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Arguments string `json:"arguments,omitempty"`
|
||||
}
|
||||
|
||||
func (fc *FunctionCall) toRaw() map[string]any {
|
||||
res := map[string]interface{}{
|
||||
"name": fc.Name,
|
||||
}
|
||||
|
||||
if fc.Arguments != "" {
|
||||
res["arguments"] = fc.Arguments
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
Reference in New Issue
Block a user