File size: 1,821 Bytes
258021f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import requests
import time
import logging
from enum import Enum
import json

FAILED_THRESHOLD = 10


def GPT4_requests(query):
    pass


def get_access_token_wenxinyiyan(client_id, client_secret):
        url = f"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}"
        
        payload = ""
        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        }
        
        response = requests.request("POST", url, headers=headers, data=payload)
        
        print(response.text)
        res_json = json.loads(response.text)
        return res_json['access_token']
    
def WenXinYiYan4_requests(query):
    API_KEY = ""
    SECRET_KEY = ""
    
    app_token = ''

    url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token=" + app_token
    
    response_log = str.upper("response_log")
    
    payload = json.dumps({
        "temperature":0.95,
        "top_p":0.8,
        "penalty_score":1.0,
        "messages": [
            {
                "role": "user",
                "content": query
            }
        ]
    })
    
    headers = {
        'Content-Type': 'application/json'
    }

    failed_count = 0
    while True:
        try:
            response = requests.request("POST", url, headers=headers, data=payload)
            text = json.loads(response.text)
            response_log = json.dumps(text)
            return text['result']
            
        except Exception as e:
            failed_count += 1
            logging.error(response_log)
            logging.error(e)
            if failed_count >= FAILED_THRESHOLD:
                break
            time.sleep(1)
    return response_log