Skip to content

Commit

Permalink
fix bugs && refine the variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
Suchun-sv committed Aug 25, 2024
1 parent 56314d7 commit 85549d0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 21 deletions.
10 changes: 5 additions & 5 deletions plugins/wasm-go/extensions/ai-cache/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type KVExtractor struct {

type PluginConfig struct {
EmbeddingProviderConfig embedding.ProviderConfig `required:"true" yaml:"embeddingProvider" json:"embeddingProvider"`
vectorProviderConfig vector.ProviderConfig `required:"true" yaml:"vectorBaseProvider" json:"vectorBaseProvider"`
VectorProviderConfig vector.ProviderConfig `required:"true" yaml:"vectorBaseProvider" json:"vectorBaseProvider"`
CacheKeyFrom KVExtractor `required:"true" yaml:"cacheKeyFrom" json:"cacheKeyFrom"`
CacheValueFrom KVExtractor `required:"true" yaml:"cacheValueFrom" json:"cacheValueFrom"`
CacheStreamValueFrom KVExtractor `required:"true" yaml:"cacheStreamValueFrom" json:"cacheStreamValueFrom"`
Expand Down Expand Up @@ -46,7 +46,7 @@ type PluginConfig struct {

func (c *PluginConfig) FromJson(json gjson.Result) {
c.EmbeddingProviderConfig.FromJson(json.Get("embeddingProvider"))
c.vectorProviderConfig.FromJson(json.Get("vectorProvider"))
c.VectorProviderConfig.FromJson(json.Get("vectorProvider"))
c.RedisConfig.FromJson(json.Get("redis"))
if c.CacheKeyFrom.RequestBody == "" {
c.CacheKeyFrom.RequestBody = "messages.@reverse.0.content"
Expand Down Expand Up @@ -84,7 +84,7 @@ func (c *PluginConfig) Validate() error {
if err := c.EmbeddingProviderConfig.Validate(); err != nil {
return err
}
if err := c.vectorProviderConfig.Validate(); err != nil {
if err := c.VectorProviderConfig.Validate(); err != nil {
return err
}
return nil
Expand All @@ -96,7 +96,7 @@ func (c *PluginConfig) Complete(log wrapper.Log) error {
if err != nil {
return err
}
c.vectorProvider, err = vector.CreateProvider(c.vectorProviderConfig)
c.vectorProvider, err = vector.CreateProvider(c.VectorProviderConfig)
if err != nil {
return err
}
Expand All @@ -111,7 +111,7 @@ func (c *PluginConfig) GetEmbeddingProvider() embedding.Provider {
return c.embeddingProvider
}

func (c *PluginConfig) GetvectorProvider() vector.Provider {
func (c *PluginConfig) GetVectorProvider() vector.Provider {
return c.vectorProvider
}

Expand Down
14 changes: 4 additions & 10 deletions plugins/wasm-go/extensions/ai-cache/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"net/http"

"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-cache/config"
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-cache/embedding"
Expand Down Expand Up @@ -53,20 +52,15 @@ func HandleCacheMiss(key string, err error, response resp.Value, ctx wrapper.Htt
func FetchAndProcessEmbeddings(key string, ctx wrapper.HttpContext, config config.PluginConfig, log wrapper.Log, queryString string, stream bool) {
activeEmbeddingProvider := config.GetEmbeddingProvider()
activeEmbeddingProvider.GetEmbedding(queryString, ctx, log,
func(emb []float64, statusCode int, responseHeaders http.Header, responseBody []byte) {
if statusCode != 200 {
log.Errorf("Failed to fetch embeddings, statusCode: %d, responseBody: %s", statusCode, string(responseBody))
proxywasm.ResumeHttpRequest() // 当取 Embedding 失败了也继续流程
} else {
log.Debugf("Successfully fetched embeddings for key: %s", key)
QueryVectorDB(key, emb, ctx, config, log, stream)
}
func(emb []float64) {
log.Debugf("Successfully fetched embeddings for key: %s", key)
QueryVectorDB(key, emb, ctx, config, log, stream)
})
}

func QueryVectorDB(key string, text_embedding []float64, ctx wrapper.HttpContext, config config.PluginConfig, log wrapper.Log, stream bool) {
log.Debugf("QueryVectorDB key: %s", key)
activeVectorProvider := config.GetvectorProvider()
activeVectorProvider := config.GetVectorProvider()
log.Debugf("activeVectorProvider: %+v", activeVectorProvider)
activeVectorProvider.QueryEmbedding(text_embedding, ctx, log,
func(results []vector.QueryEmbeddingResult, ctx wrapper.HttpContext, log wrapper.Log) {
Expand Down
7 changes: 2 additions & 5 deletions plugins/wasm-go/extensions/ai-cache/embedding/dashscope.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,18 @@ func (d *DSProvider) constructParameters(texts []string, log wrapper.Log) (strin
},
}

// 序列化请求体并处理错误
requestBody, err := json.Marshal(data)
if err != nil {
log.Errorf("Failed to marshal request data: %v", err)
return "", nil, nil, err
}

// 检查 DashScopeKey 是否为空
if d.config.apiKey == "" {
err := errors.New("DashScopeKey is empty")
log.Errorf("Failed to construct headers: %v", err)
return "", nil, nil, err
}

// 设置请求头
headers := [][2]string{
{"Authorization", "Bearer " + d.config.apiKey},
{"Content-Type", "application/json"},
Expand Down Expand Up @@ -147,14 +144,14 @@ func (d *DSProvider) GetEmbedding(
ctx wrapper.HttpContext,
log wrapper.Log,
callback func(emb []float64)) error {
Emb_url, Emb_headers, Emb_requestBody, err := d.constructParameters([]string{queryString}, log)
embUrl, embHeaders, embRequestBody, err := d.constructParameters([]string{queryString}, log)
if err != nil {
log.Errorf("Failed to construct parameters: %v", err)
return err
}

var resp *Response
d.client.Post(Emb_url, Emb_headers, Emb_requestBody,
d.client.Post(embUrl, embHeaders, embRequestBody, // TODO: 函数调用返回的error要进行处理
func(statusCode int, responseHeaders http.Header, responseBody []byte) {
if statusCode != http.StatusOK {
log.Errorf("Failed to fetch embeddings, statusCode: %d, responseBody: %s", statusCode, string(responseBody))
Expand Down
1 change: 0 additions & 1 deletion plugins/wasm-go/extensions/ai-cache/embedding/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package embedding

import (
"errors"
"net/http"

"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
Expand Down

0 comments on commit 85549d0

Please sign in to comment.