first commit
This commit is contained in:
80
core/providers/openai/models.go
Normal file
80
core/providers/openai/models.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package openai
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
providerUtils "github.com/maximhq/bifrost/core/providers/utils"
|
||||
"github.com/maximhq/bifrost/core/schemas"
|
||||
)
|
||||
|
||||
// ToBifrostListModelsResponse converts an OpenAI list models response to a Bifrost list models response
|
||||
func (response *OpenAIListModelsResponse) ToBifrostListModelsResponse(providerKey schemas.ModelProvider, allowedModels schemas.WhiteList, blacklistedModels schemas.BlackList, aliases map[string]string, unfiltered bool) *schemas.BifrostListModelsResponse {
|
||||
if response == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
bifrostResponse := &schemas.BifrostListModelsResponse{
|
||||
Data: make([]schemas.Model, 0, len(response.Data)),
|
||||
}
|
||||
|
||||
pipeline := &providerUtils.ListModelsPipeline{
|
||||
AllowedModels: allowedModels,
|
||||
BlacklistedModels: blacklistedModels,
|
||||
Aliases: aliases,
|
||||
Unfiltered: unfiltered,
|
||||
ProviderKey: providerKey,
|
||||
MatchFns: providerUtils.DefaultMatchFns(),
|
||||
}
|
||||
if pipeline.ShouldEarlyExit() {
|
||||
return bifrostResponse
|
||||
}
|
||||
|
||||
included := make(map[string]bool)
|
||||
|
||||
for _, model := range response.Data {
|
||||
for _, result := range pipeline.FilterModel(model.ID) {
|
||||
entry := schemas.Model{
|
||||
ID: string(providerKey) + "/" + result.ResolvedID,
|
||||
Created: model.Created,
|
||||
OwnedBy: schemas.Ptr(model.OwnedBy),
|
||||
ContextLength: model.ContextWindow,
|
||||
}
|
||||
if result.AliasValue != "" {
|
||||
entry.Alias = schemas.Ptr(result.AliasValue)
|
||||
}
|
||||
bifrostResponse.Data = append(bifrostResponse.Data, entry)
|
||||
included[strings.ToLower(result.ResolvedID)] = true
|
||||
}
|
||||
}
|
||||
|
||||
bifrostResponse.Data = append(bifrostResponse.Data,
|
||||
pipeline.BackfillModels(included)...)
|
||||
|
||||
return bifrostResponse
|
||||
}
|
||||
|
||||
// ToOpenAIListModelsResponse converts a Bifrost list models response to an OpenAI list models response
|
||||
func ToOpenAIListModelsResponse(response *schemas.BifrostListModelsResponse) *OpenAIListModelsResponse {
|
||||
if response == nil {
|
||||
return nil
|
||||
}
|
||||
openaiResponse := &OpenAIListModelsResponse{
|
||||
Data: make([]OpenAIModel, 0, len(response.Data)),
|
||||
}
|
||||
for _, model := range response.Data {
|
||||
openaiModel := OpenAIModel{
|
||||
ID: model.ID,
|
||||
Object: "model",
|
||||
}
|
||||
if model.Created != nil {
|
||||
openaiModel.Created = model.Created
|
||||
}
|
||||
if model.OwnedBy != nil {
|
||||
openaiModel.OwnedBy = *model.OwnedBy
|
||||
}
|
||||
|
||||
openaiResponse.Data = append(openaiResponse.Data, openaiModel)
|
||||
|
||||
}
|
||||
return openaiResponse
|
||||
}
|
||||
Reference in New Issue
Block a user