Skip to main content

A Python library for the Cohere API

Project description

ci badge version badge license badge

Cohere Python SDK

This package provides functionality developed to simplify interfacing with the Cohere API in Python 3.

Documentation

See the API documentation.

Installation

The package can be installed with pip:

pip install --upgrade cohere

Install from source:

python setup.py install

Requirements

  • Python 3.6+

Quick Start

To use this library, you must have an API key and specify it as a string when creating the cohere.Client object. API keys can be created through the platform. This is a basic example of the creating the client and using the generate endpoint.

import cohere

# initialize the Cohere Client with an API Key
co = cohere.Client('YOUR_API_KEY')

# generate a prediction for a prompt
prediction = co.generate(
            model='large',
            prompt='co:here',
            max_tokens=10)

# print the predicted text
print('prediction: {}'.format(prediction.generations[0].text))

Versioning

To use the SDK with a specific API version, you can specify it when creating the Cohere Client:

import cohere

co = cohere.Client('YOUR_API_KEY', '2022-12-06')

Endpoints

For a full breakdown of endpoints and arguments, please consult the Cohere Docs.

Cohere Endpoint Function
/generate co.generate()
/embed co.embed()
/classify co.classify()
/tokenize co.tokenize()
/detokenize co.detokenize()
/detect-language co.detect_language()

Models

When you call Cohere's APIs we decide on a good default model for your use-case behind the scenes. The default model is great to get you started, but in production environments we recommend that you specify the model size yourself via the model parameter. Learn more about the available models here(https://os.cohere.ai)

Responses

All of the endpoint functions will return a Cohere object corresponding to the endpoint (e.g. for generation, it would be Generation). The responses can be found as instance variables of the object (e.g. generation would be Generation.text). The names of these instance variables and a detailed breakdown of the response body can be found in the Cohere Docs. Printing the Cohere response object itself will display an organized view of the instance variables.

Exceptions

Unsuccessful API calls from the SDK will raise an exception. Please see the documentation's page on errors for more information about what the errors mean.

Project details


Release history Release notifications | RSS feed

This version

3.2.6

Download files

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

Source Distribution

cohere-3.2.6.tar.gz (12.0 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: cohere-3.2.6.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for cohere-3.2.6.tar.gz
Algorithm Hash digest
SHA256 9cac6b71606d6a7b5d8aedb361d9ff89ed12d1972fa37bc4aa417a4d777fb143
MD5 51bdcf42d6f602ef5880187130582811
BLAKE2b-256 e0e43853b6138a8c322235d0dae4cfc62f618c6cd571b19f9ca3d7313f3fe910

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page