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

fix: [Go] added missing prompt metadata #439

Merged
merged 3 commits into from
Jun 20, 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
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 = ""
pavelgj marked this conversation as resolved.
Show resolved Hide resolved

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
Loading