Conversation
|
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. |
syumai
left a comment
There was a problem hiding this comment.
Thank you for PR! Please check comments.
_examples/ai/README.md
Outdated
| @@ -0,0 +1,57 @@ | |||
| # worker-template-go | |||
There was a problem hiding this comment.
Please refer to other examples and update your README 🙏
_examples/ai/go.mod
Outdated
|
|
||
| go 1.22.2 | ||
|
|
||
| require github.com/syumai/workers v0.28.1 |
There was a problem hiding this comment.
| require github.com/syumai/workers v0.28.1 | |
| require github.com/syumai/workers v0.0.0 |
_examples/ai/main.go
Outdated
| "strings" | ||
|
|
||
| "github.com/syumai/workers" | ||
| ai "github.com/syumai/workers/cloudflare/ai" |
There was a problem hiding this comment.
| ai "github.com/syumai/workers/cloudflare/ai" | |
| "github.com/syumai/workers/cloudflare/ai" |
_examples/ai/main.go
Outdated
| // initialize AI namespace instance | ||
| aiCaller, err := ai.NewNamespace("AI") | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "failed to init KV: %v", err) |
There was a problem hiding this comment.
| fmt.Fprintf(os.Stderr, "failed to init KV: %v", err) | |
| fmt.Fprintf(os.Stderr, "failed to init AI instance: %v", err) |
_examples/ai/main.go
Outdated
| aiCaller, err := ai.NewNamespace("AI") | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "failed to init KV: %v", err) | ||
| os.Exit(1) |
There was a problem hiding this comment.
I think os.Exit(1) doesn't have the desired effect. The existing KV example was wrong. Sorry 🙏
| os.Exit(1) | |
| return |
cloudflare/ai/call_js.go
Outdated
| // - 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) { |
There was a problem hiding this comment.
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.
cloudflare/ai/call_js.go
Outdated
| return &AI{instance: inst}, nil | ||
| } | ||
|
|
||
| func (ns *AI) WaitUntil(task func()) { |
There was a problem hiding this comment.
This method seems not used in example. Is this necessary?
cloudflare/ai/call_js.go
Outdated
| } | ||
|
|
||
| func (ns *AI) Run(key string, opts map[string]interface{}) (string, error) { | ||
| p := ns.instance.Call("run", key, mapToJS(opts, "text")) |
There was a problem hiding this comment.
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")) |
There was a problem hiding this comment.
Please add a link to the documentation for the stream option.
There was a problem hiding this comment.
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...
cloudflare/ai/call_js.go
Outdated
| }))) | ||
| } | ||
|
|
||
| func mapToJS(opts map[string]interface{}, type_ string) js.Value { |
There was a problem hiding this comment.
Please use any instead of interface{}. (The same applies to other places.)
_examples/ai/wrangler.jsonc
Outdated
| */ | ||
| { | ||
| "$schema": "node_modules/wrangler/config-schema.json", | ||
| "name": "workers-ia", |
There was a problem hiding this comment.
| "name": "workers-ia", | |
| "name": "workers-ai-example", |
cloudflare/ai/call.go
Outdated
| } | ||
|
|
||
| func New(varName string) (*AI, error) { | ||
| fmt.Println("NewNamespace called with varName:", varName) |
There was a problem hiding this comment.
using fmt.Println makes this a verbose logging that cannot be turned off - maybe consider using slog since it is in a standard library? :)
There was a problem hiding this comment.
i put for my debug... i remove as in other part of the library...
_examples/ai/main.go
Outdated
| // } | ||
| // reader := strings.NewReader(string(aiResultStr)) | ||
|
|
||
| // // Leer todo como string |
There was a problem hiding this comment.
Is this commented code a valuable snippet? If so, can you write the comments in English?
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
update the comments for wait help :)
_examples/ai/package.json
Outdated
| @@ -0,0 +1,14 @@ | |||
| { | |||
| "name": "workers-ia", | |||
_examples/ai/Makefile
Outdated
| .PHONY: build | ||
| build: | ||
| go run ../../cmd/workers-assets-gen | ||
| tinygo build -o ./build/app.wasm -target wasm -no-debug ./... |
There was a problem hiding this comment.
All other examples are using "big Go" by default now 🙂
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
change name... but seems in all the examples Makefile use tinygo i'll leave the same... if you dont mind
There was a problem hiding this comment.
After #175 has been merged, all examples are in Go 🙂
There was a problem hiding this comment.
#175 is merged.
So I hope that this PR is picked up again :)
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.