@@ -106,287 +106,16 @@ This is **bold** and *italic* and ``inline code``.
106106
107107.. code-block :: python
108108
109- from collections import deque
110- from typing import Protocol
111-
112-
113- class Limit (Protocol ):
114- limit_id: str
115-
116- def is_allowed (
117- self ,
118- user_id : int ,
119- request_url : str ,
120- request_body : bytes ,
121- request_timestamp : float ,
122- ) -> bool : ...
123-
124- def record_request (
125- self ,
126- user_id : int ,
127- request_url : str ,
128- request_body : bytes ,
129- request_timestamp : float ,
130- ) -> None : ...
131-
132-
133- class RequestCountLimit :
134- def __init__ (
135- self ,
136- limit_id : str ,
137- urls : list[str ],
138- max_requests : int ,
139- window_seconds : float ,
140- ) -> None :
141- self .limit_id = limit_id
142- self .urls = urls
143- self .max_requests = max_requests
144- self .window_seconds = window_seconds
145- self .user_requests: dict[int , deque[float ]] = {}
146-
147- def is_allowed (
148- self ,
149- user_id : int ,
150- request_url : str ,
151- request_body : bytes ,
152- request_timestamp : float ,
153- ) -> bool :
154- del request_body
155- if request_url not in self .urls:
156- return True
157-
158- if user_id not in self .user_requests:
159- self .user_requests[user_id] = deque()
160-
161- requests = self .user_requests[user_id]
162- cutoff_time = request_timestamp - self .window_seconds
163-
164- while requests and requests[0 ] <= cutoff_time:
165- requests.popleft()
166-
167- return len (requests) < self .max_requests
168-
169- def record_request (
170- self ,
171- user_id : int ,
172- request_url : str ,
173- request_body : bytes ,
174- request_timestamp : float ,
175- ) -> None :
176- del request_body
177- if request_url not in self .urls:
178- return
179-
180- self .user_requests[user_id].append(request_timestamp)
181-
182-
183- class BodySizeLimit :
184- def __init__ (
185- self ,
186- limit_id : str ,
187- url : str ,
188- max_bytes : int ,
189- window_seconds : float ,
190- ) -> None :
191- self .limit_id = limit_id
192- self .url = url
193- self .max_bytes = max_bytes
194- self .window_seconds = window_seconds
195- self .user_body_sizes: dict[int , deque[tuple[float , int ]]] = {}
196-
197- def is_allowed (
198- self ,
199- user_id : int ,
200- request_url : str ,
201- request_body : bytes ,
202- request_timestamp : float ,
203- ) -> bool :
204- if request_url != self .url:
205- return True
206-
207- if user_id not in self .user_body_sizes:
208- self .user_body_sizes[user_id] = deque()
209-
210- body_sizes = self .user_body_sizes[user_id]
211- cutoff_time = request_timestamp - self .window_seconds
212-
213- while body_sizes and body_sizes[0 ][0 ] <= cutoff_time:
214- body_sizes.popleft()
215-
216- current_total = sum (size for _, size in body_sizes)
217- return current_total + len (request_body) <= self .max_bytes
218-
219- def record_request (
220- self ,
221- user_id : int ,
222- request_url : str ,
223- request_body : bytes ,
224- request_timestamp : float ,
225- ) -> None :
226- if request_url != self .url:
227- return
228-
229- self .user_body_sizes[user_id].append((request_timestamp, len (request_body)))
230-
231-
232- class RateLimiter :
233- def __init__ (self ) -> None :
234- self ._limits: list[Limit] = [
235- RequestCountLimit(
236- limit_id = " v1_60s" ,
237- urls = [" /v1/completions.stream" ],
238- max_requests = 3 ,
239- window_seconds = 60.0 ,
240- ),
241- RequestCountLimit(
242- limit_id = " v1_v2_3600s" ,
243- urls = [" /v1/completions.stream" , " /v2/completions.stream" ],
244- max_requests = 7 ,
245- window_seconds = 3600.0 ,
246- ),
247- BodySizeLimit(
248- limit_id = " v1_body_60s" ,
249- url = " /v1/completions.stream" ,
250- max_bytes = 100 ,
251- window_seconds = 60.0 ,
252- ),
253- ]
254-
255- def allow_request (
256- self ,
257- * ,
258- user_id : int ,
259- request_url : str ,
260- request_body : bytes ,
261- request_timestamp : float ,
262- ) -> list[str ]:
263- violated_limit_ids: list[str ] = [
264- limit.limit_id
265- for limit in self ._limits
266- if not limit.is_allowed(
267- user_id = user_id,
268- request_url = request_url,
269- request_body = request_body,
270- request_timestamp = request_timestamp,
271- )
272- ]
273-
274- if violated_limit_ids:
275- return violated_limit_ids
276-
277- for limit in self ._limits:
278- limit.record_request(
279- user_id = user_id,
280- request_url = request_url,
281- request_body = request_body,
282- request_timestamp = request_timestamp,
283- )
284- return []
285-
286- class BodySizeLimit :
287- def __init__ (
288- self ,
289- limit_id : str ,
290- url : str ,
291- max_bytes : int ,
292- window_seconds : float ,
293- ) -> None :
294- self .limit_id = limit_id
295- self .url = url
296- self .max_bytes = max_bytes
297- self .window_seconds = window_seconds
298- self .user_body_sizes: dict[int , deque[tuple[float , int ]]] = {}
299-
300- def is_allowed (
301- self ,
302- user_id : int ,
303- request_url : str ,
304- request_body : bytes ,
305- request_timestamp : float ,
306- ) -> bool :
307- if request_url != self .url:
308- return True
309-
310- if user_id not in self .user_body_sizes:
311- self .user_body_sizes[user_id] = deque()
312-
313- body_sizes = self .user_body_sizes[user_id]
314- cutoff_time = request_timestamp - self .window_seconds
315-
316- while body_sizes and body_sizes[0 ][0 ] <= cutoff_time:
317- body_sizes.popleft()
318-
319- current_total = sum (size for _, size in body_sizes)
320- return current_total + len (request_body) <= self .max_bytes
321-
322- def record_request (
323- self ,
324- user_id : int ,
325- request_url : str ,
326- request_body : bytes ,
327- request_timestamp : float ,
328- ) -> None :
329- if request_url != self .url:
330- return
331-
332- self .user_body_sizes[user_id].append((request_timestamp, len (request_body)))
333-
334-
335- class RateLimiter :
336- def __init__ (self ) -> None :
337- self ._limits: list[Limit] = [
338- RequestCountLimit(
339- limit_id = " v1_60s" ,
340- urls = [" /v1/completions.stream" ],
341- max_requests = 3 ,
342- window_seconds = 60.0 ,
343- ),
344- RequestCountLimit(
345- limit_id = " v1_v2_3600s" ,
346- urls = [" /v1/completions.stream" , " /v2/completions.stream" ],
347- max_requests = 7 ,
348- window_seconds = 3600.0 ,
349- ),
350- BodySizeLimit(
351- limit_id = " v1_body_60s" ,
352- url = " /v1/completions.stream" ,
353- max_bytes = 100 ,
354- window_seconds = 60.0 ,
355- ),
356- ]
357-
358- def allow_request (
359- self ,
360- * ,
361- user_id : int ,
362- request_url : str ,
363- request_body : bytes ,
364- request_timestamp : float ,
365- ) -> list[str ]:
366- violated_limit_ids: list[str ] = [
367- limit.limit_id
368- for limit in self ._limits
369- if not limit.is_allowed(
370- user_id = user_id,
371- request_url = request_url,
372- request_body = request_body,
373- request_timestamp = request_timestamp,
374- )
375- ]
376-
377- if violated_limit_ids:
378- return violated_limit_ids
379-
380- for limit in self ._limits:
381- limit.record_request(
382- user_id = user_id,
383- request_url = request_url,
384- request_body = request_body,
385- request_timestamp = request_timestamp,
386- )
387- return []
109+ """ Python code."""
388110
389111
112+ def hello_world () -> int :
113+ """ Return the answer."""
114+ return 42
115+
116+
117+ hello_world()
118+
390119 .. code-block :: console
391120
392121 $ pip install sphinx-notionbuilder
0 commit comments