Hi,
The bare minimum example of querying the API via Python is not correct.
If you try the code below it will fail with an 401 Authorization error:
from openai import OpenAI
YOUR_API_KEY = "INSERT API KEY HERE"
messages = [
{
"role": "system",
"content": (
"You are an artificial intelligence assistant and you need to "
"engage in a helpful, detailed, polite conversation with a user."
),
},
{
"role": "user",
"content": (
"Count to 100, with a comma between each number and no newlines. "
"E.g., 1, 2, 3, ..."
),
},
]
client = OpenAI(api_key=YOUR_API_KEY, base_url="https://api.perplexity.ai")
# demo chat completion without streaming
response = client.chat.completions.create(
model="mistral-7b-instruct",
messages=messages,
)
print(response)
The example that should be shown instead is the one that features the addition of the extra_headers
that allow this to work:
response = client.chat.completions.create(
model="mistral-7b-instruct",
messages=messages,
extra_headers={"Authorization": f"Bearer {YOUR_API_KEY}"},
)
Looking forward to having this fixed as it is easy for beginners to trip here.
This also resulted in libraries like litellm
and magentic
not properly supporting adding a custom header for Perplextiy to work with custom keys that aren’t defined as environment variables.