# LlamaIndex RAG Pipeline — API Store Hub

> VI: Tích hợp `OpenAILike` LLM và Embedding trên https://apistorehub.com/v1 trong LlamaIndex để truy vấn tài liệu doanh nghiệp tiếng Việt, giảm ảo giác và tối ưu chi phí ST.
> EN: Wire LlamaIndex `OpenAILike` LLM and embeddings to https://apistorehub.com/v1 for accurate enterprise retrieval-augmented generation with predictable ST token budgets.

- Canonical VI: https://apistorehub.com/vi/tich-hop/llamaindex-rag-pipeline
- Canonical EN: https://apistorehub.com/en/tich-hop/llamaindex-rag-pipeline
- Language: Python
- Runtime: Python 3.10+, llama-index-core, llama-index-llms-openai-like
- Protocol: OpenAI Compatible (/v1/chat/completions, /v1/embeddings)
- Install: `pip install -U llama-index-core llama-index-llms-openai-like`
- Auth Env Var: `API_KEY` (prefix `sk-ash-`)

## Step-by-Step Setup
1. Install `llama-index-core` and `llama-index-llms-openai-like` so arbitrary catalog model IDs pass validation.
2. Set `OpenAILike(api_base="https://apistorehub.com/v1", is_chat_model=True, context_window=...)`.
3. Chunk documents at 512–1024 tokens with 10-15% overlap and cap `similarity_top_k=5`.
4. Rerank top passages before synthesis to cut input token costs by 40-60%.

## Production Code Sample
```
import os
from llama_index.core import Document, Settings, VectorStoreIndex
from llama_index.llms.openai_like import OpenAILike

Settings.llm = OpenAILike(
    model="gemini-4-neon",
    api_base="https://apistorehub.com/v1",
    api_key=os.environ["API_KEY"],
    context_window=200000,
    is_chat_model=True,
    is_function_calling_model=True,
)

docs = [Document(text="Credit ST uses a 1:1 VND rate across all gateway models.")]
index = VectorStoreIndex.from_documents(docs)
engine = index.as_query_engine(similarity_top_k=5)
print(engine.query("How does ST billing work?"))
```

## Quick cURL Check
```bash
curl https://apistorehub.com/v1/chat/completions \
  -H "Authorization: Bearer sk-ash-your_key" \
  -H "Content-Type: application/json" \
  -d '{"model":"gemini-4-neon","messages":[{"role":"system","content":"Answer only from context."},{"role":"user","content":"Context: 1 ST = 1 VND. Question: What is 1 ST?"}]}'
```

## Recommended Models (Live ST Pricing)
| Model | Live Price | Context | Datasheet |
|---|---|---|---|
| `gemini-4-neon` | 20,400 ST / 1M tokens | 2M | https://apistorehub.com/llms/models/gemini-4-neon.md |
| `claude-sonnet-5-5` | 7,700 ST / 1M tokens | 1M | https://apistorehub.com/llms/models/claude-sonnet-5-5.md |
| `deepseek-v4-flash` | 1,500 ST / 1M tokens | 1M | https://apistorehub.com/llms/models/deepseek-v4-flash.md |

## FAQ
- **Why should I choose `OpenAILike` over `OpenAI` in LlamaIndex?**: `OpenAILike` accepts every model ID on API Store Hub without local hardcoded model-name allowlist errors.
- **Which models work best for long-document RAG?**: `gemini-4-neon` and `claude-sonnet-5-5` excel at long-context synthesis, while `deepseek-v4-flash` minimizes query cost.
- **Should I re-embed documents on every startup?**: No. Persist vectors in pgvector, Qdrant, or disk storage and only embed newly added documents.
