run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
37.13k stars 5.33k forks source link

[Question]: Get Node with score with astreamchat #17122

Open mirallm opened 2 days ago

mirallm commented 2 days ago

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):

self.chat_engine = self.index_handler.vector_index.as_chat_engine(
    chat_mode="condense_plus_context",
    memory=self.memory,
    llm=self.llm,
    context_prompt=(
        "You are a chatbot, You always answer in the language of the query."
        "You only answer questions based on the context"
        "Here are the relevant documents for the context:\n"
        "{context_str}"
        "\nInstruction: Use the previous chat history, or the context above, to interact and help the user."
        "Do NOT answer questions that are not related to the context or any normal interactions. always say I am sorry in that case."
    ),
    verbose=False,
    streaming=True,
    use_async=True

)

async def get_response(self, text: str): try:

    response = await self.chat_engine.astream_chat(text)

i cant get it

can you please let me know with astreamchat

dosubot[bot] commented 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.

mirallm commented 2 days ago

@dosu when i do source_nodes[0].score, i get this error ^^^^^^^^^^^^^^^^^^^^^ | AttributeError: 'dict' object has no attribute 'score'

dosubot[bot] commented 2 days ago

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.