Python Library for Accessing the Cohere API
See the code
The Cohere Python SDK allows access to Cohere models across many different platforms: the cohere platform, AWS (Bedrock, Sagemaker), Azure, GCP and Oracle OCI. For a full list of support and snippets, please take a look at the SDK support docs page.
Cohere documentation and API reference is available here.
pip install cohere
import cohere
co = cohere.ClientV2()
response = co.chat(
model="command-r-plus-08-2024",
messages=[{"role": "user", "content": "hello world!"}],
)
print(response)
[!TIP] You can set a system environment variable
CO_API_KEYto avoid writing your api key within your code, e.g. addexport CO_API_KEY=theapikeyforyouraccountin your ~/.zshrc or ~/.bashrc, open a new terminal, then code callingcohere.Client()will read this key.
The SDK supports streaming endpoints. To take advantage of this feature for chat,
use chat_stream.
import cohere
co = cohere.ClientV2()
response = co.chat_stream(
model="command-r-plus-08-2024",
messages=[{"role": "user", "content": "hello world!"}],
)
for event in response:
if event.type == "content-delta":
print(event.delta.message.content.text, end="")
The SDK supports Oracle Cloud Infrastructure (OCI) Generative AI service. First, install the OCI SDK:
pip install 'cohere[oci]'
Then use the OciClient or OciClientV2:
import cohere
# Using OCI config file authentication (default: ~/.oci/config)
co = cohere.OciClient(
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
response = co.embed(
model="embed-english-v3.0",
texts=["Hello world"],
input_type="search_document",
)
print(response.embeddings)
1. Config File (Default)
co = cohere.OciClient(
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
# Uses ~/.oci/config with DEFAULT profile
)
2. Custom Profile
co = cohere.OciClient(
oci_profile="MY_PROFILE",
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
3. Session-based Authentication (Security Token)
# Works with OCI CLI session tokens
co = cohere.OciClient(
oci_profile="MY_SESSION_PROFILE", # Profile with security_token_file
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
4. Direct Credentials
co = cohere.OciClient(
oci_user_id="ocid1.user.oc1...",
oci_fingerprint="xx:xx:xx:...",
oci_tenancy_id="ocid1.tenancy.oc1...",
oci_private_key_path="~/.oci/key.pem",
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
5. Instance Principal (for OCI Compute instances)
co = cohere.OciClient(
auth_type="instance_principal",
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
The OCI client supports the following Cohere APIs:
OciClient) and V2 (OciClientV2) APIs
chat_stream()Available on OCI On-Demand Inference:
OciClient (V1) and OciClientV2 (V2)Not Available on OCI On-Demand Inference:
float and int8 simultaneously)Note: To use Generate or Rerank models on OCI, you need to:
For the latest model availability, see the OCI Generative AI documentation.
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!
On the other hand, contributions to the README are always very welcome!
78 followers · starred Sep 2023
Python Library for Accessing the Cohere API
See the code
The Cohere Python SDK allows access to Cohere models across many different platforms: the cohere platform, AWS (Bedrock, Sagemaker), Azure, GCP and Oracle OCI. For a full list of support and snippets, please take a look at the SDK support docs page.
Cohere documentation and API reference is available here.
pip install cohere
import cohere
co = cohere.ClientV2()
response = co.chat(
model="command-r-plus-08-2024",
messages=[{"role": "user", "content": "hello world!"}],
)
print(response)
[!TIP] You can set a system environment variable
CO_API_KEYto avoid writing your api key within your code, e.g. addexport CO_API_KEY=theapikeyforyouraccountin your ~/.zshrc or ~/.bashrc, open a new terminal, then code callingcohere.Client()will read this key.
The SDK supports streaming endpoints. To take advantage of this feature for chat,
use chat_stream.
import cohere
co = cohere.ClientV2()
response = co.chat_stream(
model="command-r-plus-08-2024",
messages=[{"role": "user", "content": "hello world!"}],
)
for event in response:
if event.type == "content-delta":
print(event.delta.message.content.text, end="")
The SDK supports Oracle Cloud Infrastructure (OCI) Generative AI service. First, install the OCI SDK:
pip install 'cohere[oci]'
Then use the OciClient or OciClientV2:
import cohere
# Using OCI config file authentication (default: ~/.oci/config)
co = cohere.OciClient(
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
response = co.embed(
model="embed-english-v3.0",
texts=["Hello world"],
input_type="search_document",
)
print(response.embeddings)
1. Config File (Default)
co = cohere.OciClient(
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
# Uses ~/.oci/config with DEFAULT profile
)
2. Custom Profile
co = cohere.OciClient(
oci_profile="MY_PROFILE",
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
3. Session-based Authentication (Security Token)
# Works with OCI CLI session tokens
co = cohere.OciClient(
oci_profile="MY_SESSION_PROFILE", # Profile with security_token_file
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
4. Direct Credentials
co = cohere.OciClient(
oci_user_id="ocid1.user.oc1...",
oci_fingerprint="xx:xx:xx:...",
oci_tenancy_id="ocid1.tenancy.oc1...",
oci_private_key_path="~/.oci/key.pem",
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
5. Instance Principal (for OCI Compute instances)
co = cohere.OciClient(
auth_type="instance_principal",
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
The OCI client supports the following Cohere APIs:
OciClient) and V2 (OciClientV2) APIs
chat_stream()Available on OCI On-Demand Inference:
OciClient (V1) and OciClientV2 (V2)Not Available on OCI On-Demand Inference:
float and int8 simultaneously)Note: To use Generate or Rerank models on OCI, you need to:
For the latest model availability, see the OCI Generative AI documentation.
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!
On the other hand, contributions to the README are always very welcome!
78 followers · starred Sep 2023