langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
92.26k stars 14.73k forks source link

DOC: issue with rag tutorial code : name 'llm' is not defined #23958

Closed alkuma closed 2 months ago

alkuma commented 2 months ago

URL

https://python.langchain.com/v0.2/docs/tutorials/rag/

Checklist

Issue with current documentation:

The documentation and corresponding code provided with https://python.langchain.com/v0.2/docs/tutorials/rag/ has an issue, the moment I run the stub with this pipeline

rag_chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

this error is thrown :

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[1], line 37
     30 def format_docs(docs):
     31     return "\n\n".join(doc.page_content for doc in docs)
     34 rag_chain = (
     35         {"context": retriever | format_docs, "question": RunnablePassthrough()}
     36         | prompt
---> 37         | llm
     38         | StrOutputParser()
     39 )
     41 rag_chain.invoke("What is Task Decomposition?")

NameError: name 'llm' is not defined

It loooks like there needs to be a change to either the installations before running this cell or there is something wrong with the code in the cell.

Either way the document must be updated so that this error does not occur.

Idea or request for content:

If this issue is specific to a particular python / pip version then there can be a "if you get this kind of error" section where this is highlighted along with resolution

alkuma commented 2 months ago

This got resolved, I had the llm instantiation in a different cell in my jupyter notebook and it was not reflecting in the context probably because of a restart.

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-3.5-turbo-0125")

rerunning this resolved the issue, so closing it.