1
1
from openai import OpenAI
2
2
from etlapp .common .config import app_config
3
+ from etlapp .common .rate_limiter import RateLimiter
3
4
from typing import List , Dict
4
5
5
6
@@ -9,6 +10,7 @@ def __init__(
9
10
api_key : str = app_config .llm .api_key ,
10
11
api_base : str = app_config .llm .api_base ,
11
12
model_name : str = app_config .llm .model_name ,
13
+ max_rpm : int = app_config .llm .max_rpm ,
12
14
system_prompt : str = "你是一个乐于解答各种问题的助手。" ,
13
15
temperature : float = 0.7 ,
14
16
top_p : float = 0.7 ,
@@ -18,8 +20,13 @@ def __init__(
18
20
self .system_prompt = system_prompt
19
21
self .temperature = temperature
20
22
self .top_p = top_p
23
+ # 初始化限流器
24
+ self .rate_limiter = RateLimiter (max_requests = max_rpm , window_seconds = 60 )
21
25
22
26
def _create_completion (self , messages : List [Dict [str , str ]]) -> str :
27
+ # 在发送请求前进行限流
28
+ self .rate_limiter .wait_and_acquire ()
29
+
23
30
completion = self .client .chat .completions .create (
24
31
model = self .model_name ,
25
32
messages = messages ,
@@ -37,6 +44,23 @@ def chat(self, content: str) -> str:
37
44
38
45
def chat_with_messages (self , messages : List [Dict [str , str ]]) -> str :
39
46
return self ._create_completion (messages )
47
+
48
+ def get_rate_limit_status (self ) -> dict :
49
+ """
50
+ 获取当前限流状态
51
+
52
+ Returns:
53
+ dict: 包含剩余请求数和重置时间的状态信息
54
+ """
55
+ remaining = self .rate_limiter .get_remaining_requests ()
56
+ reset_time = self .rate_limiter .get_reset_time ()
57
+
58
+ return {
59
+ "remaining_requests" : remaining ,
60
+ "reset_time" : reset_time ,
61
+ "max_rpm" : self .rate_limiter .max_requests ,
62
+ "window_seconds" : self .rate_limiter .window_seconds
63
+ }
40
64
41
65
42
66
# Create a default instance
0 commit comments