File size: 1,998 Bytes
36c4cac
 
 
a243baa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# coding:utf-8
import requests
import json
from datetime import datetime, timedelta, timezone


class CerebrasUnofficial:
    def __init__(self, authjs_session_token: str):
        self.api_url = 'https://api.cerebras.ai'
        self.authjs_session_token = authjs_session_token
        self.key = None
        self.expiry = None
        self.session = requests.Session()
        self.session.headers.update({
            'Content-Type': 'application/json',
            'Cookie': f'authjs.session-token={self.authjs_session_token}',
        })


    def _get_key_from_graphql(self):
        json_data = {
            'operationName': 'GetMyDemoApiKey',
            'variables': {},
            'query': 'query GetMyDemoApiKey {\n  GetMyDemoApiKey\n}',
        }
        response = self.session.post(
            'https://inference.cerebras.ai/api/graphql', json=json_data
        )
        response.raise_for_status()

        data = response.json()
        try:
            if 'data' in data and 'GetMyDemoApiKey' in data['data']:
                self.key = data['data']['GetMyDemoApiKey']
        except Exception:
            raise Exception('Maybe your authjs.session-token is invalid.')


    def _get_expiry_from_session(self):
        response = self.session.get('https://cloud.cerebras.ai/api/auth/session')
        response.raise_for_status()
        data = response.json()
        if 'user' in data and 'demoApiKeyExpiry' in data['user']:
            self.expiry = datetime.fromisoformat(
                data['user']['demoApiKeyExpiry'].replace('Z', '+00:00')
            )


    def get_api_key(self) -> str:
        if self.key is None:
            self._get_key_from_graphql()
        else:
            if self.expiry is None:
                self._get_expiry_from_session()
            if datetime.now(timezone.utc) >= self.expiry:
                self._get_key_from_graphql()
                self.expiry = datetime.now(timezone.utc) + timedelta(minutes=10)

        return self.key