Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add openai provider support for chat completion
- Loading branch information
Showing
7 changed files
with
231 additions
and
156 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,48 @@ | ||
package openai | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
|
||
llmv1 "github.com/missingstudio/studio/protos/pkg/llm" | ||
) | ||
|
||
var OpenAIAPIURL = "https://api.openai.com/v1/chat/completions" | ||
|
||
type OpenAI struct { | ||
APIKey string | ||
} | ||
|
||
func (oai OpenAI) ChatCompilation(ctx context.Context, cr *llmv1.CompletionRequest) (*llmv1.CompletionResponse, error) { | ||
payload, err := json.Marshal(cr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client := &http.Client{} | ||
req, _ := http.NewRequestWithContext(ctx, "POST", OpenAIAPIURL, bytes.NewReader(payload)) | ||
req.Header.Add("Content-Type", "application/json") | ||
req.Header.Add("Authorization", "Bearer "+oai.APIKey) | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var data llmv1.CompletionResponse | ||
err = json.Unmarshal(body, &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,27 @@ | ||
package providers | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
"strings" | ||
|
||
"connectrpc.com/connect" | ||
"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) | ||
} | ||
|
||
func NewLLMProvider(headers http.Header) (LLMProvider, error) { | ||
provider := headers.Get("x-ms-provider") | ||
if provider == "" { | ||
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 | ||
} |
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,19 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
|
||
"connectrpc.com/connect" | ||
) | ||
|
||
func NewLogInterceptor() connect.UnaryInterceptorFunc { | ||
interceptor := func(next connect.UnaryFunc) connect.UnaryFunc { | ||
return connect.UnaryFunc(func( | ||
ctx context.Context, | ||
req connect.AnyRequest, | ||
) (connect.AnyResponse, error) { | ||
return next(ctx, req) | ||
}) | ||
} | ||
return connect.UnaryInterceptorFunc(interceptor) | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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