ESC
Tutorials > AI API Calls
Go to the react-native-starter-backend/functions/src/index.ts file. Any function declaration with onRequest is a new AI endpoint. ✨ 🤖 We've built custom retry logic to handle AI API outages for you. 👊
Inside your newly created API function declaration, you can implement an example AI API call like below enjoying the perks of our retry logic doing its magic. ✨ We already have many pre-built AI endpoints; you can also edit them or start using them as is.
/react-native-starter-backend/functions/src/index.ts
1
2 try {
3 // Custom retry for outages in AI APIs
4 axiosRetry(axios, {
5 retryDelay: (retryCount) => {
6 return retryCount * 1;
7 },
8 retries: 15,
9 });
10
11 const res = await axios.post(url, body, options);
12
13 const answer = res.data.choices[0].message.content;
14 const usage = res?.data?.usage;
15
16 console.log(">>> " + answer);
17 console.log(
18 "TOKENS USED: " +
19 usage?.total_tokens +
20 " (prompt: " +
21 usage?.prompt_tokens +
22 " / response: " +
23 usage?.completion_tokens +
24 ")"
25 );
26 console.log("
27");
28
29 return answer;
30 } catch (e) {
31 console.error("GPT Error: " + e?.response?.status, e?.response?.data);
32 return null;
33 }
34