Building RAG Agents for LLMS - Assessment Support - ValueError

Greetings,

I hope this post finds you well.

I’m nearing the end of my allotted time and I’m attempting to solve the assessment. However, I’ve come to a brick wall. I’ve gone through all the posts involving this course, but I cannot seem to get my retriever chain to work. The Generator is giving me an OK, but I’m getting the following error when I attempt to talk to the RAG agent.

File "/usr/local/lib/python3.11/site-packages/pydantic/v1/main.py", line 716, in validate
    value_as_dict = dict(value)
                    ^^^^^^^^^^^
ValueError: dictionary update sequence element #0 has length 1; 2 is required

This is my notebook code and the full respective error message I’m getting.

Do you think you could assist me @vkudlay ? Any help is appreciated to get me pointed in the correct direction.

Side Note: I suspect that as you’ve stated previously to another user, I’m doing too much in my retriever chain. Though I’m not sure what less I could possibly do.

Hey @mrpk91! Good stuff!
You are exactly correct. You are doing too much in your retriever chain (and also just in general). The source code in the frontend is already running, so you should not be re-implementing the things that are running there. That whole process with the chains_dict = {...} is what the frontend is actively doing to keep track of everything. As such, all you need to deploy on your langserve routes are the components that should slot into the system. For example, the following route - which defines “/retriever” - should deliver the functionality that chains_dict['retriever'] is there to achieve.

add_routes(  
    app,
    retrieval_chain,
    path="/retriever",
) 

Check out the example code in notebook 8, and you’ll find a section that looks very similar to the following block of code:

retrieval_chain = (
    {'input' : (lambda x: x)}
    | RunnableAssign(
        {'context' : itemgetter('input') 
        | chains_dict['retriever']    ## Except this line is different...
        | LongContextReorder().transform_documents
        | docs2str
    })
)

Maybe that will serve as a hint? Additionally, note that your generator chain is also too complicated, and may or may not work.

Thank you for the direction @vkudlay , I deeply appreciate it.

After what you told me and reviewing notebook 8, I made a the following changes to my code:

I removed the chains_dict variable since it is redundant.

chains_dict = {
    'basic' : RemoteRunnable("http://lab:9012/basic_chat/"),
    'retriever' : RemoteRunnable("http://lab:9012/retriever/"),
    'generator' : RemoteRunnable("http://lab:9012/generator/"),
}

I replaced the chains_dict['retriever'] in the retrieval chain with docstore.as_retriever(). I also decided to make it more in-line with how it is implemented in notebook 8 and made the following change:

From this.

retrieval_chain = (
    {'input' : (lambda x: x)}
    | RunnableAssign(
        {'context' : itemgetter('input') 
        | docstore.as_retriever()
        | LongContextReorder().transform_documents
        | docs2str
    })
)

To this.

long_reorder = RunnableLambda(LongContextReorder().transform_documents)
context_getter = itemgetter('input') | docstore.as_retriever() | long_reorder | docs2str
retrieval_chain = (
    {'input' : (lambda x: x)}
    | RunnableAssign({'context' : context_getter})
)

In addition to these changes to the retrieval chain, I modified the generator chain in response to it’s complexity.

I went from this:

generator_chain = (
	{"output": chains_dict['generator']} 
	| RunnableLambda(output_puller)
)

To this:

generator_chain = RunnableAssign({'output' : chat_prompt| llm})

Unfortunately, these changes have not solved my issue. I am still receiving the same error ValueError: dictionary update sequence element #0 has length 1; 2 is required. I took some time to try and figure out what in my code could be causing this, and the conclusion I came to was that the information I am passing to the retrieval chain is possibly not in the correct format or in the way it is expected to be received. I’m not confident that this is correct, but it is the best I can come up with.

I obviously don’t want you to give me the direct answer, but I think helping me better understand this error should allow me to come to a better conclusion. As of right now, I don’t know how to debug this error and any direction you provide will be helpful. If it helps, I’m doing this assignment in the course environment only. Thank you once again for the help so far :)

Side Note: If I run out of time, is there any way for me to get more?

My Current Code and the full error:

This is still too much, both for the generator and retriever.

### This line should be called by you to deliver your retrieval chain.
add_routes(  
    app,
    retrieval_chain,
    path="/retriever",
) 
### This line is perpetually running in the frontend. This should not be anywhere in your code. 
retrieval_chain = (
    {'input' : (lambda x: x)}
    | RunnableAssign(
        {'context' : itemgetter('input') 
        | chains_dict['retriever']   ### AKA RemoteRunnable("http://lab:9012/retriever/")  
        | LongContextReorder().transform_documents
        | docs2str
    })
)
1 Like

To address the other questions:

  • It is likely the same issue as encountered here: python - Error: “dictionary update sequence element #0 has length 1; 2 is required” on Django 1.4 - Stack Overflow. It’s a bit opaque, but it’s definitely happening because you are accidentally passing in something illegal. At some point, you are accidentally trying to cast a string to a dict along some protocol. This is likely because your retrieval chain is tossing back a dictionary with values {"context": "..."} instead of a list of documents.
  • Sure! Let me know if you run out of time (and include the email address of your account) and I can extend

Thank you! @vkudlay
With your insight and this post I was able to pass the course!

What really made me click was the following part of what you sent above.

| chains_dict['retriever']   ### AKA RemoteRunnable("http://lab:9012/retriever/")

Thank you very much, I deeply appreciate it!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.