Implement the following plan: Plan: Modify ch05/11agenticrag/graph.py Context The user…
- User
Implement the following plan:
Plan: Modify ch05/11_agentic_rag/graph.py
Context
The user wants to update
graph.pyin the agentic RAG module to:- Replace the
JinaEmbeddingsmodel withOpenAIEmbeddingsusing a hardcoded Qwen model name - Accept
index_dirandcollection_nameviaargparse(instead of hardcoded values or env vars) - Add Chinese docstrings throughout for readability
Currently,
graph.pyinitializes the vectorstore and retriever at module level using hardcoded paths and runs the graph at module level too. Thesearch()node references a module-levelretrieverglobal.File to Modify
/Users/nanwang/Codes/nan/RAG-theory-to-code/ch05/11_agentic_rag/graph.pyDetailed Changes
1. Imports
- Remove:
from langchain_community.embeddings import JinaEmbeddings - Add:
import argparse(new import) - Keep:
from langchain_openai import OpenAIEmbeddings— OpenAIEmbeddings is already inlangchain_openaialongside ChatOpenAI
2. Extract initialization into
setup_retriever(index_dir, collection_name)Move lines 40–73 (vectorstore/retriever setup) into a dedicated function that accepts
index_dirandcollection_nameas parameters. This function will set the module-levelretrieverglobal so that node functions can still reference it.Key change inside: replace
JinaEmbeddings(model_name="jina-embeddings-v3")withOpenAIEmbeddings(model="Qwen/Qwen3-Embedding-0.6B").3. Extract graph compilation into
build_graph()Move lines 241–255 (builder + compile) into a
build_graph()function that returns the compiled graph.4. Add
main()with argparse--index-dir(default:../../data_chroma_jina_embeddings)--collection-name(default:olympic_games)- Call
setup_retriever(args.index_dir, args.collection_name) - Call
build_graph() - Invoke graph with test query
5. Declare global
retrieverat module level (set to None), then assign in - Replace the
Assistant
Now I'll implement all the changes from the plan.
Assistant
Let me verify the final file looks correct:
Assistant
The file looks correct. Let me also check the
.env.exampleto see if it needs updating (since we removedJinaEmbeddingsand may needOPENAI_API_BASEfor the embedding model):Assistant
The
.env.examplecomment forJINA_API_KEYstill mentionsJinaEmbeddings, which is no longer used for embeddings. Update the comment:Assistant
All changes are complete. Here's a summary of what was done:
graph.pychanges:-
Imports — Removed
JinaEmbeddings, addedargparse, addedOpenAIEmbeddings(alongside existingChatOpenAIfromlangchain_openai) -
retriever = None— Declared as a module-level global (line 63), assigned insidesetup_retriever() -
setup_retriever(index_dir, collection_name)(lines 66–101) — All vectorstore/retriever init code moved here; usesOpenAIEmbeddings(model="Qwen/Qwen3-Embedding-0.6B")instead ofJinaEmbeddings; raisesFileNotFoundErrorif the directory doesn't exist -
build_graph()(lines 319–340) — Graph builder/compile extracted into a function returning the compiled graph -
main()(lines 343–361) — Parses--index-dirand--collection-nameCLI args, callssetup_retriever()thenbuild_graph(), then runs the test query
-
- User
in @ch05/11_agentic_rag/graph.py, when
load_documents, use--index_input_dirto get the input from the cli argument. Load all the .txt files fromindex_input_dir. Assistant
Three changes needed:
get_all_splits()acceptsinput_dir,setup_retriever()passes it through, andmain()adds the CLI arg.Assistant
The three changes made:
get_all_splits(input_dir)— now acceptsinput_dirand usesf"{input_dir}/*.txt"instead of the hardcoded pathsetup_retriever(..., input_dir)— addedinput_dirparameter, passes it toget_all_splits(input_dir)main()— added--index-input-dirargparse argument (default../../data), passesargs.index_input_dirtosetup_retriever()
Usage:
bashpython graph.py --index-input-dir ../../data --index-dir ../../data_chroma_qwen_embeddings- User
make a commit
Assistant
Committed as
14e636e:graph.py— all refactoring changes.env.example— updated Jina comment + added missing env varsconfiguration.py— updated defaultreasoning_llm
- User
rename @ch05/11_agentic_rag/graph.py to main.py. Update @ch05/11_agentic_rag/README.md accordingly.
Assistant
Done.
graph.pyis nowmain.py, and the README table and run command have been updated to match.- User
update @ch05/11_agentic_rag/README.md. Create a new section with the name
运行步骤to incluce the example shell scripts for runningmain.py. Add minimal explaination in Chinese. Assistant
Done. The README now has a
运行步骤section with:- Instructions to copy and fill in
.env - A default-args run command
- A full explicit-args example
- A parameter reference table explaining all three CLI flags
- Instructions to copy and fill in