-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#51: Build routers & models based on provided config
- Loading branch information
1 parent
ae3cf1b
commit 65405ef
Showing
6 changed files
with
198 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package providers | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"glide/pkg/providers/openai" | ||
"glide/pkg/telemetry" | ||
"time" | ||
) | ||
|
||
var ( | ||
ErrProviderNotFound = errors.New("provider not found") | ||
) | ||
|
||
type LangModelConfig struct { | ||
ID string `yaml:"id"` | ||
Enabled bool `yaml:"enabled"` | ||
Timeout *time.Duration `yaml:"timeout,omitempty"` | ||
OpenAI *openai.Config | ||
// Add other providers like | ||
// Cohere *cohere.Config | ||
// Anthropic *anthropic.Config | ||
} | ||
|
||
func DefaultLangModelConfig() *LangModelConfig { | ||
defaultTimeout := 10 * time.Second | ||
|
||
return &LangModelConfig{ | ||
Enabled: true, | ||
Timeout: &defaultTimeout, | ||
} | ||
} | ||
|
||
func (c *LangModelConfig) ToModel(tel *telemetry.Telemetry) (LanguageModel, error) { | ||
if c.OpenAI != nil { | ||
client, err := openai.NewClient(c.OpenAI, tel) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("error initing openai client: %v", err) | ||
} | ||
|
||
return client, nil | ||
} | ||
|
||
return nil, ErrProviderNotFound | ||
} | ||
|
||
func (m *LangModelConfig) validateOneProvider() error { | ||
providersConfigured := 0 | ||
|
||
if m.OpenAI != nil { | ||
providersConfigured++ | ||
} | ||
|
||
// check other providers here | ||
if providersConfigured == 0 { | ||
return fmt.Errorf("exactly one provider must be cofigured for model \"%v\", none is configured", m.ID) | ||
} | ||
|
||
if providersConfigured > 1 { | ||
return fmt.Errorf( | ||
"exactly one provider must be cofigured for model \"%v\", %v are configured", | ||
m.ID, | ||
providersConfigured, | ||
) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *LangModelConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
*m = *DefaultLangModelConfig() | ||
|
||
type plain LangModelConfig // to avoid recursion | ||
|
||
if err := unmarshal((*plain)(m)); err != nil { | ||
return err | ||
} | ||
|
||
return m.validateOneProvider() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
package providers | ||
|
||
import "errors" | ||
import ( | ||
"context" | ||
"errors" | ||
"glide/pkg/api/schemas" | ||
) | ||
|
||
var ErrProviderUnavailable = errors.New("provider is not available") | ||
|
||
// ModelProvider defines an interface all model providers should support | ||
type ModelProvider interface { | ||
Provider() string | ||
} | ||
|
||
// LanguageModel defines the interface a provider should fulfill to be able to serve language chat requests | ||
type LanguageModel interface { | ||
Chat(ctx context.Context, request *schemas.UnifiedChatRequest) (*schemas.UnifiedChatResponse, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters