Thank you, Marco san, for your informative posts. I am using the Azure OPENAI API, so I am using my own function for direct API Calls that refer to Ben Kickert san and Jofre Espigule-Pons san's presentations at this year' s Wolfram Virtual Technology Conference.
Occasionally I get “Rate limit reached for requests” in error code:429, so I have implemented a retry. For your reference.
Out[]=
errorcode429,messageRequests to the ChatCompletions_Create Operation under Azure OpenAI API version 2023-08-01-preview have exceeded call rate limit of your current OpenAI S0 pricing tier. Please retry after 2 seconds. Please go here: https://aka.ms/oai/quotaincrease if you would like to further increase the default rate limit.
Clear[AOAIgpt];​​Options[AOAIgpt]={"Model""gpt-4-32k","SystemMessage"->"","Temperature"0.1,"Metadata"->False};​​​​AOAIgpt[prompt_String,OptionsPattern[]]:=Block[{url,systemmessage,messages,result,content},​​url="https://YOUR_RESOURCE_NAME.openai.azure.com/openai/deployments/YOUR_DEPLOYMENT_NAME/chat/completions?api-version=2023-08-01-preview";​​systemmessage=<|"role"->"system","content"->OptionValue["SystemMessage"]|>;​​messages=If[OptionValue["SystemMessage"]==="",{<|"role"->"user","content"->prompt|>},{systemmessage,<|"role"->"user","content"->prompt|>}];​​​​result=Catch[Do[Block[{res,errmsg,waittime},​​res=URLExecute[​​HTTPRequest[url,​​<|​​Method"POST",​​"Headers"->{"api-key"->SystemCredential["Azure_OPENAI_API_KEY"]},​​"Body"->ExportString[<|"messages"messages|>,"JSON"],​​"ContentType""application/json"​​|>],"RAWJSON"];​​If[MissingQ[res[["choices"]]],​​(*error*)​​errmsg=res[["error","message"]];​​If[StringContainsQ[errmsg,"Please retry after"],​​(*Ratelimitreachedforrequests*)​​waittime=ToExpression[StringCases[errmsg,RegularExpression["Please retry after (\\d+) second"]->"$1"][[1]]];​​Pause[waittime],​​(*elseerror*)​​Throw@res],​​(*normal*)​​Throw@res​​]],2];$Failed];​​​​If[OptionValue["Metadata"],​​result,​​content=If[MissingQ[result[["choices"]]],​​"Error: "<>result[["error","message"]],​​result[["choices",1,"message","content"]]​​]​​]​​];