Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.env
venv/
/__pycache__/
*.pyc

# Byte-compiled / optimized / DLL files
.DS_Store
*.egg-info/
*.egg
*.whl
*.pdb
*.pyo
*.pyd
.vscode/
1 change: 0 additions & 1 deletion 27.Code_Explainer/app.py

This file was deleted.

Binary file removed 27.Code_Explainer/faiss_index/index.faiss
Binary file not shown.
Binary file removed 27.Code_Explainer/faiss_index/index.pkl
Binary file not shown.
117 changes: 117 additions & 0 deletions 28.Advance_RAG/Cohere_reranker.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "5b3e05c6",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"USER_AGENT environment variable not set, consider setting it to identify your requests.\n"
]
}
],
"source": [
"# Load blog\n",
"import bs4\n",
"from langchain_community.document_loaders import WebBaseLoader\n",
"from dotenv import load_dotenv\n",
"\n",
"load_dotenv()\n",
"\n",
"loader = WebBaseLoader(\n",
" web_paths=(\"https://lilianweng.github.io/posts/2023-06-23-agent/\",),\n",
" bs_kwargs=dict(\n",
" parse_only=bs4.SoupStrainer(\n",
" class_=(\"post-content\", \"post-title\", \"post-header\")\n",
" )\n",
" ),\n",
")\n",
"blog_docs = loader.load()\n",
"\n",
"# Split\n",
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
"text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(\n",
" chunk_size=300, \n",
" chunk_overlap=50)\n",
"\n",
"# Make splits\n",
"splits = text_splitter.split_documents(blog_docs)\n",
"\n",
"# Index\n",
"from langchain_openai import OpenAIEmbeddings\n",
"# from langchain_cohere import CohereEmbeddings\n",
"from langchain_community.vectorstores import Chroma\n",
"vectorstore = Chroma.from_documents(documents=splits, \n",
" # embedding=CohereEmbeddings()\n",
" embedding=OpenAIEmbeddings())\n",
"\n",
"\n",
"# retriever = vectorstore.as_retriever()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "10625350",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Document(metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/', 'relevance_score': 0.998844}, page_content='Component One: Planning#\\nA complicated task usually involves many steps. An agent needs to know what they are and plan ahead.\\nTask Decomposition#\\nChain of thought (CoT; Wei et al. 2022) has become a standard prompting technique for enhancing model performance on complex tasks. The model is instructed to “think step by step” to utilize more test-time computation to decompose hard tasks into smaller and simpler steps. CoT transforms big tasks into multiple manageable tasks and shed lights into an interpretation of the model’s thinking process.\\nTree of Thoughts (Yao et al. 2023) extends CoT by exploring multiple reasoning possibilities at each step. It first decomposes the problem into multiple thought steps and generates multiple thoughts per step, creating a tree structure. The search process can be BFS (breadth-first search) or DFS (depth-first search) with each state evaluated by a classifier (via a prompt) or majority vote.\\nTask decomposition can be done (1) by LLM with simple prompting like \"Steps for XYZ.\\\\n1.\", \"What are the subgoals for achieving XYZ?\", (2) by using task-specific instructions; e.g. \"Write a story outline.\" for writing a novel, or (3) with human inputs.'),\n",
" Document(metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/', 'relevance_score': 0.99293363}, page_content='LLM Powered Autonomous Agents\\n \\nDate: June 23, 2023 | Estimated Reading Time: 31 min | Author: Lilian Weng\\n\\n\\nBuilding agents with LLM (large language model) as its core controller is a cool concept. Several proof-of-concepts demos, such as AutoGPT, GPT-Engineer and BabyAGI, serve as inspiring examples. The potentiality of LLM extends beyond generating well-written copies, stories, essays and programs; it can be framed as a powerful general problem solver.\\nAgent System Overview#\\nIn a LLM-powered autonomous agent system, LLM functions as the agent’s brain, complemented by several key components:\\n\\nPlanning\\n\\nSubgoal and decomposition: The agent breaks down large tasks into smaller, manageable subgoals, enabling efficient handling of complex tasks.\\nReflection and refinement: The agent can do self-criticism and self-reflection over past actions, learn from mistakes and refine them for future steps, thereby improving the quality of final results.\\n\\n\\nMemory'),\n",
" Document(metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/', 'relevance_score': 0.9695556}, page_content='Finite context length: The restricted context capacity limits the inclusion of historical information, detailed instructions, API call context, and responses. The design of the system has to work with this limited communication bandwidth, while mechanisms like self-reflection to learn from past mistakes would benefit a lot from long or infinite context windows. Although vector stores and retrieval can provide access to a larger knowledge pool, their representation power is not as powerful as full attention.\\n\\n\\nChallenges in long-term planning and task decomposition: Planning over a lengthy history and effectively exploring the solution space remain challenging. LLMs struggle to adjust plans when faced with unexpected errors, making them less robust compared to humans who learn from trial and error.\\n\\n\\nReliability of natural language interface: Current agent system relies on natural language as an interface between LLMs and external components such as memory and tools. However, the reliability of model outputs is questionable, as LLMs may make formatting errors and occasionally exhibit rebellious behavior (e.g. refuse to follow an instruction). Consequently, much of the agent demo code focuses on parsing model output.\\n\\n\\nCitation#\\nCited as:\\n\\nWeng, Lilian. (Jun 2023). “LLM-powered Autonomous Agents”. Lil’Log. https://lilianweng.github.io/posts/2023-06-23-agent/.')]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_community.llms import Cohere\n",
"from langchain.retrievers import ContextualCompressionRetriever\n",
"from langchain.retrievers.document_compressors import CohereRerank\n",
"\n",
"load_dotenv()\n",
"llm = Cohere(temperature=0)\n",
"compressor = CohereRerank(model=\"rerank-english-v3.0\")\n",
"\n",
"retriever = vectorstore.as_retriever(search_kwargs={\"k\": 10})\n",
"compression_retriever = ContextualCompressionRetriever(\n",
" base_compressor=compressor, base_retriever=retriever\n",
")\n",
"\n",
"\n",
"question = \"What is task decomposition for LLM agents?\"\n",
"compressed_docs = compression_retriever.get_relevant_documents(question)\n",
"compressed_docs"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
163 changes: 163 additions & 0 deletions 28.Advance_RAG/Logical_Routing.ipynb

Large diffs are not rendered by default.

124 changes: 124 additions & 0 deletions 28.Advance_RAG/Query_Translation_Hyde.ipynb

Large diffs are not rendered by default.

115 changes: 115 additions & 0 deletions 28.Advance_RAG/Query_Translation_MultiQuery.ipynb

Large diffs are not rendered by default.

133 changes: 133 additions & 0 deletions 28.Advance_RAG/Query_Translation_RAG_Fusion.ipynb

Large diffs are not rendered by default.

171 changes: 171 additions & 0 deletions 28.Advance_RAG/Query_Translation_Step_Back.ipynb

Large diffs are not rendered by default.

234 changes: 234 additions & 0 deletions 28.Advance_RAG/Query_decomposition.ipynb

Large diffs are not rendered by default.

133 changes: 133 additions & 0 deletions 28.Advance_RAG/Semantic_Routing.ipynb

Large diffs are not rendered by default.

Loading