-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathindex.html
More file actions
322 lines (296 loc) · 15.2 KB
/
index.html
File metadata and controls
322 lines (296 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>LlamaIndex — aimock</title>
<link rel="icon" type="image/svg+xml" href="../favicon.svg" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:ital,wght@0,300;0,400;0,500;0,600;0,700;1,400&family=Instrument+Sans:wght@400;500;600;700&display=swap"
rel="stylesheet"
/>
<link rel="stylesheet" href="../style.css" />
</head>
<body>
<nav class="top-nav">
<div class="nav-inner">
<div style="display: flex; align-items: center; gap: 1rem">
<button
class="sidebar-toggle"
onclick="document.querySelector('.sidebar').classList.toggle('open')"
aria-label="Toggle sidebar"
>
☰
</button>
<a href="/" class="nav-brand"> <span class="prompt">$</span> aimock </a>
</div>
<ul class="nav-links">
<li><a href="/">Home</a></li>
<li><a href="/docs" style="color: var(--accent)">Docs</a></li>
<li>
<a href="https://github.com/CopilotKit/aimock" class="gh-link" target="_blank"
><svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
<path
d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z"
/>
</svg>
GitHub</a
>
</li>
</ul>
</div>
</nav>
<div class="docs-layout">
<aside class="sidebar" id="sidebar"></aside>
<main class="docs-content">
<h1>LlamaIndex</h1>
<p class="lead">
Test your LlamaIndex RAG pipelines end-to-end. aimock mocks both the LLM and the vector
database — retriever and generator in one server.
</p>
<h2>Quick Start</h2>
<p>
Point the LlamaIndex OpenAI LLM at aimock instead of the real API. No code changes to your
RAG pipeline — just swap the base URL.
</p>
<div class="code-block">
<div class="code-block-header">Python <span class="lang-tag">python</span></div>
<pre><code><span class="kw">from</span> llama_index.llms.openai <span class="kw">import</span> OpenAI
<span class="cm"># Point at aimock instead of api.openai.com</span>
llm = OpenAI(
api_base=<span class="str">"http://localhost:4010/v1"</span>,
api_key=<span class="str">"test"</span>,
)
<span class="cm"># Configure LlamaIndex to use aimock for both LLM and embeddings</span>
<span class="kw">from</span> llama_index.core <span class="kw">import</span> Settings, VectorStoreIndex, SimpleDirectoryReader
<span class="kw">from</span> llama_index.embeddings.openai <span class="kw">import</span> OpenAIEmbedding
Settings.llm = llm
Settings.embed_model = OpenAIEmbedding(api_base=<span class="str">"http://localhost:4010/v1"</span>, api_key=<span class="str">"test"</span>)
documents = SimpleDirectoryReader(<span class="str">"data"</span>).load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query(<span class="str">"What is gravity?"</span>)</code></pre>
</div>
<p>Start aimock with fixtures that match the queries your pipeline will send:</p>
<div class="code-block">
<div class="code-block-header">Terminal <span class="lang-tag">shell</span></div>
<pre><code>npx @copilotkit/aimock --fixtures ./fixtures/llamaindex</code></pre>
</div>
<h2>Mock Both LLM and Vector DB</h2>
<p>
This is where aimock shines for RAG testing. A LlamaIndex RAG pipeline has two external
dependencies: the <strong>retriever</strong> (vector database) and the
<strong>generator</strong> (LLM). aimock serves both on one port, so a single server
replaces Pinecone/Qdrant <em>and</em> OpenAI/Anthropic.
</p>
<div class="code-block">
<div class="code-block-header">
fixtures/rag-pipeline.json <span class="lang-tag">json</span>
</div>
<pre><code>{
<span class="prop">"fixtures"</span>: [
{
<span class="prop">"match"</span>: { <span class="prop">"userMessage"</span>: <span class="str">"What is gravity?"</span> },
<span class="prop">"response"</span>: {
<span class="prop">"content"</span>: <span class="str">"Based on the retrieved documents, gravity is a fundamental force of nature that attracts objects with mass toward one another. It is described by Newton's law of universal gravitation and Einstein's general theory of relativity."</span>
}
},
{
<span class="prop">"match"</span>: { <span class="prop">"inputText"</span>: <span class="str">"What is gravity?"</span>, <span class="prop">"endpoint"</span>: <span class="str">"embedding"</span> },
<span class="prop">"response"</span>: {
<span class="prop">"embedding"</span>: [<span class="num">0.9</span>, <span class="num">0.1</span>, <span class="num">0.05</span>]
}
}
]
}</code></pre>
</div>
<div class="code-block">
<div class="code-block-header">aimock.json <span class="lang-tag">json</span></div>
<pre><code>{
<span class="prop">"llm"</span>: {
<span class="prop">"fixtures"</span>: <span class="str">"./fixtures/rag-pipeline.json"</span>
},
<span class="prop">"vector"</span>: {
<span class="prop">"collections"</span>: [
{
<span class="prop">"name"</span>: <span class="str">"knowledge-base"</span>,
<span class="prop">"dimension"</span>: <span class="num">3</span>,
<span class="prop">"vectors"</span>: [
{
<span class="prop">"id"</span>: <span class="str">"doc-gravity"</span>,
<span class="prop">"values"</span>: [<span class="num">0.9</span>, <span class="num">0.1</span>, <span class="num">0.05</span>],
<span class="prop">"metadata"</span>: { <span class="prop">"source"</span>: <span class="str">"physics.pdf"</span>, <span class="prop">"page"</span>: <span class="num">12</span> }
}
],
<span class="prop">"queryResults"</span>: [
{
<span class="prop">"id"</span>: <span class="str">"doc-gravity"</span>,
<span class="prop">"score"</span>: <span class="num">0.97</span>,
<span class="prop">"metadata"</span>: { <span class="prop">"source"</span>: <span class="str">"physics.pdf"</span>, <span class="prop">"page"</span>: <span class="num">12</span> }
}
]
}
]
}
}</code></pre>
</div>
<p>
Load both with <code>npx @copilotkit/aimock --config aimock.json</code>. The config points
to the fixture file via <code>llm.fixtures</code>, so aimock handles both legs of the RAG
pipeline:
</p>
<ul>
<li><code>/v1/chat/completions</code> — matches LLM fixtures for the generator</li>
<li><code>/vector</code> — serves vector query results for the retriever</li>
</ul>
<div class="code-block">
<div class="code-block-header">
Python — dual mock <span class="lang-tag">python</span>
</div>
<pre><code><span class="kw">from</span> llama_index.llms.openai <span class="kw">import</span> OpenAI
<span class="kw">from</span> llama_index.embeddings.openai <span class="kw">import</span> OpenAIEmbedding
<span class="kw">from</span> llama_index.vector_stores.qdrant <span class="kw">import</span> QdrantVectorStore
<span class="cm"># Generator: LLM pointed at aimock</span>
llm = OpenAI(
api_base=<span class="str">"http://localhost:4010/v1"</span>,
api_key=<span class="str">"test"</span>,
)
<span class="cm"># Embeddings: also served by aimock</span>
embed_model = OpenAIEmbedding(
api_base=<span class="str">"http://localhost:4010/v1"</span>,
api_key=<span class="str">"test"</span>,
)
<span class="cm"># Retriever: aimock's vector endpoint</span>
<span class="cm"># Point your vector store client at localhost:4010/vector</span>
<span class="cm"># aimock implements the Qdrant-compatible REST API</span>
<span class="cm"># Now your entire RAG pipeline runs against one mock server</span></code></pre>
</div>
<h2>Embedding Fixtures</h2>
<p>
LlamaIndex indexes documents by generating embeddings. Use <code>inputText</code> matching
to return deterministic embedding vectors for specific inputs, ensuring your indexing and
retrieval paths produce consistent results in tests.
</p>
<div class="code-block">
<div class="code-block-header">
fixtures/embeddings.json <span class="lang-tag">json</span>
</div>
<pre><code>{
<span class="prop">"fixtures"</span>: [
{
<span class="prop">"match"</span>: { <span class="prop">"inputText"</span>: <span class="str">"What is gravity?"</span>, <span class="prop">"endpoint"</span>: <span class="str">"embedding"</span> },
<span class="prop">"response"</span>: {
<span class="prop">"embedding"</span>: [<span class="num">0.9</span>, <span class="num">0.1</span>, <span class="num">0.05</span>]
}
},
{
<span class="prop">"match"</span>: { <span class="prop">"inputText"</span>: <span class="str">"Gravity is a fundamental force"</span>, <span class="prop">"endpoint"</span>: <span class="str">"embedding"</span> },
<span class="prop">"response"</span>: {
<span class="prop">"embedding"</span>: [<span class="num">0.88</span>, <span class="num">0.12</span>, <span class="num">0.07</span>]
}
}
]
}</code></pre>
</div>
<p>
The <code>inputText</code> matcher performs substring matching, so
<code>"gravity"</code> matches any input containing that word. Use exact strings when you
need precision.
</p>
<h2>With aimock-pytest</h2>
<p>
The <code>aimock-pytest</code> plugin starts and stops the server automatically per test.
Install with <code>pip install aimock-pytest</code>.
</p>
<div class="code-block">
<div class="code-block-header">test_rag.py <span class="lang-tag">python</span></div>
<pre><code><span class="kw">from</span> llama_index.llms.openai <span class="kw">import</span> OpenAI
<span class="kw">def</span> <span class="fn">test_rag_query</span>(aimock):
<span class="cm"># Load fixtures before making LLM calls</span>
aimock.load_fixtures(<span class="str">"./fixtures/llamaindex/rag.json"</span>)
llm = OpenAI(
api_base=f<span class="str">"{aimock.url}/v1"</span>,
api_key=<span class="str">"test"</span>,
)
response = llm.complete(<span class="str">"What is gravity?"</span>)
<span class="kw">assert</span> <span class="str">"force"</span> <span class="kw">in</span> str(response).lower()</code></pre>
</div>
<h2>CI with GitHub Action</h2>
<p>
Run your LlamaIndex test suite in CI with the aimock GitHub Action. The action starts
aimock as a background service and exposes it on the default port.
</p>
<div class="code-block">
<div class="code-block-header">
.github/workflows/test.yml <span class="lang-tag">yaml</span>
</div>
<pre><code><span class="prop">name</span>: LlamaIndex Tests
<span class="prop">on</span>: [push, pull_request]
<span class="prop">jobs</span>:
<span class="prop">test</span>:
<span class="prop">runs-on</span>: ubuntu-latest
<span class="prop">steps</span>:
- <span class="prop">uses</span>: actions/checkout@v4
- <span class="prop">name</span>: Start aimock
<span class="prop">uses</span>: CopilotKit/aimock@v1
<span class="prop">with</span>:
<span class="prop">fixtures</span>: ./fixtures/llamaindex
- <span class="prop">name</span>: Install dependencies
<span class="prop">run</span>: pip install -r requirements.txt
- <span class="prop">name</span>: Run tests
<span class="prop">run</span>: pytest tests/
<span class="prop">env</span>:
<span class="prop">OPENAI_BASE_URL</span>: http://127.0.0.1:4010/v1
<span class="prop">OPENAI_API_KEY</span>: test</code></pre>
</div>
<p>
No API keys needed in CI. Your LlamaIndex pipeline talks to aimock, which returns
deterministic responses from fixtures.
</p>
<h2>Record & Replay</h2>
<p>
Record a RAG query end-to-end against real services, then replay it in tests. aimock
captures both the LLM completions and the embedding calls, so the full pipeline is
reproducible.
</p>
<div class="code-block">
<div class="code-block-header">Record mode <span class="lang-tag">shell</span></div>
<pre><code><span class="cm"># Record LLM and embedding calls from a live session</span>
npx @copilotkit/aimock \
--record \
--provider-openai https://api.openai.com \
--fixtures ./fixtures/llamaindex
<span class="cm"># Run your LlamaIndex pipeline against aimock</span>
python my_rag_pipeline.py
<span class="cm"># aimock saves fixtures to ./fixtures/llamaindex/</span>
<span class="cm"># Next run replays them without hitting the real API</span></code></pre>
</div>
<div class="code-block">
<div class="code-block-header">Replay in tests <span class="lang-tag">python</span></div>
<pre><code><span class="kw">def</span> <span class="fn">test_rag_query</span>(aimock):
<span class="cm"># Load the recorded fixtures</span>
aimock.load_fixtures(<span class="str">"./fixtures/llamaindex/recorded.json"</span>)
<span class="kw">from</span> llama_index.llms.openai <span class="kw">import</span> OpenAI
llm = OpenAI(api_base=f<span class="str">"{aimock.url}/v1"</span>, api_key=<span class="str">"test"</span>)
<span class="cm"># ... run your RAG pipeline, assert on results</span></code></pre>
</div>
</main>
<aside class="page-toc" id="page-toc"></aside>
</div>
<footer class="docs-footer">
<div class="footer-inner">
<div class="footer-left"><span>$</span> aimock · MIT License</div>
<ul class="footer-links">
<li><a href="https://github.com/CopilotKit/aimock" target="_blank">GitHub</a></li>
<li>
<a href="https://www.npmjs.com/package/@copilotkit/aimock" target="_blank">npm</a>
</li>
</ul>
</div>
</footer>
<script src="../sidebar.js"></script>
<script src="../cli-tabs.js"></script>
</body>
</html>