Skip to content

Commit 43b6795

Browse files
fede-kamelclaude
andcommitted
feat: Add comprehensive Oracle Cloud Infrastructure (OCI) client support
Implements full OCI Generative AI integration following the proven AWS client architecture pattern. Features: - OciClient (v1) and OciClientV2 (v2) for complete API coverage - All authentication methods: config file, direct credentials, instance principal, resource principal - Complete API support: embed, chat, generate, rerank (including streaming variants) - Automatic model name normalization (adds 'cohere.' prefix if needed) - Request/response transformation between Cohere and OCI formats - Comprehensive integration tests with multiple test suites - Full documentation with usage examples Implementation Details: - Uses httpx event hooks for clean request/response interception - Lazy loading of OCI SDK as optional dependency - Follows BedrockClient architecture pattern for consistency - Supports all OCI regions and compartment-based access control Testing: - 40+ integration tests across 5 test suites - Tests all authentication methods - Validates all APIs (embed, chat, generate, rerank, streaming) - Tests multiple Cohere models (embed-v3, light-v3, multilingual-v3, command-r-plus, rerank-v3) - Error handling and edge case coverage Documentation: - Comprehensive docstrings with usage examples - README section with authentication examples - Installation instructions for OCI optional dependency Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent c2c3f3e commit 43b6795

6 files changed

Lines changed: 1152 additions & 0 deletions

File tree

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,85 @@ for event in response:
5858
print(event.delta.message.content.text, end="")
5959
```
6060

61+
## Oracle Cloud Infrastructure (OCI)
62+
63+
The SDK supports Oracle Cloud Infrastructure (OCI) Generative AI service. First, install the OCI SDK:
64+
65+
```
66+
pip install 'cohere[oci]'
67+
```
68+
69+
Then use the `OciClient` or `OciClientV2`:
70+
71+
```Python
72+
import cohere
73+
74+
# Using OCI config file authentication (default: ~/.oci/config)
75+
co = cohere.OciClient(
76+
oci_region="us-chicago-1",
77+
oci_compartment_id="ocid1.compartment.oc1...",
78+
)
79+
80+
response = co.embed(
81+
model="embed-english-v3.0",
82+
texts=["Hello world"],
83+
input_type="search_document",
84+
)
85+
86+
print(response.embeddings)
87+
```
88+
89+
### OCI Authentication Methods
90+
91+
**1. Config File (Default)**
92+
```Python
93+
co = cohere.OciClient(
94+
oci_region="us-chicago-1",
95+
oci_compartment_id="ocid1.compartment.oc1...",
96+
# Uses ~/.oci/config with DEFAULT profile
97+
)
98+
```
99+
100+
**2. Custom Profile**
101+
```Python
102+
co = cohere.OciClient(
103+
oci_profile="MY_PROFILE",
104+
oci_region="us-chicago-1",
105+
oci_compartment_id="ocid1.compartment.oc1...",
106+
)
107+
```
108+
109+
**3. Direct Credentials**
110+
```Python
111+
co = cohere.OciClient(
112+
oci_user_id="ocid1.user.oc1...",
113+
oci_fingerprint="xx:xx:xx:...",
114+
oci_tenancy_id="ocid1.tenancy.oc1...",
115+
oci_private_key_path="~/.oci/key.pem",
116+
oci_region="us-chicago-1",
117+
oci_compartment_id="ocid1.compartment.oc1...",
118+
)
119+
```
120+
121+
**4. Instance Principal (for OCI Compute instances)**
122+
```Python
123+
co = cohere.OciClient(
124+
auth_type="instance_principal",
125+
oci_region="us-chicago-1",
126+
oci_compartment_id="ocid1.compartment.oc1...",
127+
)
128+
```
129+
130+
### Supported OCI APIs
131+
132+
The OCI client supports all Cohere APIs:
133+
- Embed (with multiple embedding types)
134+
- Chat (with streaming via `chat_stream`)
135+
- Generate (with streaming via `generate_stream`)
136+
- Rerank
137+
138+
See the [OCI client documentation](https://docs.cohere.com/docs/cohere-works-everywhere) for more details.
139+
61140
## Contributing
62141

63142
While we value open-source contributions to this SDK, the code is generated programmatically. Additions made directly would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ python-dateutil = "^2.9.0"
5353
types-python-dateutil = "^2.9.0.20240316"
5454
ruff = "==0.11.5"
5555

56+
[tool.poetry.group.oci]
57+
optional = true
58+
59+
[tool.poetry.group.oci.dependencies]
60+
oci = "^2.165.0"
61+
5662
[tool.pytest.ini_options]
5763
testpaths = [ "tests" ]
5864
asyncio_mode = "auto"

src/cohere/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@
516516
"NotFoundError": ".errors",
517517
"NotImplementedError": ".errors",
518518
"OAuthAuthorizeResponse": ".types",
519+
"OciClient": ".oci_client",
520+
"OciClientV2": ".oci_client",
519521
"ParseInfo": ".types",
520522
"RerankDocument": ".types",
521523
"RerankRequestDocumentsItem": ".types",
@@ -848,6 +850,8 @@ def __dir__():
848850
"NotFoundError",
849851
"NotImplementedError",
850852
"OAuthAuthorizeResponse",
853+
"OciClient",
854+
"OciClientV2",
851855
"ParseInfo",
852856
"RerankDocument",
853857
"RerankRequestDocumentsItem",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Lazy loading for optional OCI SDK dependency."""
2+
3+
from typing import Any
4+
5+
OCI_INSTALLATION_MESSAGE = """
6+
The OCI SDK is required to use OciClient or OciClientV2.
7+
8+
Install it with:
9+
pip install oci
10+
11+
Or with the optional dependency group:
12+
pip install cohere[oci]
13+
"""
14+
15+
16+
def lazy_oci() -> Any:
17+
"""
18+
Lazily import the OCI SDK.
19+
20+
Returns:
21+
The oci module
22+
23+
Raises:
24+
ImportError: If the OCI SDK is not installed
25+
"""
26+
try:
27+
import oci
28+
return oci
29+
except ImportError:
30+
raise ImportError(OCI_INSTALLATION_MESSAGE)

0 commit comments

Comments
 (0)