Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add factory pattern to create provider
- Loading branch information
Showing
4 changed files
with
57 additions
and
12 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
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,16 @@ | ||
package base | ||
|
||
import ( | ||
"context" | ||
|
||
llmv1 "github.com/missingstudio/studio/protos/pkg/llm" | ||
) | ||
|
||
type ProviderConfig struct { | ||
BaseURL string | ||
ChatCompletions string | ||
} | ||
|
||
type LLMProvider interface { | ||
ChatCompilation(ctx context.Context, ra *llmv1.CompletionRequest) (*llmv1.CompletionResponse, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,34 @@ | ||
package providers | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
"strings" | ||
|
||
"connectrpc.com/connect" | ||
"github.com/missingstudio/studio/backend/internal/providers/base" | ||
"github.com/missingstudio/studio/backend/internal/providers/openai" | ||
llmv1 "github.com/missingstudio/studio/protos/pkg/llm" | ||
) | ||
|
||
type LLMProvider interface { | ||
ChatCompilation(ctx context.Context, ra *llmv1.CompletionRequest) (*llmv1.CompletionResponse, error) | ||
type ProviderFactory interface { | ||
Create(token string) base.LLMProvider | ||
} | ||
|
||
func NewLLMProvider(headers http.Header) (LLMProvider, error) { | ||
var providerFactories = make(map[string]ProviderFactory) | ||
|
||
func init() { | ||
providerFactories["openai"] = openai.OpenAIProviderFactory{} | ||
} | ||
|
||
func GetProvider(headers http.Header) (base.LLMProvider, error) { | ||
provider := headers.Get("x-ms-provider") | ||
if provider == "" { | ||
providerFactory, ok := providerFactories[provider] | ||
if !ok { | ||
return nil, connect.NewError(connect.CodeNotFound, errors.New("provider not found")) | ||
} | ||
|
||
authHeader := headers.Get("Authorization") | ||
accessToken := strings.Replace(authHeader, "Bearer ", "", 1) | ||
|
||
return &openai.OpenAI{APIKey: accessToken}, nil | ||
return providerFactory.Create(accessToken), nil | ||
} |