mirror of
https://github.com/mudler/LocalAI.git
synced 2025-06-30 06:30:43 +00:00
big progress checkin. Still quite broken, but now it shows the new direction. Time to start hooking things up again.
This commit is contained in:
parent
20a0cd2f66
commit
f9133b5a61
12 changed files with 228 additions and 364 deletions
232
apiv2/localai.go
232
apiv2/localai.go
|
@ -1,11 +1,8 @@
|
|||
package apiv2
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
|
@ -15,222 +12,183 @@ type LocalAIServer struct {
|
|||
configManager *ConfigManager
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type ModelOnlyRequest struct {
|
||||
Model string `json:"model" yaml:"model"`
|
||||
}
|
||||
|
||||
// This function grabs the name of the function that calls it, skipping up the callstack `skip` levels.
|
||||
// This is probably a go war crime, but NJ method and all. It's an awesome way to index EndpointConfigMap
|
||||
func printCurrentFunctionName(skip int) string {
|
||||
pc, _, _, _ := runtime.Caller(skip)
|
||||
funcName := runtime.FuncForPC(pc).Name()
|
||||
fmt.Println("Current function:", funcName)
|
||||
return funcName
|
||||
}
|
||||
|
||||
func sendError(w http.ResponseWriter, code int, message string) {
|
||||
localAiError := Error{
|
||||
Code: code,
|
||||
Message: message,
|
||||
}
|
||||
w.WriteHeader(code)
|
||||
json.NewEncoder(w).Encode(localAiError)
|
||||
}
|
||||
|
||||
// TODO: Is it a good idea to return "" in cases where the model isn't provided?
|
||||
// Or is that actually an error condition?
|
||||
// NO is a decent guess as any to start with?
|
||||
// r *http.Request
|
||||
func (server *LocalAIServer) getRequestModelName(body []byte) string {
|
||||
var modelOnlyRequest = ModelOnlyRequest{}
|
||||
if err := json.Unmarshal(body, &modelOnlyRequest); err != nil {
|
||||
fmt.Printf("ERR in getRequestModelName, %+v", err)
|
||||
return ""
|
||||
}
|
||||
return modelOnlyRequest.Model
|
||||
}
|
||||
|
||||
func (server *LocalAIServer) combineRequestAndConfig(endpointName string, body []byte) (interface{}, error) {
|
||||
model := server.getRequestModelName(body)
|
||||
|
||||
lookup := ConfigRegistration{Model: model, Endpoint: endpointName}
|
||||
|
||||
config, exists := server.configManager.GetConfig(lookup)
|
||||
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("Config not found for %+v", lookup)
|
||||
}
|
||||
|
||||
// fmt.Printf("Model: %s\nConfig: %+v\n", model, config)
|
||||
|
||||
request := config.GetRequestDefaults()
|
||||
// fmt.Printf("BEFORE rD: %T\n%+v\n\n", request, request)
|
||||
tmpUnmarshal := map[string]interface{}{}
|
||||
if err := json.Unmarshal(body, &tmpUnmarshal); err != nil {
|
||||
return nil, fmt.Errorf("error unmarshalling json to temp map\n%w", err)
|
||||
}
|
||||
// fmt.Printf("$$$ tmpUnmarshal: %+v\n", tmpUnmarshal)
|
||||
mapstructure.Decode(tmpUnmarshal, &request)
|
||||
fmt.Printf("AFTER rD: %T\n%+v\n\n", request, request)
|
||||
return request, nil
|
||||
}
|
||||
|
||||
func (server *LocalAIServer) getRequest(w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
sendError(w, http.StatusBadRequest, "Failed to read body")
|
||||
}
|
||||
func combineRequestAndConfig[RequestType any](configManager *ConfigManager, model string, requestFromInput *RequestType) (*RequestType, error) {
|
||||
|
||||
splitFnName := strings.Split(printCurrentFunctionName(2), ".")
|
||||
|
||||
endpointName := splitFnName[len(splitFnName)-1]
|
||||
|
||||
return server.combineRequestAndConfig(endpointName, body)
|
||||
lookup := ConfigRegistration{Model: model, Endpoint: endpointName}
|
||||
|
||||
config, exists := configManager.GetConfig(lookup)
|
||||
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("Config not found for %+v", lookup)
|
||||
}
|
||||
|
||||
// fmt.Printf("Model: %s\nConfig: %+v\nrequestFromInput: %+v\n", model, config, requestFromInput)
|
||||
|
||||
request, ok := config.GetRequestDefaults().(RequestType)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Config failed casting for %+v", lookup)
|
||||
}
|
||||
|
||||
// configMergingConfig := GetConfigMergingDecoderConfig(&request)
|
||||
// configMergingDecoder, err := mapstructure.NewDecoder(&configMergingConfig)
|
||||
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// configMergingDecoder.Decode(requestFromInput)
|
||||
|
||||
// TODO try decoding hooks again later. For testing, do a stupid copy
|
||||
decodeErr := mapstructure.Decode(structToStrippedMap(*requestFromInput), &request)
|
||||
|
||||
if decodeErr != nil {
|
||||
return nil, decodeErr
|
||||
}
|
||||
|
||||
fmt.Printf("AFTER rD: %T\n%+v\n\n", request, request)
|
||||
|
||||
return &request, nil
|
||||
}
|
||||
|
||||
// CancelFineTune implements ServerInterface
|
||||
func (*LocalAIServer) CancelFineTune(w http.ResponseWriter, r *http.Request, fineTuneId string) {
|
||||
// CancelFineTune implements StrictServerInterface
|
||||
func (*LocalAIServer) CancelFineTune(ctx context.Context, request CancelFineTuneRequestObject) (CancelFineTuneResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateChatCompletion implements ServerInterface
|
||||
func (server *LocalAIServer) CreateChatCompletion(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("HIT APIv2 CreateChatCompletion!")
|
||||
// CreateChatCompletion implements StrictServerInterface
|
||||
func (las *LocalAIServer) CreateChatCompletion(ctx context.Context, request CreateChatCompletionRequestObject) (CreateChatCompletionResponseObject, error) {
|
||||
|
||||
request, err := server.getRequest(w, r)
|
||||
chatRequest, err := combineRequestAndConfig(las.configManager, request.Body.Model, request.Body)
|
||||
|
||||
if err != nil {
|
||||
sendError(w, http.StatusBadRequest, err.Error())
|
||||
fmt.Printf("CreateChatCompletion ERROR combining config and input!\n%s\n", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// fmt.Printf("\n!!! Survived to attempt cast. BEFORE:\n\tType: %T\n\t%+v", request, request)
|
||||
fmt.Printf("\n===CreateChatCompletion===\n%+v\n", chatRequest)
|
||||
|
||||
chatRequest, castSuccess := request.(CreateChatCompletionRequest)
|
||||
|
||||
if !castSuccess {
|
||||
sendError(w, http.StatusInternalServerError, "Cast Fail???")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("\n\n!! AFTER !!\ntemperature %f\n top_p %f \n %d\n", *chatRequest.Temperature, *chatRequest.TopP, *chatRequest.XLocalaiExtensions.TopK)
|
||||
fmt.Printf("\n\n!! TYPED CreateChatCompletion !!\ntemperature %f\n top_p %f \n %d\n", *chatRequest.Temperature, *chatRequest.TopP, *chatRequest.XLocalaiExtensions.TopK)
|
||||
|
||||
fmt.Printf("chatRequest: %+v\nlen(messages): %d", chatRequest, len(chatRequest.Messages))
|
||||
for i, m := range chatRequest.Messages {
|
||||
fmt.Printf("message #%d: %+v", i, m)
|
||||
}
|
||||
|
||||
return CreateChatCompletion200JSONResponse{}, nil
|
||||
|
||||
// panic("unimplemented")
|
||||
}
|
||||
|
||||
// switch chatRequest := requestDefault.(type) {
|
||||
// case CreateChatCompletionRequest:
|
||||
|
||||
// CreateCompletion implements ServerInterface
|
||||
func (*LocalAIServer) CreateCompletion(w http.ResponseWriter, r *http.Request) {
|
||||
// CreateCompletion implements StrictServerInterface
|
||||
func (*LocalAIServer) CreateCompletion(ctx context.Context, request CreateCompletionRequestObject) (CreateCompletionResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateEdit implements ServerInterface
|
||||
func (*LocalAIServer) CreateEdit(w http.ResponseWriter, r *http.Request) {
|
||||
// CreateEdit implements StrictServerInterface
|
||||
func (*LocalAIServer) CreateEdit(ctx context.Context, request CreateEditRequestObject) (CreateEditResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateEmbedding implements ServerInterface
|
||||
func (*LocalAIServer) CreateEmbedding(w http.ResponseWriter, r *http.Request) {
|
||||
// CreateEmbedding implements StrictServerInterface
|
||||
func (*LocalAIServer) CreateEmbedding(ctx context.Context, request CreateEmbeddingRequestObject) (CreateEmbeddingResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateFile implements ServerInterface
|
||||
func (*LocalAIServer) CreateFile(w http.ResponseWriter, r *http.Request) {
|
||||
// CreateFile implements StrictServerInterface
|
||||
func (*LocalAIServer) CreateFile(ctx context.Context, request CreateFileRequestObject) (CreateFileResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateFineTune implements ServerInterface
|
||||
func (*LocalAIServer) CreateFineTune(w http.ResponseWriter, r *http.Request) {
|
||||
// CreateFineTune implements StrictServerInterface
|
||||
func (*LocalAIServer) CreateFineTune(ctx context.Context, request CreateFineTuneRequestObject) (CreateFineTuneResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateImage implements ServerInterface
|
||||
func (*LocalAIServer) CreateImage(w http.ResponseWriter, r *http.Request) {
|
||||
// CreateImage implements StrictServerInterface
|
||||
func (*LocalAIServer) CreateImage(ctx context.Context, request CreateImageRequestObject) (CreateImageResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateImageEdit implements ServerInterface
|
||||
func (*LocalAIServer) CreateImageEdit(w http.ResponseWriter, r *http.Request) {
|
||||
// CreateImageEdit implements StrictServerInterface
|
||||
func (*LocalAIServer) CreateImageEdit(ctx context.Context, request CreateImageEditRequestObject) (CreateImageEditResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateImageVariation implements ServerInterface
|
||||
func (*LocalAIServer) CreateImageVariation(w http.ResponseWriter, r *http.Request) {
|
||||
// CreateImageVariation implements StrictServerInterface
|
||||
func (*LocalAIServer) CreateImageVariation(ctx context.Context, request CreateImageVariationRequestObject) (CreateImageVariationResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateModeration implements ServerInterface
|
||||
func (*LocalAIServer) CreateModeration(w http.ResponseWriter, r *http.Request) {
|
||||
// CreateModeration implements StrictServerInterface
|
||||
func (*LocalAIServer) CreateModeration(ctx context.Context, request CreateModerationRequestObject) (CreateModerationResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateTranscription implements ServerInterface
|
||||
func (*LocalAIServer) CreateTranscription(w http.ResponseWriter, r *http.Request) {
|
||||
// CreateTranscription implements StrictServerInterface
|
||||
func (*LocalAIServer) CreateTranscription(ctx context.Context, request CreateTranscriptionRequestObject) (CreateTranscriptionResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateTranslation implements ServerInterface
|
||||
func (*LocalAIServer) CreateTranslation(w http.ResponseWriter, r *http.Request) {
|
||||
// CreateTranslation implements StrictServerInterface
|
||||
func (*LocalAIServer) CreateTranslation(ctx context.Context, request CreateTranslationRequestObject) (CreateTranslationResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// DeleteFile implements ServerInterface
|
||||
func (*LocalAIServer) DeleteFile(w http.ResponseWriter, r *http.Request, fileId string) {
|
||||
// DeleteFile implements StrictServerInterface
|
||||
func (*LocalAIServer) DeleteFile(ctx context.Context, request DeleteFileRequestObject) (DeleteFileResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// DeleteModel implements ServerInterface
|
||||
func (*LocalAIServer) DeleteModel(w http.ResponseWriter, r *http.Request, model string) {
|
||||
// DeleteModel implements StrictServerInterface
|
||||
func (*LocalAIServer) DeleteModel(ctx context.Context, request DeleteModelRequestObject) (DeleteModelResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// DownloadFile implements ServerInterface
|
||||
func (*LocalAIServer) DownloadFile(w http.ResponseWriter, r *http.Request, fileId string) {
|
||||
// DownloadFile implements StrictServerInterface
|
||||
func (*LocalAIServer) DownloadFile(ctx context.Context, request DownloadFileRequestObject) (DownloadFileResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// ListFiles implements ServerInterface
|
||||
func (*LocalAIServer) ListFiles(w http.ResponseWriter, r *http.Request) {
|
||||
// ListFiles implements StrictServerInterface
|
||||
func (*LocalAIServer) ListFiles(ctx context.Context, request ListFilesRequestObject) (ListFilesResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// ListFineTuneEvents implements ServerInterface
|
||||
func (*LocalAIServer) ListFineTuneEvents(w http.ResponseWriter, r *http.Request, fineTuneId string, params ListFineTuneEventsParams) {
|
||||
// ListFineTuneEvents implements StrictServerInterface
|
||||
func (*LocalAIServer) ListFineTuneEvents(ctx context.Context, request ListFineTuneEventsRequestObject) (ListFineTuneEventsResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// ListFineTunes implements ServerInterface
|
||||
func (*LocalAIServer) ListFineTunes(w http.ResponseWriter, r *http.Request) {
|
||||
// ListFineTunes implements StrictServerInterface
|
||||
func (*LocalAIServer) ListFineTunes(ctx context.Context, request ListFineTunesRequestObject) (ListFineTunesResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// ListModels implements ServerInterface
|
||||
func (*LocalAIServer) ListModels(w http.ResponseWriter, r *http.Request) {
|
||||
// ListModels implements StrictServerInterface
|
||||
func (*LocalAIServer) ListModels(ctx context.Context, request ListModelsRequestObject) (ListModelsResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// RetrieveFile implements ServerInterface
|
||||
func (*LocalAIServer) RetrieveFile(w http.ResponseWriter, r *http.Request, fileId string) {
|
||||
// RetrieveFile implements StrictServerInterface
|
||||
func (*LocalAIServer) RetrieveFile(ctx context.Context, request RetrieveFileRequestObject) (RetrieveFileResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// RetrieveFineTune implements ServerInterface
|
||||
func (*LocalAIServer) RetrieveFineTune(w http.ResponseWriter, r *http.Request, fineTuneId string) {
|
||||
// RetrieveFineTune implements StrictServerInterface
|
||||
func (*LocalAIServer) RetrieveFineTune(ctx context.Context, request RetrieveFineTuneRequestObject) (RetrieveFineTuneResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// RetrieveModel implements ServerInterface
|
||||
func (*LocalAIServer) RetrieveModel(w http.ResponseWriter, r *http.Request, model string) {
|
||||
// RetrieveModel implements StrictServerInterface
|
||||
func (*LocalAIServer) RetrieveModel(ctx context.Context, request RetrieveModelRequestObject) (RetrieveModelResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
var _ ServerInterface = (*LocalAIServer)(nil)
|
||||
var _ StrictServerInterface = (*LocalAIServer)(nil)
|
||||
|
||||
// var _ ServerInterface = NewStrictHandler((*LocalAIServer)(nil), nil)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue