Skip to content

[Go] When using the dotprompt plugin with a ModelName config, it will panic #932

@dhiaayachi

Description

@dhiaayachi

Describe the bug
When creating a prompt with a ModelName Config, the provided model name is not considered and it will panic. This is due to a bug when looking up the model in:

model := p.Model
if model == nil {
modelName := p.ModelName
if pr.Model != "" {
modelName = pr.Model
}
if modelName == "" {
return nil, errors.New("dotprompt execution: model not specified")
}
provider, name, found := strings.Cut(modelName, "/")
if !found {
return nil, errors.New("dotprompt model not in provider/name format")
}
model := ai.LookupModel(provider, name)
if model == nil {
return nil, fmt.Errorf("no model named %q for provider %q", name, provider)
}
}

The model is shadowed inside the if statement and result to the lookedup model going out of context after the if statement.

To Reproduce

prompt, _ := dotprompt.Define("menu", `Suggest an item for the menu of a {{ type }} themed restaurant`, dotprompt.Config{ModelName: "googleai/gemini-1.5-flash", InputSchema: r.Reflect(Input{}), OutputSchema: os, OutputFormat: ai.OutputFormatJSON})

resp, _ := prompt.Generate(ctx,
			&dotprompt.PromptRequest{
				Variables: input,
				Config:    &ai.GenerationCommonConfig{Temperature: 1},
			},
			nil,
		)

Expected behavior

When a model name is provided, either use the provided model name or return an error if the model lookup fails.

Runtime (please complete the following information):
Not relevant

** Go version
Not relevant

Additional context
The panic will happen in line:

resp, err := model.Generate(ctx, genReq, cb)

This is similar to #847 but not quite the same issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggo

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions