Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add togetherai provider support
- Loading branch information
Showing
4 changed files
with
108 additions
and
1 deletion.
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
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 togetherai | ||
|
||
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 TogetherAIProviderFactory struct{} | ||
|
||
type TogetherAIHeaders struct { | ||
APIKey string `validate:"required" json:"Authorization" error:"API key is required"` | ||
} | ||
|
||
func (ta TogetherAIProviderFactory) Validate(headers http.Header) (*TogetherAIHeaders, error) { | ||
var togetherAIHeaders TogetherAIHeaders | ||
if err := utils.UnmarshalHeader(headers, &togetherAIHeaders); err != nil { | ||
return nil, errors.New(err) | ||
} | ||
|
||
if err := utils.ValidateHeaders(togetherAIHeaders); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &togetherAIHeaders, nil | ||
} | ||
|
||
func (ta TogetherAIProviderFactory) Create(headers http.Header) (base.ProviderInterface, error) { | ||
togetherAIHeaders, err := ta.Validate(headers) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
togetherAIHeaders.APIKey = strings.Replace(togetherAIHeaders.APIKey, "Bearer ", "", 1) | ||
openAIProvider := NewTogetherAIProvider(*togetherAIHeaders) | ||
return openAIProvider, nil | ||
} | ||
|
||
type TogetherAIProvider struct { | ||
Name string | ||
Config base.ProviderConfig | ||
TogetherAIHeaders | ||
} | ||
|
||
func NewTogetherAIProvider(headers TogetherAIHeaders) *TogetherAIProvider { | ||
config := getTogetherAIConfig("https://api.together.xyz") | ||
|
||
return &TogetherAIProvider{ | ||
Name: "TogetherAI", | ||
Config: config, | ||
TogetherAIHeaders: headers, | ||
} | ||
} | ||
|
||
func (togetherAI TogetherAIProvider) GetName() string { | ||
return togetherAI.Name | ||
} | ||
|
||
func getTogetherAIConfig(baseURL string) base.ProviderConfig { | ||
return base.ProviderConfig{ | ||
BaseURL: baseURL, | ||
ChatCompletions: "/v1/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 togetherai | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/missingstudio/studio/backend/pkg/requester" | ||
llmv1 "github.com/missingstudio/studio/protos/pkg/llm" | ||
) | ||
|
||
func (ta *TogetherAIProvider) 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", ta.Config.BaseURL, ta.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", ta.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 | ||
} |