• Stars
    star
    294
  • Rank 141,303 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 4 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python Library for Accessing the Cohere API

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

  • SDK Documentation is hosted on Read the docs.
    • You can build SDK documentation locally using cd docs; make clean html.
  • For more details on advanced parameters, you can also consult the API documentation.
  • See the examples directory for examples, including some additional functionality for visualizations in Jupyter notebooks.

Installation

The package can be installed with pip:

pip install --upgrade cohere

Install from source:

pip install .

Requirements

  • Python 3.7+

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

There is also an asyncio compatible client called cohere.AsyncClient with an equivalent interface. Consult the SDK Docs for more details.

Versioning

Each SDK release is only compatible with the latest version of the Cohere API at the time of release. To use the SDK with an older API version, you need to download a version of the SDK tied to the API version you want. Look at the Changelog to see which SDK version to download.

Endpoints

For a full breakdown of endpoints and arguments, please consult the SDK Docs and Cohere API 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 SDK Docs and 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.

Contributing

To set up a development environment, run:

poetry shell    # any time you want to run code or tests
poetry install  # install and update dependencies in your environment, the first time

In addition, to ensure your code is formatted correctly, install pre-commit hooks using:

pre-commit install

You can run tests locally using:

python -m pytest

You can configure a different base url with:

CO_API_URL="https://localhost:8050" python3 foo.py

or

cohere.COHERE_API_URL = "https://localhost:8050" # Place before client initilization

More Repositories

1

cohere-toolkit

Cohere Toolkit is a collection of prebuilt components enabling users to quickly build and deploy RAG applications.
TypeScript
2,814
star
2

notebooks

Code examples and jupyter notebooks for the Cohere Platform
Jupyter Notebook
472
star
3

sandbox-topically

Topic modeling helpers using managed language models from Cohere. Name text clusters using large GPT models.
Jupyter Notebook
214
star
4

DiskVectorIndex

Python
202
star
5

BinaryVectorDB

Efficient vector database for hundred millions of embeddings.
Python
198
star
6

sandbox-conversant-lib

Conversational AI tooling & personas built on Cohere's LLMs
Python
173
star
7

cohere-terrarium

A simple Python sandbox for helpful LLM data agents
Python
153
star
8

quick-start-connectors

This open-source repository offers reference code for integrating workplace datastores with Cohere's LLMs, enabling developers and businesses to perform seamless retrieval-augmented generation (RAG) on their own data.
Python
140
star
9

sandbox-grounded-qa

A sandbox repo for grounded question answering with Cohere and Google Search
Python
135
star
10

magikarp

Python
125
star
11

cohere-typescript

The Cohere TypeScript SDK
TypeScript
124
star
12

sandbox-toy-semantic-search

A demonstration of how a toy (but usable!) semantic search engine can be quickly built using Cohere's platform.
Python
114
star
13

sandbox-multilingual

A demonstration of a multilingual semantic search engine you can be quickly built using Cohere's platform.
Python
59
star
14

cohere-aws

Jupyter Notebook
57
star
15

sandbox-accelerating-chatbot-training

Leveraging Cohere's models to enable zero-shot routing
Python
53
star
16

cohere-go

Go Library for Accessing the Cohere API
Go
45
star
17

samples

Sample integrations built by Cohere.
JavaScript
25
star
18

examples

Examples and Demos using the Cohere APIs
Jupyter Notebook
23
star
19

sandbox-condense

Webpage Summarizer
JavaScript
20
star
20

human-feedback-paper

Code and data from the paper 'Human Feedback is not Gold Standard'
Jupyter Notebook
18
star
21

public-demos

Public demos using the Cohere platform!
TypeScript
11
star
22

tokenizer

BPE tokenization implemented in Golang 💙
Go
10
star
23

cohere-java

Cohere's Java SDK
Java
7
star
24

cohere-developer-experience

Docs, Snippets, Guides
MDX
5
star
25

co

Cohere command line tool
Shell
3
star
26

cohere-compass-sdk

Provides an SDK for interacting with Compass, and Compass Parsing
Python
2
star
27

co-cli

Shell
1
star
28

cohere-ruby

Ruby
1
star