Skip to content

breaking(go): refactored prompts API #1491 #1980

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

Merged
merged 9 commits into from
Mar 5, 2025
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
20 changes: 20 additions & 0 deletions go/ai/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ func LookupModel(r *registry.Registry, provider, name string) Model {
return (*modelActionDef)(action)
}

// LookupModelByName looks up a [Model] registered by [DefineModel].
// It returns an error if the model was not defined.
func LookupModelByName(r *registry.Registry, modelName string) (Model, error) {
if modelName == "" {
return nil, errors.New("generate.LookupModelByName: model not specified")
}

parts := strings.Split(modelName, "/")
if len(parts) != 2 {
return nil, errors.New("generate.LookupModelByName: prompt model not in provider/name format")
}

model := LookupModel(r, parts[0], parts[1])
if model == nil {
return nil, fmt.Errorf("generate.LookupModelByName: no model named %q for provider %q", parts[1], parts[0])
}

return model, nil
}

// generateParams represents various params of the Generate call.
type generateParams struct {
Request *ModelRequest
Expand Down
15 changes: 15 additions & 0 deletions go/ai/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,21 @@ func TestLookupModel(t *testing.T) {
})
}

func TestLookupModelByName(t *testing.T) {
t.Run("should return model", func(t *testing.T) {
model, _ := LookupModelByName(r, "test/"+modelName)
if model == nil {
t.Errorf("LookupModelByName did not return model")
}
})
t.Run("should return nil", func(t *testing.T) {
_, err := LookupModelByName(r, "foo/bar")
if err == nil {
t.Errorf("LookupModelByName did not return error")
}
})
}

func JSONMarkdown(text string) string {
return "```json\n" + text + "\n```"
}
Expand Down
Loading
Loading