File size: 3,306 Bytes
5c20520
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
import openai
import time
from openai import OpenAI
# from zhipuai import ZhipuAI
import requests
import re
# from utils.parse_llm_output import try_parse_json_object

def get_oai_completion(prompt):
    # api_pools = [
    #     ("your_api_key","base_url","model_name"),
    # ]
    api_pools = [
        ("sk-0060332083ea440bb35b676df023ce01","https://dashscope.aliyuncs.com/compatible-mode/v1","deepseek-v3")
    ]
    api = api_pools[0]
    api_key, base_url, model = api
    
    if "GLM" in model:
        client = ZhipuAI(api_key=api_key)
        # from utils.prompts import GLM_JSON_RESPONSE_PREFIX, GLM_JSON_RESPONSE_SUFFIX, system_prompt
        # system_prompt = f"{GLM_JSON_RESPONSE_PREFIX}{system_prompt}"
        # user_prompt = f"{prompt}{GLM_JSON_RESPONSE_SUFFIX}"
        system_prompt = "You are a helpful assistant."
        user_prompt = prompt
    else:
        client = OpenAI(api_key=api_key, base_url=base_url)
        system_prompt = "You are a helpful assistant."
        user_prompt = prompt

    try:
        if "GLM" in model:
            response = client.chat.completions.create(
                model=model,
                messages=[
                    {"role": "system", "content": system_prompt},
                    {"role": "user", "content": user_prompt},
                ],
                # response_format={ "type": "json_object" },
                temperature=0.1,
                top_p=0.7,
                stream=False
            )
        else:
            # print(user_prompt)
            response = client.chat.completions.create(
                model=model,
                messages=[
                    {"role": "system", "content": system_prompt},
                    {"role": "user", "content": user_prompt},
                ],
                # response_format={ "type": "json_object" },
                #根据任务的不同来调整
                temperature=0.8,
                max_tokens=8000,
                stream=False
            )
        res = response.choices[0].message.content

        # if "GLM" in model:
        #     pattern = re.compile(r"```(?:json\s+)?(\{.*?\})\s*```", re.DOTALL)
        #     match = pattern.search(res)
        #     if match:
        #         gpt_output, _ = try_parse_json_object(match.group(1).strip())
        #     else:
        #         gpt_output = res
        # else:
        #     gpt_output = res
            
        pattern = re.compile(r"```(?:json\s+)?(\{.*?\})\s*```", re.DOTALL)
        match = pattern.search(res)
        # if match:
        #     gpt_output, _ = try_parse_json_object(match.group(1).strip())
        # else:
        #     gpt_output = res
        gpt_output = res

        return gpt_output
        
    except requests.exceptions.Timeout:
        print("The API request timed out. Please try again later.")
        return None
    except Exception as e:
        print(e)
        return None

def call_chatgpt(ins):
    success = False
    re_try_count = 5
    ans = ''
    while not success and re_try_count >= 0:
        re_try_count -= 1
        try:
            ans = get_oai_completion(ins)
            success = True
        except Exception as e:
            print(f"Retry times: {re_try_count}; Error: {e}", flush=True)
            time.sleep(5)
    return ans