File size: 2,564 Bytes
99ecf5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
import requests
import json

def fetch_ai_response():
    url = "https://api.deepinfra.com/v1/openai/chat/completions"
    headers = {
        "Accept-Language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7",
        "Connection": "keep-alive",
        "Content-Type": "application/json",
        "Origin": "https://deepinfra.com",
        "Referer": "https://deepinfra.com/",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-site",
        "User-Agent": "Mozilla/5.0 (Linux; Android 11.0; Surface Duo) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Mobile Safari/537.36",
        "X-Deepinfra-Source": "model-embed",
        "accept": "text/event-stream",
        "sec-ch-ua": "\"Chromium\";v=\"128\", \"Not;A=Brand\";v=\"24\", \"Google Chrome\";v=\"128\"",
        "sec-ch-ua-mobile": "?1",
        "sec-ch-ua-platform": "\"Android\""
    }
    data = {
        "model": "meta-llama/Meta-Llama-3.1-405B-Instruct",
        "messages": [{"role": "user", "content": "c'est quoi un trou noir ?"}],
        "temperature": 0.1, 
        "max_tokens": 100000,   
        "stream": True
    }

    response = requests.post(url, headers=headers, json=data, stream=True)
    
    if response.status_code != 200:
        print(f"Erreur lors de la requête : {response.status_code}")
        return
    
    # Initialisation des variables
    full_text = []
    output_size = 0
    
    for line in response.iter_lines():
        if line:
            try:
                decoded_line = line.decode('utf-8')
                if decoded_line.startswith('data:'):
                    json_data = decoded_line[5:].strip()
                    if json_data == '[DONE]':
                        break
                    parsed_data = json.loads(json_data)
                    
                    # Extraction du texte
                    choices = parsed_data.get("choices", [])
                    for choice in choices:
                        delta = choice.get("delta", {})
                        content = delta.get("content", "")
                        if content:
                            full_text.append(content)
                            output_size += len(content)
                    
            except json.JSONDecodeError:
                continue
    
    # Affichage du texte complet et des informations supplémentaires
    complete_text = ''.join(full_text)
    print(complete_text)

if __name__ == "__main__":
    fetch_ai_response()