Fix TextNode score handling - store in metadata instead
TextNode doesn't support a direct score attribute in this version of llama-index.
Instead, store the similarity score in the node's metadata during creation and
retrieve it from metadata during response formatting.
This ensures full compatibility with Pydantic validation and prevents attribute errors.
🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
2a0f07f951
commit
5a14284d62
1 changed files with 13 additions and 5 deletions
|
|
@ -58,11 +58,15 @@ class SimpleVectorRetriever:
|
|||
# Convert distance back to similarity score (1 - distance)
|
||||
similarity = float(1 - distance) # Convert numpy float to Python float
|
||||
|
||||
# Store similarity score in metadata since TextNode doesn't have a score attribute
|
||||
if metadata is None:
|
||||
metadata = {}
|
||||
metadata["similarity_score"] = similarity
|
||||
|
||||
node = TextNode(
|
||||
text=content,
|
||||
id_=doc_id,
|
||||
metadata=metadata,
|
||||
score=similarity # Pass score during initialization for Pydantic validation
|
||||
metadata=metadata
|
||||
)
|
||||
nodes.append(node)
|
||||
|
||||
|
|
@ -318,14 +322,18 @@ class RAGEngine:
|
|||
"""Format query response for return."""
|
||||
sources = []
|
||||
for node in source_nodes:
|
||||
# Convert score to Python float for JSON serialization
|
||||
score = node.score if hasattr(node, "score") else None
|
||||
# Retrieve score from metadata (stored during node creation)
|
||||
metadata = node.metadata if hasattr(node, "metadata") else {}
|
||||
score = metadata.get("similarity_score") if metadata else None
|
||||
if score is not None:
|
||||
score = float(score)
|
||||
|
||||
# Return metadata without the internal similarity_score
|
||||
display_metadata = {k: v for k, v in metadata.items() if k != "similarity_score"} if metadata else {}
|
||||
|
||||
sources.append({
|
||||
"text": node.text,
|
||||
"metadata": node.metadata if hasattr(node, "metadata") else {},
|
||||
"metadata": display_metadata,
|
||||
"score": score
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue