From f35e33380c1ef5d18e51a8fa31d5f65e5563fdb6 Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Tue, 8 Oct 2024 12:37:05 -0700 Subject: [PATCH 1/8] Remove old exe names from dockerfile --- runtime/Dockerfile | 4 ---- 1 file changed, 4 deletions(-) diff --git a/runtime/Dockerfile b/runtime/Dockerfile index a778de8c..096bc4c8 100644 --- a/runtime/Dockerfile +++ b/runtime/Dockerfile @@ -26,10 +26,6 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ # copy runtime binary from the build phase COPY --from=builder /src/modus_runtime /usr/bin/modus_runtime -# create a link to the old name(s) for backwards compatibility -RUN ln -s /usr/bin/modus_runtime /usr/bin/hmruntime -RUN ln -s /usr/bin/modus_runtime /usr/bin/hypruntime - # update certificates every build RUN apt-get update && apt-get install -y --no-install-recommends \ ca-certificates \ From 3a0375aba682b4d389e84debce97b293be4ac896 Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Tue, 8 Oct 2024 13:52:49 -0700 Subject: [PATCH 2/8] Remove v1 metadata compatibility --- runtime/plugins/metadata/compat.go | 97 ------------------- .../plugins/metadata/legacy/v1/metadata.go | 88 ----------------- runtime/plugins/metadata/reader.go | 8 +- 3 files changed, 1 insertion(+), 192 deletions(-) delete mode 100644 runtime/plugins/metadata/compat.go delete mode 100644 runtime/plugins/metadata/legacy/v1/metadata.go diff --git a/runtime/plugins/metadata/compat.go b/runtime/plugins/metadata/compat.go deleted file mode 100644 index 1a7b549e..00000000 --- a/runtime/plugins/metadata/compat.go +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2024 Hypermode Inc. - * Licensed under the terms of the Apache License, Version 2.0 - * See the LICENSE file that accompanied this code for further details. - * - * SPDX-FileCopyrightText: 2024 Hypermode Inc. - * SPDX-License-Identifier: Apache-2.0 - */ - -package metadata - -import ( - "encoding/json" - "fmt" - "strings" - - v1 "github.com/hypermodeinc/modus/runtime/plugins/metadata/legacy/v1" -) - -const timeFormat = "2006-01-02T15:04:05.000Z" - -func getPluginMetadata_v1(wasmCustomSections map[string][]byte) (*Metadata, error) { - metadataJson, found := wasmCustomSections["hypermode_meta"] - if !found { - return nil, ErrMetadataNotFound - } - - md := v1.Metadata{} - err := json.Unmarshal(metadataJson, &md) - if err != nil { - return nil, fmt.Errorf("failed to parse plugin metadata: %w", err) - } - - return metadataV1toV2(&md), nil -} - -func metadataV1toV2(m *v1.Metadata) *Metadata { - - // legacy support for the deprecated "library" field - // (functions-as before v0.10.0) - sdk := m.SDK - if sdk == "" { - sdk = strings.TrimPrefix(m.Library, "@hypermode/") - } - - // convert the v1 metadata to v2 - res := Metadata{ - Plugin: m.Plugin, - SDK: sdk, - BuildId: m.BuildId, - BuildTime: m.BuildTime.UTC().Format(timeFormat), - GitRepo: m.GitRepo, - GitCommit: m.GitCommit, - FnExports: make(FunctionMap, len(m.Functions)), - FnImports: make(FunctionMap), - Types: make(TypeMap, len(m.Types)), - } - - for _, f := range m.Functions { - fn := &Function{Name: f.Name} - - fn.Parameters = make([]*Parameter, len(f.Parameters)) - for i, p := range f.Parameters { - fn.Parameters[i] = &Parameter{ - Name: p.Name, - Type: p.Type.Path, - Default: p.Default, - Optional: p.Optional, // deprecated - } - } - - if f.ReturnType.Name != "" && f.ReturnType.Name != "void" { - fn.Results = []*Result{{Type: f.ReturnType.Path}} - } - - res.FnExports[f.Name] = fn - } - - for _, t := range m.Types { - td := &TypeDefinition{ - Name: t.Path, - Id: t.Id, - Fields: make([]*Field, len(t.Fields)), - } - - for i, f := range t.Fields { - td.Fields[i] = &Field{ - Name: f.Name, - Type: f.Type.Path, - } - } - - res.Types[td.Name] = td - } - - return &res -} diff --git a/runtime/plugins/metadata/legacy/v1/metadata.go b/runtime/plugins/metadata/legacy/v1/metadata.go deleted file mode 100644 index cf2545fb..00000000 --- a/runtime/plugins/metadata/legacy/v1/metadata.go +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2024 Hypermode Inc. - * Licensed under the terms of the Apache License, Version 2.0 - * See the LICENSE file that accompanied this code for further details. - * - * SPDX-FileCopyrightText: 2024 Hypermode Inc. - * SPDX-License-Identifier: Apache-2.0 - */ - -package v1 - -import ( - "time" - - "github.com/tidwall/gjson" -) - -type Metadata struct { - Plugin string `json:"plugin"` - SDK string `json:"sdk"` - Library string `json:"library"` // deprecated - BuildId string `json:"buildId"` - BuildTime time.Time `json:"buildTs"` - GitRepo string `json:"gitRepo"` - GitCommit string `json:"gitCommit"` - Functions []*Function `json:"functions"` - Types []*TypeDefinition `json:"types"` -} - -type Function struct { - Name string `json:"name"` - Parameters []*Parameter `json:"parameters"` - ReturnType *TypeInfo `json:"returnType"` -} - -type TypeDefinition struct { - Id uint32 `json:"id"` - Path string `json:"path"` - Name string `json:"name"` - Fields []*Field `json:"fields"` -} - -type Parameter struct { - Name string `json:"name"` - Type *TypeInfo `json:"type"` - Optional bool `json:"optional"` // deprecated - Default *any `json:"default"` -} - -type Field struct { - Name string `json:"name"` - Type *TypeInfo `json:"type"` -} - -type TypeInfo struct { - Name string `json:"name"` - Path string `json:"path"` -} - -func (p *Parameter) UnmarshalJSON(data []byte) error { - - // We need to manually unmarshal the JSON to distinguish between a null default - // value and the absence of a default value. - - gjson.ParseBytes(data).ForEach(func(key, value gjson.Result) bool { - switch key.String() { - case "name": - p.Name = value.String() - case "type": - p.Type = &TypeInfo{ - Name: value.Get("name").String(), - Path: value.Get("path").String(), - } - case "optional": - p.Optional = value.Bool() - case "default": - val := value.Value() - if val == nil { - p.Default = new(any) - } else { - p.Default = &val - } - } - return true - }) - - return nil -} diff --git a/runtime/plugins/metadata/reader.go b/runtime/plugins/metadata/reader.go index 06a7f2de..c42eab50 100644 --- a/runtime/plugins/metadata/reader.go +++ b/runtime/plugins/metadata/reader.go @@ -26,8 +26,6 @@ func GetMetadata(wasmCustomSections map[string][]byte) (*Metadata, error) { switch ver { case MetadataVersion: // current version return getPluginMetadata(wasmCustomSections) - case 1: - return getPluginMetadata_v1(wasmCustomSections) default: return nil, fmt.Errorf("unsupported plugin metadata version: %d", ver) } @@ -35,11 +33,7 @@ func GetMetadata(wasmCustomSections map[string][]byte) (*Metadata, error) { func getPluginMetadataVersion(wasmCustomSections map[string][]byte) (byte, error) { verData, found := wasmCustomSections["hypermode_version"] - if !found { - return 1, nil // version 1 did not have a version section - } - - if len(verData) != 1 { + if !found || len(verData) != 1 { return 0, errors.New("failed to parse plugin metadata version") } From 681b47a46aa224a4e1f1725bb625604f099b2a3a Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Tue, 8 Oct 2024 13:53:58 -0700 Subject: [PATCH 3/8] Remove deprecated "optional" parameter flag --- runtime/graphql/schemagen/schemagen.go | 9 --------- runtime/langsupport/executionplan.go | 14 +------------- runtime/plugins/metadata/metadata.go | 7 +++---- 3 files changed, 4 insertions(+), 26 deletions(-) diff --git a/runtime/graphql/schemagen/schemagen.go b/runtime/graphql/schemagen/schemagen.go index a7afec9d..87c4c1bd 100644 --- a/runtime/graphql/schemagen/schemagen.go +++ b/runtime/graphql/schemagen/schemagen.go @@ -371,15 +371,6 @@ func convertParameters(parameters []*metadata.Parameter, lti langsupport.Languag return nil, err } - // maintain compatibility with the deprecated "optional" field - if p.Optional { - output[i] = &ParameterSignature{ - Name: p.Name, - Type: strings.TrimSuffix(t, "!"), - } - continue - } - output[i] = &ParameterSignature{ Name: p.Name, Type: t, diff --git a/runtime/langsupport/executionplan.go b/runtime/langsupport/executionplan.go index 654200e5..1aedaea4 100644 --- a/runtime/langsupport/executionplan.go +++ b/runtime/langsupport/executionplan.go @@ -147,20 +147,12 @@ func (plan *executionPlan) getWasmParameters(ctx context.Context, wa WasmAdapter cleaner = utils.NewCleanerN(len(plan.ParamHandlers())) } - var mask uint64 - var has_opt bool handlers := plan.ParamHandlers() for i, p := range plan.FnMetadata().Parameters { val, found := parameters[p.Name] - if found { - mask |= 1 << i - } else if p.Default != nil { + if !found && p.Default != nil { val = *p.Default - mask |= 1 << i - } else if p.Optional { - has_opt = true - continue } encVals, cln, err := handlers[i].Encode(ctx, wa, val) @@ -172,10 +164,6 @@ func (plan *executionPlan) getWasmParameters(ctx context.Context, wa WasmAdapter paramVals = append(paramVals, encVals...) } - if has_opt { - paramVals = append(paramVals, mask) - } - return paramVals, cleaner, nil } diff --git a/runtime/plugins/metadata/metadata.go b/runtime/plugins/metadata/metadata.go index 7e278b40..1121fccd 100644 --- a/runtime/plugins/metadata/metadata.go +++ b/runtime/plugins/metadata/metadata.go @@ -51,10 +51,9 @@ type TypeDefinition struct { } type Parameter struct { - Name string `json:"name"` - Type string `json:"type"` - Default *any `json:"default,omitempty"` - Optional bool `json:"-"` // deprecated + Name string `json:"name"` + Type string `json:"type"` + Default *any `json:"default,omitempty"` } type Result struct { From 21aac68daf683d9eff1b25b1e472549ff67f9fe3 Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Tue, 8 Oct 2024 13:54:32 -0700 Subject: [PATCH 4/8] Remove compatibility shims for imports --- .../compatibility/compatibility.go | 273 ------------------ .../compatibility/compatibility_test.go | 78 ----- runtime/plugins/plugins.go | 61 +--- 3 files changed, 13 insertions(+), 399 deletions(-) delete mode 100644 runtime/hostfunctions/compatibility/compatibility.go delete mode 100644 runtime/hostfunctions/compatibility/compatibility_test.go diff --git a/runtime/hostfunctions/compatibility/compatibility.go b/runtime/hostfunctions/compatibility/compatibility.go deleted file mode 100644 index f18e5c34..00000000 --- a/runtime/hostfunctions/compatibility/compatibility.go +++ /dev/null @@ -1,273 +0,0 @@ -/* - * Copyright 2024 Hypermode Inc. - * Licensed under the terms of the Apache License, Version 2.0 - * See the LICENSE file that accompanied this code for further details. - * - * SPDX-FileCopyrightText: 2024 Hypermode Inc. - * SPDX-License-Identifier: Apache-2.0 - */ - -package compatibility - -import ( - "fmt" - - "github.com/hypermodeinc/modus/runtime/plugins/metadata" -) - -// This is a compatibility shim for host functions that are not referenced in the metadata. -// It is only needed to allow previous AssemblyScript SDK versions to work with the latest runtime. -// This should be removed when all plugins are updated to the latest SDK version that uses the V2 metadata format. - -func GetImportMetadataShim(fnName string) (fn *metadata.Function, err error) { - switch fnName { - case "hypermode.log": - fn = metadata.NewFunction(fnName). - WithParameter("level", "~lib/string/String"). - WithParameter("message", "~lib/string/String") - - /* Model function imports */ - - case "hypermode.lookupModel": - fn = metadata.NewFunction(fnName). - WithParameter("modelName", "~lib/string/String"). - WithResult("~lib/@hypermode/models-as/index/ModelInfo") - - case "hypermode.invokeModel": - fn = metadata.NewFunction(fnName). - WithParameter("modelName", "~lib/string/String"). - WithParameter("input", "~lib/string/String"). - WithResult("~lib/string/String | null") - - /* HTTP, GraphQL, and SQL function imports */ - - case "hypermode.httpFetch": - fn = metadata.NewFunction(fnName). - WithParameter("url", "~lib/@hypermode/functions-as/assembly/http/Request"). - WithResult("~lib/@hypermode/functions-as/assembly/http/Response") - - case "hypermode.executeGQL": - fn = metadata.NewFunction(fnName). - WithParameter("hostName", "~lib/string/String"). - WithParameter("statement", "~lib/string/String"). - WithParameter("variables", "~lib/string/String"). - WithResult("~lib/string/String") - - case "hypermode.databaseQuery": - fn = metadata.NewFunction(fnName). - WithParameter("hostName", "~lib/string/String"). - WithParameter("dbType", "~lib/string/String"). - WithParameter("statement", "~lib/string/String"). - WithParameter("paramsJson", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/database/HostQueryResponse") - - /* Dgraph function imports */ - - case "hypermode.dgraphAlterSchema": - fn = metadata.NewFunction(fnName). - WithParameter("hostName", "~lib/string/String"). - WithParameter("schema", "~lib/string/String"). - WithResult("~lib/string/String") - - case "hypermode.dgraphDropAll": - fn = metadata.NewFunction(fnName). - WithParameter("hostName", "~lib/string/String"). - WithResult("~lib/string/String") - - case "hypermode.dgraphDropAttr": - fn = metadata.NewFunction(fnName). - WithParameter("hostName", "~lib/string/String"). - WithParameter("attr", "~lib/string/String"). - WithResult("~lib/string/String") - - case "hypermode.executeDQL": - fn = metadata.NewFunction(fnName). - WithParameter("hostName", "~lib/string/String"). - WithParameter("request", "~lib/@hypermode/functions-as/assembly/dgraph/Request"). - WithResult("~lib/@hypermode/functions-as/assembly/dgraph/Response") - - /* Collection function imports */ - - case "hypermode.computeDistance_v2": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("key1", "~lib/string/String"). - WithParameter("key2", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionSearchResultObject") - - case "hypermode.deleteFromCollection_v2": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithParameter("key", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionMutationResult") - - case "hypermode.getNamespacesFromCollection": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithResult("~lib/array/Array<~lib/string/String>") - - case "hypermode.getTextFromCollection_v2": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithParameter("key", "~lib/string/String"). - WithResult("~lib/string/String") - - case "hypermode.getTextsFromCollection_v2": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithResult("~lib/map/Map<~lib/string/String,~lib/string/String>") - - case "hypermode.getVector": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("key", "~lib/string/String"). - WithResult("~lib/array/Array") - - case "hypermode.getLabels": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithParameter("key", "~lib/string/String"). - WithResult("~lib/array/Array<~lib/string/String>") - - case "hypermode.nnClassifyCollection_v2": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("text", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionClassificationResult") - - case "hypermode.recomputeSearchMethod_v2": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/SearchMethodMutationResult") - - case "hypermode.searchCollection_v2": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespaces", "~lib/array/Array<~lib/string/String>"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("text", "~lib/string/String"). - WithParameter("limit", "i32"). - WithParameter("returnText", "bool"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionSearchResult") - - case "hypermode.searchCollectionByVector": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespaces", "~lib/array/Array<~lib/string/String>"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("vector", "~lib/array/Array"). - WithParameter("limit", "i32"). - WithParameter("returnText", "bool"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionSearchResult") - - case "hypermode.upsertToCollection_v2": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithParameter("keys", "~lib/array/Array<~lib/string/String>"). - WithParameter("texts", "~lib/array/Array<~lib/string/String>"). - WithParameter("labels", "~lib/array/Array<~lib/array/Array<~lib/string/String>>"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionMutationResult") - - /* Older collection function imports */ - - case "hypermode.computeDistance": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("key1", "~lib/string/String"). - WithParameter("key2", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionSearchResultObject") - - case "hypermode.computeSimilarity": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("key1", "~lib/string/String"). - WithParameter("key2", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionSearchResultObject") - - case "hypermode.deleteFromCollection": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("key", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionMutationResult") - - case "hypermode.getTextFromCollection": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("key", "~lib/string/String"). - WithResult("~lib/string/String") - - case "hypermode.getTextsFromCollection": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithResult("~lib/map/Map<~lib/string/String,~lib/string/String>") - - case "hypermode.nnClassifyCollection": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("text", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionClassificationResult") - - case "hypermode.recomputeSearchMethod": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/SearchMethodMutationResult") - - case "hypermode.searchCollection": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("text", "~lib/string/String"). - WithParameter("limit", "i32"). - WithParameter("returnText", "bool"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionSearchResult") - - case "hypermode.upsertToCollection": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("keys", "~lib/array/Array<~lib/string/String>"). - WithParameter("texts", "~lib/array/Array<~lib/string/String>"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionMutationResult") - - /* Legacy model function imports */ - - case "hypermode.invokeClassifier": - fn = metadata.NewFunction(fnName). - WithParameter("modelName", "~lib/string/String"). - WithParameter("sentenceMap", "~lib/map/Map<~lib/string/String,~lib/string/String>"). - WithResult("~lib/map/Map<~lib/string/String,~lib/map/Map<~lib/string/String,f32>>") - - case "hypermode.computeEmbedding": - fn = metadata.NewFunction(fnName). - WithParameter("modelName", "~lib/string/String"). - WithParameter("sentenceMap", "~lib/map/Map<~lib/string/String,~lib/string/String>"). - WithResult("~lib/map/Map<~lib/string/String,~lib/array/Array>") - - case "hypermode.invokeTextGenerator": - fn = metadata.NewFunction(fnName). - WithParameter("modelName", "~lib/string/String"). - WithParameter("instruction", "~lib/string/String"). - WithParameter(("format"), "~lib/string/String"). - WithResult("~lib/string/String") - - default: - return nil, fmt.Errorf("unknown host function: %s", fnName) - } - return fn, nil -} diff --git a/runtime/hostfunctions/compatibility/compatibility_test.go b/runtime/hostfunctions/compatibility/compatibility_test.go deleted file mode 100644 index 3291d0c2..00000000 --- a/runtime/hostfunctions/compatibility/compatibility_test.go +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2024 Hypermode Inc. - * Licensed under the terms of the Apache License, Version 2.0 - * See the LICENSE file that accompanied this code for further details. - * - * SPDX-FileCopyrightText: 2024 Hypermode Inc. - * SPDX-License-Identifier: Apache-2.0 - */ - -package compatibility_test - -import ( - "context" - "testing" - - "github.com/hypermodeinc/modus/runtime/functions" - "github.com/hypermodeinc/modus/runtime/hostfunctions" - "github.com/hypermodeinc/modus/runtime/hostfunctions/compatibility" - "github.com/hypermodeinc/modus/runtime/plugins" - "github.com/hypermodeinc/modus/runtime/utils" - "github.com/hypermodeinc/modus/runtime/wasmhost" - - "github.com/tetratelabs/wazero" - wasm "github.com/tetratelabs/wazero/api" -) - -func Test_HostFunctionsHaveCompatibilityShim(t *testing.T) { - - registrations := hostfunctions.GetRegistrations() - - host := &mockWasmHost{} - for _, reg := range registrations { - if err := reg(host); err != nil { - t.Errorf("Error registering host function: %v", err) - } - } - - for _, name := range host.hostFunctions { - fn, err := compatibility.GetImportMetadataShim(name) - if err != nil { - t.Errorf("Missing import metadata shim for host function: %v", err) - } else if fn == nil { - t.Errorf("Invalid import metadata shim for host function: %v", name) - } - } - -} - -type mockWasmHost struct { - hostFunctions []string -} - -func (m *mockWasmHost) RegisterHostFunction(modName, funcName string, fn any, opts ...wasmhost.HostFunctionOption) error { - m.hostFunctions = append(m.hostFunctions, modName+"."+funcName) - return nil -} - -func (m *mockWasmHost) CallFunction(ctx context.Context, fnInfo functions.FunctionInfo, parameters map[string]any) (wasmhost.ExecutionInfo, error) { - panic("not implemented") -} -func (m *mockWasmHost) CallFunctionByName(ctx context.Context, fnName string, paramValues ...any) (wasmhost.ExecutionInfo, error) { - panic("not implemented") -} -func (m *mockWasmHost) Close(ctx context.Context) { - panic("not implemented") -} -func (m *mockWasmHost) CompileModule(ctx context.Context, bytes []byte) (wazero.CompiledModule, error) { - panic("not implemented") -} -func (m *mockWasmHost) GetFunctionInfo(fnName string) (functions.FunctionInfo, error) { - panic("not implemented") -} -func (m *mockWasmHost) GetFunctionRegistry() functions.FunctionRegistry { - panic("not implemented") -} -func (m *mockWasmHost) GetModuleInstance(ctx context.Context, plugin *plugins.Plugin, buffers utils.OutputBuffers) (wasm.Module, error) { - panic("not implemented") -} diff --git a/runtime/plugins/plugins.go b/runtime/plugins/plugins.go index f3a73cf4..d812dd3e 100644 --- a/runtime/plugins/plugins.go +++ b/runtime/plugins/plugins.go @@ -12,16 +12,14 @@ package plugins import ( "context" "fmt" - "strings" - "github.com/hypermodeinc/modus/runtime/hostfunctions/compatibility" "github.com/hypermodeinc/modus/runtime/langsupport" "github.com/hypermodeinc/modus/runtime/languages" - "github.com/hypermodeinc/modus/runtime/logger" "github.com/hypermodeinc/modus/runtime/plugins/metadata" "github.com/hypermodeinc/modus/runtime/utils" "github.com/tetratelabs/wazero" + wasm "github.com/tetratelabs/wazero/api" ) type Plugin struct { @@ -62,23 +60,25 @@ func NewPlugin(ctx context.Context, cm wazero.CompiledModule, filename string, m plans[fnName] = plan } - warn := true + importsMap := make(map[string]wasm.FunctionDefinition, len(imports)) for _, fnDef := range imports { - modName, fnName, _ := fnDef.Import() - impName := fmt.Sprintf("%s.%s", modName, fnName) + if modName, fnName, ok := fnDef.Import(); ok { + importName := modName + "." + fnName + importsMap[importName] = fnDef + } + } - fnMeta, err := getImportMetadata(ctx, modName, fnName, md, &warn) - if err != nil { - return nil, err - } else if fnMeta == nil { - continue + for importName, fnMeta := range md.FnImports { + fnDef, ok := importsMap[importName] + if !ok { + return nil, fmt.Errorf("no wasm function definition found for %s", importName) } plan, err := planner.GetPlan(ctx, fnMeta, fnDef) if err != nil { - return nil, fmt.Errorf("failed to get execution plan for %s: %w", impName, err) + return nil, fmt.Errorf("failed to get execution plan for %s: %w", importName, err) } - plans[impName] = plan + plans[importName] = plan } plugin := &Plugin{ @@ -93,41 +93,6 @@ func NewPlugin(ctx context.Context, cm wazero.CompiledModule, filename string, m return plugin, nil } -func getImportMetadata(ctx context.Context, modName, fnName string, md *metadata.Metadata, warn *bool) (*metadata.Function, error) { - impName := fmt.Sprintf("%s.%s", modName, fnName) - if fnMeta, ok := md.FnImports[impName]; ok { - return fnMeta, nil - } - - if modName == "hypermode" { - if *warn { - *warn = false - logger.Warn(ctx). - Str("plugin", md.Name()). - Str("build_id", md.BuildId). - Msg("Function imports are missing from the metadata. Using compatibility shims. Please update your SDK to the latest version.") - } - if fnMeta, err := compatibility.GetImportMetadataShim(impName); err != nil { - return nil, fmt.Errorf("error creating compatibility shim for %s: %w", impName, err) - } else { - return fnMeta, nil - } - } else if shouldIgnoreModule(modName) { - return nil, nil - } - - return nil, fmt.Errorf("no metadata found for import %s", impName) -} - -func shouldIgnoreModule(name string) bool { - switch name { - case "wasi_snapshot_preview1", "wasi", "env", "runtime", "syscall", "test": - return true - } - - return strings.HasPrefix(name, "wasi_") -} - func (p *Plugin) NameAndVersion() (name string, version string) { return p.Metadata.NameAndVersion() } From 9d228ef28b8895ad9beffa8e59dc37ea0a465c69 Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Tue, 8 Oct 2024 20:57:25 -0700 Subject: [PATCH 5/8] Remove legacy host functions --- runtime/hostfunctions/collections.go | 104 ------------------ runtime/hostfunctions/legacy.go | 45 -------- runtime/models/legacymodels/classifier.go | 54 --------- runtime/models/legacymodels/common.go | 57 ---------- runtime/models/legacymodels/embeddings.go | 35 ------ .../models/legacymodels/legacymodels_test.go | 87 --------------- runtime/models/legacymodels/openai.go | 101 ----------------- runtime/models/legacymodels/textgeneration.go | 72 ------------ sdk/assemblyscript/src/assembly/compat.ts | 43 -------- 9 files changed, 598 deletions(-) delete mode 100644 runtime/hostfunctions/legacy.go delete mode 100644 runtime/models/legacymodels/classifier.go delete mode 100644 runtime/models/legacymodels/common.go delete mode 100644 runtime/models/legacymodels/embeddings.go delete mode 100644 runtime/models/legacymodels/legacymodels_test.go delete mode 100644 runtime/models/legacymodels/openai.go delete mode 100644 runtime/models/legacymodels/textgeneration.go delete mode 100644 sdk/assemblyscript/src/assembly/compat.ts diff --git a/runtime/hostfunctions/collections.go b/runtime/hostfunctions/collections.go index c7a14afb..2a96817b 100644 --- a/runtime/hostfunctions/collections.go +++ b/runtime/hostfunctions/collections.go @@ -10,18 +10,13 @@ package hostfunctions import ( - "context" "fmt" "github.com/hypermodeinc/modus/runtime/collections" ) func init() { - registerCollectionsHostFunctions() - registerLegacyCollectionsHostFunctions() -} -func registerCollectionsHostFunctions() { registerHostFunction("hypermode", "computeDistance_v2", collections.ComputeDistance, withCancelledMessage("Cancelled computing distance."), withErrorMessage("Error computing distance."), @@ -108,102 +103,3 @@ func registerCollectionsHostFunctions() { return fmt.Sprintf("Collection: %s, Namespace: %s, Keys: %v", collectionName, namespace, keys) })) } - -func registerLegacyCollectionsHostFunctions() { - - // Support functions from older SDK versions. - // Each of these function wrappers must maintain the original signature. - // We can remove these when we can be sure that nobody is using them. - - registerHostFunction("hypermode", "computeDistance", - func(ctx context.Context, collectionName, searchMethod, id1, id2 string) (*collections.CollectionSearchResultObject, error) { - return collections.ComputeDistance(ctx, collectionName, "", searchMethod, id1, id2) - }, - withCancelledMessage("Cancelled computing distance."), - withErrorMessage("Error computing distance."), - withMessageDetail(func(collectionName, searchMethod string) string { - return fmt.Sprintf("Collection: %s, Method: %s", collectionName, searchMethod) - })) - - registerHostFunction("hypermode", "computeSimilarity", - func(ctx context.Context, collectionName, searchMethod, id1, id2 string) (*collections.CollectionSearchResultObject, error) { - return collections.ComputeDistance(ctx, collectionName, "", searchMethod, id1, id2) - }, - withCancelledMessage("Cancelled computing similarity."), - withErrorMessage("Error computing similarity."), - withMessageDetail(func(collectionName, searchMethod string) string { - return fmt.Sprintf("Collection: %s, Method: %s", collectionName, searchMethod) - })) - - registerHostFunction("hypermode", "deleteFromCollection", - func(ctx context.Context, collectionName, key string) (*collections.CollectionMutationResult, error) { - return collections.DeleteFromCollection(ctx, collectionName, "", key) - }, - withCancelledMessage("Cancelled deleting from collection."), - withErrorMessage("Error deleting from collection."), - withMessageDetail(func(collectionName, key string) string { - return fmt.Sprintf("Collection: %s, Key: %s", collectionName, key) - })) - - registerHostFunction("hypermode", "getTextFromCollection", - func(ctx context.Context, collectionName, key string) (string, error) { - return collections.GetTextFromCollection(ctx, collectionName, "", key) - }, - withCancelledMessage("Cancelled getting text from collection."), - withErrorMessage("Error getting text from collection."), - withMessageDetail(func(collectionName, key string) string { - return fmt.Sprintf("Collection: %s, Key: %s", collectionName, key) - })) - - registerHostFunction("hypermode", "getTextsFromCollection", - func(ctx context.Context, collectionName string) (map[string]string, error) { - return collections.GetTextsFromCollection(ctx, collectionName, "") - }, - withCancelledMessage("Cancelled getting texts from collection."), - withErrorMessage("Error getting texts from collection."), - withMessageDetail(func(collectionName string) string { - return fmt.Sprintf("Collection: %s", collectionName) - })) - - registerHostFunction("hypermode", "nnClassifyCollection", - func(ctx context.Context, collectionName, searchMethod, text string) (*collections.CollectionClassificationResult, error) { - return collections.NnClassify(ctx, collectionName, "", searchMethod, text) - }, - withCancelledMessage("Cancelled classification."), - withErrorMessage("Error during classification."), - withMessageDetail(func(collectionName, searchMethod string) string { - return fmt.Sprintf("Collection: %s, Method: %s", collectionName, searchMethod) - })) - - registerHostFunction("hypermode", "recomputeSearchMethod", - func(ctx context.Context, collectionName, searchMethod string) (*collections.SearchMethodMutationResult, error) { - return collections.RecomputeSearchMethod(ctx, collectionName, "", searchMethod) - }, - withStartingMessage("Starting recomputing search method for collection."), - withCompletedMessage("Completed recomputing search method for collection."), - withCancelledMessage("Cancelled recomputing search method for collection."), - withErrorMessage("Error recomputing search method for collection."), - withMessageDetail(func(collectionName, searchMethod string) string { - return fmt.Sprintf("Collection: %s, Method: %s", collectionName, searchMethod) - })) - - registerHostFunction("hypermode", "searchCollection", - func(ctx context.Context, collectionName, searchMethod, text string, limit int32, returnText bool) (*collections.CollectionSearchResult, error) { - return collections.SearchCollection(ctx, collectionName, nil, searchMethod, text, limit, returnText) - }, - withCancelledMessage("Cancelled searching collection."), - withErrorMessage("Error searching collection."), - withMessageDetail(func(collectionName, searchMethod string) string { - return fmt.Sprintf("Collection: %s, Method: %s", collectionName, searchMethod) - })) - - registerHostFunction("hypermode", "upsertToCollection", - func(ctx context.Context, collectionName string, keys, texts []string) (*collections.CollectionMutationResult, error) { - return collections.UpsertToCollection(ctx, collectionName, "", keys, texts, nil) - }, - withCancelledMessage("Cancelled collection upsert."), - withErrorMessage("Error upserting to collection."), - withMessageDetail(func(collectionName string, keys []string) string { - return fmt.Sprintf("Collection: %s, Keys: %v", collectionName, keys) - })) -} diff --git a/runtime/hostfunctions/legacy.go b/runtime/hostfunctions/legacy.go deleted file mode 100644 index 827b75ec..00000000 --- a/runtime/hostfunctions/legacy.go +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2024 Hypermode Inc. - * Licensed under the terms of the Apache License, Version 2.0 - * See the LICENSE file that accompanied this code for further details. - * - * SPDX-FileCopyrightText: 2024 Hypermode Inc. - * SPDX-License-Identifier: Apache-2.0 - */ - -package hostfunctions - -import ( - "fmt" - - "github.com/hypermodeinc/modus/runtime/models/legacymodels" -) - -func init() { - registerHostFunction("hypermode", "invokeClassifier", legacymodels.InvokeClassifier, - withStartingMessage("Invoking model."), - withCompletedMessage("Completed model invocation."), - withCancelledMessage("Cancelled model invocation."), - withErrorMessage("Error invoking model."), - withMessageDetail(func(modelName string) string { - return fmt.Sprintf("Model: %s", modelName) - })) - - registerHostFunction("hypermode", "computeEmbedding", legacymodels.ComputeEmbedding, - withStartingMessage("Invoking model."), - withCompletedMessage("Completed model invocation."), - withCancelledMessage("Cancelled model invocation."), - withErrorMessage("Error invoking model."), - withMessageDetail(func(modelName string) string { - return fmt.Sprintf("Model: %s", modelName) - })) - - registerHostFunction("hypermode", "invokeTextGenerator", legacymodels.InvokeTextGenerator, - withStartingMessage("Invoking model."), - withCompletedMessage("Completed model invocation."), - withCancelledMessage("Cancelled model invocation."), - withErrorMessage("Error invoking model."), - withMessageDetail(func(modelName string) string { - return fmt.Sprintf("Model: %s", modelName) - })) -} diff --git a/runtime/models/legacymodels/classifier.go b/runtime/models/legacymodels/classifier.go deleted file mode 100644 index 28a305e7..00000000 --- a/runtime/models/legacymodels/classifier.go +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2024 Hypermode Inc. - * Licensed under the terms of the Apache License, Version 2.0 - * See the LICENSE file that accompanied this code for further details. - * - * SPDX-FileCopyrightText: 2024 Hypermode Inc. - * SPDX-License-Identifier: Apache-2.0 - */ - -package legacymodels - -import ( - "context" - "errors" - - "github.com/hypermodeinc/modus/runtime/models" -) - -func InvokeClassifier(ctx context.Context, modelName string, sentenceMap map[string]string) (map[string]map[string]float32, error) { - model, err := models.GetModel(modelName) - if err != nil { - return nil, err - } - - result, err := postToModelEndpoint[classifierResult](ctx, model, sentenceMap) - if err != nil { - return nil, err - } - - if len(result) == 0 { - return nil, errors.New("empty result returned from model") - } - - resultMap := make(map[string]map[string]float32) - for k, v := range result { - resultMap[k] = make(map[string]float32) - for _, p := range v.Probabilities { - resultMap[k][p.Label] = p.Probability - } - } - - return resultMap, nil -} - -type classifierResult struct { - Label string `json:"label"` - Confidence float32 `json:"confidence"` - Probabilities []classifierLabel `json:"probabilities"` -} - -type classifierLabel struct { - Label string `json:"label"` - Probability float32 `json:"probability"` -} diff --git a/runtime/models/legacymodels/common.go b/runtime/models/legacymodels/common.go deleted file mode 100644 index 2c1072df..00000000 --- a/runtime/models/legacymodels/common.go +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2024 Hypermode Inc. - * Licensed under the terms of the Apache License, Version 2.0 - * See the LICENSE file that accompanied this code for further details. - * - * SPDX-FileCopyrightText: 2024 Hypermode Inc. - * SPDX-License-Identifier: Apache-2.0 - */ - -package legacymodels - -import ( - "context" - "fmt" - - "github.com/hypermodeinc/modus/pkg/manifest" - "github.com/hypermodeinc/modus/runtime/models" - "github.com/hypermodeinc/modus/runtime/utils" -) - -type predictionResult[T any] struct { - Predictions []T `json:"predictions"` -} - -func postToModelEndpoint[TResult any](ctx context.Context, model *manifest.ModelInfo, sentenceMap map[string]string) (map[string]TResult, error) { - span, ctx := utils.NewSentrySpanForCurrentFunc(ctx) - defer span.Finish() - - // self hosted models takes in array, can optimize for parallelizing later - keys, sentences := []string{}, []string{} - - for k, v := range sentenceMap { - // create a map of sentences to send to the model - sentences = append(sentences, v) - // create a list of keys to map the results back to the original sentences - keys = append(keys, k) - } - // create a map of sentences to send to the model - req := map[string][]string{"instances": sentences} - - res, err := models.PostToModelEndpoint[predictionResult[TResult]](ctx, model, req) - if err != nil { - return nil, err - } - - if len(res.Predictions) != len(keys) { - return nil, fmt.Errorf("number of predictions does not match number of sentences") - } - - // map the results back to the original sentences - result := make(map[string]TResult) - for i, v := range res.Predictions { - result[keys[i]] = v - } - - return result, nil -} diff --git a/runtime/models/legacymodels/embeddings.go b/runtime/models/legacymodels/embeddings.go deleted file mode 100644 index c9566d06..00000000 --- a/runtime/models/legacymodels/embeddings.go +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2024 Hypermode Inc. - * Licensed under the terms of the Apache License, Version 2.0 - * See the LICENSE file that accompanied this code for further details. - * - * SPDX-FileCopyrightText: 2024 Hypermode Inc. - * SPDX-License-Identifier: Apache-2.0 - */ - -package legacymodels - -import ( - "context" - "errors" - - "github.com/hypermodeinc/modus/runtime/models" -) - -func ComputeEmbedding(ctx context.Context, modelName string, sentenceMap map[string]string) (map[string][]float64, error) { - model, err := models.GetModel(modelName) - if err != nil { - return nil, err - } - - result, err := postToModelEndpoint[[]float64](ctx, model, sentenceMap) - if err != nil { - return nil, err - } - - if len(result) == 0 { - return nil, errors.New("empty result returned from model") - } - - return result, nil -} diff --git a/runtime/models/legacymodels/legacymodels_test.go b/runtime/models/legacymodels/legacymodels_test.go deleted file mode 100644 index 5242d2d3..00000000 --- a/runtime/models/legacymodels/legacymodels_test.go +++ /dev/null @@ -1,87 +0,0 @@ -package legacymodels - -import ( - "context" - "encoding/json" - "net/http" - "net/http/httptest" - "os" - "testing" - - "github.com/hypermodeinc/modus/pkg/manifest" - "github.com/hypermodeinc/modus/runtime/manifestdata" - "github.com/hypermodeinc/modus/runtime/secrets" - - "github.com/stretchr/testify/assert" -) - -const ( - testModelName = "test" - testHostName = "mock" -) - -type requestBody struct { - Instances []string `json:"instances"` -} -type mockModelResponse struct { - Predictions []string `json:"predictions"` -} - -// TestMain runs in the main goroutine and can do whatever setup and teardown is necessary around a call to m.Run -func TestMain(m *testing.M) { - secrets.Initialize(context.Background()) - manifestdata.SetManifest(&manifest.Manifest{ - Models: map[string]manifest.ModelInfo{ - testModelName: { - Name: testModelName, - SourceModel: "", - Provider: "", - Host: testHostName, - }, - }, - Hosts: map[string]manifest.HostInfo{ - testHostName: manifest.HTTPHostInfo{ - Name: testHostName, - Endpoint: "", - }, - }, - }) - - os.Exit(m.Run()) -} - -func TestPostExternalModelEndpoint(t *testing.T) { - - // Create an http handler that simply echoes the input strings - handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var b requestBody - err := json.NewDecoder(r.Body).Decode(&b) - assert.NoError(t, err) - - resp := mockModelResponse{ - Predictions: b.Instances, - } - w.WriteHeader(http.StatusOK) - _ = json.NewEncoder(w).Encode(resp) - }) - - // Create a mock server with the handler to act as the external model endpoint - tsrv := httptest.NewServer(handler) - defer tsrv.Close() - - h := manifestdata.GetManifest().Hosts[testHostName].(manifest.HTTPHostInfo) - h.Endpoint = tsrv.URL - manifestdata.GetManifest().Hosts[testHostName] = h - - sentenceMap := map[string]string{ - "key1": "value1", - "key2": "value2", - } - testModel := manifestdata.GetManifest().Models[testModelName] - resp, err := postToModelEndpoint[string](context.Background(), &testModel, sentenceMap) - assert.NoError(t, err) - - // Expected response is the same as the input sentence map, - // as the mock server just echoes the inputs - assert.Equal(t, sentenceMap, resp) -} diff --git a/runtime/models/legacymodels/openai.go b/runtime/models/legacymodels/openai.go deleted file mode 100644 index a211d7aa..00000000 --- a/runtime/models/legacymodels/openai.go +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright 2024 Hypermode Inc. - * Licensed under the terms of the Apache License, Version 2.0 - * See the LICENSE file that accompanied this code for further details. - * - * SPDX-FileCopyrightText: 2024 Hypermode Inc. - * SPDX-License-Identifier: Apache-2.0 - */ - -package legacymodels - -import ( - "context" - "fmt" - "regexp" - - "github.com/hypermodeinc/modus/pkg/manifest" - "github.com/hypermodeinc/modus/runtime/db" - "github.com/hypermodeinc/modus/runtime/hosts" - "github.com/hypermodeinc/modus/runtime/manifestdata" -) - -var authHeaderRegex = regexp.MustCompile(`^Bearer {{\s*\w+?\s*}}$`) - -func callOpenAIChatCompletion(ctx context.Context, model *manifest.ModelInfo, host *manifest.HTTPHostInfo, instruction string, sentence string, outputFormat outputFormat) (*chatResponse, error) { - - // We ignore the model endpoint and use the OpenAI chat completion endpoint - host.Endpoint = "https://api.openai.com/v1/chat/completions" - - // Validate that the host has an Authorization header containing a Bearer token - authHeaderValue, ok := host.Headers["Authorization"] - if !ok { - // Handle how the key was passed in the old V1 manifest format - if manifestdata.GetManifest().Version == 1 && len(host.Headers) == 1 { - for k, v := range host.Headers { - apiKey := v - delete(host.Headers, k) - host.Headers["Authorization"] = "Bearer " + apiKey - } - } else { - return nil, fmt.Errorf("host must have an Authorization header containing a Bearer token") - } - } else if !authHeaderRegex.MatchString(authHeaderValue) { - return nil, fmt.Errorf("host Authorization header must be of the form: \"Bearer {{SECRET_NAME}}\"") - } - - reqBody := chatContext{ - Model: model.SourceModel, - ResponseFormat: responseFormat{ - Type: string(outputFormat), - }, - Messages: []chatMessage{ - {Role: "system", Content: instruction}, - {Role: "user", Content: sentence}, - }, - } - - result, err := hosts.PostToHostEndpoint[chatResponse](ctx, host, reqBody) - if err != nil { - return nil, fmt.Errorf("error posting to OpenAI: %w", err) - } - - if result.Data.Error.Message != "" { - return &result.Data, fmt.Errorf("error returned from OpenAI: %s", result.Data.Error.Message) - } - - db.WriteInferenceHistory(ctx, model, reqBody, result.Data, result.StartTime, result.EndTime) - - return &result.Data, nil -} - -type chatContext struct { - Model string `json:"model"` - ResponseFormat responseFormat `json:"response_format"` - Messages []chatMessage `json:"messages"` -} - -type responseFormat struct { - Type string `json:"type"` -} - -type chatMessage struct { - Role string `json:"role"` - Content string `json:"content"` -} - -type chatResponse struct { - Choices []messageChoice `json:"choices"` - Error invokeError `json:"error"` -} - -type messageChoice struct { - Message chatMessage `json:"message"` -} - -type invokeError struct { - Message string `json:"message"` - Type string `json:"type"` - Param string `json:"param"` - Code string `json:"code"` -} diff --git a/runtime/models/legacymodels/textgeneration.go b/runtime/models/legacymodels/textgeneration.go deleted file mode 100644 index 0af1e59f..00000000 --- a/runtime/models/legacymodels/textgeneration.go +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2024 Hypermode Inc. - * Licensed under the terms of the Apache License, Version 2.0 - * See the LICENSE file that accompanied this code for further details. - * - * SPDX-FileCopyrightText: 2024 Hypermode Inc. - * SPDX-License-Identifier: Apache-2.0 - */ - -package legacymodels - -import ( - "context" - "fmt" - - "github.com/hypermodeinc/modus/runtime/hosts" - "github.com/hypermodeinc/modus/runtime/models" - "github.com/hypermodeinc/modus/runtime/utils" -) - -type outputFormat string - -const ( - outputFormatText outputFormat = "text" - outputFormatJson outputFormat = "json_object" -) - -func InvokeTextGenerator(ctx context.Context, modelName string, format string, instruction string, sentence string) (string, error) { - model, err := models.GetModel(modelName) - if err != nil { - return "", err - } - - if model.Host != hosts.OpenAIHost { - return "", fmt.Errorf("unsupported model host: %s", model.Host) - } - - host, err := hosts.GetHttpHost(model.Host) - if err != nil { - return "", err - } - - outputFormat := outputFormat(format) - if outputFormat != outputFormatText && outputFormat != outputFormatJson { - return "", fmt.Errorf("unsupported output format: %s", format) - } - - result, err := callOpenAIChatCompletion(ctx, model, host, instruction, sentence, outputFormat) - if err != nil { - return "", err - } - - if result.Error.Message != "" { - err := fmt.Errorf("error received: %s", result.Error.Message) - return "", err - } - - if outputFormat == outputFormatJson { - for _, choice := range result.Choices { - _, err := utils.JsonSerialize(choice.Message.Content) - if err != nil { - return "", fmt.Errorf("one of the generated message is not a valid JSON: %w", err) - } - } - } - - if len(result.Choices) == 0 { - return "", fmt.Errorf("empty result returned from OpenAI") - } - - return result.Choices[0].Message.Content, nil -} diff --git a/sdk/assemblyscript/src/assembly/compat.ts b/sdk/assemblyscript/src/assembly/compat.ts deleted file mode 100644 index 54feb28a..00000000 --- a/sdk/assemblyscript/src/assembly/compat.ts +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2024 Hypermode Inc. - * Licensed under the terms of the Apache License, Version 2.0 - * See the LICENSE file that accompanied this code for further details. - * - * SPDX-FileCopyrightText: 2024 Hypermode Inc. - * SPDX-License-Identifier: Apache-2.0 - */ - -import { graphql } from "."; - -// This file retains compatibility with previous function versions. - -/** - * @deprecated Import `graphql` instead. - */ -export abstract class connection { - /** - * @deprecated Use `graphql.execute` instead. - */ - static invokeGraphqlApi( - hostName: string, - statement: string, - variables: QueryVariables = new QueryVariables(), - ): GQLResponse { - const r = graphql.execute(hostName, statement, variables); - return >{ - errors: r.errors, - data: r.data, - }; - } -} - -/** - * @deprecated Import `graphql`, and use `graphql.Variables` instead. - */ -export class QueryVariables extends graphql.Variables {} - -/** - * @deprecated Import `graphql`, and use `graphql.Response` instead. - */ -@json -export class GQLResponse extends graphql.Response {} From 98704db85c48a9243494c055c36ee29e319b57df Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Tue, 8 Oct 2024 20:58:35 -0700 Subject: [PATCH 6/8] Remove _v2 suffixes from collections host functions --- runtime/hostfunctions/collections.go | 16 +++++----- .../src/assembly/collections.ts | 16 +++++----- sdk/go/pkg/collections/imports_wasi.go | 32 +++++++++---------- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/runtime/hostfunctions/collections.go b/runtime/hostfunctions/collections.go index 2a96817b..00ad8339 100644 --- a/runtime/hostfunctions/collections.go +++ b/runtime/hostfunctions/collections.go @@ -17,14 +17,14 @@ import ( func init() { - registerHostFunction("hypermode", "computeDistance_v2", collections.ComputeDistance, + registerHostFunction("hypermode", "computeDistance", collections.ComputeDistance, withCancelledMessage("Cancelled computing distance."), withErrorMessage("Error computing distance."), withMessageDetail(func(collectionName, namespace, searchMethod string) string { return fmt.Sprintf("Collection: %s, Namespace: %s, Method: %s", collectionName, namespace, searchMethod) })) - registerHostFunction("hypermode", "deleteFromCollection_v2", collections.DeleteFromCollection, + registerHostFunction("hypermode", "deleteFromCollection", collections.DeleteFromCollection, withCancelledMessage("Cancelled deleting from collection."), withErrorMessage("Error deleting from collection."), withMessageDetail(func(collectionName, namespace, key string) string { @@ -38,14 +38,14 @@ func init() { return fmt.Sprintf("Collection: %s", collectionName) })) - registerHostFunction("hypermode", "getTextFromCollection_v2", collections.GetTextFromCollection, + registerHostFunction("hypermode", "getTextFromCollection", collections.GetTextFromCollection, withCancelledMessage("Cancelled getting text from collection."), withErrorMessage("Error getting text from collection."), withMessageDetail(func(collectionName, namespace, key string) string { return fmt.Sprintf("Collection: %s, Namespace: %s, Key: %s", collectionName, namespace, key) })) - registerHostFunction("hypermode", "getTextsFromCollection_v2", collections.GetTextsFromCollection, + registerHostFunction("hypermode", "getTextsFromCollection", collections.GetTextsFromCollection, withCancelledMessage("Cancelled getting texts from collection."), withErrorMessage("Error getting texts from collection."), withMessageDetail(func(collectionName, namespace string) string { @@ -66,14 +66,14 @@ func init() { return fmt.Sprintf("Collection: %s, Namespace: %s, ID: %s", collectionName, namespace, id) })) - registerHostFunction("hypermode", "nnClassifyCollection_v2", collections.NnClassify, + registerHostFunction("hypermode", "nnClassifyCollection", collections.NnClassify, withCancelledMessage("Cancelled classification."), withErrorMessage("Error during classification."), withMessageDetail(func(collectionName, namespace, searchMethod string) string { return fmt.Sprintf("Collection: %s, Namespace: %s, Method: %s", collectionName, namespace, searchMethod) })) - registerHostFunction("hypermode", "recomputeSearchMethod_v2", collections.RecomputeSearchMethod, + registerHostFunction("hypermode", "recomputeSearchMethod", collections.RecomputeSearchMethod, withStartingMessage("Starting recomputing search method for collection."), withCompletedMessage("Completed recomputing search method for collection."), withCancelledMessage("Cancelled recomputing search method for collection."), @@ -82,7 +82,7 @@ func init() { return fmt.Sprintf("Collection: %s, Namespace: %s, Method: %s", collectionName, namespace, searchMethod) })) - registerHostFunction("hypermode", "searchCollection_v2", collections.SearchCollection, + registerHostFunction("hypermode", "searchCollection", collections.SearchCollection, withCancelledMessage("Cancelled searching collection."), withErrorMessage("Error searching collection."), withMessageDetail(func(collectionName string, namespaces []string, searchMethod string) string { @@ -96,7 +96,7 @@ func init() { return fmt.Sprintf("Collection: %s, Namespaces: %v, Method: %s", collectionName, namespaces, searchMethod) })) - registerHostFunction("hypermode", "upsertToCollection_v2", collections.UpsertToCollection, + registerHostFunction("hypermode", "upsertToCollection", collections.UpsertToCollection, withCancelledMessage("Cancelled collection upsert."), withErrorMessage("Error upserting to collection."), withMessageDetail(func(collectionName, namespace string, keys []string) string { diff --git a/sdk/assemblyscript/src/assembly/collections.ts b/sdk/assemblyscript/src/assembly/collections.ts index dac0fcc0..438547a2 100644 --- a/sdk/assemblyscript/src/assembly/collections.ts +++ b/sdk/assemblyscript/src/assembly/collections.ts @@ -146,7 +146,7 @@ export class CollectionClassificationResultObject { } // @ts-expect-error: decorator -@external("hypermode", "upsertToCollection_v2") +@external("hypermode", "upsertToCollection") declare function hostUpsertToCollection( collection: string, namespace: string, @@ -156,7 +156,7 @@ declare function hostUpsertToCollection( ): CollectionMutationResult; // @ts-expect-error: decorator -@external("hypermode", "deleteFromCollection_v2") +@external("hypermode", "deleteFromCollection") declare function hostDeleteFromCollection( collection: string, namespace: string, @@ -164,7 +164,7 @@ declare function hostDeleteFromCollection( ): CollectionMutationResult; // @ts-expect-error: decorator -@external("hypermode", "searchCollection_v2") +@external("hypermode", "searchCollection") declare function hostSearchCollection( collection: string, namespaces: string[], @@ -175,7 +175,7 @@ declare function hostSearchCollection( ): CollectionSearchResult; // @ts-expect-error: decorator -@external("hypermode", "nnClassifyCollection_v2") +@external("hypermode", "nnClassifyCollection") declare function hostNnClassifyCollection( collection: string, namespace: string, @@ -184,7 +184,7 @@ declare function hostNnClassifyCollection( ): CollectionClassificationResult; // @ts-expect-error: decorator -@external("hypermode", "recomputeSearchMethod_v2") +@external("hypermode", "recomputeSearchMethod") declare function hostRecomputeSearchMethod( collection: string, namespace: string, @@ -192,7 +192,7 @@ declare function hostRecomputeSearchMethod( ): SearchMethodMutationResult; // @ts-expect-error: decorator -@external("hypermode", "computeDistance_v2") +@external("hypermode", "computeDistance") declare function hostComputeDistance( collection: string, namespace: string, @@ -202,7 +202,7 @@ declare function hostComputeDistance( ): CollectionSearchResultObject; // @ts-expect-error: decorator -@external("hypermode", "getTextFromCollection_v2") +@external("hypermode", "getTextFromCollection") declare function hostGetTextFromCollection( collection: string, namespace: string, @@ -210,7 +210,7 @@ declare function hostGetTextFromCollection( ): string; // @ts-expect-error: decorator -@external("hypermode", "getTextsFromCollection_v2") +@external("hypermode", "getTextsFromCollection") declare function hostGetTextsFromCollection( collection: string, namespace: string, diff --git a/sdk/go/pkg/collections/imports_wasi.go b/sdk/go/pkg/collections/imports_wasi.go index dc69c980..7a9c4347 100644 --- a/sdk/go/pkg/collections/imports_wasi.go +++ b/sdk/go/pkg/collections/imports_wasi.go @@ -14,10 +14,10 @@ package collections import "unsafe" //go:noescape -//go:wasmimport hypermode upsertToCollection_v2 +//go:wasmimport hypermode upsertToCollection func _hostUpsertToCollection(collection, namespace *string, keys, texts, labels unsafe.Pointer) unsafe.Pointer -//hypermode:import hypermode upsertToCollection_v2 +//hypermode:import hypermode upsertToCollection func hostUpsertToCollection(collection, namespace *string, keys, texts *[]string, labels *[][]string) *CollectionMutationResult { keysPointer := unsafe.Pointer(keys) textsPointer := unsafe.Pointer(texts) @@ -30,10 +30,10 @@ func hostUpsertToCollection(collection, namespace *string, keys, texts *[]string } //go:noescape -//go:wasmimport hypermode deleteFromCollection_v2 +//go:wasmimport hypermode deleteFromCollection func _hostDeleteFromCollection(collection, namespace, key *string) unsafe.Pointer -//hypermode:import hypermode deleteFromCollection_v2 +//hypermode:import hypermode deleteFromCollection func hostDeleteFromCollection(collection, namespace, key *string) *CollectionMutationResult { response := _hostDeleteFromCollection(collection, namespace, key) if response == nil { @@ -43,10 +43,10 @@ func hostDeleteFromCollection(collection, namespace, key *string) *CollectionMut } //go:noescape -//go:wasmimport hypermode searchCollection_v2 +//go:wasmimport hypermode searchCollection func _hostSearchCollection(collection *string, namespaces unsafe.Pointer, searchMethod, text *string, limit int32, returnText bool) unsafe.Pointer -//hypermode:import hypermode searchCollection_v2 +//hypermode:import hypermode searchCollection func hostSearchCollection(collection *string, namespaces *[]string, searchMethod, text *string, limit int32, returnText bool) *CollectionSearchResult { namespacesPtr := unsafe.Pointer(namespaces) response := _hostSearchCollection(collection, namespacesPtr, searchMethod, text, limit, returnText) @@ -57,10 +57,10 @@ func hostSearchCollection(collection *string, namespaces *[]string, searchMethod } //go:noescape -//go:wasmimport hypermode nnClassifyCollection_v2 +//go:wasmimport hypermode nnClassifyCollection func _hostNnClassifyCollection(collection, namespace, searchMethod, text *string) unsafe.Pointer -//hypermode:import hypermode nnClassifyCollection_v2 +//hypermode:import hypermode nnClassifyCollection func hostNnClassifyCollection(collection, namespace, searchMethod, text *string) *CollectionClassificationResult { response := _hostNnClassifyCollection(collection, namespace, searchMethod, text) if response == nil { @@ -70,10 +70,10 @@ func hostNnClassifyCollection(collection, namespace, searchMethod, text *string) } //go:noescape -//go:wasmimport hypermode recomputeSearchMethod_v2 +//go:wasmimport hypermode recomputeSearchMethod func _hostRecomputeSearchMethod(collection, namespace, searchMethod *string) unsafe.Pointer -//hypermode:import hypermode recomputeSearchMethod_v2 +//hypermode:import hypermode recomputeSearchMethod func hostRecomputeSearchMethod(collection, namespace, searchMethod *string) *SearchMethodMutationResult { response := _hostRecomputeSearchMethod(collection, namespace, searchMethod) if response == nil { @@ -83,10 +83,10 @@ func hostRecomputeSearchMethod(collection, namespace, searchMethod *string) *Sea } //go:noescape -//go:wasmimport hypermode computeDistance_v2 +//go:wasmimport hypermode computeDistance func _hostComputeDistance(collection, namespace, searchMethod, key1, key2 *string) unsafe.Pointer -//hypermode:import hypermode computeDistance_v2 +//hypermode:import hypermode computeDistance func hostComputeDistance(collection, namespace, searchMethod, key1, key2 *string) *CollectionSearchResultObject { response := _hostComputeDistance(collection, namespace, searchMethod, key1, key2) if response == nil { @@ -96,10 +96,10 @@ func hostComputeDistance(collection, namespace, searchMethod, key1, key2 *string } //go:noescape -//go:wasmimport hypermode getTextFromCollection_v2 +//go:wasmimport hypermode getTextFromCollection func _hostGetTextFromCollection(collection, namespace, key *string) unsafe.Pointer -//hypermode:import hypermode getTextFromCollection_v2 +//hypermode:import hypermode getTextFromCollection func hostGetTextFromCollection(collection, namespace, key *string) *string { response := _hostGetTextFromCollection(collection, namespace, key) if response == nil { @@ -109,10 +109,10 @@ func hostGetTextFromCollection(collection, namespace, key *string) *string { } //go:noescape -//go:wasmimport hypermode getTextsFromCollection_v2 +//go:wasmimport hypermode getTextsFromCollection func _hostGetTextsFromCollection(collection, namespace *string) unsafe.Pointer -//hypermode:import hypermode getTextsFromCollection_v2 +//hypermode:import hypermode getTextsFromCollection func hostGetTextsFromCollection(collection, namespace *string) *map[string]string { response := _hostGetTextsFromCollection(collection, namespace) if response == nil { From f2becbc1eee1df8de23b0c0e26f4c4b007ff2184 Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Tue, 8 Oct 2024 21:00:45 -0700 Subject: [PATCH 7/8] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 733fbdaf..06eb26a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ In previous releases, the name "Hypermode" was used for all three._ - Fix potential array out of bounds in the runtime [#437](https://github.com/hypermodeinc/modus/pull/437) - Set minimum Go version to 1.23.0 [#438](https://github.com/hypermodeinc/modus/pull/438) - Change default for environment setting [#439](https://github.com/hypermodeinc/modus/pull/439) +- Remove compatibility code for previous versions [#441](https://github.com/hypermodeinc/modus/pull/441) ## 2024-10-02 - Version 0.12.7 From 78667408ecc194ef055aeebc114c98af452da78a Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Tue, 8 Oct 2024 21:13:11 -0700 Subject: [PATCH 8/8] Update AssemblyScript code --- .../testdata/build/testdata.wasm | Bin 115310 -> 115321 bytes .../assemblyscript/testdata/package-lock.json | 4 +- .../anthropic-functions/package-lock.json | 4 +- .../examples/classification/package-lock.json | 4 +- .../examples/collection/package-lock.json | 4 +- .../examples/dgraph/package-lock.json | 4 +- .../examples/embedding/package-lock.json | 4 +- .../examples/graphql/package-lock.json | 4 +- .../examples/http/package-lock.json | 4 +- .../examples/postgresql/package-lock.json | 4 +- .../examples/simple/package-lock.json | 4 +- .../examples/textgeneration/package-lock.json | 4 +- .../examples/vectors/package-lock.json | 8 +- sdk/assemblyscript/src/assembly/index.ts | 3 - sdk/assemblyscript/src/assembly/inference.ts | 237 ------------------ sdk/assemblyscript/src/package-lock.json | 4 +- sdk/assemblyscript/src/package.json | 2 +- 17 files changed, 29 insertions(+), 269 deletions(-) delete mode 100644 sdk/assemblyscript/src/assembly/inference.ts diff --git a/runtime/languages/assemblyscript/testdata/build/testdata.wasm b/runtime/languages/assemblyscript/testdata/build/testdata.wasm index 81f5523829fdc4c11451fcbf1fd0074901cbbd94..09ac2fa4b6ed28bc0ad0bea7548d291f11ee91d7 100644 GIT binary patch delta 2164 zcmZ8hYfuwc6wYQZM6?w##s>rp2?{8W%_a~C4_RAvM5|+^)-t1=u;Hm#AUwo^9jpec z18A>|&zUHSV43>pYO&Y{j80gZ8E#+4uKRukEG^*E4pphxjiXlbj$OtF#%7)ZW$B`>KiKj`K#c@H( z1hdYflXQzDZs7t_AnBwVS9MY(P!4C#Hmj6qpbXm*LkKg0@bg&0h%GsUkPd_d*Q^+I zg&DAdEjA=S5At5uY>uZpUD05VGd#-hZPy%&s*A(TJ>zO^LWuSrb|7ZW1=-rHnP4|( zjoGR#A%xrojnm=(5i(Z1=rw=@efHQDTp=r*Lc*)rxYu>rxK}^JA2a+-_Ii$2GTdS! zBzAj^^j*(nK=xi!)E&K~SC_dpO^T8*Z=zQ{lOTW7Qvf#ofIyd&^Z`O;xU4E^nv5J| z7cqpqZux}Dd);dS1Qlqj*T`Z?V28Yqz_zs*>DBUNK2JhyqA?YTd_KcQT3M0I<8QAm zdj`zfawpj1<;%hT&FsPo#I@x{dZHp8&^6}ARAO`;vzWVHiMjjCFQ~%YE@p34eMf$R z!6@g}c?d+O)yx8$$E?ik2WXX3HE(i|Fx0IAyS7dQySr`;*n4&2xZu<+I-1)UO<(aP zL%7;kOt#_#uKCi*HUv+7i-;dVwJ)Ex*PF?Q==9a66B2!3pc zr^gxuawO<1Aw38Z8)b47L1*Ji!sh&2V>&sFk?Bo#avs5JO=j{LCyl4oO%4P+J=QcD z8QiHYUz$>tkUwD=k-N3;1I5YHltyZ^{_|f=@TdiOQBuH}NmP z5F_{Tcoui%NjGw(mtpm`mU)mFX|aLDaXxHG1LPSn(cIQZHTHHZ#FWm~+v6aBXJ>~$ zR!v>-yVY1kdz>1}Y0uGk&tsosOnKPu4&w%>qa%@bNjzQCVdk@-)s_wme0drG-wpnk z9VzhT=^gNMz<;T8F?@Nty3-6tZ9Hx2Otr$1V{eWv-=edIW$G=KF!G)yEKC}niLK=W z?Q&-!{IL*Pc&0wV5)QO*jW(q+RWKws}SQ3vs+;+=ILjAAj<ahSmKh?ds3YOZ#5YoTz{Q|qju8#Ii0fb$R_Tk%wk_QZ- zB(@)8LjQcQC=B#3*6KX@2!&At!AH|Xmkl7*!+fNk8>rE!*(bFc@=2}6PKL0C!VpUS zI=NDlymdyCynjZM3^0U}9~eT(LzO&X2qkkbU<@U-7arrquD&!M#(VnGMUMQb zgY(vO+4vRr$KIXWxZLHAG$GZqzNW%sx29N28YBE)tw>yXbZ}(lVU(y^(%yeo~oOQYx3o_R`W)ySFUMTPiwA c1X(B*Gs~Q^vqTVFWo4p0%Q@KW=N{zz2Q>?hkN^Mx delta 2166 zcmZ9Mdu&s66vunp-(43WGALJIE3A(_b=~gzZdbazrMN#%Kj)n9_jiA< z{T_Ad|LD{o`PsnzB;PXV#-{NZLt4!+I-)33))xgi@;N8*@(SB0>Ewb&;%R|<(S)RA zm{DWWNSdhAu+;W{V8 zy>+u8K2YM6dgO4Of$pz216@(_tOl$$DG_^LHDK?al2px#S4KFKs2{D8ktGQgRaE)pjE}#Yw~I@^%LTPj|NujY!VaS}u)l z%qbr>i*l2VbjK=O$KR_gTm~J#dJpi0)eATqHLh_z_f$_;1oP#0)?CM3!0XZ+#Uox9 zodofG)T*$7K8zN#zQ1E6tG(F)HMzIrb~7np>M=N&{J% zDk}$>BW|<6byZ$2YdMUjcWVnwdXVW=*(8v4#;4;=mcNP56nlF35mR5tBmI~eA`Tj z3%AWsc=|}#R5nK`9nZglu4^)nk~A3)w8>{>dt9EancZAvfx{iFfj^f=6kYCGXRY1qv@dnm)`(8gUg{B@4qvIq OW3Ta?= - * SPDX-License-Identifier: Apache-2.0 - */ - -import * as utils from "./utils"; -import { JSON } from "json-as"; - -// NOTE: all functions in this file are deprecated and will be removed in the future. - -// @ts-expect-error: decorator -@external("hypermode", "invokeClassifier") -declare function invokeClassifier( - modelName: string, - sentenceMap: Map, -): Map>; - -// @ts-expect-error: decorator -@external("hypermode", "computeEmbedding") -declare function computeEmbedding( - modelName: string, - sentenceMap: Map, -): Map; - -// @ts-expect-error: decorator -@external("hypermode", "invokeTextGenerator") -declare function invokeTextGenerator( - modelName: string, - instruction: string, - sentence: string, - format: string, -): string; - -/** - * @deprecated - * This class is deprecated and will be removed in the future. - * Use `models.getModel` and the appropriate model interface instead. - */ -export abstract class inference { - /** - * @deprecated - * This function is deprecated and will be removed in the future. - * Use `models.getModel` with a classification model instead. - */ - public static getClassificationProbability( - modelName: string, - text: string, - label: string, - ): f32 { - const labels = this.getClassificationLabelsForText(modelName, text); - const keys = labels.keys(); - for (let i = 0; i < keys.length; i++) { - const key = keys[i]; - if (key === label) { - return labels.get(key); - } - } - return 0.0; - } - - /** - * @deprecated - * This function is deprecated and will be removed in the future. - * Use `models.getModel` with a classification model instead. - */ - public static classifyText( - modelName: string, - text: string, - threshold: f32, - ): string { - const labels = this.getClassificationLabelsForText(modelName, text); - - const keys = labels.keys(); - let max = labels.get(keys[0]); - let result = keys[0]; - for (let i = 1; i < keys.length; i++) { - const key = keys[i]; - const value = labels.get(key); - if (value >= max) { - max = value; - result = key; - } - } - - if (max < threshold) { - return ""; - } - return result; - } - - /** - * @deprecated - * This function is deprecated and will be removed in the future. - * Use `models.getModel` with a classification model instead. - */ - public static getClassificationLabelsForText( - modelName: string, - text: string, - ): Map { - const textMap = new Map(); - textMap.set("text", text); - const res = this.getClassificationLabelsForTexts(modelName, textMap); - return res.get("text"); - } - - /** - * @deprecated - * This function is deprecated and will be removed in the future. - * Use `models.getModel` with a classification model instead. - */ - public static getClassificationLabelsForTexts( - modelName: string, - texts: Map, - ): Map> { - const result = invokeClassifier(modelName, texts); - if (utils.resultIsInvalid(result)) { - throw new Error("Unable to classify text."); - } - return result; - } - - /** - * @deprecated - * This function is deprecated and will be removed in the future. - * Use `models.getModel` with an embedding model instead. - */ - public static getTextEmbedding(modelName: string, text: string): f64[] { - const textMap = new Map(); - textMap.set("text", text); - const res = this.getTextEmbeddings(modelName, textMap); - return res.get("text"); - } - - /** - * @deprecated - * This function is deprecated and will be removed in the future. - * Use `models.getModel` with an embedding model instead. - */ - public static getTextEmbeddings( - modelName: string, - texts: Map, - ): Map { - const result = computeEmbedding(modelName, texts); - if (utils.resultIsInvalid(result)) { - throw new Error("Unable to compute embeddings."); - } - return result; - } - - /** - * @deprecated - * This function is deprecated and will be removed in the future. - * Use `models.getModel` with a generative text model instead. - */ - public static generateText( - modelName: string, - instruction: string, - prompt: string, - ): string { - const result = invokeTextGenerator(modelName, instruction, prompt, "text"); - if (utils.resultIsInvalid(result)) { - throw new Error("Unable to generate text."); - } - return result; - } - - /** - * @deprecated - * This function is deprecated and will be removed in the future. - * Use `models.getModel` with a generative text model instead. - */ - public static generateData( - modelName: string, - instruction: string, - text: string, - sample: TData, - ): TData { - // Prompt trick: ask for a simple JSON object. - const modifiedInstruction = - "Only respond with valid JSON object in this format:\n" + - JSON.stringify(sample) + - "\n" + - instruction; - - const result = invokeTextGenerator( - modelName, - modifiedInstruction, - text, - "json_object", - ); - - if (utils.resultIsInvalid(result)) { - throw new Error("Unable to generate data."); - } - - return JSON.parse(result); - } - - /** - * @deprecated - * This function is deprecated and will be removed in the future. - * Use `models.getModel` with a generative text model instead. - */ - public static generateList( - modelName: string, - instruction: string, - text: string, - sample: TData, - ): TData[] { - // Prompt trick: ask for a simple JSON object containing a list. - // Note, OpenAI will not generate an array of objects directly. - const modifiedInstruction = - "Only respond with valid JSON object containing a valid JSON array named 'list', in this format:\n" + - '{"list":[' + - JSON.stringify(sample) + - "]}\n" + - instruction; - - const result = invokeTextGenerator( - modelName, - modifiedInstruction, - text, - "json_object", - ); - - if (utils.resultIsInvalid(result)) { - throw new Error("Unable to generate data."); - } - - const jsonList = JSON.parse>(result); - return jsonList.get("list"); - } -} diff --git a/sdk/assemblyscript/src/package-lock.json b/sdk/assemblyscript/src/package-lock.json index bb443382..2f55ee36 100644 --- a/sdk/assemblyscript/src/package-lock.json +++ b/sdk/assemblyscript/src/package-lock.json @@ -1,12 +1,12 @@ { "name": "@hypermode/modus-sdk-as", - "version": "0.12.0", + "version": "0.13.0-alpha1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@hypermode/modus-sdk-as", - "version": "0.12.0", + "version": "0.13.0-alpha1", "license": "Apache-2.0", "dependencies": { "@assemblyscript/wasi-shim": "^0.1.0", diff --git a/sdk/assemblyscript/src/package.json b/sdk/assemblyscript/src/package.json index e071694a..0ad1fe10 100644 --- a/sdk/assemblyscript/src/package.json +++ b/sdk/assemblyscript/src/package.json @@ -1,6 +1,6 @@ { "name": "@hypermode/modus-sdk-as", - "version": "0.12.0", + "version": "0.13.0-alpha1", "description": "Modus SDK for AssemblyScript", "author": "Hypermode Inc.", "license": "Apache-2.0",