Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add deepinfra provider support
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 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,67 @@ | ||
package deepinfra | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/missingstudio/studio/backend/internal/providers/base" | ||
"github.com/missingstudio/studio/backend/pkg/utils" | ||
"github.com/missingstudio/studio/common/errors" | ||
) | ||
|
||
type DeepinfraProviderFactory struct{} | ||
|
||
type DeepinfraHeaders struct { | ||
APIKey string `validate:"required" json:"Authorization" error:"API key is required"` | ||
} | ||
|
||
func (deepinfra DeepinfraProviderFactory) Validate(headers http.Header) (*DeepinfraHeaders, error) { | ||
var deepinfraHeaders DeepinfraHeaders | ||
if err := utils.UnmarshalHeader(headers, &deepinfraHeaders); err != nil { | ||
return nil, errors.New(err) | ||
} | ||
|
||
if err := utils.ValidateHeaders(deepinfraHeaders); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &deepinfraHeaders, nil | ||
} | ||
|
||
func (deepinfra DeepinfraProviderFactory) Create(headers http.Header) (base.ProviderInterface, error) { | ||
deepinfraHeaders, err := deepinfra.Validate(headers) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
deepinfraHeaders.APIKey = strings.Replace(deepinfraHeaders.APIKey, "Bearer ", "", 1) | ||
openAIProvider := NewDeepinfraProvider(*deepinfraHeaders) | ||
return openAIProvider, nil | ||
} | ||
|
||
type DeepinfraProvider struct { | ||
Name string | ||
Config base.ProviderConfig | ||
DeepinfraHeaders | ||
} | ||
|
||
func NewDeepinfraProvider(headers DeepinfraHeaders) *DeepinfraProvider { | ||
config := getDeepinfraConfig("https://api.deepinfra.com/v1/openai") | ||
|
||
return &DeepinfraProvider{ | ||
Name: "Deepinfra", | ||
Config: config, | ||
DeepinfraHeaders: headers, | ||
} | ||
} | ||
|
||
func (deepinfra DeepinfraProvider) GetName() string { | ||
return deepinfra.Name | ||
} | ||
|
||
func getDeepinfraConfig(baseURL string) base.ProviderConfig { | ||
return base.ProviderConfig{ | ||
BaseURL: baseURL, | ||
ChatCompletions: "/chat/completions", | ||
} | ||
} |
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,39 @@ | ||
package deepinfra | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/missingstudio/studio/backend/pkg/requester" | ||
llmv1 "github.com/missingstudio/studio/protos/pkg/llm" | ||
) | ||
|
||
func (deepinfra *DeepinfraProvider) ChatCompilation(ctx context.Context, cr *llmv1.CompletionRequest) (*llmv1.CompletionResponse, error) { | ||
payload, err := json.Marshal(cr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client := requester.NewHTTPClient() | ||
requestURL := fmt.Sprintf("%s%s", deepinfra.Config.BaseURL, deepinfra.Config.ChatCompletions) | ||
req, _ := http.NewRequestWithContext(ctx, "POST", requestURL, bytes.NewReader(payload)) | ||
|
||
req.Header.Add("Content-Type", "application/json") | ||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", deepinfra.APIKey)) | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var data llmv1.CompletionResponse | ||
err = json.Unmarshal(resp, &data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &data, nil | ||
} |
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