Skip to content

AI Support#174

Open
josejuanmontiel wants to merge 21 commits intosyumai:mainfrom
josejuanmontiel:feat/ai_only
Open

AI Support#174
josejuanmontiel wants to merge 21 commits intosyumai:mainfrom
josejuanmontiel:feat/ai_only

Conversation

@josejuanmontiel
Copy link

What

Add initial support for all AI stuff in workers

Motivation

It was a no-implemented feature, as an example of implementation take kv and create an initial aproach.

@josejuanmontiel
Copy link
Author

Take this part of the conversation: "Also, instead of creating a separate mock package, please put both implementations in the same directory (e.g., foo.go and foo_js.go)" in this PR.

Copy link
Owner

@syumai syumai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for PR! Please check comments.

@@ -0,0 +1,57 @@
# worker-template-go
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refer to other examples and update your README 🙏

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


go 1.22.2

require github.com/syumai/workers v0.28.1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
require github.com/syumai/workers v0.28.1
require github.com/syumai/workers v0.0.0

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"strings"

"github.com/syumai/workers"
ai "github.com/syumai/workers/cloudflare/ai"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ai "github.com/syumai/workers/cloudflare/ai"
"github.com/syumai/workers/cloudflare/ai"

// initialize AI namespace instance
aiCaller, err := ai.NewNamespace("AI")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to init KV: %v", err)
Copy link
Owner

@syumai syumai Apr 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fmt.Fprintf(os.Stderr, "failed to init KV: %v", err)
fmt.Fprintf(os.Stderr, "failed to init AI instance: %v", err)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

aiCaller, err := ai.NewNamespace("AI")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to init KV: %v", err)
os.Exit(1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think os.Exit(1) doesn't have the desired effect. The existing KV example was wrong. Sorry 🙏

Suggested change
os.Exit(1)
return

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// - variable name must be defined in wrangler.toml as kv_namespace's binding.
// - if the given variable name doesn't exist on runtime context, returns error.
// - This function panics when a runtime context is not found.
func NewNamespace(varName string) (*AI, error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the type name of instance is AI and the package name is same: ai, this function should be named just New.
See: https://go.dev/doc/effective_go#:~:text=Similarly%2C%20the%20function,ring.New.

Similarly, the function to make new instances of ring.Ring—which is the definition of a constructor in Go—would normally be called NewRing, but since Ring is the only type exported by the package, and since the package is called ring, it's called just New, which clients of the package see as ring.New.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return &AI{instance: inst}, nil
}

func (ns *AI) WaitUntil(task func()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method seems not used in example. Is this necessary?

}

func (ns *AI) Run(key string, opts map[string]interface{}) (string, error) {
p := ns.instance.Call("run", key, mapToJS(opts, "text"))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a link to the documentation for the text option.

// GetReader gets stream value by the specified key.
// - if a network error happens, returns error.
func (ns *AI) RunReader(key string, opts map[string]interface{}) (io.Reader, error) {
p := ns.instance.Call("run", key, mapToJS(opts, "stream"))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a link to the documentation for the stream option.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add a few new examples... and in the last /ai-image-to-image that use returned this... "data: {"0":137,"1":80,"2":78,"3":71,"4":13,"5":10,"6":26,"7":10,"8":0,"9":0,"10":0,"11":13,"12":73,"13":72..." using this https://developers.cloudflare.com/workers-ai/models/stable-diffusion-v1-5-img2img/ and stream option... i leave comented a code (that doesnt work fully) to download the "stream"... i'm still learning how works all of this.. seeing this doc .. can you point where to find (and how to "solve") the stream option... i try to find something similiar in other place of the library .. .but no sure...

})))
}

func mapToJS(opts map[string]interface{}, type_ string) js.Value {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use any instead of interface{}. (The same applies to other places.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

*/
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "workers-ia",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"name": "workers-ia",
"name": "workers-ai-example",

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

func New(varName string) (*AI, error) {
fmt.Println("NewNamespace called with varName:", varName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using fmt.Println makes this a verbose logging that cannot be turned off - maybe consider using slog since it is in a standard library? :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i put for my debug... i remove as in other part of the library...

// }
// reader := strings.NewReader(string(aiResultStr))

// // Leer todo como string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this commented code a valuable snippet? If so, can you write the comments in English?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! (as comented in discord) i leave this part as "incomplete" example of how to decode the return from this ai worker from cloudfare ... waiting to @syumai to help to discover how to treat "this sreaming response" to transleate a real image.... take a look at this part ... https://github.com/syumai/workers/pull/174/files/2ea1211dc55274f32cd979b860c753aa9cfcc8bf#r2054969323 where i comment the result and want help for how to decode this response to a real image...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update the comments for wait help :)

@@ -0,0 +1,14 @@
{
"name": "workers-ia",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this @syumai comment is still valid

.PHONY: build
build:
go run ../../cmd/workers-assets-gen
tinygo build -o ./build/app.wasm -target wasm -no-debug ./...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All other examples are using "big Go" by default now 🙂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

about this... apart from the differents in the compilation less space (dont know if big go support more things than tynygo... suppouse not) i was playing with adding in package json a "prebuild": "bash scripts/setup-tinygo.sh", that download the latest version of tinygo (it can be done in cloudfare) and execute later with "$(pwd)/_tinygo/bin/tinygo build -o ./build/app.wasm -target wasm -no-debug ./..." i used "_tinygo" as folder to download to skip this folder from go compilation :)

Copy link
Author

@josejuanmontiel josejuanmontiel May 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change name... but seems in all the examples Makefile use tinygo i'll leave the same... if you dont mind

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After #175 has been merged, all examples are in Go 🙂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#175 is merged.

So I hope that this PR is picked up again :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants