Skip to main content

Cohere Python SDK

version badge license badge fern shield

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.

Documentation

Cohere documentation and API reference is available here.

Installation

pip install cohere

Usage

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_KEY to avoid writing your api key within your code, e.g. add export CO_API_KEY=theapikeyforyouraccount in your ~/.zshrc or ~/.bashrc, open a new terminal, then code calling cohere.Client() will read this key.

Streaming

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="")

Oracle Cloud Infrastructure (OCI)

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)

OCI Authentication Methods

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...",
)

Supported OCI APIs

The OCI client supports the following Cohere APIs:

  • Embed: Full support for all embedding models
  • Chat: Full support with both V1 (OciClient) and V2 (OciClientV2) APIs
    • Streaming available via chat_stream()
    • Supports Command-R and Command-A model families

OCI Model Availability and Limitations

Available on OCI On-Demand Inference:

  • Embed models: available on OCI Generative AI
  • Chat models: available via OciClient (V1) and OciClientV2 (V2)

Not Available on OCI On-Demand Inference:

  • Generate API: OCI TEXT_GENERATION models are base models that require fine-tuning before deployment
  • Rerank API: OCI TEXT_RERANK models are base models that require fine-tuning before deployment
  • Multiple Embedding Types: OCI on-demand models only support single embedding type per request (cannot request both float and int8 simultaneously)

Note: To use Generate or Rerank models on OCI, you need to:

  1. Fine-tune the base model using OCI's fine-tuning service
  2. Deploy the fine-tuned model to a dedicated endpoint
  3. Update your code to use the deployed model endpoint

For the latest model availability, see the OCI Generative AI documentation.

