Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding issues and 403 errors #16

Closed
igorcosta opened this issue Feb 22, 2024 · 3 comments
Closed

Encoding issues and 403 errors #16

igorcosta opened this issue Feb 22, 2024 · 3 comments

Comments

@igorcosta
Copy link

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
Copy link

This is a duplicate of #7

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

@igorcosta
Copy link
Author

@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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants