File size: 3,759 Bytes
e9ce3e8
 
 
 
8391956
 
 
 
e9ce3e8
 
 
8391956
 
e9ce3e8
 
 
 
 
 
 
 
8391956
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9ce3e8
8391956
e9ce3e8
 
 
 
 
 
 
 
 
 
8391956
8d4a67d
 
8391956
 
 
e9ce3e8
 
 
 
 
 
 
 
 
 
 
8391956
e9ce3e8
 
 
 
 
 
 
 
 
 
 
 
 
 
8391956
e9ce3e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import base64
import numpy as np
import random
import logging
import os
import json
import openai
from openai import OpenAIError
from time import sleep


def _throttled_openai_chat_completion_create(
    client,
    model: str,
    messages,
    temperature: float,
    max_tokens: int,
    top_p: float,
    json_format: bool = False,
    n: int = 1,
):
    """同步的OpenAI聊天补全函数,支持限流与重试"""
    for _ in range(10):  # 进行10次尝试
        try:
            if json_format:
                return client.chat.completions.create(
                    model=model,
                    messages=messages,
                    temperature=temperature,
                    max_tokens=max_tokens,
                    top_p=top_p,
                    n=n,
                    response_format={"type": "json_object"},
                )
            else:
                return client.chat.completions.create(
                    model=model,
                    messages=messages,
                    temperature=temperature,
                    max_tokens=max_tokens,
                    top_p=top_p,
                    n=n,
                )
        except openai.RateLimitError as e:
            print("Rate limit exceeded, retrying...")
            sleep(random.randint(10, 20))  # 增加重试等待时间
        except openai.BadRequestError as e:
            print(e)
            return None
        except OpenAIError as e:
            print(e)
            sleep(random.randint(5, 10))
    return None


def generate_from_openai_chat_completion(
    client,
    messages,
    engine_name: str,
    temperature: float = 1.0,
    max_tokens: int = 512,
    top_p: float = 1.0,
    requests_per_minute: int = 100,
    json_format: bool = False,
    n: int = 1,
):
    """同步生成OpenAI聊天补全"""
    # delay = 60.0 / requests_per_minute
    # sleep(delay)  # 简单的限流处理

    responses = [
        _throttled_openai_chat_completion_create(
            client,
            model=engine_name,
            messages=message,
            temperature=temperature,
            max_tokens=max_tokens,
            top_p=top_p,
            json_format=json_format,
            n=n,
        )
        for message in messages
    ]

    empty_dict = {
        "question": "",
        "options": {
            "A": "",
            "B": "",
            "C": "",
            "D": "",
        },
        "distractors": {
            "E": "",
            "F": "",
            "G": "",
        },
        "correct_answer": ""
    }
    empty_str = ""
    outputs = []
    for response in responses:
        if n == 1:
            if json_format:
                if response and response.choices[0] and response.choices[0].message and response.choices[0].message.content:
                    outputs.append(json.loads(response.choices[0].message.content))
                else:
                    outputs.append(empty_dict)
            else:
                if response and response.choices[0] and response.choices[0].message and response.choices[0].message.content:
                    outputs.append(response.choices[0].message.content)
                else:
                    outputs.append(empty_str)
        else:
            if json_format:
                outputs.append([
                    json.loads(response.choices[i].message.content) if response and response.choices[i].message.content else empty_dict
                    for i in range(n)
                ])
            else:
                outputs.append([
                    response.choices[i].message.content if response and response.choices[i].message.content else empty_str
                    for i in range(n)
                ])
    return outputs