Skip to content

Commit

Permalink
feat: add togetherai provider support
Browse files Browse the repository at this point in the history
  • Loading branch information
pyadav committed Feb 1, 2024
1 parent c2dd771 commit 8f6f689
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
1 change: 0 additions & 1 deletion mobius/internal/providers/anyscale/anyscale.go
Expand Up @@ -25,7 +25,6 @@ func (anyscale *AnyscaleProvider) ChatCompilation(ctx context.Context, cr *llmv1
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", anyscale.APIKey))

resp, err := client.Do(req)
fmt.Println(resp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions mobius/internal/providers/providers.go
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/missingstudio/studio/backend/internal/providers/base"
"github.com/missingstudio/studio/backend/internal/providers/deepinfra"
"github.com/missingstudio/studio/backend/internal/providers/openai"
"github.com/missingstudio/studio/backend/internal/providers/togetherai"
"github.com/missingstudio/studio/common/errors"
)

Expand All @@ -24,6 +25,7 @@ func init() {
providerFactories["azure"] = azure.AzureProviderFactory{}
providerFactories["anyscale"] = anyscale.AnyscaleProviderFactory{}
providerFactories["deepinfra"] = deepinfra.DeepinfraProviderFactory{}
providerFactories["togetherai"] = togetherai.TogetherAIProviderFactory{}
}

func GetProvider(ctx context.Context, headers http.Header) (base.ProviderInterface, error) {
Expand Down
67 changes: 67 additions & 0 deletions mobius/internal/providers/togetherai/base.go
@@ -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",
}
}
39 changes: 39 additions & 0 deletions mobius/internal/providers/togetherai/togetherai.go
@@ -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
}

0 comments on commit 8f6f689

Please sign in to comment.