groq / groq-python

The official Python Library for the Groq API
Apache License 2.0
353 stars 23 forks source link

Encoding issues and 403 errors #16

Closed igorcosta closed 6 months ago

igorcosta commented 6 months ago

Hey team, excited to be in the alpha program for the API, however I tried like others using your own implementation in Python, but similar to others #7, I'm facing the same issue.

python3.11/site-packages/httpx/_utils.py", line 53, in normalize_header_value
    return value.encode(encoding or "ascii")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'ascii' codec can't encode character '\u200b' in position 140: ordinal not in range(128)

Then I tried to implement my own client to try out, I got 403 errors combined with the above errors.

The 403 error doesn't make sense, since I'm able to successfully call the api via curl via terminal.

import asyncio
import aiohttp
import json
import os
from dotenv import load_dotenv
from http import HTTPStatus

load_dotenv()

class GroqAPI:
    def __init__(self):
        self.api_key = os.getenv("GROQ_API_KEY")
        self.base_url = "https://api.groq.com/v1/request_manager/text_completion"

    async def make_api_call(self, system_prompt, user_prompt):
        headers = {
            "Authorization": f"Bearer ${self.api_key}",
            "Content-Type": "application/json",
            "Accept": "application/json",
        }
        payload = {
            "model_id": "llama2-70b-4096",
            "system_prompt": system_prompt,
            "user_prompt": user_prompt,
        }

        async with aiohttp.ClientSession() as session:
            async with session.post(self.base_url, headers=headers, json=payload) as response:
                if response.status == HTTPStatus.OK:
                    try:
                        data = await response.json()
                        return data
                    except json.JSONDecodeError as e:
                        raise ValueError(
                            "Invalid JSON response from server") from e
                else:
                    response_text = await response.text()
                    raise RuntimeError(
                        f"API request failed with status {response.status}: {response_text}")
rattrayalex commented 6 months ago

This is a duplicate of https://github.com/groq/groq-python/issues/7

It seems you may have a zero-width space character in your api key. Can you try removing it?

igorcosta commented 6 months ago

@rattrayalex spot on Alex, after further investigation, when I copied and pasted the api key values from the email, somehow got the zero-width character.

hozen-groq commented 6 months ago

@rattrayalex Thanks again, Alex! 🙌 Will work on this API key weirdness with the team...