Go: refactor embedding interface (#14757)

### What problem does this PR solve?

Provide embedding index according to the input text

### Type of change

- [x] Refactoring

---------

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-05-11 14:45:30 +08:00
committed by GitHub
parent 5ef7f50eef
commit c55e23e7e2
28 changed files with 443 additions and 567 deletions

View File

@@ -277,6 +277,48 @@ func (r *KeyValueResponse) PrintOut() {
}
}
type EmbeddingData struct {
Index int `json:"index"`
Embedding []float64 `json:"embedding"`
}
type EmbeddingsResponse struct {
Code int `json:"code"`
Data []EmbeddingData `json:"data"`
Message string `json:"message"`
Duration float64
OutputFormat OutputFormat
}
func (r *EmbeddingsResponse) Type() string {
return "common"
}
func (r *EmbeddingsResponse) TimeCost() float64 {
return r.Duration
}
func (r *EmbeddingsResponse) SetOutputFormat(format OutputFormat) {
r.OutputFormat = format
}
func (r *EmbeddingsResponse) PrintOut() {
var data []map[string]interface{}
for _, embedding := range r.Data {
data = append(data, map[string]interface{}{
"index": formatValue(embedding.Index),
"dimension": len(embedding.Embedding),
})
}
if r.Code == 0 {
PrintTableSimpleByFormat(data, r.OutputFormat)
} else {
fmt.Println("ERROR")
fmt.Printf("%d, %s\n", r.Code, r.Message)
}
}
// ==================== ContextEngine Commands ====================
// ContextListResponse represents the response for ls command
@@ -325,9 +367,9 @@ func (r *ContextSearchResponse) PrintOut() {
// ContextCatResponse represents the response for cat command
type ContextCatResponse struct {
Code int `json:"code"`
Content string `json:"content"`
Message string `json:"message"`
Code int `json:"code"`
Content string `json:"content"`
Message string `json:"message"`
Duration float64
OutputFormat OutputFormat
}
@@ -343,5 +385,3 @@ func (r *ContextCatResponse) PrintOut() {
fmt.Printf("%d, %s\n", r.Code, r.Message)
}
}

View File

@@ -1838,7 +1838,7 @@ func (c *RAGFlowClient) EmbedUserText(cmd *Command) (ResponseIf, error) {
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to embed text: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonResponse
var result EmbeddingsResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("embed text failed: invalid JSON (%w)", err)
}