GitLab

accessible_worlds.aw_requests

 1from openai import AsyncOpenAI
 2import random
 3
 4async def get_response( 
 5    semaphore,
 6    client : AsyncOpenAI,
 7    model : str,
 8    system_prompt : str,  
 9    user_prompt   : str,
10    seed          : int,
11    temperature   : float = 0.85,
12    reasoning_budget : int = 0,
13    max_request_attempts : int = 3 ) -> str :
14
15    n_tries = 0
16
17    while n_tries < max_request_attempts :
18        try:
19
20            async with semaphore :
21
22                response = await client.chat.completions.create(    
23                    model=model,
24                    messages=[
25                        {
26                            "role": "system",
27                            "content": system_prompt
28                        },
29                        {
30                            "role": "user",
31                            "content": user_prompt
32                        }
33                    ],
34                    extra_body={
35                        "reasoning_budget": reasoning_budget,
36                        "reasoning_budget_message": "\nThinking time is up. Provide the final answer now."
37                    },
38                    reasoning_effort=( "low" if reasoning_budget > 0 else "none" ), # type: ignore
39                    temperature=temperature,
40                    seed=seed
41                )
42
43            content = response.choices[0].message.content 
44
45            if content : 
46                return content
47            
48            n_tries += 1
49            base = min( 5.0, 0.5 * ( 2**n_tries ) )
50            delay = base + random.uniform( 0.0, 0.5 )
51
52            await asyncio.sleep( delay )
53
54        except Exception as e:
55            n_tries += 1
56            print( f"Request raised exception: {e}" )
57
58    raise Exception( f"Unable to get response after {max_request_attempts}" )
59
60
61async def test() :
62    ...
63
64if __name__ == "__main__":
65
66    import asyncio
67    asyncio.run( test() )
async def get_response( semaphore, client: openai.AsyncOpenAI, model: str, system_prompt: str, user_prompt: str, seed: int, temperature: float = 0.85, reasoning_budget: int = 0, max_request_attempts: int = 3) -> str:
 5async def get_response( 
 6    semaphore,
 7    client : AsyncOpenAI,
 8    model : str,
 9    system_prompt : str,  
10    user_prompt   : str,
11    seed          : int,
12    temperature   : float = 0.85,
13    reasoning_budget : int = 0,
14    max_request_attempts : int = 3 ) -> str :
15
16    n_tries = 0
17
18    while n_tries < max_request_attempts :
19        try:
20
21            async with semaphore :
22
23                response = await client.chat.completions.create(    
24                    model=model,
25                    messages=[
26                        {
27                            "role": "system",
28                            "content": system_prompt
29                        },
30                        {
31                            "role": "user",
32                            "content": user_prompt
33                        }
34                    ],
35                    extra_body={
36                        "reasoning_budget": reasoning_budget,
37                        "reasoning_budget_message": "\nThinking time is up. Provide the final answer now."
38                    },
39                    reasoning_effort=( "low" if reasoning_budget > 0 else "none" ), # type: ignore
40                    temperature=temperature,
41                    seed=seed
42                )
43
44            content = response.choices[0].message.content 
45
46            if content : 
47                return content
48            
49            n_tries += 1
50            base = min( 5.0, 0.5 * ( 2**n_tries ) )
51            delay = base + random.uniform( 0.0, 0.5 )
52
53            await asyncio.sleep( delay )
54
55        except Exception as e:
56            n_tries += 1
57            print( f"Request raised exception: {e}" )
58
59    raise Exception( f"Unable to get response after {max_request_attempts}" )
async def test():
62async def test() :
63    ...