Skip to content

Commit 61639d4

Browse files
ishaan-jaffS1LV3RJ1NX
authored andcommitted
[Docs] Using LiteLLM with vector stores / knowledge bases (#10534)
* docs vector store registry * img for kbs * docs kb with litellm * fix code qa
1 parent 698d449 commit 61639d4

File tree

9 files changed

+188
-35
lines changed

9 files changed

+188
-35
lines changed

docs/my-website/docs/completion/knowledgebase.md

Lines changed: 169 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,74 @@
1-
# Using Vector Stores (Knowledge Bases) with LiteLLM
1+
import Tabs from '@theme/Tabs';
2+
import TabItem from '@theme/TabItem';
3+
import Image from '@theme/IdealImage';
4+
5+
# Using Vector Stores (Knowledge Bases)
6+
7+
<Image
8+
img={require('../../img/kb.png')}
9+
style={{width: '100%', display: 'block', margin: '2rem auto'}}
10+
/>
11+
<p style={{textAlign: 'left', color: '#666'}}>
12+
Use Vector Stores with any LiteLLM supported model
13+
</p>
14+
215

3-
LiteLLM integrates with AWS Bedrock Knowledge Bases, allowing your models to access your organization's data for more accurate and contextually relevant responses.
16+
LiteLLM integrates with vector stores, allowing your models to access your organization's data for more accurate and contextually relevant responses.
17+
18+
## Supported Vector Stores
19+
- [Bedrock Knowledge Bases](https://aws.amazon.com/bedrock/knowledge-bases/)
420

521
## Quick Start
622

7-
In order to use a Bedrock Knowledge Base with LiteLLM, you need to pass `vector_store_ids` as a parameter to the completion request. Where `vector_store_ids` is a list of Bedrock Knowledge Base IDs.
23+
In order to use a vector store with LiteLLM, you need to
24+
25+
- Initialize litellm.vector_store_registry
26+
- Pass tools with vector_store_ids to the completion request. Where `vector_store_ids` is a list of vector store ids you initialized in litellm.vector_store_registry
827

928
### LiteLLM Python SDK
1029

30+
LiteLLM's allows you to use vector stores in the [OpenAI API spec](https://platform.openai.com/docs/api-reference/chat/create) by passing a tool with vector_store_ids you want to use
31+
1132
```python showLineNumbers title="Basic Bedrock Knowledge Base Usage"
1233
import os
1334
import litellm
1435

36+
from litellm.vector_stores.vector_store_registry import VectorStoreRegistry, LiteLLM_ManagedVectorStore
37+
38+
# Init vector store registry
39+
litellm.vector_store_registry = VectorStoreRegistry(
40+
vector_stores=[
41+
LiteLLM_ManagedVectorStore(
42+
vector_store_id="T37J8R4WTM",
43+
custom_llm_provider="bedrock"
44+
)
45+
]
46+
)
47+
1548

1649
# Make a completion request with vector_store_ids parameter
1750
response = await litellm.acompletion(
1851
model="anthropic/claude-3-5-sonnet",
1952
messages=[{"role": "user", "content": "What is litellm?"}],
20-
vector_store_ids=["YOUR_KNOWLEDGE_BASE_ID"] # e.g., "T37J8R4WTM"
53+
tools=[
54+
{
55+
"type": "file_search",
56+
"vector_store_ids": ["T37J8R4WTM"]
57+
}
58+
],
2159
)
2260

2361
print(response.choices[0].message.content)
2462
```
2563

2664
### LiteLLM Proxy
2765

28-
#### 1. Configure your proxy
66+
#### 1. Configure your vector_store_registry
67+
68+
In order to use a vector store with LiteLLM, you need to configure your vector_store_registry. This tells litellm which vector stores to use and api provider to use for the vector store.
69+
70+
<Tabs>
71+
<TabItem value="config-yaml" label="config.yaml">
2972

3073
```yaml showLineNumbers title="config.yaml"
3174
model_list:
@@ -34,12 +77,35 @@ model_list:
3477
model: anthropic/claude-3-5-sonnet
3578
api_key: os.environ/ANTHROPIC_API_KEY
3679

80+
vector_store_registry:
81+
- vector_store_name: "bedrock-litellm-website-knowledgebase"
82+
litellm_params:
83+
vector_store_id: "T37J8R4WTM"
84+
custom_llm_provider: "bedrock"
85+
vector_store_description: "Bedrock vector store for the Litellm website knowledgebase"
86+
vector_store_metadata:
87+
source: "https://www.litellm.com/docs"
88+
3789
```
3890

39-
#### 2. Make a request with vector_store_ids parameter
91+
</TabItem>
92+
93+
<TabItem value="litellm-ui" label="LiteLLM UI">
94+
95+
On the LiteLLM UI, Navigate to Experimental > Vector Stores > Create Vector Store. On this page you can create a vector store with a name, vector store id and credentials.
96+
<Image
97+
img={require('../../img/kb_2.png')}
98+
style={{width: '50%'}}
99+
/>
100+
101+
40102

41-
import Tabs from '@theme/Tabs';
42-
import TabItem from '@theme/TabItem';
103+
104+
</TabItem>
105+
106+
</Tabs>
107+
108+
#### 2. Make a request with vector_store_ids parameter
43109

44110
<Tabs>
45111
<TabItem value="curl" label="Curl">
@@ -51,7 +117,12 @@ curl http://localhost:4000/v1/chat/completions \
51117
-d '{
52118
"model": "claude-3-5-sonnet",
53119
"messages": [{"role": "user", "content": "What is litellm?"}],
54-
"vector_store_ids": ["YOUR_KNOWLEDGE_BASE_ID"]
120+
"tools": [
121+
{
122+
"type": "file_search",
123+
"vector_store_ids": ["T37J8R4WTM"]
124+
}
125+
]
55126
}'
56127
```
57128

@@ -72,7 +143,12 @@ client = OpenAI(
72143
response = client.chat.completions.create(
73144
model="claude-3-5-sonnet",
74145
messages=[{"role": "user", "content": "What is litellm?"}],
75-
extra_body={"vector_store_ids": ["YOUR_KNOWLEDGE_BASE_ID"]}
146+
tools=[
147+
{
148+
"type": "file_search",
149+
"vector_store_ids": ["T37J8R4WTM"]
150+
}
151+
]
76152
)
77153

78154
print(response.choices[0].message.content)
@@ -81,6 +157,87 @@ print(response.choices[0].message.content)
81157
</TabItem>
82158
</Tabs>
83159

160+
161+
162+
163+
## Advanced
164+
165+
### Logging Vector Store Usage
166+
167+
LiteLLM allows you to view your vector store usage in the LiteLLM UI on the `Logs` page.
168+
169+
After completing a request with a vector store, navigate to the `Logs` page on LiteLLM. Here you should be able to see the query sent to the vector store and corresponding response with scores.
170+
171+
<Image
172+
img={require('../../img/kb_4.png')}
173+
style={{width: '80%'}}
174+
/>
175+
<p style={{textAlign: 'left', color: '#666'}}>
176+
LiteLLM Logs Page: Vector Store Usage
177+
</p>
178+
179+
180+
### Listing available vector stores
181+
182+
You can list all available vector stores using the /vector_store/list endpoint
183+
184+
**Request:**
185+
```bash showLineNumbers title="List all available vector stores"
186+
curl -X GET "http://localhost:4000/vector_store/list" \
187+
-H "Authorization: Bearer $LITELLM_API_KEY"
188+
```
189+
190+
**Response:**
191+
192+
The response will be a list of all vector stores that are available to use with LiteLLM.
193+
194+
```json
195+
{
196+
"object": "list",
197+
"data": [
198+
{
199+
"vector_store_id": "T37J8R4WTM",
200+
"custom_llm_provider": "bedrock",
201+
"vector_store_name": "bedrock-litellm-website-knowledgebase",
202+
"vector_store_description": "Bedrock vector store for the Litellm website knowledgebase",
203+
"vector_store_metadata": {
204+
"source": "https://www.litellm.com/docs"
205+
},
206+
"created_at": "2023-05-03T18:21:36.462Z",
207+
"updated_at": "2023-05-03T18:21:36.462Z",
208+
"litellm_credential_name": "bedrock_credentials"
209+
}
210+
],
211+
"total_count": 1,
212+
"current_page": 1,
213+
"total_pages": 1
214+
}
215+
```
216+
217+
218+
### Always on for a model
219+
220+
**Use this if you want vector stores to be used by default for a specific model.**
221+
222+
In this config, we add `vector_store_ids` to the claude-3-5-sonnet-with-vector-store model. This means that any request to the claude-3-5-sonnet-with-vector-store model will always use the vector store with the id `T37J8R4WTM` defined in the `vector_store_registry`.
223+
224+
```yaml showLineNumbers title="Always on for a model"
225+
model_list:
226+
- model_name: claude-3-5-sonnet-with-vector-store
227+
litellm_params:
228+
model: anthropic/claude-3-5-sonnet
229+
vector_store_ids: ["T37J8R4WTM"]
230+
231+
vector_store_registry:
232+
- vector_store_name: "bedrock-litellm-website-knowledgebase"
233+
litellm_params:
234+
vector_store_id: "T37J8R4WTM"
235+
custom_llm_provider: "bedrock"
236+
vector_store_description: "Bedrock vector store for the Litellm website knowledgebase"
237+
vector_store_metadata:
238+
source: "https://www.litellm.com/docs"
239+
```
240+
84241
## How It Works
85242
86243
LiteLLM implements a `BedrockKnowledgeBaseHook` that intercepts your completion requests for handling the integration with Bedrock Knowledge Bases.
@@ -91,7 +248,7 @@ LiteLLM implements a `BedrockKnowledgeBaseHook` that intercepts your completion
91248
- Adds the retrieved context to your conversation
92249
- Sends the augmented messages to the model
93250

94-
### Example Transformation
251+
#### Example Transformation
95252

96253
When you pass `vector_store_ids=["YOUR_KNOWLEDGE_BASE_ID"]`, your request flows through these steps:
97254

@@ -137,4 +294,4 @@ When using the Knowledge Base integration with LiteLLM, you can include the foll
137294

138295
| Parameter | Type | Description |
139296
|-----------|------|-------------|
140-
| `vector_store_ids` | List[str] | List of Bedrock Knowledge Base IDs to query |
297+
| `vector_store_ids` | List[str] | List of Knowledge Base IDs to query |

docs/my-website/img/kb.png

668 KB
Loading

docs/my-website/img/kb_2.png

126 KB
Loading

docs/my-website/img/kb_3.png

249 KB
Loading

docs/my-website/img/kb_4.png

1.14 MB
Loading

enterprise/proxy/vector_stores/endpoints.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,16 @@ async def new_vector_store(
7070
vector_store.get("vector_store_metadata")
7171
)
7272

73-
new_vector_store = (
73+
_new_vector_store = (
7474
await prisma_client.db.litellm_managedvectorstorestable.create(
7575
data=vector_store
7676
)
7777
)
7878

79+
new_vector_store: LiteLLM_ManagedVectorStore = LiteLLM_ManagedVectorStore(
80+
_new_vector_store.model_dump()
81+
)
82+
7983
# Add vector store to registry
8084
if litellm.vector_store_registry is not None:
8185
litellm.vector_store_registry.add_vector_store_to_registry(

litellm/proxy/proxy_config.yaml

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,15 @@ model_list:
33
litellm_params:
44
model: openai/gpt-4o
55
api_key: os.environ/OPENAI_API_KEY
6-
- model_name: claude-3-5-sonnet-with-vector-store
7-
litellm_params:
8-
model: anthropic/claude-3-5-sonnet-latest
9-
api_key: os.environ/ANTHROPIC_API_KEY
10-
vector_store_ids: ["T37J8R4WTM"]
116

12-
# vector_stores:
13-
# - vector_store_name: "bedrock-litellm-website-knowledgebase"
14-
# litellm_params:
15-
# custom_llm_provider: "bedrock"
16-
# vector_store_id: "T37J8R4WTM"
17-
# vector_store_description: "Bedrock vector store for the Litellm website knowledgebase"
18-
# vector_store_metadata:
19-
# source: "https://www.litellm.com/docs"
20-
7+
vector_store_registry:
8+
- vector_store_name: "bedrock-litellm-website-knowledgebase"
9+
litellm_params:
10+
vector_store_id: "T37J8R4WTM"
11+
custom_llm_provider: "bedrock"
12+
vector_store_description: "Bedrock vector store for the Litellm website knowledgebase"
13+
vector_store_metadata:
14+
source: "https://www.litellm.com/docs"
2115

2216
general_settings:
23-
alerting: ["webhook"]
24-
25-
17+
store_prompts_in_spend_logs: true

litellm/proxy/proxy_server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,16 +2026,16 @@ def _init_non_llm_configs(self, config: dict):
20262026
global_mcp_server_manager.load_servers_from_config(mcp_servers_config)
20272027

20282028
## VECTOR STORES
2029-
vector_stores_config = config.get("vector_stores", None)
2030-
if vector_stores_config:
2029+
vector_store_registry_config = config.get("vector_store_registry", None)
2030+
if vector_store_registry_config:
20312031
from litellm.vector_stores.vector_store_registry import VectorStoreRegistry
20322032

20332033
if litellm.vector_store_registry is None:
20342034
litellm.vector_store_registry = VectorStoreRegistry()
20352035

20362036
# Load vector stores from config
20372037
litellm.vector_store_registry.load_vector_stores_from_config(
2038-
vector_stores_config
2038+
vector_store_registry_config
20392039
)
20402040
pass
20412041

litellm/vector_stores/vector_store_registry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# litellm/proxy/vector_stores/vector_store_registry.py
22
import json
33
from datetime import datetime, timezone
4-
from typing import TYPE_CHECKING, Any, Dict, List, Optional, cast
4+
from typing import TYPE_CHECKING, Any, Dict, List, Optional
55

66
from litellm._logging import verbose_logger
77
from litellm.types.vector_stores import (
@@ -172,7 +172,7 @@ def add_vector_store_to_registry(self, vector_store: LiteLLM_ManagedVectorStore)
172172
for vector_store in self.vector_stores:
173173
if vector_store.get("vector_store_id") == vector_store_id:
174174
return
175-
self.vector_stores.append(cast(LiteLLM_ManagedVectorStore, vector_store))
175+
self.vector_stores.append(vector_store)
176176

177177
def delete_vector_store_from_registry(self, vector_store_id: str):
178178
"""

0 commit comments

Comments
 (0)