Using a Vector Store
Using vector stores is required to call the OCI OpenAI-compatible API.
- In the Vector stores list page, select the vector store that you want to use. If you need help finding the list page, see Listing a project.
- Select the How to use tab.
- Follow the steps on the screen.
Unstructured Data
If the vector store is set up for unstructured data, which means you have documents that you want to use as part of the agentic workflow, then follow these steps:
These code in the following steps are written for the Python SDK. The flow is still the same if you use REST API or Java SDK.
1. Create Control Plane and Data Plane Clients
The control plane client is for management API such as creating a vector store and adding files to it. The data plane client is for accessing the large language models that OCI Generative AI service offers.
from oci_openai import OciOpenAI, OciSessionAuth
dp_endpoint = "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/20231130"
cp_endpoint = "https://generativeai.us-chicago-1.oci.oraclecloud.com/20231130"
profile = os.environ.get("OCI_PROFILE", "DEFAULT") //replace with your profile name if not using default
region = os.environ("OCI_REGION")
compartment_id = os.environ("OCI_COMPARTMENT_ID")
# Create DP + CP clients
auth = OciSessionAuth(profile_name=profile)
dp_client = OciOpenAI(
service_endpoint=dp_endpoint,
auth=auth,
compartment_id=compartment_id,
default_headers={"opc-compartment-id": compartment_id},
)
cp_client = OciOpenAI(
service_endpoint=cp_endpoint,
auth=auth,
compartment_id=compartment_id,
)
2. Create a Vector Store
This step creates a vector stores and then gets its ID and is here part of the complete set of tasks for creating and using a vector store. However, if you're on this page, you already have a vector store that you can use. To use the vector store that you're in its detail page, skip this step, and instead select the Details tab and copy the vector store ID into a notepad for use in the next step.
vs = cp_client.vector_stores.create(
name="a-demo-store",
description="Demo vector store",
expires_after={"anchor": "last_active_at", "days": 30},
metadata={"created_by": "oci-samples"},
)
vector_store_id = vs.id
print(f"Created vector store: {vector_store_id}")
The Vector Stores API uses the same format as the OpenAI Vector Stores API. See OpenAI Create vector store API documentation.
3. Add Files to the Vector Store
- To add one file
-
First, add that file and get the ID of that file using the create and retrieve methods of the Files API. Then, create a vector store file, using the ID of the vector store and the id of the file.
- To add a batch of files
- First, add all the files and get all the file IDs. Then, create a batch.
# File upload
file_id = "..." # <-- Replace with actual File ID
vs_file = dp_client.vector_stores.files.create(
vector_store_id=vector_store_id,
file_id=file_id
)
# Batch File Add
file_ids = ["....", "....", "...."] # <-- Replace with actual File IDs
batch = dp_client.vector_stores.file_batches.create(
vector_store_id=vector_store_id,
file_ids=file_ids,
attributes={"type": "batch"},
)We recommend that you store files in OCI Object Storage and then use a vector store connector to link the Object Storage bucket containing the files, to the vector store.
4. Search the Vector Store
For reference, see the Search vector store API in the OpenAI documentation. For the search vector store API, the following parameters aren't supported.
rewrite_query(Optional)): Whether to rewrite the natural language query for vector search - This option isn't supported and is disabled, even if you set it to true.ranking_options ranker(Optional)): Custom ranker models aren't supported. For exampledefault-2024-11-15isn't supported. However, you can set the ranker toautofor the search API to use the system ranker, or set it tononeto disable the ranker and reduce latency.
query = "What is your question?" # <-- Replace with question/query
res = dp_client.vector_stores.search(
vector_store_id=vector_store_id,
query=query,
max_num_results=10,
rewrite_query=False
)
We recommend that you store files in OCI Object Storage and then use a vector store connector to link the Object Storage bucket containing the files, to the vector store.