Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ospp/new llm embedding #19727

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 48,7 @@ require (
github.com/itchyny/gojq v0.12.16
github.com/jhump/protoreflect v1.15.2
github.com/json-iterator/go v1.1.12
github.com/ledongthuc/pdf v0.0.0-20240201131950-da5b75280b06
github.com/lni/dragonboat/v4 v4.0.0-20220815145555-6f622e8bcbef
github.com/lni/goutils v1.3.1-0.20220604063047-388d67b4dbc4
github.com/lni/vfs v0.2.1-0.20220616104132-8852fd867376
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 484,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g=
github.com/labstack/echo/v4 v4.5.0/go.mod h1:czIriw4a0C1dFun ObrXp7ok03xON0N1awStJ6ArI7Y=
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc lOruYdAhDwPK9wf0OL7NoOu k=
github.com/ledongthuc/pdf v0.0.0-20240201131950-da5b75280b06 h1:kacRlPN7EN tVpGUorNGPn/4DnB7/DfTY82AOn6ccU=
github.com/ledongthuc/pdf v0.0.0-20240201131950-da5b75280b06/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J dCPAknuiaGOshXAs=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E 4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
Expand Down
24 changes: 24 additions & 0 deletions pkg/frontend/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -3518,6 3518,30 @@ var gSysVarsDefs = map[string]SystemVariable{
Type: InitSystemVariableBoolType("experimental_ivf_index"),
Default: int64(0),
},
"llm_embedding_platform": {
Name: "llm_embedding_platform",
Scope: ScopeBoth,
Dynamic: true,
SetVarHintApplies: false,
Type: InitSystemVariableStringType("llm_embedding_platform"),
Default: "ollama",
},
"llm_server_proxy": {
Name: "llm_server_proxy",
Scope: ScopeBoth,
Dynamic: true,
SetVarHintApplies: false,
Type: InitSystemVariableStringType("llm_server_proxy"),
Default: "http://localhost:11434/api/embed",
},
"llm_model": {
Name: "llm_model",
Scope: ScopeBoth,
Dynamic: true,
SetVarHintApplies: false,
Type: InitSystemVariableStringType("llm_model"),
Default: "llama3",
},
"disable_agg_statement": {
Name: "disable_agg_statement",
Scope: ScopeSession,
Expand Down
70 changes: 70 additions & 0 deletions pkg/sql/plan/function/embedding_service.go
Original file line number Diff line number Diff line change
@@ -0,0 1,70 @@
// Copyright 2024 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package function

import (
"github.com/matrixorigin/matrixone/pkg/common/moerr"
"github.com/matrixorigin/matrixone/pkg/vm/process"
)

type EmbeddingService interface {
GetEmbedding(input string, model string, proxy string) ([]float32, error)
}

type OllamaEmbeddingService struct{}

func (o *OllamaEmbeddingService) GetEmbedding(input string, model string, proxy string) ([]float32, error) {
return getOllamaSingleEmbedding(input, model, proxy)
}

func NewEmbeddingService(modelStr string) (EmbeddingService, error) {
switch modelStr {
case "ollama":
return &OllamaEmbeddingService{}, nil
default:
return nil, moerr.NewInvalidInputNoCtxf("'%s' is not a valid chunk strategy", modelStr)
}
}

func getLLMGlobalVariable(proc *process.Process) (string, string, string, error) {
platform, err := proc.GetResolveVariableFunc()("llm_embedding_platform", true, false)
if err != nil {
return "", "", "", err
}
platformStr, ok := platform.(string)
if !ok {
return "", "", "", moerr.NewInvalidInputf(proc.Ctx, "unexpected type for llm_embedding_platform: %T", platform)
}

proxy, err1 := proc.GetResolveVariableFunc()("llm_server_proxy", true, false)
if err1 != nil {
return "", "", "", err1
}
proxyStr, ok := proxy.(string)
if !ok {
return "", "", "", moerr.NewInvalidInputf(proc.Ctx, "unexpected type for llm_server_proxy: %T", proxy)
}

llmModel, err1 := proc.GetResolveVariableFunc()("llm_model", true, false)
if err1 != nil {
return "", "", "", err1
}
llmModelStr, ok := llmModel.(string)
if !ok {
return "", "", "", moerr.NewInvalidInputf(proc.Ctx, "unexpected type for llm_model: %T", llmModel)
}

return platformStr, proxyStr, llmModelStr, nil
}
Loading
Loading