Skip to content

Commit 888e928

Browse files
committed
Sample code for the article on LlamaIndex
1 parent bc04c40 commit 888e928

File tree

8 files changed

+1841
-0
lines changed

8 files changed

+1841
-0
lines changed

python-llamaindex/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python LlamaIndex: Step by Step RAG With Examples
2+
3+
This folder provides the code examples for the Real Python tutorial [Python LlamaIndex: Step by Step RAG With Examples](https://realpython.com/python-llamaindex-examples/).

python-llamaindex/agent.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import asyncio
2+
3+
import requests
4+
from llama_index.core.agent.workflow import AgentWorkflow
5+
from llama_index.llms.openai import OpenAI
6+
7+
8+
def get_breed_info(breed_name):
9+
response = requests.get("https://api.thecatapi.com/v1/breeds")
10+
response.raise_for_status()
11+
for breed in response.json():
12+
if breed["name"] == breed_name:
13+
return breed
14+
return None
15+
16+
17+
workflow = AgentWorkflow.from_tools_or_functions(
18+
tools_or_functions=[get_breed_info],
19+
llm=OpenAI(model="gpt-4o-mini"),
20+
system_prompt="You are an agent that can provide information about cat breeds.",
21+
)
22+
23+
24+
async def main():
25+
response = await workflow.run(
26+
user_msg="Can you describe the temperament of Siamese cats?"
27+
)
28+
print(response)
29+
30+
31+
if __name__ == "__main__":
32+
33+
asyncio.run(main())

python-llamaindex/async_query.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import asyncio
2+
import logging
3+
from pathlib import Path
4+
5+
from llama_index.core import (
6+
SimpleDirectoryReader,
7+
StorageContext,
8+
VectorStoreIndex,
9+
load_index_from_storage,
10+
)
11+
12+
# Disable logging messages
13+
logging.getLogger("llama_index").setLevel(logging.WARNING)
14+
logging.getLogger("httpx").setLevel(logging.WARNING)
15+
16+
# Define the storage directory
17+
PERSIST_DIR = "./storage"
18+
19+
20+
def get_index(persist_dir=PERSIST_DIR):
21+
if Path(persist_dir).exists():
22+
storage_context = StorageContext.from_defaults(persist_dir=persist_dir)
23+
index = load_index_from_storage(storage_context)
24+
print("Index loaded from storage...")
25+
else:
26+
reader = SimpleDirectoryReader(input_files=["./data/pep8.rst"])
27+
documents = reader.load_data()
28+
index = VectorStoreIndex.from_documents(documents)
29+
index.storage_context.persist(persist_dir=persist_dir)
30+
print("Index created and persisted to storage...")
31+
32+
return index
33+
34+
35+
async def main():
36+
index = get_index()
37+
query_engine = index.as_query_engine()
38+
39+
queries = [
40+
"What is this document about?",
41+
"Summarize the naming conventions in Python?",
42+
]
43+
44+
# Run queries asynchronously
45+
tasks = [query_engine.aquery(query) for query in queries]
46+
responses = await asyncio.gather(*tasks)
47+
48+
# Print responses
49+
for i, (query, response) in enumerate(zip(queries, responses), 1):
50+
print(f"\nQuery {i}: {query}")
51+
print(f"Response: {response}\n")
52+
print("-" * 80)
53+
54+
55+
if __name__ == "__main__":
56+
asyncio.run(main())

0 commit comments

Comments
 (0)