Contributing

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!

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cohere-7.0.9.tar.gz (213.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cohere-7.0.9-py3-none-any.whl (357.6 kB view details)

Uploaded Python 3

File details

Details for the file cohere-7.0.9.tar.gz.

File metadata

  • Download URL: cohere-7.0.9.tar.gz
  • Upload date:
  • Size: 213.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.20 Linux/6.17.0-1022-azure

File hashes

Hashes for cohere-7.0.9.tar.gz
Algorithm Hash digest
SHA256 7fe21cb4625bf60dd36927a50b4161fd26162a2977336f86ae47a2b0cfceecb5
MD5 76bf0aa162fa7c355aab524843b02187
BLAKE2b-256 6d7912fc0b2703e518d7f30bcabbebacece26248e7b0c45c260965131af3ad1c

See more details on using hashes here.

File details

Details for the file cohere-7.0.9-py3-none-any.whl.

File metadata

  • Download URL: cohere-7.0.9-py3-none-any.whl
  • Upload date:
  • Size: 357.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.20 Linux/6.17.0-1022-azure

File hashes

Hashes for cohere-7.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 ad20f8c829cd380dad930e80abc48e8f7ae3e73d30bdb1a652cf5dfb07d3de1c
MD5 65506e9199850929862a05340ec98593
BLAKE2b-256 4fd74afeeb377dbffa4afbf96349e74f120405f267746f7dbb90878d0535dafc

See more details on using hashes here.

Release history Release notifications | RSS feed

7.1.0

2 files

This release

7.0.9 This release

2 files

7.0.8

2 files

7.0.7

2 files

7.0.6

2 files

7.0.5

2 files

7.0.4

2 files

7.0.3

2 files

7.0.2

2 files

7.0.1

2 files

7.0.0

2 files

6.1.0

2 files

6.0.0

2 files

5.21.1

2 files

5.21.0

2 files

5.20.7

2 files

5.20.6

2 files

5.20.5

2 files

5.20.4

2 files

5.20.2

2 files

5.20.1

2 files

5.20.0

2 files

5.19.0

2 files

5.18.0

2 files

5.17.0

2 files

5.16.3

2 files

5.16.2

2 files

5.16.1

2 files

5.16.0

2 files

5.15.0

2 files

5.14.2

2 files

5.14.0

2 files

5.13.12

2 files

5.13.11

2 files

5.13.10

2 files

5.13.9

2 files

5.13.8

2 files

5.13.6

2 files

5.13.5

2 files

5.13.4

2 files

5.13.3

2 files

5.13.2

2 files

5.13.1

2 files

5.13.0

2 files

5.12.0

2 files

5.11.4

2 files

5.11.3

2 files

5.11.2

2 files

5.11.1

2 files

5.11.0

2 files

5.10.0

2 files

5.9.4

2 files

5.9.3

2 files

5.9.2

2 files

5.9.1

2 files

5.9.0

2 files

5.8.1

2 files

5.8.0

2 files

5.6.2

2 files

5.6.1

2 files

5.6.0

2 files

5.5.8

2 files

5.5.7

2 files

5.5.6

2 files

5.5.5

2 files

5.5.4

2 files

5.5.3

2 files

5.5.2

2 files

5.5.1

2 files

5.5.0

2 files

5.4.0

2 files

5.3.5

2 files

5.3.4

2 files

5.3.3

2 files

5.3.2

2 files

5.3.1

2 files

5.3.0

2 files

5.2.6

2 files

5.2.5

2 files

5.2.4

2 files

5.2.3

2 files

5.2.2

2 files

5.2.1

2 files

5.2.0

2 files

5.1.8

2 files

5.1.7

2 files

5.1.6

2 files

5.1.5

2 files

5.1.4

2 files

5.1.3

2 files

5.1.2

2 files

5.1.1

2 files

5.1.0

2 files

5.0.2

2 files

5.0.1

2 files

5.0.0

2 files

4.57

2 files

4.56

2 files

4.55

2 files

4.54

2 files

4.53

2 files

4.52

2 files

4.51

2 files

4.50

2 files

4.49

2 files

4.48

2 files

4.47

2 files

4.46

2 files

4.45

2 files

4.44

2 files

4.43

2 files

4.42

2 files

4.41

2 files

4.40

2 files

4.39

2 files

4.38

2 files

4.37

2 files

4.36

2 files

4.35

2 files

4.34

2 files

4.33

2 files

4.32

2 files

4.31

2 files

4.30

2 files

4.27

2 files

4.26.1

2 files

4.26

2 files

4.25

2 files

4.24

2 files

4.23

2 files

4.22

2 files

4.21

2 files

4.20.2

2 files

4.20.1

2 files

4.20.0

2 files

4.19.3

2 files

4.19.2

2 files

4.18.0

2 files

4.17.0

2 files

4.16.0

2 files

4.15.0

2 files

4.14.0

2 files

4.13.1

2 files

4.13.0

2 files

4.12.1

2 files

4.12.0

2 files

4.11.2

2 files

4.11.1

2 files

4.11.0

2 files

4.10.0

2 files

4.9.1

2 files

4.9.0

2 files

4.8.0

2 files

4.7.0

2 files

4.6.1

2 files

4.6.0

2 files

4.5.1

2 files

4.5.0

2 files

4.4.2

2 files

4.4.1

2 files

4.4.0

2 files

4.3.1

2 files

4.3.0

2 files

4.2.1

2 files

4.2.0

2 files

4.1.7

2 files

4.1.6

2 files

4.1.5

2 files

4.1.4

2 files

4.1.3

2 files

4.1.2

2 files

4.1.1

2 files

4.1.0

2 files

4.0.6

2 files

4.0.5

2 files

4.0.4

2 files

4.0.3

2 files

4.0.2

2 files

4.0.1

2 files

4.0.0

2 files

3.10.0

1 file

3.9.1

1 file

3.9.0

1 file

3.8.0

1 file

3.7.0

1 file

3.6.2

1 file

3.6.1

1 file

3.6.0

1 file

3.5.0

1 file

3.4.0

1 file

3.3.4

1 file

3.3.3

1 file

3.3.2

1 file

3.3.1

1 file

3.3.0

1 file

3.2.6

1 file

3.2.5

1 file

3.2.4

1 file

3.2.3

1 file

3.2.2

1 file

3.2.1

1 file

3.2.0

1 file

3.1.9

1 file

3.1.8

1 file

3.1.6

1 file

3.1.5

1 file

3.1.4

1 file

3.1.3

1 file

3.1.2

1 file

3.1.1

1 file

3.0.1

1 file

3.0.0

1 file

2.9.1

1 file

2.9.0

1 file

2.8.0

1 file

2.7.0

1 file

2.6.1

1 file

2.6.0

1 file

2.5.0

1 file

2.4.2

1 file

2.4.1

1 file

2.4.0

1 file

2.3.0

1 file

2.2.5

1 file

2.2.4

1 file

2.2.3

1 file

2.2.2

1 file

2.2.1

1 file

2.2.0

1 file

2.1.0

6 files

2.0.0

6 files

1.3.10

6 files

1.3.9

6 files

1.3.8

6 files

1.3.7

6 files

1.3.6

6 files

1.3.5

6 files

1.3.4

6 files

1.3.3

6 files

1.3.2

1 file

1.3.1

1 file

1.2.1

2 files

1.2.0

2 files

1.1.0

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

0.0.13

2 files

0.0.12

2 files

0.0.11

2 files

0.0.10

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page