From 230df46e156ec73dc4b2fdf3163a200f4b4c2b6d Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Thu, 20 Jun 2024 17:13:17 -0400 Subject: [PATCH 1/3] fix: [Go] added missing prompt metadata --- go/ai/prompt.go | 1 - go/plugins/dotprompt/dotprompt.go | 12 ++++++++---- go/plugins/dotprompt/genkit.go | 17 +++++++++++++++-- go/samples/coffee-shop/main.go | 10 +++++----- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/go/ai/prompt.go b/go/ai/prompt.go index 394932740..5fb25cfb8 100644 --- a/go/ai/prompt.go +++ b/go/ai/prompt.go @@ -38,7 +38,6 @@ func DefinePrompt(provider, name string, metadata map[string]any, render func(co mm = make(map[string]any) } mm["type"] = "prompt" - mm["prompt"] = true // required by genkit ui return core.DefineActionWithInputSchema(provider, name, atype.Prompt, mm, render, inputSchema) } diff --git a/go/plugins/dotprompt/dotprompt.go b/go/plugins/dotprompt/dotprompt.go index 457326342..f4faf4d33 100644 --- a/go/plugins/dotprompt/dotprompt.go +++ b/go/plugins/dotprompt/dotprompt.go @@ -66,6 +66,9 @@ type Prompt struct { // The template for the prompt. Template *raymond.Template + // The template for the prompt. + TemplateText string + // A hash of the prompt contents. hash string @@ -197,10 +200,11 @@ func newPrompt(name, templateText, hash string, config Config) (*Prompt, error) } template.RegisterHelpers(templateHelpers) return &Prompt{ - Name: name, - Config: config, - hash: hash, - Template: template, + Name: name, + Config: config, + hash: hash, + Template: template, + TemplateText: templateText, }, nil } diff --git a/go/plugins/dotprompt/genkit.go b/go/plugins/dotprompt/genkit.go index 814a1bbbe..d8004fe0e 100644 --- a/go/plugins/dotprompt/genkit.go +++ b/go/plugins/dotprompt/genkit.go @@ -52,6 +52,9 @@ func (p *Prompt) buildVariables(variables any) (map[string]any, error) { } v := reflect.Indirect(reflect.ValueOf(variables)) + if v.Kind() == reflect.Map { + return variables.(map[string]any), nil + } if v.Kind() != reflect.Struct { return nil, errors.New("dotprompt: fields not a struct or pointer to a struct") } @@ -99,6 +102,8 @@ fieldLoop: func (p *Prompt) buildRequest(ctx context.Context, input any) (*ai.GenerateRequest, error) { req := &ai.GenerateRequest{} + fmt.Println(" - - - - - -- - - - - -", input) + m, err := p.buildVariables(input) if err != nil { return nil, err @@ -137,8 +142,16 @@ func (p *Prompt) Register() error { if p.Variant != "" { name += "." + p.Variant } - - p.action = ai.DefinePrompt("dotprompt", name, nil, p.buildRequest, p.Config.InputSchema) + // TODO: Unwind this change once Monaco Editor supports newer than JSON schema draft-07. + p.InputSchema.Version = "" + p.action = ai.DefinePrompt("dotprompt", name, map[string]any{ + "prompt": map[string]any{ + "name": p.Name, + "input": map[string]any{"schema": p.InputSchema}, + "output": map[string]any{"format": p.OutputFormat}, + "template": p.TemplateText, + }, + }, p.buildRequest, p.Config.InputSchema) return nil } diff --git a/go/samples/coffee-shop/main.go b/go/samples/coffee-shop/main.go index 92c857395..9cfda041f 100755 --- a/go/samples/coffee-shop/main.go +++ b/go/samples/coffee-shop/main.go @@ -109,11 +109,15 @@ func main() { log.Fatal(err) } + r := &jsonschema.Reflector{ + AllowAdditionalProperties: false, + DoNotReference: true, + } g := googleai.Model("gemini-1.5-pro") simpleGreetingPrompt, err := dotprompt.Define("simpleGreeting", simpleGreetingPromptTemplate, dotprompt.Config{ ModelAction: g, - InputSchema: jsonschema.Reflect(simpleGreetingInput{}), + InputSchema: r.Reflect(simpleGreetingInput{}), OutputFormat: ai.OutputFormatText, }, ) @@ -176,10 +180,6 @@ func main() { return text, nil }) - r := &jsonschema.Reflector{ - AllowAdditionalProperties: false, - DoNotReference: true, - } schema := r.Reflect(simpleGreetingOutput{}) jsonBytes, err := schema.MarshalJSON() if err != nil { From 4831ec1b5b76d644eded586dc74759b1f5311acc Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Thu, 20 Jun 2024 17:14:37 -0400 Subject: [PATCH 2/3] remote print --- go/plugins/dotprompt/genkit.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/go/plugins/dotprompt/genkit.go b/go/plugins/dotprompt/genkit.go index d8004fe0e..4ef4ca1cd 100644 --- a/go/plugins/dotprompt/genkit.go +++ b/go/plugins/dotprompt/genkit.go @@ -102,8 +102,6 @@ fieldLoop: func (p *Prompt) buildRequest(ctx context.Context, input any) (*ai.GenerateRequest, error) { req := &ai.GenerateRequest{} - fmt.Println(" - - - - - -- - - - - -", input) - m, err := p.buildVariables(input) if err != nil { return nil, err From 8630af8f4cceb21d5e671396f65e95182af7bf48 Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Thu, 20 Jun 2024 18:41:56 -0400 Subject: [PATCH 3/3] feedback --- go/plugins/dotprompt/dotprompt.go | 4 ++-- go/plugins/dotprompt/genkit.go | 11 +++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/go/plugins/dotprompt/dotprompt.go b/go/plugins/dotprompt/dotprompt.go index f4faf4d33..8c1b5385f 100644 --- a/go/plugins/dotprompt/dotprompt.go +++ b/go/plugins/dotprompt/dotprompt.go @@ -63,10 +63,10 @@ type Prompt struct { Config - // The template for the prompt. + // The parsed prompt template. Template *raymond.Template - // The template for the prompt. + // The original prompt template text. TemplateText string // A hash of the prompt contents. diff --git a/go/plugins/dotprompt/genkit.go b/go/plugins/dotprompt/genkit.go index 4ef4ca1cd..6d20c67c4 100644 --- a/go/plugins/dotprompt/genkit.go +++ b/go/plugins/dotprompt/genkit.go @@ -56,7 +56,7 @@ func (p *Prompt) buildVariables(variables any) (map[string]any, error) { return variables.(map[string]any), nil } if v.Kind() != reflect.Struct { - return nil, errors.New("dotprompt: fields not a struct or pointer to a struct") + return nil, errors.New("dotprompt: fields not a struct or pointer to a struct or a map") } vt := v.Type() @@ -140,16 +140,19 @@ func (p *Prompt) Register() error { if p.Variant != "" { name += "." + p.Variant } - // TODO: Unwind this change once Monaco Editor supports newer than JSON schema draft-07. + + // TODO: Undo clearing of the Version once Monaco Editor supports newer than JSON schema draft-07. p.InputSchema.Version = "" - p.action = ai.DefinePrompt("dotprompt", name, map[string]any{ + + metadata := map[string]any{ "prompt": map[string]any{ "name": p.Name, "input": map[string]any{"schema": p.InputSchema}, "output": map[string]any{"format": p.OutputFormat}, "template": p.TemplateText, }, - }, p.buildRequest, p.Config.InputSchema) + } + p.action = ai.DefinePrompt("dotprompt", name, metadata, p.buildRequest, p.Config.InputSchema) return nil }