This guide will walk you through integrating LlamaIndex with APIpie, enabling you to build powerful RAG (Retrieval Augmented Generation) applications that connect your custom data sources to various LLMs through a unified interface.
LlamaIndex is a comprehensive data framework for connecting custom data to LLMs. It provides tools for:
By connecting LlamaIndex with APIpie, you gain access to a wide range of powerful language models while leveraging LlamaIndex's sophisticated data management capabilities.
Install LlamaIndex core and required packages:
pip install llama-index-core
pip install llama-index-llms-openai # For OpenAI-compatible endpoints like APIpie
For advanced use cases, you may need additional packages:
pip install llama-index-embeddings-openai # For embeddings
pip install llama-index-vector-stores-qdrant # For Qdrant vector store
# or other integrations as needed
Create a custom LLM configuration that points to APIpie:
import os
from llama_index.llms.openai import OpenAI
from llama_index.core import Settings
# Configure APIpie as the LLM provider
api_key = "your-apipie-api-key"
apipie_llm = OpenAI(
api_key=api_key,
base_url="https://apipie.ai/v1",
model="gpt-4o-mini", # You can use any model available on APIpie
temperature=0.1,
)
# Set as the default LLM
Settings.llm = apipie_llm
| Application Type | What LlamaIndex Helps You Build |
|---|---|
| Document Q&A | Systems that answer questions about specific documents |
| Knowledge Bases | Comprehensive knowledge systems from multiple data sources |
| Research Assistants | Tools that analyze and synthesize information |
| Data Analysis | Systems that query and analyze structured data |
| Multi-Agent Applications | Complex agent systems with coordinated data access |
import os
from llama_index.llms.openai import OpenAI
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
Settings,
)
# Configure APIpie
api_key = "your-apipie-api-key"
apipie_llm = OpenAI(
api_key=api_key,
base_url="https://apipie.ai/v1",
model="gpt-4o-mini", # You can use any model available on APIpie
temperature=0.1,
)
# Set as the default LLM
Settings.llm = apipie_llm
# Load your documents
documents = SimpleDirectoryReader("./data").load_data()
# Create an index from the documents
index = VectorStoreIndex.from_documents(documents)
# Create a query engine
query_engine = index.as_query_engine()
# Query your data
response = query_engine.query("What is the main topic discussed in these documents?")
print(response)
import os
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
Settings,
ServiceContext,
)
# Configure APIpie for LLM
api_key = "your-apipie-api-key"
apipie_llm = OpenAI(
api_key=api_key,
base_url="https://apipie.ai/v1",
model="gpt-4o",
temperature=0.1,
)
# Configure APIpie for embeddings
apipie_embed_model = OpenAIEmbedding(
api_key=api_key,
base_url="https://apipie.ai/v1",
model_name="text-embedding-3-large",
embed_batch_size=100,
)
# Set the default models
Settings.llm = apipie_llm
Settings.embed_model = apipie_embed_model
# Load your documents
documents = SimpleDirectoryReader("./data").load_data()
# Create an index with the custom settings
index = VectorStoreIndex.from_documents(documents)
# Create a query engine with more advanced settings
query_engine = index.as_query_engine(
similarity_top_k=5, # Retrieve top 5 most similar chunks
streaming=True, # Enable streaming responses
)
# Query your data
response = query_engine.query(
"Provide a detailed summary of these documents and their key insights."
)
print(response)
import os
from llama_index.llms.openai import OpenAI
from llama_index.vector_stores.qdrant import QdrantVectorStore
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
Settings,
StorageContext,
)
import qdrant_client
# Configure APIpie
api_key = "your-apipie-api-key"
apipie_llm = OpenAI(
api_key=api_key,
base_url="https://apipie.ai/v1",
model="gpt-4o-mini",
)
# Set as the default LLM
Settings.llm = apipie_llm
# Create a Qdrant client (local or cloud)
client = qdrant_client.QdrantClient(
location=":memory:", # Use a real URL for production
)
# Create a QdrantVectorStore
vector_store = QdrantVectorStore(
client=client,
collection_name="documents",
)
# Create a storage context
storage_context = StorageContext.from_defaults(vector_store=vector_store)
# Load your documents
documents = SimpleDirectoryReader("./data").load_data()
# Create an index with the custom vector store
index = VectorStoreIndex.from_documents(
documents,
storage_context=storage_context,
)
# Create a query engine
query_engine = index.as_query_engine()
# Query your data
response = query_engine.query("What are the main points in these documents?")
print(response)
import os
from llama_index.llms.openai import OpenAI
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
Settings,
)
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.core.query_engine import SubQuestionQueryEngine
from llama_index.core.callbacks import CallbackManager, ConsoleCallbackHandler
# Configure callbacks for logging
Settings.callback_manager = CallbackManager([ConsoleCallbackHandler()])
# Configure APIpie
api_key = "your-apipie-api-key"
apipie_llm = OpenAI(
api_key=api_key,
base_url="https://apipie.ai/v1",
model="gpt-4o", # Using a more capable model for the agent
temperature=0.1,
)
# Set as the default LLM
Settings.llm = apipie_llm
# Load different document sets
financial_docs = SimpleDirectoryReader("./financial_data").load_data()
product_docs = SimpleDirectoryReader("./product_data").load_data()
# Create indices for each document set
financial_index = VectorStoreIndex.from_documents(financial_docs)
product_index = VectorStoreIndex.from_documents(product_docs)
# Create query engines for each index
financial_engine = financial_index.as_query_engine()
product_engine = product_index.as_query_engine()
# Create tools from the query engines
tools = [
QueryEngineTool(
query_engine=financial_engine,
metadata=ToolMetadata(
name="financial_data",
description="Provides information about financial statements, revenue, and business performance",
),
),
QueryEngineTool(
query_engine=product_engine,
metadata=ToolMetadata(
name="product_data",
description="Provides information about products, features, and specifications",
),
),
]
# Create a sub-question query engine that can route to the appropriate tool
query_engine = SubQuestionQueryEngine.from_defaults(
query_engine_tools=tools,
verbose=True,
)
# Query across both datasets
response = query_engine.query(
"Compare the financial performance of our top-selling product to the overall company results last quarter."
)
print(response)
chunk_size and chunk_overlap parameters in the document loading process.verbose=True parameter when creating query engines and enable the console callback handler to see detailed logs of the retrieval process.Settings.embed_model configuration.For more information, see the LlamaIndex documentation or the GitHub repository.
If you encounter any issues during the integration process, please reach out on APIpie Discord for assistance.