Similarity score not available (Knowledge graph) #323

Closed
opened 2026-02-16 00:17:28 -05:00 by yindo · 1 comment
Owner

Originally created by @JohnTaco93 on GitHub (Nov 6, 2024).

Hello I am getting this code. I want to get the similarity_top_k score but in the output I am just getting
one score (score=1000.0). I think the similarity_top_k (3) is related to 'kg_rel_texts': ["('Alice', 'Is friends with', 'Bob')", "('Bob', 'Is friends with', 'Alice')", "('Bob', 'Works at', 'Acme corp')"]
Then I would say I should get 3 scores. Please help.

code:

documents = SimpleDirectoryReader(....
).load_data()

llm = OpenAI(temperature=0, model="gpt-4o-mini")
Settings.llm = llm
Settings.chunk_size = 512

graph_store = SimpleGraphStore()
storage_context = StorageContext.from_defaults(graph_store=graph_store)

from llama_index.embeddings.openai import OpenAIEmbedding
embedding_model = OpenAIEmbedding(model='text-embedding-3-small') # or another embedding model

index = KnowledgeGraphIndex.from_documents(
documents,
max_triplets_per_chunk=3,
storage_context=storage_context,
embedding_model=embedding_model, # Ensure embeddings are generated
include_embeddings=True,
)

query_engine = index.as_query_engine(
include_text=False,
response_mode="tree_summarize",
embedding_mode="semantic",
similarity_top_k=3,
include_embeddings=True,
return_source_nodes=True
)

response = query_engine.query(
'Who is bob?',
)
print(response.response)

print(response.source_nodes[0])
NodeWithScore(node=TextNode(id_='5905cdf8-70dd-4417-a205-f6554379bde2', embedding=None, metadata={'kg_rel_texts': ["('Alice', 'Is friends with', 'Bob')", "('Bob', 'Is friends with', 'Alice')", "('Bob', 'Works at', 'Acme corp')"], 'kg_rel_map': {'bob': [], 'identity': [], 'person': [], 'details': [], 'individual': [], 'who': [], 'biography': [], 'information': []}}, excluded_embed_metadata_keys=['kg_rel_map', 'kg_rel_texts'], excluded_llm_metadata_keys=['kg_rel_map', 'kg_rel_texts'], relationships={}, text="The following are knowledge sequence in max depth 2 in the form of directed graph like:\nsubject -[predicate]->, object, <-[predicate_next_hop]-, object_next_hop ...\n('Alice', 'Is friends with', 'Bob')\n('Bob', 'Is friends with', 'Alice')\n('Bob', 'Works at', 'Acme corp')", mimetype='text/plain', start_char_idx=None, end_char_idx=None, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=1000.0)

Originally created by @JohnTaco93 on GitHub (Nov 6, 2024). Hello I am getting this code. I want to get the similarity_top_k score but in the output I am just getting one score (score=1000.0). I think the similarity_top_k (3) is related to 'kg_rel_texts': ["('Alice', 'Is friends with', 'Bob')", "('Bob', 'Is friends with', 'Alice')", "('Bob', 'Works at', 'Acme corp')"] Then I would say I should get 3 scores. Please help. ## code: documents = SimpleDirectoryReader(.... ).load_data() llm = OpenAI(temperature=0, model="gpt-4o-mini") Settings.llm = llm Settings.chunk_size = 512 graph_store = SimpleGraphStore() storage_context = StorageContext.from_defaults(graph_store=graph_store) from llama_index.embeddings.openai import OpenAIEmbedding embedding_model = OpenAIEmbedding(model='text-embedding-3-small') # or another embedding model index = KnowledgeGraphIndex.from_documents( documents, max_triplets_per_chunk=3, storage_context=storage_context, embedding_model=embedding_model, # Ensure embeddings are generated include_embeddings=True, ) query_engine = index.as_query_engine( include_text=False, response_mode="tree_summarize", embedding_mode="semantic", similarity_top_k=3, include_embeddings=True, return_source_nodes=True ) response = query_engine.query( 'Who is bob?', ) print(response.response) print(response.source_nodes[0]) NodeWithScore(node=TextNode(id_='5905cdf8-70dd-4417-a205-f6554379bde2', embedding=None, metadata={'kg_rel_texts': ["('Alice', 'Is friends with', 'Bob')", "('Bob', 'Is friends with', 'Alice')", "('Bob', 'Works at', 'Acme corp')"], 'kg_rel_map': {'bob': [], 'identity': [], 'person': [], 'details': [], 'individual': [], 'who': [], 'biography': [], 'information': []}}, excluded_embed_metadata_keys=['kg_rel_map', 'kg_rel_texts'], excluded_llm_metadata_keys=['kg_rel_map', 'kg_rel_texts'], relationships={}, text="The following are knowledge sequence in max depth 2 in the form of directed graph like:\n`subject -[predicate]->, object, <-[predicate_next_hop]-, object_next_hop ...`\n('Alice', 'Is friends with', 'Bob')\n('Bob', 'Is friends with', 'Alice')\n('Bob', 'Works at', 'Acme corp')", mimetype='text/plain', start_char_idx=None, end_char_idx=None, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=1000.0)
yindo added the bug label 2026-02-16 00:17:28 -05:00
yindo closed this issue 2026-02-16 00:17:28 -05:00
Author
Owner

@logan-markewich commented on GitHub (Nov 6, 2024):

Did you mean to create this on the llama-parse repo?

The knowledge graph index is deprecated. Please use the property graph index
https://docs.llamaindex.ai/en/stable/module_guides/indexing/lpg_index_guide/

@logan-markewich commented on GitHub (Nov 6, 2024): Did you mean to create this on the llama-parse repo? The knowledge graph index is deprecated. Please use the property graph index https://docs.llamaindex.ai/en/stable/module_guides/indexing/lpg_index_guide/
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: run-llama/llama_cloud_services#323