The code creates a retrieval-based AI system using several language model and retrieval libraries, particularly LangChain, Cassio, and LangGraph. Below is a breakdown of the code:
The first few lines install necessary libraries for handling language models, embeddings, and vector storage. Packages such as langchain, langgraph, cassio, langchain_community, and chromadb are key tools for document retrieval, text splitting, and vector search.
import cassio
ASTRA_DB_APPLICATION_TOKEN = "..."
ASTRA_DB_ID = "..."
cassio.init(token=ASTRA_DB_APPLICATION_TOKEN, database_id=ASTRA_DB_ID)
This connects to Astra DB (a cloud-based database by DataStax) using a secure token and ID. This allows storage and retrieval of document vectors.
-
Loading Documents: Using URLs, the code loads documents through the WebBaseLoader.
-
Splitting Text: Texts are split into manageable chunks (500 characters, no overlap) for indexing, improving search efficiency.
-
Embedding Creation: The HuggingFaceEmbeddings model ("all-MiniLM-L6-v2") is used to convert text into vector embeddings.
-
Storing Embeddings in Cassandra: The Cassandra vector store is used to store the generated embeddings. This allows fast retrieval of semantically similar documents.
retriever = astra_vector_store.as_retriever()
retriever.invoke("What is agent", ConsistencyLevel="LOCAL_ONE")
This creates a retriever for querying the stored embeddings. When queried (e.g., "What is agent"), it will find the most relevant documents from the stored vectors.
- Routing Model: The RouteQuery class is defined to decide if the question should be answered by the vector store or Wikipedia.
- Prompt Template: The system prompt is set to route questions to Wikipedia if they're not related to "agents", "prompt engineering", or "adversarial attacks".
- LLM-Based Router: Using ChatGroq (a language model), a prompt router decides the data source for each query.
ArxivAPIWrapper and WikipediaAPIWrapper are tools to query academic articles and Wikipedia articles, respectively, allowing the code to access broader information.
class GraphState(TypedDict):
question: str
generation: str
documents: List[str]
This defines the graph state for storing question data, generated responses, and document lists.
-
retrieve: Retrieves documents using the vector store.
-
wiki_search: Retrieves documents using Wikipedia.
def route_question(state):
source = question_router.invoke({"question": question})
if source.datasource == "wiki_search":
return "wiki_search"
elif source.datasource == "vectorstore":
return "vectorstore"
This function routes a question to either Wikipedia or the vector store based on its content.
workflow = StateGraph(GraphState)
This creates a graph workflow that conditionally calls either the wiki_search or retrieve nodes based on the route determined by route_question.
The workflow is executed with example questions, routing and fetching the relevant answers for each. It prints out the graph's decisions, outputting either Wikipedia articles or vector store results.
The code displays the graph with Mermaid and prints the final documents, depending on whether the search was routed to Wikipedia or the vector store.