Python code for every LLM API: OpenAI, Anthropic, Cohere, Mistral and Gemini ⚡️
Tags: llm, openai, cohere, mistral
Published: 2023-12-24 20:09:19

Did you know you don't need to import any special libraries to generate text via an LLM?

All you need is an API key from the LLM platform, a Python environment, and the requests library. Then in a few lines of code you can get started.

# Quick start!

If you are working from the command-line, you can run any of the scripts below with a simple call: 

`OPENAI_API_KEY=yourkeygoeshere python3 script.py`

OR, you can add `OPENAI_API_KEY=yourkeygoeshere` to a `.env` file in the same directory, and add this code to the top of your script:
```python
from dotenv import load_dotenv
load_dotenv()
```

OR, if you're using Google Colab, click the 🔑 icon in the left sidebar and follow the resulting instructions.

# API Reference

Start by copying the example code below for the API you need, then scroll to the end of this article for more tips.

## OpenAI

🔑 Get API key [here](https://platform.openai.com/account/api-keys).

📃 API [docs](https://platform.openai.com/docs/).

```python
import requests
import os

response = requests.post(
    "https://api.openai.com/v1/chat/completions",
    headers={
        "Content-Type": "application/json",
        "Authorization": "Bearer " + os.environ["OPENAI_API_KEY"]
    },
    json={
        "model": "gpt-3.5-turbo",
        "messages": [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Hello!"}
        ]
    }
)

print(response.content)
```

## Anthropic

🔑 Get API key [here](https://console.anthropic.com/account/keys).

📃 API [docs](https://docs.anthropic.com/).

```python
import requests
import os

response = requests.post(
    "https://api.anthropic.com/v1/complete",
    headers={
        "accept": "application/json",
        "anthropic-version": "2023-06-01",
        "content-type": "application/json",
        "x-api-key": os.environ["ANTHROPIC_API_KEY"]
    },
    json={
        "model": "claude-2.1",
        "prompt": "\n\nHuman: Hello, world!\n\nAssistant:",
        "max_tokens_to_sample": 256
    }
)

print(response.content)
```

## Cohere

🔑 Get API key [here](https://dashboard.cohere.com/api-keys).

📃 API [docs](https://docs.cohere.com/).

```python
import requests
import os

response = requests.post(
    "https://api.cohere.ai/v1/chat",
    headers={
        "accept": "application/json",
        "content-type": "application/json",
        "Authorization": f"Bearer {os.environ['COHERE_API_KEY']}"
    },
    json={
        "chat_history": [
            {"role": "USER", "message": "Who discovered gravity?"},
            {"role": "CHATBOT", "message": "The man who is widely credited with discovering gravity is Sir Isaac Newton"}
        ],
        "message": "What year was he born?",
        "connectors": [{"id": "web-search"}]
    }
)

print(response.content)
```

## Mistral

🔑 Get API key [here](https://console.mistral.ai/users/api-keys/).

📃 API [docs](https://docs.mistral.ai/api/).

```python
import requests
import os

response = requests.post(
    "https://api.mistral.ai/v1/chat/completions",
    headers={
        "Content-Type": "application/json",
        "Accept": "application/json",
        "Authorization": f"Bearer {os.environ['MISTRAL_API_KEY']}"
    },
    json={
        "model": "mistral-tiny",
        "messages": [{"role": "user", "content": "Who is the most renowned French writer?"}]
    }
)

print(response.content)
```

## Google

🔑 Get API key [here](https://makersuite.google.com/app/apikey).

📃 API [docs](https://ai.google.dev/api/rest).

```python
import requests
import os

response = requests.post(
    "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=" + os.environ["GOOGLE_API_KEY"],
    headers={
        "Content-Type": "application/json"
    },
    json={
        "contents": [
            {
                "parts": [
                    {
                        "text": "Write a story about a magic backpack."
                    }
                ]
            }
        ]
    }
)

print(response.content)
```