11from openai import OpenAI
22from etlapp .common .config import app_config
3+ from etlapp .common .rate_limiter import RateLimiter
34from typing import List , Dict
45
56
@@ -9,6 +10,7 @@ def __init__(
910 api_key : str = app_config .llm .api_key ,
1011 api_base : str = app_config .llm .api_base ,
1112 model_name : str = app_config .llm .model_name ,
13+ max_rpm : int = app_config .llm .max_rpm ,
1214 system_prompt : str = "你是一个乐于解答各种问题的助手。" ,
1315 temperature : float = 0.7 ,
1416 top_p : float = 0.7 ,
@@ -18,8 +20,13 @@ def __init__(
1820 self .system_prompt = system_prompt
1921 self .temperature = temperature
2022 self .top_p = top_p
23+ # 初始化限流器
24+ self .rate_limiter = RateLimiter (max_requests = max_rpm , window_seconds = 60 )
2125
2226 def _create_completion (self , messages : List [Dict [str , str ]]) -> str :
27+ # 在发送请求前进行限流
28+ self .rate_limiter .wait_and_acquire ()
29+
2330 completion = self .client .chat .completions .create (
2431 model = self .model_name ,
2532 messages = messages ,
@@ -37,6 +44,23 @@ def chat(self, content: str) -> str:
3744
3845 def chat_with_messages (self , messages : List [Dict [str , str ]]) -> str :
3946 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+ }
4064
4165
4266# Create a default instance
0 commit comments