Skip to content

Commit caf435d

Browse files
Merge branch 'main' into odifix
2 parents f5d6a48 + 914f39e commit caf435d

170 files changed

Lines changed: 4393 additions & 700 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Copyright (c) 2026 Oracle and/or its affiliates.
2+
3+
The Universal Permissive License (UPL), Version 1.0
4+
5+
Subject to the condition set forth below, permission is hereby granted to any
6+
person obtaining a copy of this software, associated documentation and/or data
7+
(collectively the "Software"), free of charge and under any and all copyright
8+
rights in the Software, and any and all patent rights owned or freely
9+
licensable by each licensor hereunder covering either (i) the unmodified
10+
Software as contributed to or provided by such licensor, or (ii) the Larger
11+
Works (as defined below), to deal in both
12+
13+
(a) the Software, and
14+
15+
(b) any piece of software and/or hardware listed in the lrg_coverage.txt file,
16+
if one is included with the Software (each a "Covered Work", collectively
17+
"Covered Works"), that is available under a license listed in the
18+
lrg_coverage.txt file, if one is included with the Software (the "Covered
19+
License"), and
20+
21+
without limitation, the right to copy, create derivative works of, display,
22+
perform, and distribute the Software and make, use, sell, offer for sale,
23+
import, export, have made, and have sold the Software and the Covered Works,
24+
in each case subject to the conditions and limitations set forth herein.
25+
26+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32+
SOFTWARE.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Use Embedding Models on OCI Generative AI
2+
3+
*The absolute simplest code examples for generating text embeddings with the OCI Generative AI Service — embed a sentence or a whole file in about 20 lines of Python.*
4+
5+
Author: Brona Nilsson
6+
7+
Reviewed: 30.07.2026
8+
9+
# When to use this asset?
10+
11+
Use this asset when you need a minimal, copy-paste starting point for calling OCI Generative AI embedding models with the OCI Python SDK.
12+
13+
### Who
14+
- Developers making their first embedding call on OCI
15+
- Solution architects preparing demos or proofs of concept
16+
- Anyone building semantic search, RAG, or clustering on OCI
17+
18+
### When
19+
- You want to verify your tenancy, policies, and API key setup with the shortest possible script
20+
- You need a reference snippet to embed either a single sentence or the contents of a text file
21+
22+
# How to use this asset?
23+
24+
### Prerequisites
25+
26+
1. An OCI tenancy with access to the Generative AI Service in a supported region (the examples use `eu-frankfurt-1` — change the `service_endpoint` if you use another region).
27+
2. An API key configured in `~/.oci/config` ([setup guide](https://docs.oracle.com/en-us/iaas/Content/generative-ai/setup-oci-api-auth.htm)).
28+
3. Python 3.9+ with the OCI SDK installed:
29+
30+
```bash
31+
pip install -r files/requirements.txt
32+
```
33+
34+
### Run the examples
35+
36+
Copy `files/.env.example` to `files/.env` and set `OCI_COMPARTMENT_ID` to your compartment OCID (the `.env` file is git-ignored, so your OCID stays out of version control). Then:
37+
38+
```bash
39+
# Embed a single sentence
40+
python files/embed_sentence.py
41+
42+
# Embed a text file (one vector per paragraph)
43+
python files/embed_file.py files/sample.txt
44+
```
45+
46+
Both scripts call the `cohere.embed-v4.0` model on demand and print the resulting embedding vectors.
47+
48+
### The embedding model
49+
50+
The scripts use `cohere.embed-v4.0`, the current embedding model on OCI Generative AI ([official model page](https://docs.oracle.com/en-us/iaas/Content/generative-ai/cohere-embed-4.htm)):
51+
52+
| Model | Dimensions | Max tokens per input | Languages | Notes |
53+
|---|---|---|---|---|
54+
| `cohere.embed-v4.0` | 1536 (also 256 / 512 / 1024) | 512 per text input; up to 128,000 total per API call | Multilingual | Multimodal: can also embed one image per request (API only); Matryoshka embeddings — dimensions are truncatable; output types: float, int8, uint8, binary |
55+
56+
The entire Cohere `embed-*-v3.0` family (English, Multilingual, Light, and Image variants) is **deprecated** — do not use it for new projects.
57+
58+
The scripts call the model **on demand** (pay per request). If you host the model on a **dedicated AI cluster** instead, swap the `OnDemandServingMode` for the `DedicatedServingMode` shown commented out in each script, filling in your endpoint OCID ([dedicated AI clusters documentation](https://docs.oracle.com/en-us/iaas/Content/generative-ai/ai-cluster.htm)).
59+
60+
Practical guidance:
61+
62+
- `cohere.embed-v4.0` accepts `input_type` hints (`search_document` when indexing, `search_query` when querying) that improve retrieval quality.
63+
- Vectors are only comparable when produced by the same model at the same dimensions — use identical settings for both indexing and querying.
64+
- When storing vectors in a database (e.g. Oracle Database 26ai `VECTOR` columns), the column dimension must match the model output (1536 by default here). Note the service limits: at most 96 inputs per request, and each input must stay under 512 tokens — for large documents, split the text into chunks first (as `embed_file.py` does with paragraphs).
65+
66+
### Repository structure
67+
68+
```
69+
use-embedding-models/
70+
├── README.md
71+
├── LICENSE
72+
└── files/
73+
├── embed_sentence.py # Embed one sentence
74+
├── embed_file.py # Embed a text file, paragraph by paragraph
75+
├── sample.txt # Demo file: 4 topic pairs to showcase semantic similarity
76+
├── .env.example # Copy to .env and set your compartment OCID
77+
└── requirements.txt
78+
```
79+
80+
# Useful Links
81+
82+
- [OCI Generative AI Documentation](https://docs.oracle.com/en-us/iaas/Content/generative-ai/home.htm)
83+
- [OCI Generative AI — Embedding Models](https://docs.oracle.com/en-us/iaas/Content/generative-ai/embed-models.htm)
84+
- [OCI API Key Authentication Setup](https://docs.oracle.com/en-us/iaas/Content/generative-ai/setup-oci-api-auth.htm)
85+
86+
# License
87+
88+
Copyright (c) 2026 Oracle and/or its affiliates.
89+
Licensed under the Universal Permissive License (UPL), Version 1.0.
90+
91+
See [LICENSE](LICENSE) for more details.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copy to .env and fill in your own value
2+
OCI_COMPARTMENT_ID=...
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Embed the paragraphs of a text file with OCI Generative AI.
2+
3+
Usage: python embed_file.py <path-to-text-file>
4+
"""
5+
import os
6+
import sys
7+
import oci
8+
from dotenv import load_dotenv
9+
10+
load_dotenv()
11+
COMPARTMENT_ID = os.environ["OCI_COMPARTMENT_ID"]
12+
13+
MODEL_ID = "cohere.embed-v4.0"
14+
15+
# One embedding per paragraph (each input must stay under 512 tokens,
16+
# max 96 inputs per request)
17+
text = open(sys.argv[1], encoding="utf-8").read()
18+
paragraphs = [p.strip() for p in text.split("\n\n") if p.strip()][:96]
19+
20+
client = oci.generative_ai_inference.GenerativeAiInferenceClient(
21+
config=oci.config.from_file(),
22+
service_endpoint="https://inference.generativeai.eu-frankfurt-1.oci.oraclecloud.com",
23+
)
24+
25+
response = client.embed_text(
26+
oci.generative_ai_inference.models.EmbedTextDetails(
27+
serving_mode=oci.generative_ai_inference.models.OnDemandServingMode(
28+
model_id=MODEL_ID
29+
),
30+
# For a dedicated AI cluster, replace the serving_mode above with the
31+
# endpoint OCID of your hosted model:
32+
# serving_mode=oci.generative_ai_inference.models.DedicatedServingMode(
33+
# endpoint_id="ocid1.generativeaiendpoint.oc1.eu-frankfurt-1.replace-with-your-endpoint-ocid"
34+
# ),
35+
compartment_id=COMPARTMENT_ID,
36+
inputs=paragraphs,
37+
)
38+
)
39+
40+
for paragraph, embedding in zip(paragraphs, response.data.embeddings):
41+
print(f"{len(embedding)}-dim vector for: {paragraph[:60]}...")
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Embed a single sentence with OCI Generative AI."""
2+
import os
3+
import oci
4+
from dotenv import load_dotenv
5+
6+
load_dotenv()
7+
COMPARTMENT_ID = os.environ["OCI_COMPARTMENT_ID"]
8+
9+
MODEL_ID = "cohere.embed-v4.0"
10+
11+
client = oci.generative_ai_inference.GenerativeAiInferenceClient(
12+
config=oci.config.from_file(),
13+
service_endpoint="https://inference.generativeai.eu-frankfurt-1.oci.oraclecloud.com",
14+
)
15+
16+
response = client.embed_text(
17+
oci.generative_ai_inference.models.EmbedTextDetails(
18+
serving_mode=oci.generative_ai_inference.models.OnDemandServingMode(
19+
model_id=MODEL_ID
20+
),
21+
# For a dedicated AI cluster, replace the serving_mode above with the
22+
# endpoint OCID of your hosted model:
23+
# serving_mode=oci.generative_ai_inference.models.DedicatedServingMode(
24+
# endpoint_id="ocid1.generativeaiendpoint.oc1.eu-frankfurt-1.replace-with-your-endpoint-ocid"
25+
# ),
26+
compartment_id=COMPARTMENT_ID,
27+
inputs=["Oracle Cloud Infrastructure makes embeddings easy."],
28+
)
29+
)
30+
31+
embedding = response.data.embeddings[0]
32+
print(f"Vector with {len(embedding)} dimensions:")
33+
print(embedding[:5], "...")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
oci
2+
python-dotenv
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
The chef reduced the sauce slowly over low heat, folding in butter until it turned glossy and rich.
2+
3+
Compute instances can be scaled up or down on demand, and you only pay for the capacity you actually use.
4+
5+
A good risotto needs constant stirring and warm stock added one ladle at a time until the rice is creamy.
6+
7+
Object storage keeps unstructured data durable and highly available, replicated across fault domains in a region.
8+
9+
The telescope captured light that had travelled for billions of years, revealing galaxies from the early universe.
10+
11+
Astronomers detected water vapour in the atmosphere of a distant exoplanet orbiting within its star's habitable zone.
12+
13+
The midfielder threaded a pass between two defenders, and the striker slotted the ball into the bottom corner.
14+
15+
The home team's goalkeeper saved a penalty in stoppage time to secure the championship title.

oci-and-db/database/autonomous-ai-database/autonomous-ai-json/BasicMongoDemo/README.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

oci-and-db/database/autonomous-ai-database/autonomous-ai-json/MongoDBAsyncDriverDemo/README.md

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)