def _get_chat_completion()

in src/alpaca_eval/decoders/jinachat.py [0:0]


def _get_chat_completion(api_key, prompt):
    url = "https://api.chat.jina.ai/v1/chat/completions"
    headers = {"authorization": f"Bearer {api_key}", "content-type": "application/json"}
    json_payload = {"messages": prompt}

    max_retries = 10

    for attempt in range(max_retries):
        try:
            response = requests.post(url, headers=headers, json=json_payload)
            response.raise_for_status()  # Will raise an HTTPError if one occurred.
            message = response.json()["choices"][0]["message"]["content"]
            message_tokens = response.json()["usage"]["completion_tokens"]
            return message, message_tokens
        except (json.JSONDecodeError, requests.exceptions.HTTPError) as e:
            logging.warning(f"Error occurred: {e}, Attempt {attempt + 1} of {max_retries}")
            time.sleep(5)
            if attempt + 1 == max_retries:
                logging.exception("Max retries reached. Raising exception.")
                logging.exception(f"Request data -> URL: {url}, Headers: {headers}, JSON Payload: {json_payload}")
                raise
        except Exception as e:
            logging.exception(f"An unexpected error occurred: {e}")
            raise