mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-25 18:03:29 +08:00
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:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user