optimize plugin sdk (#1930)

This commit is contained in:
澄潭
2025-03-22 22:46:37 +08:00
committed by GitHub
parent 1812a6b0a9
commit 45fbc8b084
117 changed files with 1036 additions and 766 deletions

View File

@@ -7,6 +7,7 @@ import (
"net/http"
"strconv"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
@@ -79,7 +80,7 @@ type CohereProvider struct {
func (t *CohereProvider) GetProviderType() string {
return PROVIDER_TYPE_COHERE
}
func (t *CohereProvider) constructParameters(texts []string, log wrapper.Log) (string, [][2]string, []byte, error) {
func (t *CohereProvider) constructParameters(texts []string, log log.Log) (string, [][2]string, []byte, error) {
model := t.config.model
if model == "" {
@@ -118,7 +119,7 @@ func (t *CohereProvider) parseTextEmbedding(responseBody []byte) (*cohereRespons
func (t *CohereProvider) GetEmbedding(
queryString string,
ctx wrapper.HttpContext,
log wrapper.Log,
log log.Log,
callback func(emb []float64, err error)) error {
embUrl, embHeaders, embRequestBody, err := t.constructParameters([]string{queryString}, log)
if err != nil {

View File

@@ -7,6 +7,7 @@ import (
"net/http"
"strconv"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
@@ -103,7 +104,7 @@ type DSProvider struct {
client wrapper.HttpClient
}
func (d *DSProvider) constructParameters(texts []string, log wrapper.Log) (string, [][2]string, []byte, error) {
func (d *DSProvider) constructParameters(texts []string, log log.Log) (string, [][2]string, []byte, error) {
model := d.config.model
@@ -159,7 +160,7 @@ func (d *DSProvider) parseTextEmbedding(responseBody []byte) (*Response, error)
func (d *DSProvider) GetEmbedding(
queryString string,
ctx wrapper.HttpContext,
log wrapper.Log,
log log.Log,
callback func(emb []float64, err error)) error {
embUrl, embHeaders, embRequestBody, err := d.constructParameters([]string{queryString}, log)
if err != nil {

View File

@@ -4,11 +4,13 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
"net/http"
"strconv"
"strings"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
const (
@@ -78,7 +80,7 @@ type HuggingFaceEmbeddingRequest struct {
} `json:"options"`
}
func (t *HuggingFaceProvider) constructParameters(text string, log wrapper.Log) (string, [][2]string, []byte, error) {
func (t *HuggingFaceProvider) constructParameters(text string, log log.Log) (string, [][2]string, []byte, error) {
if text == "" {
err := errors.New("queryString text cannot be empty")
return "", nil, nil, err
@@ -127,7 +129,7 @@ func (t *HuggingFaceProvider) parseTextEmbedding(responseBody []byte) ([]float64
func (t *HuggingFaceProvider) GetEmbedding(
queryString string,
ctx wrapper.HttpContext,
log wrapper.Log,
log log.Log,
callback func(emb []float64, err error)) error {
embUrl, embHeaders, embRequestBody, err := t.constructParameters(queryString, log)
if err != nil {

View File

@@ -4,10 +4,12 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
"net/http"
"strconv"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
const (
@@ -69,7 +71,7 @@ type ollamaEmbeddingRequest struct {
Model string `json:"model"`
}
func (t *ollamaProvider) constructParameters(text string, log wrapper.Log) (string, [][2]string, []byte, error) {
func (t *ollamaProvider) constructParameters(text string, log log.Log) (string, [][2]string, []byte, error) {
if text == "" {
err := errors.New("queryString text cannot be empty")
return "", nil, nil, err
@@ -105,7 +107,7 @@ func (t *ollamaProvider) parseTextEmbedding(responseBody []byte) (*ollamaRespons
func (t *ollamaProvider) GetEmbedding(
queryString string,
ctx wrapper.HttpContext,
log wrapper.Log,
log log.Log,
callback func(emb []float64, err error)) error {
embUrl, embHeaders, embRequestBody, err := t.constructParameters(queryString, log)
if err != nil {

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
@@ -93,7 +94,7 @@ type OpenAIProvider struct {
client wrapper.HttpClient
}
func (t *OpenAIProvider) constructParameters(text string, log wrapper.Log) (string, [][2]string, []byte, error) {
func (t *OpenAIProvider) constructParameters(text string, log log.Log) (string, [][2]string, []byte, error) {
if text == "" {
err := errors.New("queryString text cannot be empty")
return "", nil, nil, err
@@ -130,7 +131,7 @@ func (t *OpenAIProvider) parseTextEmbedding(responseBody []byte) (*OpenAIRespons
func (t *OpenAIProvider) GetEmbedding(
queryString string,
ctx wrapper.HttpContext,
log wrapper.Log,
log log.Log,
callback func(emb []float64, err error)) error {
embUrl, embHeaders, embRequestBody, err := t.constructParameters(queryString, log)
if err != nil {

View File

@@ -3,6 +3,7 @@ package embedding
import (
"errors"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
@@ -108,6 +109,6 @@ type Provider interface {
GetEmbedding(
queryString string,
ctx wrapper.HttpContext,
log wrapper.Log,
log log.Log,
callback func(emb []float64, err error)) error
}

View File

@@ -7,6 +7,7 @@ import (
"net/http"
"strconv"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
@@ -97,7 +98,7 @@ type TIProvider struct {
client wrapper.HttpClient
}
func (t *TIProvider) constructParameters(texts []string, log wrapper.Log) (string, [][2]string, []byte, error) {
func (t *TIProvider) constructParameters(texts []string, log log.Log) (string, [][2]string, []byte, error) {
data := TextInEmbeddingRequest{
Input: texts,
@@ -142,7 +143,7 @@ func (t *TIProvider) parseTextEmbedding(responseBody []byte) (*TextInResponse, e
func (t *TIProvider) GetEmbedding(
queryString string,
ctx wrapper.HttpContext,
log wrapper.Log,
log log.Log,
callback func(emb []float64, err error)) error {
embUrl, embHeaders, embRequestBody, err := t.constructParameters([]string{queryString}, log)
if err != nil {