Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Go] add DefineAction #295

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go/ai/embedder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ type EmbedRequest struct {

// RegisterEmbedder registers the actions for a specific embedder.
func RegisterEmbedder(name string, embedder Embedder) {
core.RegisterAction(name, core.NewAction(name, core.ActionTypeEmbedder, nil, embedder.Embed))
core.DefineAction(name, core.ActionTypeEmbedder, nil, embedder.Embed)
}
6 changes: 2 additions & 4 deletions go/ai/retriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ func DefineRetriever(
index func(context.Context, *IndexerRequest) error,
retrieve func(context.Context, *RetrieverRequest) (*RetrieverResponse, error),
) Retriever {
ia := core.NewAction(name, core.ActionTypeIndexer, nil, func(ctx context.Context, req *IndexerRequest) (struct{}, error) {
ia := core.DefineAction(name, core.ActionTypeIndexer, nil, func(ctx context.Context, req *IndexerRequest) (struct{}, error) {
return struct{}{}, index(ctx, req)
})
core.RegisterAction(name, ia)
ra := core.NewAction(name, core.ActionTypeRetriever, nil, retrieve)
core.RegisterAction(name, ra)
ra := core.DefineAction(name, core.ActionTypeRetriever, nil, retrieve)
return &retriever{ia, ra}
}

Expand Down
3 changes: 1 addition & 2 deletions go/ai/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func RegisterTool(name string, definition *ToolDefinition, metadata map[string]a
}
metadata["type"] = "tool"

// TODO: There is no provider for a tool.
core.RegisterAction("tool", core.NewAction(definition.Name, core.ActionTypeTool, metadata, fn))
core.DefineAction(definition.Name, core.ActionTypeTool, metadata, fn)
}

// toolActionType is the instantiated core.Action type registered
Expand Down
13 changes: 12 additions & 1 deletion go/core/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,18 @@ type Action[In, Out, Stream any] struct {
Metadata map[string]any
}

// See js/common/src/types.ts
// See js/core/src/action.ts

// DefineAction creates a new Action and registers it.
func DefineAction[In, Out any](name string, atype ActionType, metadata map[string]any, fn func(context.Context, In) (Out, error)) *Action[In, Out, struct{}] {
jba marked this conversation as resolved.
Show resolved Hide resolved
return defineAction(globalRegistry, name, atype, metadata, fn)
}

func defineAction[In, Out any](r *registry, name string, atype ActionType, metadata map[string]any, fn func(context.Context, In) (Out, error)) *Action[In, Out, struct{}] {
a := NewAction(name, atype, metadata, fn)
r.registerAction(name, a)
return a
}

// NewAction creates a new Action with the given name and non-streaming function.
func NewAction[In, Out any](name string, atype ActionType, metadata map[string]any, fn func(context.Context, In) (Out, error)) *Action[In, Out, struct{}] {
Expand Down
Loading