Skip to content

Basic Usage - Direct access with HTTP request

Though it isn't the most common way to access a Chat Completions API, you can make an HTTP request directly to the API endpoint. Details on the Chat Completions API specification can be found at https://platform.openai.com/docs/api-reference/chat.

In this example we'll use the Claude 3.5 Haiku model and instruct it to be a helpful assistant. We ask it What is deep learning?. We also indicate that we do not want the response streamed. This means that it won't return any output until the full response has been generated. In the messages list, you'll notice that the first message is set to "role": "system". This is considered the system prompt. After the system prompt, we have a message set to "role": "user". This message is used to make a request to the Large Language Model. One more role that is not listed here is a message of "role": "assistant". This type of message indicates that it was received from the LLM. There are often multiple alternating user and assistant messages in the messages list.

import requests
from pprint import pprint

resp = requests.post(
    "https://chat.dartmouth.edu/api/chat/completions",
    headers={"Authorization": "bearer PLACE_KEY_HERE"},
    json={
        "model": "anthropic.claude-3-5-haiku-20241022",
        "messages": [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "What is deep learning?"},
        ],
        "stream": False,
    },
)
print(resp.json()["choices"][0]["message"]["content"])