File size: 1,555 Bytes
cdbb2b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import json
import logger
import time
def yi_moe(api_key,
           model="moyi-chat-v03-sglang", 
           user_content="如何下载github上的项目?", 
           system_content=None):
    
    url ="http://10.2.5.29:30869/v1/chat/completions"
    headers = {
        "Authorization": "Basic ZjIyMDIwYjRkMTIyM2UyNGI4NjVlMWIxZWI0YzAzZTM6NWJjS01PbENMTDRTV1MxaERkSHlTRzViSTJCd3psR1A=" + api_key,
        "Content-Type": "application/json"
    }
    data = {
        "model": model,
        "messages": [
        {
        "role": "system",
        "content": system_content
        },
        {
        "role": "user",
        "content": user_content
        }
        ],
        "temperature": 0.7,
        "stream": False,
        "max_tokens": 4096
    }
    max_retries = 5
    retry_count = 0
    while retry_count < max_retries:
        try:
            response = requests.post(url, json=data, headers=headers, timeout=60)
            if response.status_code == 200:
                response = response.json()
                text = response["choices"][0]["message"]["content"]
                return text
            else:
                logger.warning(f"{response.status_code}, {response.text}")
                retry_count += 1
                time.sleep(2)
                logger.info(f"Retrying... attempt {retry_count}")
        except Exception as e:
            logger.error(e)
            retry_count += 1
            time.sleep(2)
            logger.info(f"Retrying... attempt {retry_count}")
    return "error"