使用向量存储(知识库)
将向量存储与任何 LiteLLM 支持的模型一起使用
LiteLLM 集成了向量存储,使您的模型能够访问您组织的数据,从而获得更准确、更具上下文相关性的响应。
支持的向量存储
快速入门
要在 LiteLLM 中使用向量存储,您需要:
- 初始化 litellm.vector_store_registry
- 将带有 vector_store_ids 的工具传递给 completion 请求。其中
vector_store_ids
是您在 litellm.vector_store_registry 中初始化的一系列向量存储 ID。
LiteLLM Python SDK
LiteLLM 允许您通过传递带有您想要使用的 vector_store_ids 的工具,在 OpenAI API 规范中使用向量存储。
import os
import litellm
from litellm.vector_stores.vector_store_registry import VectorStoreRegistry, LiteLLM_ManagedVectorStore
# Init vector store registry
litellm.vector_store_registry = VectorStoreRegistry(
vector_stores=[
LiteLLM_ManagedVectorStore(
vector_store_id="T37J8R4WTM",
custom_llm_provider="bedrock"
)
]
)
# Make a completion request with vector_store_ids parameter
response = await litellm.acompletion(
model="anthropic/claude-3-5-sonnet",
messages=[{"role": "user", "content": "What is litellm?"}],
tools=[
{
"type": "file_search",
"vector_store_ids": ["T37J8R4WTM"]
}
],
)
print(response.choices[0].message.content)
LiteLLM 代理
1. 配置您的 vector_store_registry
为了在 LiteLLM 中使用向量存储,您需要配置 vector_store_registry。这会告诉 litellm 使用哪些向量存储以及用于该向量存储的 API 提供商。
- config.yaml
- LiteLLM UI
model_list:
- model_name: claude-3-5-sonnet
litellm_params:
model: anthropic/claude-3-5-sonnet
api_key: os.environ/ANTHROPIC_API_KEY
vector_store_registry:
- vector_store_name: "bedrock-litellm-website-knowledgebase"
litellm_params:
vector_store_id: "T37J8R4WTM"
custom_llm_provider: "bedrock"
vector_store_description: "Bedrock vector store for the Litellm website knowledgebase"
vector_store_metadata:
source: "https://www.litellm.com/docs"
在 LiteLLM UI 上,导航至“实验性”>“向量存储”>“创建向量存储”。在此页面上,您可以创建具有名称、向量存储 ID 和凭据的向量存储。
2. 使用 vector_store_ids 参数发出请求
- Curl
- OpenAI Python SDK
curl http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LITELLM_API_KEY" \
-d '{
"model": "claude-3-5-sonnet",
"messages": [{"role": "user", "content": "What is litellm?"}],
"tools": [
{
"type": "file_search",
"vector_store_ids": ["T37J8R4WTM"]
}
]
}'
from openai import OpenAI
# Initialize client with your LiteLLM proxy URL
client = OpenAI(
base_url="http://localhost:4000",
api_key="your-litellm-api-key"
)
# Make a completion request with vector_store_ids parameter
response = client.chat.completions.create(
model="claude-3-5-sonnet",
messages=[{"role": "user", "content": "What is litellm?"}],
tools=[
{
"type": "file_search",
"vector_store_ids": ["T37J8R4WTM"]
}
]
)
print(response.choices[0].message.content)
高级
日志记录向量存储使用情况
LiteLLM 允许您在 LiteLLM UI 的日志
页面上查看向量存储使用情况。
在完成带有向量存储的请求后,导航至 LiteLLM 的日志
页面。在这里,您应该能够看到发送到向量存储的查询以及带有分数的相应响应。
LiteLLM 日志页面:向量存储使用情况
列出可用向量存储
您可以使用 /vector_store/list 端点列出所有可用的向量存储
请求
curl -X GET "http://localhost:4000/vector_store/list" \
-H "Authorization: Bearer $LITELLM_API_KEY"
响应
响应将是可用于 LiteLLM 的所有向量存储的列表。
{
"object": "list",
"data": [
{
"vector_store_id": "T37J8R4WTM",
"custom_llm_provider": "bedrock",
"vector_store_name": "bedrock-litellm-website-knowledgebase",
"vector_store_description": "Bedrock vector store for the Litellm website knowledgebase",
"vector_store_metadata": {
"source": "https://www.litellm.com/docs"
},
"created_at": "2023-05-03T18:21:36.462Z",
"updated_at": "2023-05-03T18:21:36.462Z",
"litellm_credential_name": "bedrock_credentials"
}
],
"total_count": 1,
"current_page": 1,
"total_pages": 1
}
针对特定模型始终启用
如果您希望向量存储在特定模型上默认使用,请使用此选项。
在此配置中,我们将 vector_store_ids
添加到 claude-3-5-sonnet-with-vector-store 模型。这意味着对 claude-3-5-sonnet-with-vector-store 模型的任何请求将始终使用在 vector_store_registry
中定义的 ID 为 T37J8R4WTM
的向量存储。
model_list:
- model_name: claude-3-5-sonnet-with-vector-store
litellm_params:
model: anthropic/claude-3-5-sonnet
vector_store_ids: ["T37J8R4WTM"]
vector_store_registry:
- vector_store_name: "bedrock-litellm-website-knowledgebase"
litellm_params:
vector_store_id: "T37J8R4WTM"
custom_llm_provider: "bedrock"
vector_store_description: "Bedrock vector store for the Litellm website knowledgebase"
vector_store_metadata:
source: "https://www.litellm.com/docs"
工作原理
如果您的请求包含 vector_store_ids
参数,并且其中任何向量存储 ID 在 vector_store_registry
中找到,LiteLLM 将自动为该请求使用向量存储。
- 您发出一个带有
vector_store_ids
参数的 completion 请求,并且任何向量存储 ID 在litellm.vector_store_registry
中找到 - LiteLLM 自动:
- 使用您的最后一条消息作为查询,从知识库中检索相关信息
- 将检索到的上下文添加到您的对话中
- 将增强后的消息发送给模型
转换示例
当您传递 vector_store_ids=["YOUR_KNOWLEDGE_BASE_ID"]
时,您的请求会经过以下步骤:
1. 原始请求发往 LiteLLM
{
"model": "anthropic/claude-3-5-sonnet",
"messages": [
{"role": "user", "content": "What is litellm?"}
],
"vector_store_ids": ["YOUR_KNOWLEDGE_BASE_ID"]
}
2. 请求发往 AWS Bedrock 知识库
{
"retrievalQuery": {
"text": "What is litellm?"
}
}
这将发送到:https://bedrock-agent-runtime.{aws_region}.amazonaws.com/knowledgebases/YOUR_KNOWLEDGE_BASE_ID/retrieve
3. 最终请求发往 LiteLLM
{
"model": "anthropic/claude-3-5-sonnet",
"messages": [
{"role": "user", "content": "What is litellm?"},
{"role": "user", "content": "Context: \n\nLiteLLM is an open-source SDK to simplify LLM API calls across providers (OpenAI, Claude, etc). It provides a standardized interface with robust error handling, streaming, and observability tools."}
]
}
每当您在请求中包含 vector_store_ids
参数时,此过程会自动发生。
API 参考
LiteLLM Completion 知识库参数
在使用 LiteLLM 的知识库集成时,您可以包含以下参数
参数 | 类型 | 描述 |
---|---|---|
vector_store_ids | 列表[str] | 要查询的知识库 ID 列表 |
VectorStoreRegistry
VectorStoreRegistry
是 LiteLLM 中管理向量存储的核心组件。它充当一个注册中心,您可以在其中配置和访问您的向量存储。
什么是 VectorStoreRegistry?
VectorStoreRegistry
是一个类,它:
- 维护 LiteLLM 可以使用的向量存储集合
- 允许您注册带有其凭据和元数据的向量存储
- 使向量存储通过其 ID 在您的 completion 请求中可访问
在 Python 中使用 VectorStoreRegistry
from litellm.vector_stores.vector_store_registry import VectorStoreRegistry, LiteLLM_ManagedVectorStore
# Initialize the vector store registry with one or more vector stores
litellm.vector_store_registry = VectorStoreRegistry(
vector_stores=[
LiteLLM_ManagedVectorStore(
vector_store_id="YOUR_VECTOR_STORE_ID", # Required: Unique ID for referencing this store
custom_llm_provider="bedrock" # Required: Provider (e.g., "bedrock")
)
]
)
LiteLLM_ManagedVectorStore 参数
注册表中的每个向量存储都使用带有这些参数的 LiteLLM_ManagedVectorStore
对象进行配置
参数 | 类型 | 必需 | 描述 |
---|---|---|---|
vector_store_id | str | 是 | 向量存储的唯一标识符 |
custom_llm_provider | str | 是 | 向量存储提供商(例如,“bedrock”) |
vector_store_name | str | 否 | 向量存储的友好名称 |
vector_store_description | str | 否 | 向量存储内容的描述 |
vector_store_metadata | dict 或 str | 否 | 关于向量存储的附加元数据 |
litellm_credential_name | str | 否 | 用于此向量存储的凭据名称 |
在 config.yaml 中配置 VectorStoreRegistry
对于 LiteLLM 代理,您可以在 config.yaml
文件中配置相同的注册表
vector_store_registry:
- vector_store_name: "bedrock-litellm-website-knowledgebase" # Optional friendly name
litellm_params:
vector_store_id: "T37J8R4WTM" # Required: Unique ID
custom_llm_provider: "bedrock" # Required: Provider
vector_store_description: "Bedrock vector store for the Litellm website knowledgebase"
vector_store_metadata:
source: "https://www.litellm.com/docs"
litellm_params
部分接受与 Python SDK 中的 LiteLLM_ManagedVectorStore
构造函数相同的所有参数。