Skip to content

Commit e5e6363

Browse files
committed
refactor, Introduce model method for model ID without provider prefix
1 parent a3c7f01 commit e5e6363

File tree

7 files changed

+41
-17
lines changed

7 files changed

+41
-17
lines changed

model/llm/llm.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,21 @@ func (m *Model) ID() (id string) {
7373
return m.id
7474
}
7575

76-
// ModelID returns the unique identifier of this model.
76+
// ModelID returns the unique identifier of this model with its provider.
7777
func (m *Model) ModelID() (modelID string) {
7878
return m.modelID
7979
}
8080

81+
// ModelIDWithoutProvider returns the unique identifier of this model without its provider.
82+
func (m *Model) ModelIDWithoutProvider() (modelID string) {
83+
_, modelID, ok := strings.Cut(m.modelID, provider.ProviderModelSeparator)
84+
if !ok {
85+
panic(m.modelID)
86+
}
87+
88+
return modelID
89+
}
90+
8191
// Attributes returns query attributes.
8292
func (m *Model) Attributes() (attributes map[string]string) {
8393
return m.attributes

model/model.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111
type Model interface {
1212
// ID returns full identifier, including the provider and attributes.
1313
ID() (id string)
14-
// ModelID returns the unique identifier of this model.
14+
// ModelID returns the unique identifier of this model with its provider.
1515
ModelID() (modelID string)
16+
// ModelIDWithoutProvider returns the unique identifier of this model without its provider.
17+
ModelIDWithoutProvider() (modelID string)
1618

1719
// Attributes returns query attributes.
1820
Attributes() (attributes map[string]string)

model/symflower/symflower.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,16 @@ func (m *Model) ID() (id string) {
6767
return "symflower" + provider.ProviderModelSeparator + m.id
6868
}
6969

70-
// ModelID returns the unique identifier of this model.
70+
// ModelID returns the unique identifier of this model with its provider.
7171
func (m *Model) ModelID() (modelID string) {
7272
return "symflower" + provider.ProviderModelSeparator + m.id
7373
}
7474

75+
// ModelIDWithoutProvider returns the unique identifier of this model without its provider.
76+
func (m *Model) ModelIDWithoutProvider() (modelID string) {
77+
return m.id
78+
}
79+
7580
// Attributes returns query attributes.
7681
func (m *Model) Attributes() (attributes map[string]string) {
7782
return nil

model/testing/Model_mock_gen.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

provider/ollama/ollama.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ var _ provider.Query = (*Provider)(nil)
8282

8383
// Query queries the provider with the given model name.
8484
func (p *Provider) Query(ctx context.Context, model model.Model, promptText string) (response string, err error) {
85-
client := p.client()
86-
modelIdentifier := strings.TrimPrefix(model.ModelID(), p.ID()+provider.ProviderModelSeparator)
87-
88-
return openaiapi.QueryOpenAIAPIModel(ctx, client, modelIdentifier, model.Attributes(), promptText)
85+
return openaiapi.QueryOpenAIAPIModel(ctx, p.client(), model.ModelIDWithoutProvider(), model.Attributes(), promptText)
8986
}
9087

9188
// client returns a new client with the current configuration.

provider/openai-api/openai.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package openaiapi
22

33
import (
44
"context"
5-
"strings"
65

76
"github.com/sashabaranov/go-openai"
87

@@ -61,10 +60,7 @@ var _ provider.Query = (*Provider)(nil)
6160

6261
// Query queries the provider with the given model name.
6362
func (p *Provider) Query(ctx context.Context, model model.Model, promptText string) (response string, err error) {
64-
client := p.client()
65-
modelIdentifier := strings.TrimPrefix(model.ModelID(), p.ID()+provider.ProviderModelSeparator)
66-
67-
return QueryOpenAIAPIModel(ctx, client, modelIdentifier, model.Attributes(), promptText)
63+
return QueryOpenAIAPIModel(ctx, p.client(), model.ModelIDWithoutProvider(), model.Attributes(), promptText)
6864
}
6965

7066
// client returns a new client with the current configuration.

provider/openrouter/openrouter.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"io"
88
"net/http"
99
"net/url"
10-
"strings"
1110
"time"
1211

1312
"github.com/avast/retry-go"
@@ -139,10 +138,7 @@ var _ provider.Query = (*Provider)(nil)
139138

140139
// Query queries the provider with the given model name.
141140
func (p *Provider) Query(ctx context.Context, model model.Model, promptText string) (response string, err error) {
142-
client := p.client()
143-
modelIdentifier := strings.TrimPrefix(model.ModelID(), p.ID()+provider.ProviderModelSeparator)
144-
145-
return openaiapi.QueryOpenAIAPIModel(ctx, client, modelIdentifier, model.Attributes(), promptText)
141+
return openaiapi.QueryOpenAIAPIModel(ctx, p.client(), model.ModelIDWithoutProvider(), model.Attributes(), promptText)
146142
}
147143

148144
// client returns a new client with the current configuration.

0 commit comments

Comments
 (0)