mirror of
https://github.com/mudler/LocalAI.git
synced 2025-06-30 06:30:43 +00:00
Not anywhere close to done, but getting back to this and staging some progress. Preliminary pass on loading a config and overriding params with request json.
This commit is contained in:
parent
fc3c105d42
commit
20a0cd2f66
13 changed files with 812 additions and 388 deletions
218
apiv2/config.go
218
apiv2/config.go
|
@ -2,123 +2,203 @@ package apiv2
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Name string `yaml:"name"`
|
||||
Endpoint string `yaml:"endpoint"`
|
||||
Template string `yaml:"template"`
|
||||
RequestDefaults interface{} `yaml:"request_defaults"`
|
||||
type ConfigRegistration struct {
|
||||
Endpoint string `yaml:"endpoint" json:"endpoint" mapstructure:"endpoint"`
|
||||
Model string `yaml:"model" json:"model" mapstructure:"model"`
|
||||
}
|
||||
|
||||
type ConfigMerger struct {
|
||||
configs map[string]Config
|
||||
type ConfigLocalPaths struct {
|
||||
Model string `yaml:"model" mapstructure:"model"`
|
||||
Template string `yaml:"template" mapstructure:"template"`
|
||||
}
|
||||
|
||||
type ConfigStub struct {
|
||||
Registration ConfigRegistration `yaml:"registration" mapstructure:"registration"`
|
||||
LocalPaths ConfigLocalPaths `yaml:"local_paths" mapstructure:"local_paths"`
|
||||
}
|
||||
|
||||
type SpecificConfig[RequestModel any] struct {
|
||||
ConfigStub `mapstructure:",squash"`
|
||||
RequestDefaults RequestModel `yaml:"request_defaults" mapstructure:"request_defaults"`
|
||||
}
|
||||
|
||||
// type Config struct {
|
||||
// Registration ConfigRegistration `yaml:"registration"`
|
||||
// LocalPaths ConfigLocalPaths `yaml:"local_paths"`
|
||||
// RequestDefaults interface{} `yaml:"request_defaults"`
|
||||
// }
|
||||
|
||||
type Config interface {
|
||||
GetRequestDefaults() interface{}
|
||||
GetLocalPaths() ConfigLocalPaths
|
||||
GetRegistration() ConfigRegistration
|
||||
}
|
||||
|
||||
func (sc SpecificConfig[RequestModel]) GetRequestDefaults() interface{} {
|
||||
return sc.RequestDefaults
|
||||
}
|
||||
|
||||
func (sc SpecificConfig[RequestModel]) GetLocalPaths() ConfigLocalPaths {
|
||||
return sc.LocalPaths
|
||||
}
|
||||
|
||||
func (sc SpecificConfig[RequestModel]) GetRegistration() ConfigRegistration {
|
||||
return sc.Registration
|
||||
}
|
||||
|
||||
type ConfigManager struct {
|
||||
configs map[ConfigRegistration]Config
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
func NewConfigMerger() *ConfigMerger {
|
||||
return &ConfigMerger{
|
||||
configs: make(map[string]Config),
|
||||
func NewConfigManager() *ConfigManager {
|
||||
return &ConfigManager{
|
||||
configs: make(map[ConfigRegistration]Config),
|
||||
}
|
||||
}
|
||||
func ReadConfigFile(file string) ([]*Config, error) {
|
||||
c := &[]*Config{}
|
||||
f, err := os.ReadFile(file)
|
||||
|
||||
// Private helper method doesn't enforce the mutex. This is because loading at the directory level keeps the lock up the whole time, and I like that.
|
||||
func (cm *ConfigManager) loadConfigFile(path string) (*Config, error) {
|
||||
fmt.Printf("INTERNAL loadConfigFile for %s\n", path)
|
||||
stub := ConfigStub{}
|
||||
f, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read config file: %w", err)
|
||||
}
|
||||
if err := yaml.Unmarshal(f, c); err != nil {
|
||||
if err := yaml.Unmarshal(f, &stub); err != nil {
|
||||
return nil, fmt.Errorf("cannot unmarshal config file: %w", err)
|
||||
}
|
||||
fmt.Printf("RAW STUB: %+v\n", stub)
|
||||
// fmt.Printf("DUMB SHIT: %+v\n%T\n", EndpointToRequestBodyMap[rawConfig.Registration.Endpoint], EndpointToRequestBodyMap[rawConfig.Registration.Endpoint])
|
||||
|
||||
return *c, nil
|
||||
}
|
||||
endpoint := stub.Registration.Endpoint
|
||||
|
||||
func ReadConfig(file string) (*Config, error) {
|
||||
c := &Config{}
|
||||
f, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read config file: %w", err)
|
||||
}
|
||||
if err := yaml.Unmarshal(f, c); err != nil {
|
||||
return nil, fmt.Errorf("cannot unmarshal config file: %w", err)
|
||||
// EndpointConfigMap is generated over in localai.gen.go
|
||||
// It's a map that translates a string endpoint function name to an empty SpecificConfig[T], with the type parameter for that request.
|
||||
if structType, ok := EndpointConfigMap[endpoint]; ok {
|
||||
fmt.Printf("~~ EndpointConfigMap[%s]: %+v\n", endpoint, structType)
|
||||
tmpUnmarshal := map[string]interface{}{}
|
||||
if err := yaml.Unmarshal(f, &tmpUnmarshal); err != nil {
|
||||
if e, ok := err.(*yaml.TypeError); ok {
|
||||
fmt.Println("\n!!!!!Type error:", e)
|
||||
}
|
||||
return nil, fmt.Errorf("cannot unmarshal config file for %s: %w", endpoint, err)
|
||||
}
|
||||
fmt.Printf("$$$ tmpUnmarshal: %+v\n", tmpUnmarshal)
|
||||
mapstructure.Decode(tmpUnmarshal, &structType)
|
||||
|
||||
fmt.Printf("AFTER UNMARSHAL %T\n%+v\n=======\n", structType, structType)
|
||||
|
||||
// rawConfig.RequestDefaults = structType.GetRequestDefaults()
|
||||
|
||||
cm.configs[structType.GetRegistration()] = structType
|
||||
// fmt.Printf("\n\n\n!!!!!HIT BOTTOM!!!!!!")
|
||||
return &structType, nil
|
||||
// fmt.Printf("\n\n\n!!!!!\n\n\nBIG MISS!\n\n%+v\n\n%T\n%T=====", specificStruct, specificStruct, structType)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
// for i, ts := range EndpointToRequestBodyMap {
|
||||
// fmt.Printf("%s: %+v\n", i, ts)
|
||||
// }
|
||||
|
||||
return nil, fmt.Errorf("failed to parse config for endpoint %s", endpoint)
|
||||
}
|
||||
|
||||
func (cm ConfigMerger) LoadConfigFile(file string) error {
|
||||
func (cm *ConfigManager) LoadConfigFile(path string) (*Config, error) {
|
||||
fmt.Printf("LoadConfigFile TOP for %s", path)
|
||||
|
||||
cm.Lock()
|
||||
fmt.Println("cm.Lock done")
|
||||
|
||||
defer cm.Unlock()
|
||||
fmt.Println("cm.Unlock done")
|
||||
|
||||
return cm.loadConfigFile(path)
|
||||
}
|
||||
|
||||
func (cm *ConfigManager) LoadConfigDirectory(path string) ([]ConfigRegistration, error) {
|
||||
fmt.Printf("LoadConfigDirectory TOP for %s\n", path)
|
||||
cm.Lock()
|
||||
defer cm.Unlock()
|
||||
c, err := ReadConfigFile(file)
|
||||
files, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load config file: %w", err)
|
||||
return []ConfigRegistration{}, err
|
||||
}
|
||||
fmt.Printf("os.ReadDir done, found %d files\n", len(files))
|
||||
|
||||
for _, file := range files {
|
||||
// Skip anything that isn't yaml
|
||||
if !strings.Contains(file.Name(), ".yaml") {
|
||||
continue
|
||||
}
|
||||
_, err := cm.loadConfigFile(filepath.Join(path, file.Name()))
|
||||
if err != nil {
|
||||
return []ConfigRegistration{}, err
|
||||
}
|
||||
}
|
||||
|
||||
for _, cc := range c {
|
||||
cm.configs[cc.Name] = *cc
|
||||
}
|
||||
return nil
|
||||
fmt.Printf("LoadConfigDirectory DONE %d", len(cm.configs))
|
||||
|
||||
return cm.listConfigs(), nil
|
||||
}
|
||||
|
||||
func (cm ConfigMerger) LoadConfig(file string) error {
|
||||
func (cm *ConfigManager) GetConfig(r ConfigRegistration) (Config, bool) {
|
||||
cm.Lock()
|
||||
defer cm.Unlock()
|
||||
c, err := ReadConfig(file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot read config file: %w", err)
|
||||
}
|
||||
|
||||
cm.configs[c.Name] = *c
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cm ConfigMerger) GetConfig(m string) (Config, bool) {
|
||||
cm.Lock()
|
||||
defer cm.Unlock()
|
||||
v, exists := cm.configs[m]
|
||||
v, exists := cm.configs[r]
|
||||
return v, exists
|
||||
}
|
||||
|
||||
func (cm ConfigMerger) ListConfigs() []string {
|
||||
cm.Lock()
|
||||
defer cm.Unlock()
|
||||
var res []string
|
||||
func (cm *ConfigManager) listConfigs() []ConfigRegistration {
|
||||
var res []ConfigRegistration
|
||||
for k := range cm.configs {
|
||||
res = append(res, k)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (cm ConfigMerger) LoadConfigs(path string) error {
|
||||
func (cm *ConfigManager) ListConfigs() []ConfigRegistration {
|
||||
cm.Lock()
|
||||
defer cm.Unlock()
|
||||
files, err := ioutil.ReadDir(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
// Skip templates, YAML and .keep files
|
||||
if !strings.Contains(file.Name(), ".yaml") {
|
||||
continue
|
||||
}
|
||||
c, err := ReadConfig(filepath.Join(path, file.Name()))
|
||||
if err == nil {
|
||||
cm.configs[c.Name] = *c
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return cm.listConfigs()
|
||||
}
|
||||
|
||||
// // Not sure about this one, but it seems like a decent place to stick it for an experiment at least.
|
||||
// func (cm *ConfigManager) GetTextConfigForRequest()
|
||||
|
||||
// func (cm *ConfigMerger) LoadConfigs(path string) error {
|
||||
// cm.Lock()
|
||||
// defer cm.Unlock()
|
||||
// files, err := ioutil.ReadDir(path)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// for _, file := range files {
|
||||
// // Skip templates, YAML and .keep files
|
||||
// if !strings.Contains(file.Name(), ".yaml") {
|
||||
// continue
|
||||
// }
|
||||
// c, err := ReadConfig(filepath.Join(path, file.Name()))
|
||||
// if err == nil {
|
||||
// cm.configs[ConfigLookup{Name: c.Name, Endpoint: c.Endpoint}] = *c
|
||||
// }
|
||||
// }
|
||||
|
||||
// return nil
|
||||
// }
|
||||
|
||||
// func (cm *ConfigMerger) Get
|
||||
|
||||
// func updateConfig(config *Config, input *OpenAIRequest) {
|
||||
// if input.Echo {
|
||||
// config.Echo = input.Echo
|
||||
|
|
174
apiv2/localai.go
174
apiv2/localai.go
|
@ -3,20 +3,36 @@ package apiv2
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
type LocalAIServer struct {
|
||||
configMerger *ConfigMerger
|
||||
configManager *ConfigManager
|
||||
}
|
||||
|
||||
var _ ServerInterface = (*LocalAIServer)(nil)
|
||||
|
||||
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,
|
||||
|
@ -26,39 +42,91 @@ func sendError(w http.ResponseWriter, code int, message string) {
|
|||
json.NewEncoder(w).Encode(localAiError)
|
||||
}
|
||||
|
||||
// It won't work, but it's worth a try.
|
||||
const nyiErrorMessageFormatString = "%s is not yet implemented by LocalAI\nThere is no need to contact support about this error and retrying will not help.\nExpect an update at https://github.com/go-skynet/LocalAI if this changes!"
|
||||
// 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
|
||||
}
|
||||
|
||||
// Do we want or need an additional "wontfix" template that is even stronger than this?
|
||||
const nyiDepreciatedErrorMessageFormatString = "%s is a depreciated portion of the OpenAI API, and is not yet implemented by LocalAI\nThere is no need to contact support about this error and retrying will not help."
|
||||
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")
|
||||
}
|
||||
|
||||
splitFnName := strings.Split(printCurrentFunctionName(2), ".")
|
||||
|
||||
endpointName := splitFnName[len(splitFnName)-1]
|
||||
|
||||
return server.combineRequestAndConfig(endpointName, body)
|
||||
}
|
||||
|
||||
// CancelFineTune implements ServerInterface
|
||||
func (*LocalAIServer) CancelFineTune(w http.ResponseWriter, r *http.Request, fineTuneId string) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "Fine Tune"))
|
||||
return
|
||||
}
|
||||
|
||||
// CreateAnswer implements ServerInterface
|
||||
func (*LocalAIServer) CreateAnswer(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiDepreciatedErrorMessageFormatString, "CreateAnswer"))
|
||||
return
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateChatCompletion implements ServerInterface
|
||||
func (*LocalAIServer) CreateChatCompletion(w http.ResponseWriter, r *http.Request) {
|
||||
var chatRequest CreateChatCompletionRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&chatRequest); err != nil {
|
||||
sendError(w, http.StatusBadRequest, "Invalid CreateChatCompletionRequest")
|
||||
func (server *LocalAIServer) CreateChatCompletion(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("HIT APIv2 CreateChatCompletion!")
|
||||
|
||||
request, err := server.getRequest(w, r)
|
||||
|
||||
if err != nil {
|
||||
sendError(w, http.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
// fmt.Printf("\n!!! Survived to attempt cast. BEFORE:\n\tType: %T\n\t%+v", request, request)
|
||||
|
||||
chatRequest, castSuccess := request.(CreateChatCompletionRequest)
|
||||
|
||||
if !castSuccess {
|
||||
sendError(w, http.StatusInternalServerError, "Cast Fail???")
|
||||
return
|
||||
}
|
||||
configMerger.GetConfig(chatRequest.Model)
|
||||
|
||||
fmt.Printf("\n\n!! AFTER !!\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)
|
||||
}
|
||||
}
|
||||
|
||||
// CreateClassification implements ServerInterface
|
||||
func (*LocalAIServer) CreateClassification(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiDepreciatedErrorMessageFormatString, "CreateClassification"))
|
||||
return
|
||||
}
|
||||
// switch chatRequest := requestDefault.(type) {
|
||||
// case CreateChatCompletionRequest:
|
||||
|
||||
// CreateCompletion implements ServerInterface
|
||||
func (*LocalAIServer) CreateCompletion(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -67,8 +135,7 @@ func (*LocalAIServer) CreateCompletion(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
// CreateEdit implements ServerInterface
|
||||
func (*LocalAIServer) CreateEdit(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "CreateEdit"))
|
||||
return
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateEmbedding implements ServerInterface
|
||||
|
@ -78,14 +145,12 @@ func (*LocalAIServer) CreateEmbedding(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
// CreateFile implements ServerInterface
|
||||
func (*LocalAIServer) CreateFile(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "Create File"))
|
||||
return
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateFineTune implements ServerInterface
|
||||
func (*LocalAIServer) CreateFineTune(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "Create Fine Tune"))
|
||||
return
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateImage implements ServerInterface
|
||||
|
@ -105,14 +170,7 @@ func (*LocalAIServer) CreateImageVariation(w http.ResponseWriter, r *http.Reques
|
|||
|
||||
// CreateModeration implements ServerInterface
|
||||
func (*LocalAIServer) CreateModeration(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "CreateModeration"))
|
||||
return
|
||||
}
|
||||
|
||||
// CreateSearch implements ServerInterface
|
||||
func (*LocalAIServer) CreateSearch(w http.ResponseWriter, r *http.Request, engineId string) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiDepreciatedErrorMessageFormatString, "CreateSearch"))
|
||||
return
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateTranscription implements ServerInterface
|
||||
|
@ -127,44 +185,32 @@ func (*LocalAIServer) CreateTranslation(w http.ResponseWriter, r *http.Request)
|
|||
|
||||
// DeleteFile implements ServerInterface
|
||||
func (*LocalAIServer) DeleteFile(w http.ResponseWriter, r *http.Request, fileId string) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "DeleteFile"))
|
||||
return
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// DeleteModel implements ServerInterface
|
||||
func (*LocalAIServer) DeleteModel(w http.ResponseWriter, r *http.Request, model string) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "DeleteModel"))
|
||||
return
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// DownloadFile implements ServerInterface
|
||||
func (*LocalAIServer) DownloadFile(w http.ResponseWriter, r *http.Request, fileId string) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "DownloadFile"))
|
||||
return
|
||||
}
|
||||
|
||||
// ListEngines implements ServerInterface
|
||||
func (*LocalAIServer) ListEngines(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiDepreciatedErrorMessageFormatString, "List Engines"))
|
||||
return
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// ListFiles implements ServerInterface
|
||||
func (*LocalAIServer) ListFiles(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "ListFiles"))
|
||||
return
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// ListFineTuneEvents implements ServerInterface
|
||||
func (*LocalAIServer) ListFineTuneEvents(w http.ResponseWriter, r *http.Request, fineTuneId string, params ListFineTuneEventsParams) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "List Fine Tune Events"))
|
||||
return
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// ListFineTunes implements ServerInterface
|
||||
func (*LocalAIServer) ListFineTunes(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "List Fine Tunes"))
|
||||
return
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// ListModels implements ServerInterface
|
||||
|
@ -172,25 +218,19 @@ func (*LocalAIServer) ListModels(w http.ResponseWriter, r *http.Request) {
|
|||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// RetrieveEngine implements ServerInterface
|
||||
func (*LocalAIServer) RetrieveEngine(w http.ResponseWriter, r *http.Request, engineId string) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiDepreciatedErrorMessageFormatString, "RetrieveEngine"))
|
||||
return
|
||||
}
|
||||
|
||||
// RetrieveFile implements ServerInterface
|
||||
func (*LocalAIServer) RetrieveFile(w http.ResponseWriter, r *http.Request, fileId string) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "RetrieveFile"))
|
||||
return
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// RetrieveFineTune implements ServerInterface
|
||||
func (*LocalAIServer) RetrieveFineTune(w http.ResponseWriter, r *http.Request, fineTuneId string) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "Retrieve Fine Tune"))
|
||||
return
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// RetrieveModel implements ServerInterface
|
||||
func (*LocalAIServer) RetrieveModel(w http.ResponseWriter, r *http.Request, model string) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
var _ ServerInterface = (*LocalAIServer)(nil)
|
||||
|
|
196
apiv2/localai.go.old
Normal file
196
apiv2/localai.go.old
Normal file
|
@ -0,0 +1,196 @@
|
|||
package apiv2
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type LocalAIServer struct {
|
||||
configMerger *ConfigMerger
|
||||
}
|
||||
|
||||
var _ ServerInterface = (*LocalAIServer)(nil)
|
||||
|
||||
type Error struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func sendError(w http.ResponseWriter, code int, message string) {
|
||||
localAiError := Error{
|
||||
Code: code,
|
||||
Message: message,
|
||||
}
|
||||
w.WriteHeader(code)
|
||||
json.NewEncoder(w).Encode(localAiError)
|
||||
}
|
||||
|
||||
// It won't work, but it's worth a try.
|
||||
const nyiErrorMessageFormatString = "%s is not yet implemented by LocalAI\nThere is no need to contact support about this error and retrying will not help.\nExpect an update at https://github.com/go-skynet/LocalAI if this changes!"
|
||||
|
||||
// Do we want or need an additional "wontfix" template that is even stronger than this?
|
||||
const nyiDepreciatedErrorMessageFormatString = "%s is a depreciated portion of the OpenAI API, and is not yet implemented by LocalAI\nThere is no need to contact support about this error and retrying will not help."
|
||||
|
||||
// CancelFineTune implements ServerInterface
|
||||
func (*LocalAIServer) CancelFineTune(w http.ResponseWriter, r *http.Request, fineTuneId string) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "Fine Tune"))
|
||||
return
|
||||
}
|
||||
|
||||
// CreateAnswer implements ServerInterface
|
||||
func (*LocalAIServer) CreateAnswer(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiDepreciatedErrorMessageFormatString, "CreateAnswer"))
|
||||
return
|
||||
}
|
||||
|
||||
// CreateChatCompletion implements ServerInterface
|
||||
func (*LocalAIServer) CreateChatCompletion(w http.ResponseWriter, r *http.Request) {
|
||||
var chatRequest CreateChatCompletionRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&chatRequest); err != nil {
|
||||
sendError(w, http.StatusBadRequest, "Invalid CreateChatCompletionRequest")
|
||||
return
|
||||
}
|
||||
configMerger.GetConfig(chatRequest.Model)
|
||||
}
|
||||
|
||||
// CreateClassification implements ServerInterface
|
||||
func (*LocalAIServer) CreateClassification(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiDepreciatedErrorMessageFormatString, "CreateClassification"))
|
||||
return
|
||||
}
|
||||
|
||||
// CreateCompletion implements ServerInterface
|
||||
func (*LocalAIServer) CreateCompletion(w http.ResponseWriter, r *http.Request) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateEdit implements ServerInterface
|
||||
func (*LocalAIServer) CreateEdit(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "CreateEdit"))
|
||||
return
|
||||
}
|
||||
|
||||
// CreateEmbedding implements ServerInterface
|
||||
func (*LocalAIServer) CreateEmbedding(w http.ResponseWriter, r *http.Request) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateFile implements ServerInterface
|
||||
func (*LocalAIServer) CreateFile(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "Create File"))
|
||||
return
|
||||
}
|
||||
|
||||
// CreateFineTune implements ServerInterface
|
||||
func (*LocalAIServer) CreateFineTune(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "Create Fine Tune"))
|
||||
return
|
||||
}
|
||||
|
||||
// CreateImage implements ServerInterface
|
||||
func (*LocalAIServer) CreateImage(w http.ResponseWriter, r *http.Request) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateImageEdit implements ServerInterface
|
||||
func (*LocalAIServer) CreateImageEdit(w http.ResponseWriter, r *http.Request) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateImageVariation implements ServerInterface
|
||||
func (*LocalAIServer) CreateImageVariation(w http.ResponseWriter, r *http.Request) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateModeration implements ServerInterface
|
||||
func (*LocalAIServer) CreateModeration(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "CreateModeration"))
|
||||
return
|
||||
}
|
||||
|
||||
// CreateSearch implements ServerInterface
|
||||
func (*LocalAIServer) CreateSearch(w http.ResponseWriter, r *http.Request, engineId string) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiDepreciatedErrorMessageFormatString, "CreateSearch"))
|
||||
return
|
||||
}
|
||||
|
||||
// CreateTranscription implements ServerInterface
|
||||
func (*LocalAIServer) CreateTranscription(w http.ResponseWriter, r *http.Request) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// CreateTranslation implements ServerInterface
|
||||
func (*LocalAIServer) CreateTranslation(w http.ResponseWriter, r *http.Request) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// DeleteFile implements ServerInterface
|
||||
func (*LocalAIServer) DeleteFile(w http.ResponseWriter, r *http.Request, fileId string) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "DeleteFile"))
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteModel implements ServerInterface
|
||||
func (*LocalAIServer) DeleteModel(w http.ResponseWriter, r *http.Request, model string) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "DeleteModel"))
|
||||
return
|
||||
}
|
||||
|
||||
// DownloadFile implements ServerInterface
|
||||
func (*LocalAIServer) DownloadFile(w http.ResponseWriter, r *http.Request, fileId string) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "DownloadFile"))
|
||||
return
|
||||
}
|
||||
|
||||
// ListEngines implements ServerInterface
|
||||
func (*LocalAIServer) ListEngines(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiDepreciatedErrorMessageFormatString, "List Engines"))
|
||||
return
|
||||
}
|
||||
|
||||
// ListFiles implements ServerInterface
|
||||
func (*LocalAIServer) ListFiles(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "ListFiles"))
|
||||
return
|
||||
}
|
||||
|
||||
// ListFineTuneEvents implements ServerInterface
|
||||
func (*LocalAIServer) ListFineTuneEvents(w http.ResponseWriter, r *http.Request, fineTuneId string, params ListFineTuneEventsParams) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "List Fine Tune Events"))
|
||||
return
|
||||
}
|
||||
|
||||
// ListFineTunes implements ServerInterface
|
||||
func (*LocalAIServer) ListFineTunes(w http.ResponseWriter, r *http.Request) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "List Fine Tunes"))
|
||||
return
|
||||
}
|
||||
|
||||
// ListModels implements ServerInterface
|
||||
func (*LocalAIServer) ListModels(w http.ResponseWriter, r *http.Request) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// RetrieveEngine implements ServerInterface
|
||||
func (*LocalAIServer) RetrieveEngine(w http.ResponseWriter, r *http.Request, engineId string) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiDepreciatedErrorMessageFormatString, "RetrieveEngine"))
|
||||
return
|
||||
}
|
||||
|
||||
// RetrieveFile implements ServerInterface
|
||||
func (*LocalAIServer) RetrieveFile(w http.ResponseWriter, r *http.Request, fileId string) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "RetrieveFile"))
|
||||
return
|
||||
}
|
||||
|
||||
// RetrieveFineTune implements ServerInterface
|
||||
func (*LocalAIServer) RetrieveFineTune(w http.ResponseWriter, r *http.Request, fineTuneId string) {
|
||||
sendError(w, 501, fmt.Sprintf(nyiErrorMessageFormatString, "Retrieve Fine Tune"))
|
||||
return
|
||||
}
|
||||
|
||||
// RetrieveModel implements ServerInterface
|
||||
func (*LocalAIServer) RetrieveModel(w http.ResponseWriter, r *http.Request, model string) {
|
||||
panic("unimplemented")
|
||||
}
|
14
apiv2/localai_nethttp.go
Normal file
14
apiv2/localai_nethttp.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
package apiv2
|
||||
|
||||
import "net/http"
|
||||
|
||||
func NewLocalAINetHTTPServer(configManager *ConfigManager) *LocalAIServer {
|
||||
localAI := LocalAIServer{
|
||||
configManager: configManager,
|
||||
}
|
||||
|
||||
http.Handle("/", Handler(&localAI))
|
||||
|
||||
http.ListenAndServe(":8085", nil)
|
||||
return &localAI
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue