Call AI models
Send AI requests with the user's access token. rNet forwards them to the model provider and charges the user's credits.
Make a request
Create a ModelClient with a model ID, then call chat with a request body and the access token.
javascript
const response = await ai.chat({
contents: [{ role: 'user', parts: [{ text: 'Write a haiku about credits.' }] }],
}, accessToken);
console.log(response);Request bodies
rNet passes your body to the provider unchanged, so send the provider's own request format and you get the provider's response back.
| Models | Body format |
|---|---|
| Gemini and Gemma (Google) | Gemini generateContent body with contents |
gemini-embedding-001 | Gemini embedContent body with content |
text-embedding-3-small | OpenAI embeddings body with model and input |
| Llama, Mixtral, Gemma 2 (Groq) | OpenAI-style chat body with model and messages |
javascript
// Groq model: OpenAI-style chat completions body
const groq = new ModelClient('llama-3.1-8b-instant');
const reply = await groq.chat({
model: 'llama-3.1-8b-instant',
messages: [{ role: 'user', content: 'Hello!' }],
}, accessToken);
// OpenAI embeddings
const embed = new ModelClient('text-embedding-3-small');
const vectors = await embed.chat({
model: 'text-embedding-3-small',
input: 'rNet credits work in any app',
}, accessToken);Stream responses
Use chatStream (chat_stream in Python) to receive the response as it is generated. For Groq models, also set "stream": true in the body.
javascript
const stream = await ai.chatStream(payload, accessToken); // ReadableStream
const reader = stream.getReader();
const decoder = new TextDecoder();
for (;;) {
const { value, done } = await reader.read();
if (done) break;
res.write(decoder.decode(value)); // forward server-sent events to your client
}
res.end();Handle errors
The SDKs throw when rNet returns an error. The message contains the HTTP status, for example Request failed: 402 - Insufficient funds.
javascript
try {
const response = await ai.chat(payload, accessToken);
} catch (err) {
// SDK errors look like: "Request failed: 402 - Insufficient funds"
const status = Number(/Request failed: (\d{3})/.exec(err.message)?.[1]);
if (status === 401) {
// Access token expired: refresh it and retry once
} else if (status === 402) {
// The user is out of credits: ask them to top up at rnetai.org/user-home
} else if (status === 429) {
// Too many requests: wait and retry
} else {
throw err;
}
}| Status | What to do |
|---|---|
| 401 | Access token expired or invalid: refresh it and retry once. |
| 402 | User is out of credits: ask them to top up in their Credit Wallet. |
| 422 | Unknown model ID or a model this app can’t use: check the model list. |
| 429 | Too many requests: wait and retry. |
See Errors for every status code.
Protect your users' data
Run prompts through Context Eraser to remove emails, keys and other private data before they reach a model.