Open mirallm opened 2 days ago
To retrieve the node with its score using astream_chat
in the chat engine setup, you can access the source_nodes
attribute from the StreamingAgentChatResponse
returned by the astream_chat
method. This attribute contains the nodes along with their scores. Here's a brief example of how you can do this:
async def get_response(self, text: str):
try:
response = await self.chat_engine.astream_chat(text)
for node in response.source_nodes:
print("Node score:", node.score)
print("Node text:", node.text)
print("Node metadata:", node.metadata)
except Exception as e:
print("Error:", e)
In this example, response.source_nodes
provides access to the list of nodes with their scores, which you can iterate over to print or process as needed [1][2].
To continue talking to Dosu, mention @dosu.
@dosu when i do source_nodes[0].score, i get this error ^^^^^^^^^^^^^^^^^^^^^ | AttributeError: 'dict' object has no attribute 'score'
The error you're encountering suggests that the elements in source_nodes
are being returned as dictionaries instead of NodeWithScore
objects. This is unexpected, as source_nodes
is typically a list of NodeWithScore
objects, which have attributes like score
and node
[1][2].
To troubleshoot this issue, you can check the type of the elements in source_nodes
before accessing their attributes. Here's how you can modify your code to handle both cases:
async def get_response(self, text: str):
try:
response = await self.chat_engine.astream_chat(text)
for node in response.source_nodes:
if isinstance(node, dict):
print("Node score:", node.get('score'))
print("Node text:", node.get('text'))
print("Node metadata:", node.get('metadata'))
else:
print("Node score:", node.score)
print("Node text:", node.node.text) # Assuming node.node has a text attribute
print("Node metadata:", node.node.metadata) # Assuming node.node has a metadata attribute
except Exception as e:
print("Error:", e)
This code checks if each element is a dictionary and accesses its values accordingly. If it's not a dictionary, it assumes the element is a NodeWithScore
object and accesses its attributes directly.
To continue talking to Dosu, mention @dosu.
Question Validation
Question
@dosu @logan-markewich
when i use : response = retriever.retrieve(query) for node in response: print("node", node.score) print("node", node.text) print("node", node.metadata)
i can get it
but when i use sync def setup_chatengine(self):
async def get_response(self, text: str): try:
i cant get it
can you please let me know with astreamchat