Sets the token used to fetch the next page of models in the AI session.
| Name | Type | Description |
|---|---|---|
nextModelsPageToken |
string |
The token representing the next page of model results. |
void– This method does not return a value.
- Updates the internal
_nextModelsPageTokenproperty with the provided token if it is a string. - If the value is not a string, it sets
_nextModelsPageTokentonull.
session._setNextModelsPageToken("eyJwYWdlIjoxfQ==");Note: This token is used automatically in
getModels()if no token is explicitly passed.
This method sets a custom function to fetch the list of models from an API. The function you provide will handle the process of fetching models and returning the necessary data.
| Name | Type | Description |
|---|---|---|
getModels |
Function |
The function that will fetch model data. It should return a promise with model information. |
// Example of how to use _setGetModels
aiSession._setGetModels(
(apiKey, pageSize, pageToken) => {
return new Promise((resolve, reject) => {
// Simulate an API request to fetch model data
fetch(`https://api.example.com/models?key=${apiKey}&pageSize=${pageSize}&pageToken=${pageToken}`)
.then(response => response.json())
.then(result => {
if (result.error) {
reject("Error fetching models");
} else {
// Simulate filtering and categorizing models
const models = result.models.map(model => ({
id: model.id,
name: model.name,
category: model.category,
version: model.version,
}));
resolve(models);
}
})
.catch(reject);
});
}
);-
getModelsfunction: This function you define will handle fetching model data from an external source (like an API). It should accept parameters such asapiKey,pageSize, andpageToken, and return a promise with the result. -
Promise: The
getModelsfunction uses a promise (new Promise()) to handle the asynchronous nature of an API call. It resolves with the model data or rejects with an error.
-
Sets the
getModelsfunction: This lets you define the custom logic for retrieving the list of available models. You can fetch this data from an API and format it as needed. -
Simulate fetching model data: The example shows how the
fetchfunction can be used to get model data from an API and process it into a simpler format. -
Returns model data: After fetching and processing the models, the function returns the formatted data, including details like
id,name,category, andversion.
| Type | Description |
|---|---|
void |
This method does not return a value directly. It sets the function for fetching models. |
Inserts a new model into the AI session's models list. If the model already exists, it will not be inserted again.
| Name | Type | Description |
|---|---|---|
model |
Object |
The model to insert. |
model.id |
string |
The unique identifier for the model. |
model.name |
string |
The name of the model (optional). |
model.displayName |
string |
The display name of the model (optional). |
model.version |
string |
The version of the model (optional). |
model.description |
string |
A description of the model (optional). |
model.inputTokenLimit |
number |
The input token limit for the model (optional). |
model.outputTokenLimit |
number |
The output token limit for the model (optional). |
model.temperature |
number |
The temperature setting for the model (optional). |
model.maxTemperature |
number |
The maximum temperature setting for the model (optional). |
model.topP |
number |
The top P setting for the model (optional). |
model.topK |
number |
The top K setting for the model (optional). |
model.supportedGenerationMethods |
Array<string> |
The generation methods supported by the model (optional). |
model.category |
Object |
The category of the model (optional). |
model.category.id |
string |
The unique identifier for the category. |
model.category.displayName |
string |
The display name of the category. |
model.category.index |
number |
The index of the category. |
| Type | Description |
|---|---|
Object |
The inserted model data if the model was successfully added, or null if the model already exists. |
const newModel = {
id: 'modelId123',
name: 'AI Model 1',
displayName: 'AI Model One',
version: 'v1.0',
description: 'An AI model designed for specific tasks.',
inputTokenLimit: 1000,
outputTokenLimit: 500,
temperature: 0.7,
maxTemperature: 1.0,
topP: 0.9,
topK: 50,
supportedGenerationMethods: ['text', 'image'],
category: {
id: 'categoryId1',
displayName: 'Category One',
index: 0,
},
};
const insertedModel = session._insertNewModel(newModel);
console.log(insertedModel);Sets a function to handle the count of tokens in the AI session. If a valid function is provided, it will be used to count tokens.
| Name | Type | Description |
|---|---|---|
countTokens |
Function |
The function that will handle the token count. This function will be called to count tokens. |
| Error Type | Description |
|---|---|
Error |
Throws an error if the provided value is not a function. |
| Type | Description |
|---|---|
void |
This function does not return a value. |
// Example of a token counting function
const countTokens = (text) => {
return text.split(' ').length; // Simple token count based on spaces
};
// Set the function to count tokens
session._setCountTokens(countTokens);
// Now you can use the function to count tokens
const tokens = session.#_countTokens("This is a sample sentence.");
console.log(tokens); // Output: 5This method sets a custom function to handle token counting for an AI session. The function you provide will be responsible for counting tokens, and it must return a promise that resolves with the token count data.
| Name | Type | Description |
|---|---|---|
countTokens |
Function |
The function that will count the tokens. It should return a promise resolving with token count information. |
// Example of how to use _setCountTokens
aiSession._setCountTokens(
(apiKey, model, requestData) => {
return new Promise((resolve, reject) => {
// Simulate an API request to count tokens
const tokenData = { totalTokens: 1000, promptTokens: 500 }; // Example response data
// If the API call is successful, resolve the data
resolve(tokenData);
// If something goes wrong, reject the promise with an error
// reject("Error counting tokens");
});
}
);-
countTokensfunction: This is a function that you define. It will count the tokens used in an AI session. It should take necessary parameters likeapiKey,model, andrequestData(or any other data needed) and return a promise. The promise should resolve with an object containing token count information. -
Promise: The function you provide will use a promise (
new Promise()) to simulate asynchronous behavior. This allows the AI session to wait for the token count result before continuing.
- Sets the
countTokensfunction: It allows you to define how the token count is performed for the AI session. - Asynchronous behavior: The
countTokensfunction can perform tasks such as making API calls to get token data, and it needs to return a promise that resolves with the result. - Token Count Data: The returned data can include things like the total number of tokens, prompt tokens, and any other token-related information.
| Type | Description |
|---|---|
void |
This method does not return any value directly. It just sets the function for token counting. |
This method allows you to define custom error codes for a session. Error codes help in identifying the reasons behind specific failures or issues during the session. You can set a list of predefined error codes with descriptions, so the program knows how to handle or display them when needed.
| Name | Type | Description |
|---|---|---|
errors |
Object |
An object containing key-value pairs where keys are error codes and values are error descriptions. |
// Set custom error codes for the session
tinyAi._setErrorCodes({
FINISH_REASON_UNSPECIFIED: { text: 'Default value. This value is unused.' },
STOP: { text: 'Natural stop point of the model or provided stop sequence.', hide: true },
MAX_TOKENS: { text: 'The maximum number of tokens as specified in the request was reached.' },
SAFETY: { text: 'The response candidate content was flagged for safety reasons.' },
RECITATION: { text: 'The response candidate content was flagged for recitation reasons.' },
LANGUAGE: { text: 'The response candidate content was flagged for using an unsupported language.' },
OTHER: { text: 'Unknown reason.' },
BLOCKLIST: { text: 'Token generation stopped because the content contains forbidden terms.' },
PROHIBITED_CONTENT: { text: 'Token generation stopped for potentially containing prohibited content.' },
SPII: { text: 'Token generation stopped because the content potentially contains Sensitive Personally Identifiable Information (SPII).' },
MALFORMED_FUNCTION_CALL: { text: 'The function call generated by the model is invalid.' },
IMAGE_SAFETY: { text: 'Token generation stopped because generated images contain safety violations.' },
});-
Error Codes Object: This object contains multiple error codes (e.g.,
STOP,MAX_TOKENS, etc.) and their associated descriptions. These descriptions help explain the reason for the error. -
Setting Error Codes: When you call
_setErrorCodes, you're telling the system how to handle specific errors by defining them in a structured way. -
Error Details: Each error code has a
textfield, which explains the reason for the error. Some error codes might also include other attributes, likehide, which can control whether the error is shown or not.
-
If the model hits the maximum token limit, the error code
MAX_TOKENSwill be set, and the corresponding messageThe maximum number of tokens as specified in the request was reached.will be used to explain the issue. -
The
STOPerror might be triggered when the model naturally reaches the end of its generation, or a stop sequence is provided.
| Type | Description |
|---|---|
void |
This method does not return anything. It simply sets the error codes for use in the session. |
-
Clear Error Management: This method ensures that all possible errors have clear definitions, so the program knows how to respond or display appropriate error messages.
-
Easier Debugging: By defining error codes with descriptions, developers can easily understand why an error occurred and how to handle it.
This method allows you to define a custom callback function that will handle content generation. It's used to set up how the AI should generate or stream content when requested.
| Name | Type | Description |
|---|---|---|
callback |
Function |
A function that will handle the content generation process. It will be passed several parameters and should return a Promise. |
- This method accepts a function (callback) that takes care of generating content for the AI.
- The function will be called during the AI session whenever content needs to be generated, and it should handle the logic for making a request to the API, processing the response, and sending the result back.
Here's a simplified example that shows how you can use _setGenContent:
tinyAi._setGenContent(
(apiKey, isStream, data, model, streamingCallback, controller) =>
new Promise((resolve, reject) => {
// Build the request body using the provided data
const requestBody = requestBuilder(data);
// Handle the response from the API
const handleResponse = (result) => {
const finalData = { _response: result };
if (!result.error) {
finalData.contents = [];
finalData.modelVersion = result.modelVersion || null;
// Token Usage metadata
const [tokenUsage] = buildUsageMetadata(result);
finalData.tokenUsage = tokenUsage;
// Process the content
if (result.candidates) {
for (const item of result.candidates) {
if (item.content) {
tinyAi.buildContents(finalData.contents, item.content, item.content.role);
}
}
}
}
return finalData;
};
// Handle streaming responses
const streamingResponse = async (stream) => {
try {
const reader = stream.getReader();
const decoder = new TextDecoder('utf-8');
let done = false;
while (!done) {
const readerData = await reader.read();
const { value, done: streamDone } = readerData;
done = streamDone;
if (value) {
const chunk = decoder.decode(value, { stream: true });
const cleanedJson = jsonrepair(chunk.trim());
const jsonChunk = JSON.parse(cleanedJson);
for (const result of jsonChunk) {
const tinyData = { contents: [] };
buildContent(result, tinyData);
streamingCallback({ contents: tinyData.contents, done: false });
}
}
}
// Complete the streaming
streamingCallback({ done: true });
resolve(handleResponse(streamResult));
} catch (err) {
reject(err);
}
};
// Make the API request
fetch(
`${apiUrl}/models/${model}:${!isStream ? 'generateContent' : 'streamGenerateContent'}?key=${encodeURIComponent(apiKey)}`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(requestBody),
signal: controller ? controller.signal : undefined,
}
)
.then((res) => {
if (!isStream) {
res.json().then((result) => resolve(handleResponse(result))).catch(reject);
} else {
if (!res.body) reject(new Error('No AI streaming value found.'));
else streamingResponse(res.body);
}
})
.catch(reject);
})
);-
Request Body:
- The function begins by creating the request body using the provided
datawith a helper function (requestBuilder).
- The function begins by creating the request body using the provided
-
Handling API Response:
- The
handleResponsefunction processes the result from the API, building content and collecting metadata.
- The
-
Streaming:
- If the content is being streamed (e.g., large responses), the function handles the streaming process and sends data chunks to
streamingCallbackas they arrive.
- If the content is being streamed (e.g., large responses), the function handles the streaming process and sends data chunks to
-
Error Handling:
- If something goes wrong (like a network error), it gets caught and handled in the
.catch()block.
- If something goes wrong (like a network error), it gets caught and handled in the
- The AI system will use the custom callback you provided whenever it needs to generate or stream content.
- Your callback is in charge of making the request to the AI API, processing the response, and passing the result back.
-
What It Does: This method lets you define how the AI will generate or stream content. You set up a function (
callback) to handle that process, from requesting data to processing the result. -
When to Use: Use this method when you need to define the content generation behavior for your AI system, especially if you're dealing with custom API calls or need to handle streaming data.