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
85 additions
and
49 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,33 @@ | ||
package v1 | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"connectrpc.com/connect" | ||
"github.com/missingstudio/studio/backend/internal/providers" | ||
"github.com/missingstudio/studio/backend/internal/providers/base" | ||
llmv1 "github.com/missingstudio/studio/protos/pkg/llm" | ||
) | ||
|
||
func (s *V1Handler) ChatCompletions( | ||
ctx context.Context, | ||
req *connect.Request[llmv1.CompletionRequest], | ||
) (*connect.Response[llmv1.CompletionResponse], error) { | ||
provider, err := providers.GetProvider(ctx) | ||
if err != nil { | ||
return nil, connect.NewError(connect.CodeInternal, err) | ||
} | ||
|
||
completionProvider, ok := provider.(base.ChatCompilationInterface) | ||
if !ok { | ||
return nil, connect.NewError(connect.CodeUnimplemented, errors.New("method not implemented")) | ||
} | ||
|
||
data, err := completionProvider.ChatCompilation(ctx, req.Msg) | ||
if err != nil { | ||
return nil, connect.NewError(connect.CodeInternal, err) | ||
} | ||
|
||
return connect.NewResponse(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,37 @@ | ||
package v1 | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"connectrpc.com/connect" | ||
"connectrpc.com/validate" | ||
"connectrpc.com/vanguard" | ||
"github.com/missingstudio/studio/backend/pkg/utils" | ||
"github.com/missingstudio/studio/protos/pkg/llm/llmv1connect" | ||
) | ||
|
||
type V1Handler struct { | ||
llmv1connect.UnimplementedLLMServiceHandler | ||
} | ||
|
||
func Register() (http.Handler, error) { | ||
validateInterceptor, err := validate.NewInterceptor() | ||
if err != nil { | ||
return nil, fmt.Errorf("validate interceptor not created: %w", err) | ||
} | ||
|
||
compress1KB := connect.WithCompressMinBytes(1024) | ||
services := []*vanguard.Service{ | ||
vanguard.NewService(llmv1connect.NewLLMServiceHandler( | ||
&V1Handler{}, | ||
compress1KB, | ||
connect.WithInterceptors(validateInterceptor, utils.ProviderInterceptor()), | ||
)), | ||
} | ||
transcoderOptions := []vanguard.TranscoderOption{ | ||
vanguard.WithUnknownHandler(utils.Custom404handler()), | ||
} | ||
|
||
return vanguard.NewTranscoder(services, transcoderOptions...) | ||
} |
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,9 @@ | ||
package utils | ||
|
||
import "net/http" | ||
|
||
func Custom404handler() http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
http.Error(w, "custom 404 error", http.StatusNotFound) | ||
}) | ||
} |