Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UNIT] llama-index framework - release 3/3/25 #22

Open
wants to merge 72 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
f5ae4bd
Add general outline llama-index based on TOC
davidberenstein1957 Feb 4, 2025
151f6c3
Add LlamaIndex framework documentation files
davidberenstein1957 Feb 7, 2025
6a1938f
Merge branch 'main' into frameworks/llama-index
davidberenstein1957 Feb 7, 2025
d4ae36c
Update LlamaIndex documentation with comprehensive framework overview
davidberenstein1957 Feb 7, 2025
98373b9
Refine LlamaIndex documentation with clearer language and improved ex…
davidberenstein1957 Feb 7, 2025
f74d175
Restructure LlamaIndex documentation with detailed components and int…
davidberenstein1957 Feb 10, 2025
c454a03
Merge branch 'main' into frameworks/llama-index
davidberenstein1957 Feb 10, 2025
5e1a7e5
Refactor LlamaIndex documentation: Update content structure and remov…
davidberenstein1957 Feb 11, 2025
7cb76e5
Update LlamaIndex components documentation with detailed QueryEngine …
davidberenstein1957 Feb 11, 2025
43e8a2f
Merge branch 'main' into frameworks/llama-index
davidberenstein1957 Feb 11, 2025
7b82baa
Reorganize Unit 2 LlamaIndex content and update README
davidberenstein1957 Feb 11, 2025
0ae5664
Update LlamaIndex documentation with detailed tools and agent creatio…
davidberenstein1957 Feb 11, 2025
cb74d10
Update LlamaIndex agents and tools documentation with comprehensive e…
davidberenstein1957 Feb 12, 2025
fb992bf
Update LlamaIndex unit structure and add new documentation files for …
davidberenstein1957 Feb 12, 2025
0392515
Refactor LlamaIndex documentation with comprehensive workflow and mul…
davidberenstein1957 Feb 12, 2025
19d5c0e
Add workflow creation guide and evaluation techniques in LlamaIndex d…
davidberenstein1957 Feb 12, 2025
42315c8
Add LlamaIndex workflow documentation link and introduction to AgentW…
davidberenstein1957 Feb 12, 2025
21d4078
Enhance LlamaIndex components documentation with detailed storage and…
davidberenstein1957 Feb 13, 2025
10914a2
Enhance LlamaIndex workflow documentation with comprehensive examples…
davidberenstein1957 Feb 13, 2025
dd00899
Remove best practices section from LlamaIndex workflow documentation
davidberenstein1957 Feb 13, 2025
d25186e
Apply suggestions from code review
davidberenstein1957 Feb 13, 2025
209e026
Apply suggestions from code review
davidberenstein1957 Feb 13, 2025
f16e94e
Update LlamaIndex documentation with refined explanations of componen…
davidberenstein1957 Feb 13, 2025
9cc0aef
Merge branch 'frameworks/llama-index' of https://github.com/huggingfa…
davidberenstein1957 Feb 13, 2025
e48c3c7
Refactor LlamaIndex documentation with improved workflow and tool cre…
davidberenstein1957 Feb 13, 2025
acbf8e7
Improve QueryEngine documentation with narrative context and Alfred a…
davidberenstein1957 Feb 13, 2025
37e1fd6
Reorganize LlamaIndex documentation structure and update content flow
davidberenstein1957 Feb 13, 2025
220f67e
Update LlamaIndex documentation file structure and content for unit2
davidberenstein1957 Feb 13, 2025
a5b2b04
Refactor LlamaIndex README with updated file structure and section ti…
davidberenstein1957 Feb 13, 2025
fd8c94f
Remove LlamaIndex introduction file from unit2
davidberenstein1957 Feb 13, 2025
ba29b29
Update introduction and LlamaIndex agents documentation with refined …
davidberenstein1957 Feb 13, 2025
abce3cb
Merge branch 'main' into frameworks/llama-index
davidberenstein1957 Feb 18, 2025
fc4d32f
Add LlamaHub documentation and update LlamaIndex unit2 content
davidberenstein1957 Feb 18, 2025
5260e93
Add LlamaHub documentation and update LlamaIndex unit2 content
davidberenstein1957 Feb 18, 2025
2ce32bb
Update LlamaIndex unit2 toctree with LlamaHub section
davidberenstein1957 Feb 18, 2025
6b212ef
Add visual elements to LlamaIndex tools documentation
davidberenstein1957 Feb 18, 2025
cd820b9
Update LlamaTrace installation instructions in components documentation
davidberenstein1957 Feb 18, 2025
32f45a7
Enhance LlamaIndex components documentation with RAG explanation
davidberenstein1957 Feb 18, 2025
81de803
Update LlamaIndex documentation with visual and formatting improvements
davidberenstein1957 Feb 18, 2025
eec73d8
Refine LlamaIndex documentation with improved text emphasis and forma…
davidberenstein1957 Feb 18, 2025
784d645
Update installation instructions for Google Toolspec and Workflow pac…
davidberenstein1957 Feb 18, 2025
1817bc3
Enhance LlamaIndex workflows documentation with improved explanations…
davidberenstein1957 Feb 18, 2025
bb811b4
Convert external link to HTML anchor tag in workflows documentation
davidberenstein1957 Feb 18, 2025
4931b95
Merge branch 'main' into frameworks/llama-index
davidberenstein1957 Feb 19, 2025
1f95517
Add quizzes for LlamaIndex unit2 documentation
davidberenstein1957 Feb 19, 2025
b9b0d53
Merge branch 'frameworks/llama-index' of https://github.com/huggingfa…
davidberenstein1957 Feb 19, 2025
b6c8074
Reorder LlamaIndex unit2 navigation in _toctree.yml
davidberenstein1957 Feb 19, 2025
52dc2a7
Add conclusion section to LlamaIndex unit and update section titles
davidberenstein1957 Feb 25, 2025
1d4e0a1
Remove Unit 2 introduction and README, simplify LlamaIndex section in…
davidberenstein1957 Feb 25, 2025
ab462e7
Remove Unit 2 quiz data file for LlamaIndex
davidberenstein1957 Feb 25, 2025
032d15f
Update quiz2 conclusion text for LlamaIndex unit
davidberenstein1957 Feb 25, 2025
b145177
Update LlamaIndex components documentation with Tip component and lin…
davidberenstein1957 Feb 25, 2025
3d0c0a1
Add conclusion content for LlamaIndex unit module
davidberenstein1957 Feb 25, 2025
8d23f02
Update image paths in LlamaIndex unit2 documentation
davidberenstein1957 Feb 25, 2025
f39ca58
Enhance LlamaIndex components documentation with RAG explanation and …
davidberenstein1957 Feb 25, 2025
36954da
Refine text description of Alfred's information processing in LlamaIn…
davidberenstein1957 Feb 25, 2025
962845d
Apply suggestions from code review
davidberenstein1957 Feb 27, 2025
7006f3f
Refine LlamaIndex unit documentation with updates to agents, componen…
davidberenstein1957 Feb 27, 2025
b266339
Apply suggestions from code review
davidberenstein1957 Feb 27, 2025
7403844
Merge branch 'main' into frameworks/llama-index
davidberenstein1957 Feb 27, 2025
35ec4ce
Update HuggingFace API import in agents documentation
davidberenstein1957 Feb 27, 2025
d34513b
Add LlamaIndex introduction and update toctree structure
davidberenstein1957 Feb 27, 2025
d965ba6
Minor language and formatting improvements across LlamaIndex document…
davidberenstein1957 Feb 27, 2025
097a7b7
Update LlamaIndex documentation with minor improvements and fixes
davidberenstein1957 Feb 27, 2025
c38b15a
Remove deprecated LlamaHub notebook from LlamaIndex documentation
davidberenstein1957 Feb 27, 2025
2a0301d
Update LlamaIndex notebooks with minor improvements and code refinements
davidberenstein1957 Feb 28, 2025
fbea19a
Enhance LlamaIndex agents notebook with comprehensive agent workflow …
davidberenstein1957 Feb 28, 2025
0cacd97
Update LlamaIndex workflows notebook with advanced workflow techniques
davidberenstein1957 Mar 1, 2025
01b450f
Remove unnecessary workflow visualization HTML files and clean up not…
davidberenstein1957 Mar 1, 2025
2ea60f8
Apply suggestions from code review
davidberenstein1957 Mar 3, 2025
9134385
Refactor LlamaIndex notebooks with code improvements and syntax updates
davidberenstein1957 Mar 3, 2025
df4ab11
Add Colab notebook links to LlamaIndex documentation
davidberenstein1957 Mar 3, 2025
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
notebooks/unit2/llama-index/data
notebooks/unit2/llama-index/alfred_chroma_db
.DS_Store
334 changes: 334 additions & 0 deletions notebooks/unit2/llama-index/agents.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"source": [
"# Agents in LlamaIndex\n",
"\n",
"This notebook is part of the [Hugging Face Agents Course](https://www.hf.co/learn/agents-course), a free Course from beginner to expert, where you learn to build Agents.\n",
"\n",
"![Agents course share](https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/share.png)\n",
"\n",
"## Let's install the dependencies\n",
"\n",
"We will install the dependencies for this unit."
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"!pip install llama-index datasets llama-index-callbacks-arize-phoenix llama-index-vector-stores-chroma llama-index-llms-huggingface-api -U -q"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And, let's log in to Hugging Face to use serverless Inference APIs."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from huggingface_hub import login\n",
"\n",
"login()"
]
},
{
"cell_type": "markdown",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"source": [
"## Initialising agents\n",
"\n",
"Let's start by initialising an agent. We will use the basic `AgentWorkflow` class to create an agent."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI\n",
"from llama_index.core.agent.workflow import AgentWorkflow, ToolCallResult, AgentStream\n",
"\n",
"\n",
"def add(a: int, b: int) -> int:\n",
" \"\"\"Add two numbers\"\"\"\n",
" return a + b\n",
"\n",
"\n",
"def subtract(a: int, b: int) -> int:\n",
" \"\"\"Subtract two numbers\"\"\"\n",
" return a - b\n",
"\n",
"\n",
"def multiply(a: int, b: int) -> int:\n",
" \"\"\"Multiply two numbers\"\"\"\n",
" return a * b\n",
"\n",
"\n",
"def divide(a: int, b: int) -> int:\n",
" \"\"\"Divide two numbers\"\"\"\n",
" return a / b\n",
"\n",
"\n",
"llm = HuggingFaceInferenceAPI(model_name=\"Qwen/Qwen2.5-Coder-32B-Instruct\")\n",
"\n",
"agent = AgentWorkflow.from_tools_or_functions(\n",
" tools_or_functions=[subtract, multiply, divide, add],\n",
" llm=llm,\n",
" system_prompt=\"You are a math agent that can add, subtract, multiply, and divide numbers using provided tools.\",\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, we can run the agent and get the response and reasoning behind the tool calls."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"handler = agent.run(\"What is (2 + 2) * 2?\")\n",
"async for ev in handler.stream_events():\n",
" if isinstance(ev, ToolCallResult):\n",
" print(\"\")\n",
" print(\"Called tool: \", ev.tool_name, ev.tool_kwargs, \"=>\", ev.tool_output)\n",
" elif isinstance(ev, AgentStream): # showing the thought process\n",
" print(ev.delta, end=\"\", flush=True)\n",
"\n",
"resp = await handler\n",
"resp"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In a similar fashion, we can pass state and context to the agent.\n"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AgentOutput(response=ChatMessage(role=<MessageRole.ASSISTANT: 'assistant'>, additional_kwargs={}, blocks=[TextBlock(block_type='text', text='Your name is Bob.')]), tool_calls=[], raw={'id': 'chatcmpl-B5sDHfGpSwsVyzvMVH8EWokYwdIKT', 'choices': [{'delta': {'content': None, 'function_call': None, 'refusal': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'stop', 'index': 0, 'logprobs': None}], 'created': 1740739735, 'model': 'gpt-4o-2024-08-06', 'object': 'chat.completion.chunk', 'service_tier': 'default', 'system_fingerprint': 'fp_eb9dce56a8', 'usage': None}, current_agent_name='Agent')"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from llama_index.core.workflow import Context\n",
"\n",
"ctx = Context(agent)\n",
"\n",
"response = await agent.run(\"My name is Bob.\", ctx=ctx)\n",
"response = await agent.run(\"What was my name again?\", ctx=ctx)\n",
"response"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Creating RAG Agents with QueryEngineTools\n",
"\n",
"Let's now re-use the `QueryEngine` we defined in the [previous unit on tools](/tools.ipynb) and convert it into a `QueryEngineTool`. We will pass it to the `AgentWorkflow` class to create a RAG agent."
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"import chromadb\n",
"\n",
"from llama_index.core import VectorStoreIndex\n",
"from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI\n",
"from llama_index.embeddings.huggingface_api import HuggingFaceInferenceAPIEmbedding\n",
"from llama_index.core.tools import QueryEngineTool\n",
"from llama_index.vector_stores.chroma import ChromaVectorStore\n",
"\n",
"# Create a vector store\n",
"db = chromadb.PersistentClient(path=\"./alfred_chroma_db\")\n",
"chroma_collection = db.get_or_create_collection(\"alfred\")\n",
"vector_store = ChromaVectorStore(chroma_collection=chroma_collection)\n",
"\n",
"# Create a query engine\n",
"embed_model = HuggingFaceInferenceAPIEmbedding(model_name=\"BAAI/bge-small-en-v1.5\")\n",
"llm = HuggingFaceInferenceAPI(model_name=\"Qwen/Qwen2.5-Coder-32B-Instruct\")\n",
"index = VectorStoreIndex.from_vector_store(\n",
" vector_store=vector_store, embed_model=embed_model\n",
")\n",
"query_engine = index.as_query_engine(llm=llm)\n",
"query_engine_tool = QueryEngineTool.from_defaults(\n",
" query_engine=query_engine,\n",
" name=\"personas\",\n",
" description=\"descriptions for various types of personas\",\n",
" return_direct=False,\n",
")\n",
"\n",
"# Create a RAG agent\n",
"query_engine_agent = AgentWorkflow.from_tools_or_functions(\n",
" tools_or_functions=[query_engine_tool],\n",
" llm=llm,\n",
" system_prompt=\"You are a helpful assistant that has access to a database containing persona descriptions. \",\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And, we can once more get the response and reasoning behind the tool calls."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"handler = query_engine_agent.run(\n",
" \"Search the database for 'science fiction' and return some persona descriptions.\"\n",
")\n",
"async for ev in handler.stream_events():\n",
" if isinstance(ev, ToolCallResult):\n",
" print(\"\")\n",
" print(\"Called tool: \", ev.tool_name, ev.tool_kwargs, \"=>\", ev.tool_output)\n",
" elif isinstance(ev, AgentStream): # showing the thought process\n",
" print(ev.delta, end=\"\", flush=True)\n",
"\n",
"resp = await handler\n",
"resp"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Creating multi-agent systems\n",
"\n",
"We can also create multi-agent systems by passing multiple agents to the `AgentWorkflow` class."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core.agent.workflow import (\n",
" AgentWorkflow,\n",
" ReActAgent,\n",
")\n",
"\n",
"\n",
"# Define some tools\n",
"def add(a: int, b: int) -> int:\n",
" \"\"\"Add two numbers.\"\"\"\n",
" return a + b\n",
"\n",
"\n",
"def subtract(a: int, b: int) -> int:\n",
" \"\"\"Subtract two numbers.\"\"\"\n",
" return a - b\n",
"\n",
"\n",
"# Create agent configs\n",
"# NOTE: we can use FunctionAgent or ReActAgent here.\n",
"# FunctionAgent works for LLMs with a function calling API.\n",
"# ReActAgent works for any LLM.\n",
"calculator_agent = ReActAgent(\n",
" name=\"calculator\",\n",
" description=\"Performs basic arithmetic operations\",\n",
" system_prompt=\"You are a calculator assistant. Use your tools for any math operation.\",\n",
" tools=[add, subtract],\n",
" llm=llm,\n",
")\n",
"\n",
"query_agent = ReActAgent(\n",
" name=\"info_lookup\",\n",
" description=\"Looks up information about XYZ\",\n",
" system_prompt=\"Use your tool to query a RAG system to answer information about XYZ\",\n",
" tools=[query_engine_tool],\n",
" llm=llm,\n",
")\n",
"\n",
"# Create and run the workflow\n",
"agent = AgentWorkflow(agents=[calculator_agent, query_agent], root_agent=\"calculator\")\n",
"\n",
"# Run the system\n",
"handler = agent.run(user_msg=\"Can you add 5 and 3?\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"async for ev in handler.stream_events():\n",
" if isinstance(ev, ToolCallResult):\n",
" print(\"\")\n",
" print(\"Called tool: \", ev.tool_name, ev.tool_kwargs, \"=>\", ev.tool_output)\n",
" elif isinstance(ev, AgentStream): # showing the thought process\n",
" print(ev.delta, end=\"\", flush=True)\n",
"\n",
"resp = await handler\n",
"resp"
]
}
],
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading