LlamaIndex Async

Code

1import asyncio
2from pathlib import Path
3from shutil import rmtree
4
5import httpx
6from kern.agent import Agent
7from kern.knowledge.knowledge import Knowledge
8from kern.vectordb.llamaindex import LlamaIndexVectorDb
9from llama_index.core import (
10 SimpleDirectoryReader,
11 StorageContext,
12 VectorStoreIndex,
13)
14from llama_index.core.node_parser import SentenceSplitter
15from llama_index.core.retrievers import VectorIndexRetriever
16
17data_dir = Path(__file__).parent.parent.parent.joinpath("wip", "data", "paul_graham")
18if data_dir.is_dir():
19 rmtree(path=data_dir, ignore_errors=True)
20data_dir.mkdir(parents=True, exist_ok=True)
21
22url = "https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt"
23file_path = data_dir.joinpath("paul_graham_essay.txt")
24response = httpx.get(url)
25if response.status_code == 200:
26 with open(file_path, "wb") as file:
27 file.write(response.content)
28 print(f"File downloaded and saved as {file_path}")
29else:
30 print("Failed to download the file")
31
32documents = SimpleDirectoryReader(str(data_dir)).load_data()
33
34splitter = SentenceSplitter(chunk_size=1024)
35
36nodes = splitter.get_nodes_from_documents(documents)
37
38storage_context = StorageContext.from_defaults()
39
40index = VectorStoreIndex(nodes=nodes, storage_context=storage_context)
41
42knowledge_retriever = VectorIndexRetriever(index)
43
44knowledge = Knowledge(
45 vector_db=LlamaIndexVectorDb(knowledge_retriever=knowledge_retriever)
46)
47
48# Create an agent with the knowledge instance
49agent = Agent(
50 knowledge=knowledge,
51 search_knowledge=True,
52 debug_mode=True,
53)
54
55if __name__ == "__main__":
56 asyncio.run(
57 agent.aprint_response(
58 "Explain what this text means: low end eats the high end", markdown=True
59 )
60 )

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Install dependencies

1uv pip install -U llama-index-core llama-index-readers-file llama-index-embeddings-openai pypdf openai kern-ai

Run Agent

1python cookbook/08_knowledge/vector_db/llamaindex_db/llamaindex_db.py