Skip to content

Commit

Permalink
fix: [Go] added missing prompt metadata (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelgj authored Jun 20, 2024
1 parent a02ff2f commit 7e0d2bb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
1 change: 0 additions & 1 deletion go/ai/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
14 changes: 9 additions & 5 deletions go/plugins/dotprompt/dotprompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ type Prompt struct {

Config

// The template for the prompt.
// The parsed prompt template.
Template *raymond.Template

// The original prompt template text.
TemplateText string

// A hash of the prompt contents.
hash string

Expand Down Expand Up @@ -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
}

Expand Down
18 changes: 16 additions & 2 deletions go/plugins/dotprompt/genkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ 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")
return nil, errors.New("dotprompt: fields not a struct or pointer to a struct or a map")
}
vt := v.Type()

Expand Down Expand Up @@ -138,7 +141,18 @@ func (p *Prompt) Register() error {
name += "." + p.Variant
}

p.action = ai.DefinePrompt("dotprompt", name, nil, p.buildRequest, p.Config.InputSchema)
// TODO: Undo clearing of the Version once Monaco Editor supports newer than JSON schema draft-07.
p.InputSchema.Version = ""

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.action = ai.DefinePrompt("dotprompt", name, metadata, p.buildRequest, p.Config.InputSchema)

return nil
}
Expand Down
10 changes: 5 additions & 5 deletions go/samples/coffee-shop/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 7e0d2bb

Please sign in to comment.