RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
Go: unify tofloat (#20797)
Jin Hai49871eebd7bd
diff --git a/internal/agent/canvas/loop_subgraph.go b/internal/agent/canvas/loop_subgraph.goindex 11230b395c..d74762fcf8 100644--- a/internal/agent/canvas/loop_subgraph.go+++ b/internal/agent/canvas/loop_subgraph.go@@ -38,6 +38,7 @@ import ("context""encoding/json""fmt"+ "ragflow/internal/common""slices""strings"@@ -717,7 +718,7 @@ func evalBoolOp(b bool, op string, value any) (bool, error) {}func evalNumberOp(n float64, op string, value any) (bool, error) {- cmp, ok := toFloat(value)+ cmp, ok := common.ToFloat(value)if !ok && !isNilOp(op) {return false, fmt.Errorf("invalid operator: %s (number variable, non-numeric value)", op)}@@ -786,22 +787,6 @@ func listEqual(lst []any, value any) bool {return true}-func toFloat(v any) (float64, bool) {- switch x := v.(type) {- case float64:- return x, true- case float32:- return float64(x), true- case int:- return float64(x), true- case int32:- return float64(x), true- case int64:- return float64(x), true- }- return 0, false-}-func isNilOp(op string) bool {return op == "empty" || op == "not empty"}diff --git a/internal/common/type_conversion.go b/internal/common/type_conversion.goindex cbc485685a..a0e8edb74c 100644--- a/internal/common/type_conversion.go+++ b/internal/common/type_conversion.go@@ -17,6 +17,7 @@package commonimport (+ "encoding/json""strconv""strings")@@ -65,3 +66,31 @@ func ToIntNum(v any) (int, bool) {}return 0, false}++func ToFloat(v any) (float64, bool) {+ switch x := v.(type) {+ case float64:+ return x, true+ case float32:+ return float64(x), true+ case int:+ return float64(x), true+ case int32:+ return float64(x), true+ case int64:+ return float64(x), true+ case json.Number:+ f, err := x.Float64()+ if err != nil {+ return 0, false+ }+ return f, true+ case string:+ f, err := strconv.ParseFloat(strings.TrimSpace(x), 64)+ if err != nil {+ return 0, false+ }+ return f, true+ }+ return 0, false+}diff --git a/internal/ingestion/component/chunker/positions_slice.go b/internal/ingestion/component/chunker/positions_slice.goindex 8462348b21..59515f54d2 100644--- a/internal/ingestion/component/chunker/positions_slice.go+++ b/internal/ingestion/component/chunker/positions_slice.go@@ -18,33 +18,10 @@ package chunkerimport ("encoding/json"+ "ragflow/internal/common""unicode/utf8")-// toFloat converts a JSON number (int, int64, float64) to float64.-func toFloat(v any) (float64, bool) {- switch x := v.(type) {- case float64:- return x, true- case float32:- return float64(x), true- case int:- return float64(x), true- case int64:- return float64(x), true- case int32:- return float64(x), true- case json.Number:- f, err := x.Float64()- if err != nil {- return 0, false- }- return f, true- default:- return 0, false- }-}-// slicePositionsByTextRatio proportionally slices a PDF position matrix// (JSON array of [page, left, right, top, bottom] rows) by vertical ratio.//@@ -84,8 +61,8 @@ func slicePositionsByTextRatio(raw json.RawMessage, startRatio, endRatio float64if len(row) < 5 {return nil}- top, ok1 := toFloat(row[3])- bottom, ok2 := toFloat(row[4])+ top, ok1 := common.ToFloat(row[3])+ bottom, ok2 := common.ToFloat(row[4])if !ok1 || !ok2 || bottom <= top {return nil}@@ -121,8 +98,8 @@ func slicePositionsByTextRatio(raw json.RawMessage, startRatio, endRatio float64}localStartRatio := (interStart - segStart) / hlocalEndRatio := (interEnd - segStart) / h- oldTop, _ := toFloat(row[3])- oldBottom, _ := toFloat(row[4])+ oldTop, _ := common.ToFloat(row[3])+ oldBottom, _ := common.ToFloat(row[4])oldH := oldBottom - oldTopnewTop := oldTop + localStartRatio*oldHnewBottom := oldTop + localEndRatio*oldHdiff --git a/internal/rag/agentic-rag/graph_util.go b/internal/rag/agentic-rag/graph_util.goindex 8f0c4b669a..cf9fe60100 100644--- a/internal/rag/agentic-rag/graph_util.go+++ b/internal/rag/agentic-rag/graph_util.go@@ -38,10 +38,10 @@ import (// It was written three times (the SCA view, the draft's ordering, the citation// reference) before it lived here.func similarityOrScore(c map[string]any) float64 {- if v, ok := toFloat(c["similarity"]); ok && v != 0 {+ if v, ok := common.ToFloat(c["similarity"]); ok && v != 0 {return v}- if v, ok := toFloat(c["score"]); ok && v != 0 {+ if v, ok := common.ToFloat(c["score"]); ok && v != 0 {return v}return 0.0@@ -137,24 +137,6 @@ func asSliceOfAny(v any) []any {}}-// toFloat parses a numeric value.-func toFloat(v any) (float64, bool) {- switch x := v.(type) {- case float64:- return x, true- case int:- return float64(x), true- case int64:- return float64(x), true- case string:- var f float64- if _, err := fmt.Sscanf(x, "%f", &f); err == nil {- return f, true- }- }- return 0, false-}-// anyString reads a string field from a chunk-like map.func anyString(v any) string {switch x := v.(type) {diff --git a/internal/rag/agentic-rag/runtime/action_session.go b/internal/rag/agentic-rag/runtime/action_session.goindex fff1a393d3..a1bd00061d 100644--- a/internal/rag/agentic-rag/runtime/action_session.go+++ b/internal/rag/agentic-rag/runtime/action_session.go@@ -316,7 +316,7 @@ func ApplyPatch(base State, branchPatches []map[string]any) *State {changed = true}if raw, has := pv["candidate_strength"]; has && raw != nil {- if f, ok := toFloat(raw); ok {+ if f, ok := common.ToFloat(raw); ok {v := math.Min(math.Max(f, 0.0), 1.0)nv.CandidateStrength = &vchanged = true@@ -356,25 +356,6 @@ func truncateRunes(s string, n int) string {return string([]rune(s)[:n])}-// toFloat coerces a JSON-decoded value to float64.-func toFloat(v any) (float64, bool) {- switch n := v.(type) {- case float64:- return n, true- case float32:- return float64(n), true- case int:- return float64(n), true- case int32:- return float64(n), true- case int64:- return float64(n), true- case string:- return ParseFloat(n)- }- return 0, false-}-// ParseFloat parses a float64 from a string, tolerating surrounding// whitespace. Exported for the orchestrator package, whose SCA verdict// coercion needs the same lenient parsing.diff --git a/internal/rag/agentic-rag/runtime/orchestrator/sufficient_context.go b/internal/rag/agentic-rag/runtime/orchestrator/sufficient_context.goindex 2e9743f3ba..489a175127 100644--- a/internal/rag/agentic-rag/runtime/orchestrator/sufficient_context.go+++ b/internal/rag/agentic-rag/runtime/orchestrator/sufficient_context.go@@ -19,6 +19,7 @@ package orchestratorimport ("context""fmt"+ "ragflow/internal/common""regexp""strings""unicode/utf8"@@ -503,7 +504,7 @@ func coerceDict(v any) map[string]any {// clamp: coerce to [0,1], defaulting to 1.0 on failure.func clamp(v any) float64 {- f, ok := toFloat(v)+ f, ok := common.ToFloat(v)if !ok {return 1.0}@@ -609,19 +610,3 @@ func containsStr(ss []string, s string) bool {}return false}--func toFloat(v any) (float64, bool) {- switch n := v.(type) {- case float64:- return n, true- case float32:- return float64(n), true- case int:- return float64(n), true- case int64:- return float64(n), true- case string:- return runtime.ParseFloat(n)- }- return 0, false-}