Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
112 additions
and
3 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,40 @@ | ||
package anyscale | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/missingstudio/studio/backend/pkg/requester" | ||
llmv1 "github.com/missingstudio/studio/protos/pkg/llm" | ||
) | ||
|
||
func (anyscale *AnyscaleProvider) 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", anyscale.Config.BaseURL, anyscale.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", anyscale.APIKey)) | ||
|
||
resp, err := client.Do(req) | ||
fmt.Println(resp) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package anyscale | ||
|
||
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 AnyscaleProviderFactory struct{} | ||
|
||
type AnyscaleHeaders struct { | ||
APIKey string `validate:"required" json:"Authorization" error:"API key is required"` | ||
} | ||
|
||
func (anyscale AnyscaleProviderFactory) Validate(headers http.Header) (*AnyscaleHeaders, error) { | ||
var anyscaleHeaders AnyscaleHeaders | ||
if err := utils.UnmarshalHeader(headers, &anyscaleHeaders); err != nil { | ||
return nil, errors.New(err) | ||
} | ||
|
||
if err := utils.ValidateHeaders(anyscaleHeaders); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &anyscaleHeaders, nil | ||
} | ||
|
||
func (anyscale AnyscaleProviderFactory) Create(headers http.Header) (base.ProviderInterface, error) { | ||
anyscaleHeaders, err := anyscale.Validate(headers) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
anyscaleHeaders.APIKey = strings.Replace(anyscaleHeaders.APIKey, "Bearer ", "", 1) | ||
openAIProvider := NewAnyscaleProvider(*anyscaleHeaders) | ||
return openAIProvider, nil | ||
} | ||
|
||
type AnyscaleProvider struct { | ||
Name string | ||
Config base.ProviderConfig | ||
AnyscaleHeaders | ||
} | ||
|
||
func NewAnyscaleProvider(headers AnyscaleHeaders) *AnyscaleProvider { | ||
config := getAnyscaleConfig("https://api.endpoints.anyscale.com") | ||
|
||
return &AnyscaleProvider{ | ||
Name: "Anyscale", | ||
Config: config, | ||
AnyscaleHeaders: headers, | ||
} | ||
} | ||
|
||
func (anyscale AnyscaleProvider) GetName() string { | ||
return anyscale.Name | ||
} | ||
|
||
func getAnyscaleConfig(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
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