### N-shot Prompting with LanguageModel.create Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Demonstrates how to create a language model session with initial prompts that include user-assistant interaction examples for n-shot learning. The `initialPrompts` array can be populated with system, user, and assistant roles to guide the model's responses. Sessions can be cloned for efficiency. ```javascript const session = await LanguageModel.create({ initialPrompts: [ { role: "system", content: "Predict up to 5 emojis as a response to a comment. Output emojis, comma-separated." }, { role: "user", content: "This is amazing!" }, { role: "assistant", content: "❤️, ➕" }, { role: "user", content: "LGTM" }, { role: "assistant", content: "👍, 🚢" } ] }); // Clone an existing session for efficiency, instead of recreating one each time. async function predictEmoji(comment) { const freshSession = await session.clone(); return await freshSession.prompt(comment); } const result1 = await predictEmoji("Back to the drawing board"); const result2 = await predictEmoji("This code is so good you should get promoted"); ``` -------------------------------- ### N-shot Prompting Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Demonstrates how to provide example user/assistant interactions to guide the language model's responses. This is useful for setting a specific tone or task for the model. ```APIDOC ## N-shot Prompting ### Description Provides example user/assistant interactions to prime the language model for specific response styles or tasks. ### Method `LanguageModel.create()` with `initialPrompts` array. ### Endpoint N/A (Client-side API) ### Parameters #### Request Body - **initialPrompts** (array) - Required - An array of prompt objects, each with `role` (e.g., 'system', 'user', 'assistant') and `content` (string). - **role** (string) - Required - The role of the message sender ('system', 'user', or 'assistant'). - **content** (string) - Required - The text of the message. ### Request Example ```javascript const session = await LanguageModel.create({ initialPrompts: [ { role: "system", content: "Predict up to 5 emojis as a response to a comment. Output emojis, comma-separated." }, { role: "user", content: "This is amazing!" }, { role: "assistant", content: "❤️, ➕" }, { role: "user", content: "LGTM" }, { role: "assistant", content: "👍, 🚢" } ] }); async function predictEmoji(comment) { const freshSession = await session.clone(); return await freshSession.prompt(comment); } ``` ### Response #### Success Response (200) - **result** (string) - The model's generated response. #### Response Example ``` 👍 ``` ### Error Handling - `TypeError`: If the `{ role: "system" }` prompt is not at the 0th position in `initialPrompts`. - `QuotaExceededError`: If the combined token length of `initialPrompts` exceeds the context window limits. ``` -------------------------------- ### Prompting with Image and Audio Inputs (JavaScript) Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Shows how to construct a prompt for a multimodal session. It includes text, image, and audio content within a single user message. This example fetches an image as a blob and uses a canvas element for another image, and captures audio input. ```javascript const session = await LanguageModel.create({ expectedInputs: [ { type: "audio" }, { type: "image" } ] }); const referenceImage = await (await fetch("/reference-image.jpeg")).blob(); const userDrawnImage = document.querySelector("canvas"); const response1 = await session.prompt([ { role: "user", content: [ { type: "text", value: "Give a helpful artistic critique of how well the second image matches the first:" }, { type: "image", value: referenceImage }, { type: "image", value: userDrawnImage } ] } ]); console.log(response1); const audioBlob = await captureMicrophoneInput({ seconds: 10 }); const response2 = await session.prompt([ { role: "user", content: [ { type: "text", value: "My response to your critique:" }, { type: "audio", value: audioBlob } ] } ]); ``` -------------------------------- ### System Prompts with JavaScript Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Illustrates how to configure a language model with a system prompt for context using the `initialPrompts` option. The system prompt is formatted using the chat completions API format `{ role, content }`. This example shows setting a system prompt and then prompting the model. ```javascript const session = await LanguageModel.create({ initialPrompts: [{ role: "system", content: "Pretend to be an eloquent hamster." }] }); console.log(await session.prompt("What is your favorite food?")); ``` -------------------------------- ### Configure Multimodal Expected Inputs (JavaScript) Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Shows how to configure expected input types and their corresponding languages for a LanguageModel session. This example includes text, audio, and image inputs with specific language requirements, demonstrating flexibility in handling different modalities. ```javascript const session = await LanguageModel.create({ expectedInputs: [ // Be sure to download any material necessary for English and Japanese text // prompts, or fail-fast if the model cannot support that. { type: "text", languages: ["en", "ja"] }, // `languages` omitted: audio input processing will be best-effort based on // the base model's capability. { type: "audio" }, // Be sure to download any material necessary for OCRing French text in // images, or fail-fast if the model cannot support that. { type: "image", languages: ["fr"] } ] }); ``` -------------------------------- ### Clone Language Model Session for Parallel Continuations Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This example demonstrates how to clone an existing language model session to create multiple independent conversation threads from the same starting point. The clone operation can be managed with an AbortSignal. ```javascript const session = await LanguageModel.create({ initialPrompts: [{ role: "system", content: "You are a friendly, helpful assistant specialized in clothing choices." }] }); const session2 = await session.clone(); const controller = new AbortController(); const session2 = await session.clone({ signal: controller.signal }); ``` -------------------------------- ### Append Messages Without Prompting (JavaScript) Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This example shows how to use the `append()` method to add messages to the session ahead of time, without immediately prompting the model for a response. This is useful for pre-processing inputs, especially multimodal ones, allowing the model to get a head start. ```javascript const session = await LanguageModel.create({ initialPrompts: [{ role: "system", content: "You are a skilled analyst who correlates patterns across multiple images." }], expectedInputs: [{ type: "image" }] }); fileUpload.onchange = async (e) => { await session.append([ { role: "user", content: [ { type: "text", value: `Here's one image. Notes: ${fileNotesInput.value}` }, { type: "image", value: fileUpload.files[0] } ] } ]); }; analyzeButton.onclick = async (e) => { analysisResult.textContent = await session.prompt(userQuestionInput.value); }; ``` -------------------------------- ### Constraining Responses with Prefixes Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Demonstrates how to use the `prefix: true` option on an assistant message to guide the language model's response format. ```APIDOC ## POST /api/prompt ### Description This endpoint allows for advanced prompting, including prefilling a portion of an assistant's response to guide the model's output format. ### Method POST ### Endpoint /api/prompt ### Parameters #### Request Body - **messages** (array) - Required - An array of message objects, where each object has a `role` and `content`. For prefixing, an assistant message can have `prefix: true`. ### Request Example ```json { "messages": [ { "role": "user", "content": "Create a TOML character sheet for a gnome barbarian" }, { "role": "assistant", "content": "```toml\n", "prefix": true } ] } ``` ### Response #### Success Response (200) - **completion** (string) - The generated response from the language model. #### Response Example ```json { "completion": "[[MyGnomeBarbarian]]\nname = \"Grizelda\"\nrace = \"Gnome\"\nclass = \"Barbarian\"\nattributes = { strength = 18, dexterity = 14, constitution = 16, intelligence = 10, wisdom = 12, charisma = 8 }\nskills = [ \"Athletics\", \"Intimidation\", \"Survival\" ]\nbackground = \"Outlander\"\n" } ``` ### Errors - **SyntaxError** (DOMException) - Thrown if `prefix` is used on any message other than a final `"assistant"`-role message. ``` -------------------------------- ### Constrain Prompt Response with RegExp Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This example shows how to use a regular expression to validate the format of the prompt's response. Here, it's used to ensure a generated email address adheres to a standard email format. The returned value is guaranteed to match the provided RegExp. ```javascript const emailRegExp = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; const emailAddress = await session.prompt( `Create a fictional email address for ${characterName}.`, { responseConstraint: emailRegExp } ); console.log(emailAddress); ``` -------------------------------- ### Constrain Assistant Response with Prefix (JavaScript) Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This snippet demonstrates how to prefill a portion of the assistant's response to guide the language model towards a specific format or content. It uses the `prefix: true` option on an assistant-role message. ```javascript const followup = await session.prompt([ { role: "user", content: "I'm nervous about my presentation tomorrow" }, { role: "assistant" content: "Presentations are tough!" } ]); // `followup` might be something like "Here are some tips for staying calm.", or // "I remember my first presentation, I was nervous too!" or... const characterSheet = await session.prompt([ { role: "user", content: "Create a TOML character sheet for a gnome barbarian" }, { role: "assistant", content: "```toml\n", prefix: true } ]); ``` -------------------------------- ### Appending Messages Without Prompting Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Explains the `append()` method for adding messages to a session ahead of time, allowing the model to start processing while awaiting the final prompt. ```APIDOC ## POST /api/session/{sessionId}/append ### Description Appends messages to an existing session without immediately prompting the language model for a response. This is useful for pre-processing content. ### Method POST ### Endpoint /api/session/{sessionId}/append ### Parameters #### Path Parameters - **sessionId** (string) - Required - The unique identifier for the session. #### Request Body - **messages** (array) - Required - An array of message objects to append to the session. ### Request Example ```javascript // Assuming 'session' is an existing session object await session.append([ { "role": "user", "content": [ { "type": "text", "value": "Here's one image. Notes: " }, { "type": "image", "value": fileUpload.files[0] } ] } ]); ``` ### Response #### Success Response (200) - **status** (string) - Indicates that the messages have been successfully appended and processed. #### Response Example ```json { "status": "appended" } ``` ### Errors - **Rejection** - The promise returned by `append()` will reject if the prompt cannot be appended (e.g., too large, invalid modalities, or session overflow). - **QuotaOverflow Event** - If appending causes session overflow, the `"quotaoverflow"` event will be fired. ``` -------------------------------- ### Create Session with Multimodal Inputs (JavaScript) Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Demonstrates creating a LanguageModel session that expects both audio and image inputs. This ensures necessary downloads are handled during session creation and fails if the model doesn't support these modalities. It utilizes the `expectedInputs` option. ```javascript const session = await LanguageModel.create({ expectedInputs: [ { type: "audio" }, { type: "image" } ] }); ``` -------------------------------- ### Create Session with Expected Output Language (JavaScript) Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Demonstrates creating a LanguageModel session with a specific expected output language. This configuration helps in setting up the session by downloading necessary materials for the target output language and failing early if the language is not supported. ```javascript const session = await LanguageModel.create({ initialPrompts: [{ role: "system", content: `You are a helpful, harmless French chatbot.` }], expectedInputs: [ { type: "text", languages: ["en" /* for the system prompt */, "fr"] } ], expectedOutputs: [ { type: "text", languages: ["fr"] } ] }); ``` -------------------------------- ### Maintain Session State with Initial Prompts and Interactions Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This code illustrates how to initialize a language model session with system prompts and conduct a series of interactions. It shows how to send a prompt and receive a response, and then continue the conversation with a follow-up prompt, maintaining the session's context. ```javascript const session = await LanguageModel.create({ initialPrompts: [{ role: "system", content: "You are a friendly, helpful assistant specialized in clothing choices." }] }); const result = await session.prompt(` What should I wear today? It's sunny and I'm unsure between a t-shirt and a polo. `); console.log(result); const result2 = await session.prompt(` That sounds great, but oh no, it's actually going to rain! New advice?? `); ``` -------------------------------- ### Monitor Download Progress for Language Model Creation (JavaScript) Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This code illustrates how to monitor the download progress when creating a language model session that requires downloads. It uses a `monitor` callback within `LanguageModel.create()` to listen for 'downloadprogress' events, allowing developers to display a progress bar to the user. The snippet also notes error handling for download failures and the bundling of multiple downloads into single progress events. ```javascript const session = await LanguageModel.create({ monitor(m) { m.addEventListener("downloadprogress", e => { console.log(`Downloaded ${e.loaded * 100}%`); }); } }); ``` -------------------------------- ### Create LanguageModel Session with Expected Languages (JavaScript) Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Demonstrates creating a LanguageModel session, specifying expected input and output text languages. This allows the implementation to download necessary supporting materials and reject the promise if unsupported languages are encountered. ```javascript const session = await LanguageModel.create({ initialPrompts: [{ role: "system", content: ` You are a foreign-language tutor for Japanese. The user is Korean. If necessary, either you or the user might "break character" and ask for or give clarification in Korean. But by default, prefer speaking in Japanese, and return to the Japanese conversation once any sidebars are concluded. ` }], expectedInputs: [{ type: "text", languages: ["en" /* for the system prompt */, "ja", "ko"] }], expectedOutputs: [{ type: "text", languages: ["ja", "ko"] }], }); ``` -------------------------------- ### Check LanguageModel Availability (JavaScript) Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Illustrates how to check the availability of a specific `expectedInputs` configuration before initiating session creation using the `LanguageModel.availability()` method. It returns a status indicating whether the configuration is unavailable, downloadable, downloading, or available. ```javascript const availability = await LanguageModel.availability({ expectedInputs: [ { type: "text", languages: ["en", "ja"] }, { type: "audio", languages: ["en", "ja"] } ] }); // `availability` will be one of "unavailable", "downloadable", "downloading", or "available". ``` -------------------------------- ### Check Language Model Availability and Create Session (JavaScript) Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This snippet demonstrates how to use `LanguageModel.availability()` to check if a language model can be created with specific options before calling `LanguageModel.create()`. It handles different availability states like 'unavailable', 'downloadable', and 'available', providing a better user experience by informing users about potential downloads. ```javascript const options = { expectedInputs: [ { type: "text", languages: ["en", "es"] }, { type: "audio", languages: ["en", "es"] } ], temperature: 2 }; const availability = await LanguageModel.availability(options); if (availability !== "unavailable") { if (availability !== "available") { console.log("Sit tight, we need to do some downloading..."); } const session = await LanguageModel.create(options); // ... Use session ... } else { // Either the API overall, or the expected languages and temperature setting, is not available. console.error("No language model for us :("); } ``` -------------------------------- ### Configure Session Parameters with Temperature and Top-K Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This snippet demonstrates how to create a language model session with custom temperature and top-K values. It also shows how to retrieve default and maximum parameter values using the `params()` API and conditionally set parameters based on task requirements. Note that sampling hyperparameters are not universal among all models. ```javascript const customSession = await LanguageModel.create({ temperature: 0.8, topK: 10 }); const params = await LanguageModel.params(); const conditionalSession = await LanguageModel.create({ temperature: isCreativeTask ? params.defaultTemperature * 1.1 : params.defaultTemperature * 0.8, topK: isGeneratingIdeas ? params.maxTopK : params.defaultTopK }); ``` -------------------------------- ### Configuration of Per-Session Parameters Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This section details how to configure per-session parameters such as temperature and top-K, and how to retrieve their default and maximum values. ```APIDOC ## Configuration of Per-Session Parameters ### Description Configure per-session parameters like `temperature` and `topK`. The `params()` API provides default and maximum values for these parameters. ### Method `LanguageModel.create()` for creating a session with custom parameters. `LanguageModel.params()` for retrieving parameter information. ### Endpoint N/A (Client-side API) ### Parameters #### Request Body for `LanguageModel.create()` - **temperature** (number) - Optional - The temperature for sampling. Clamped to `maxTemperature`. `+Infinity` is allowed. - **topK** (number) - Optional - The top-K sampling parameter. Clamped to `maxTopK`. Fractional values are rounded down. #### Response from `LanguageModel.params()` - **defaultTemperature** (number) - The default temperature value. - **maxTemperature** (number) - The maximum temperature value. - **defaultTopK** (number) - The default top-K value. - **maxTopK** (number) - The maximum top-K value. ### Request Example for `LanguageModel.create()` ```javascript const customSession = await LanguageModel.create({ temperature: 0.8, topK: 10 }); ``` ### Request Example for `LanguageModel.params()` ```javascript const params = await LanguageModel.params(); ``` ### Response Example for `LanguageModel.params()` ```json { "defaultTemperature": 0.7, "maxTemperature": 1.0, "defaultTopK": 50, "maxTopK": 100 } ``` ### Error Handling - Passing values below 0 for `temperature` rejects with `RangeError`. - Passing values below 1 for `topK` rejects with `RangeError`. - `params()` fulfills with `null` if the language model is unavailable. ``` -------------------------------- ### Zero-shot Prompting with JavaScript Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Demonstrates how to perform zero-shot prompting using the Prompt API. It shows both waiting for the full result and streaming the response chunks. Assumes the existence of a `LanguageModel` class and its `create` and `prompt` methods. ```javascript const session = await LanguageModel.create(); // Prompt the model and wait for the whole result to come back. const result = await session.prompt("Write me a poem."); console.log(result); // Prompt the model and stream the result: const stream = session.promptStreaming("Write me an extra-long poem."); for await (const chunk of stream) { console.log(chunk); } ``` -------------------------------- ### Tool Use with Prompt API Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Illustrates how to enable tool use within the Prompt API by defining tools with names, descriptions, input schemas, and asynchronous execute functions. The language model can then invoke these tools based on user prompts. Results from tool executions are fed back to the model. ```javascript const session = await LanguageModel.create({ initialPrompts: [ { role: "system", content: `You are a helpful assistant. You can use tools to help the user.` } ], tools: [ { name: "getWeather", description: "Get the weather in a location.", inputSchema: { type: "object", properties: { location: { type: "string", description: "The city to check for the weather condition.", }, }, required: ["location"], }, async execute({ location }) { const res = await fetch("https://weatherapi.example/?location=" + location); // Returns the result as a JSON string. return JSON.stringify(await res.json()); }, } ] }); const result = await session.prompt("What is the weather in Seattle?"); ``` -------------------------------- ### Session Persistence and Cloning Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This section explains how to create and manage persistent language model sessions, including how to clone sessions for multiple continuations. ```APIDOC ## Session Persistence and Cloning ### Description Manage persistent language model sessions. Create a session to maintain a series of interactions and clone it to explore multiple conversational paths independently. ### Method `LanguageModel.create()` for initializing a session. `session.prompt()` for sending a prompt and receiving a response. `session.clone()` for creating a copy of the current session state. ### Endpoint N/A (Client-side API) ### Parameters #### Request Body for `LanguageModel.create()` - **initialPrompts** (Array) - Optional - An array of initial prompt objects, each with `role` and `content`. - **signal** (AbortSignal) - Optional - An `AbortSignal` to cancel the session creation. #### Request Body for `session.clone()` - **signal** (AbortSignal) - Optional - An `AbortSignal` to cancel the cloning operation. ### Request Example for `LanguageModel.create()` ```javascript const session = await LanguageModel.create({ initialPrompts: [ { role: "system", content: "You are a helpful assistant." } ] }); ``` ### Request Example for `session.prompt()` ```javascript const result = await session.prompt("What should I wear today?"); console.log(result); const result2 = await session.prompt("It's raining now, new advice?"); ``` ### Request Example for `session.clone()` ```javascript const session = await LanguageModel.create(...); const session2 = await session.clone(); // Aborting a clone operation const controller = new AbortController(); const session3 = await session.clone({ signal: controller.signal }); ``` ### Response Example for `session.prompt()` ```json { "response": "You should wear a t-shirt." } ``` ``` -------------------------------- ### Multi-Message Prompting with Images (JavaScript) Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Illustrates how to structure a prompt using multiple messages, where subsequent messages contain only image content. This approach differs from a single message containing multiple content types and affects how the model interprets the sequence of inputs. ```javascript const response = await session.prompt([ { role: "user", content: "Your compromise just made the discussion more heated. The two departments drew up posters to illustrate their strategies' advantages:" }, { role: "user", content: [{ type: "image", value: brochureFromTheMarketingDepartment }] }, { role: "user", content: [{ type: "image", value: brochureFromTheFinanceDepartment }] } ]); ``` -------------------------------- ### Abort Token Measurement with AbortSignal (JavaScript) Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Demonstrates how to abort the token measurement process for a prompt using an AbortSignal. This is useful for cancelling long-running operations. ```javascript session.measureInputUsage(promptString, { signal }) ``` -------------------------------- ### Customizing Roles in Prompt API Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Shows how to use the Prompt API with an array of message objects, allowing for multiple user or assistant messages before an assistant's response. This is useful for complex conversations or scenarios where message roles need to be explicitly defined. System prompts have specific placement rules. ```javascript const multiUserSession = await LanguageModel.create({ initialPrompts: [{ role: "system", content: "You are a mediator in a discussion between two departments." }] }); const result = await multiUserSession.prompt([ { role: "user", content: "Marketing: We need more budget for advertising campaigns." }, { role: "user", content: "Finance: We need to cut costs and advertising is on the list." }, { role: "assistant", content: "Let's explore a compromise that satisfies both departments." } ]); // `result` will contain a compromise proposal from the assistant. ``` -------------------------------- ### Abort Prompt with AbortSignal (JavaScript) Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Demonstrates how to abort a specific prompt call using an AbortSignal. This is useful for user-interactive cancellation. The prompt function accepts an options object where the signal can be passed. ```javascript const controller = new AbortController(); stopButton.onclick = () => controller.abort(); const result = await session.prompt("Write me a poem", { signal: controller.signal }); ``` -------------------------------- ### Concurrent Tool Use Handling in Prompt API Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Explains that the Prompt API can invoke tools multiple times, potentially concurrently, in response to a single prompt. The model waits for all tool call results, similar to `Promise.all()`, before composing its final response. It can also call multiple different tools if deemed relevant. ```javascript const result = await session.prompt("Which of these locations currently has the highest temperature? Seattle, Tokyo, Berlin"); ``` -------------------------------- ### Check Token Usage with Session Properties (JavaScript) Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Displays the current input token usage and the total available input quota for a language model session. This helps developers monitor progress toward the context window limit. ```javascript console.log(`${session.inputUsage} tokens used, out of ${session.inputQuota} tokens available.`); ``` -------------------------------- ### Tool Use API Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Integrates external tools into the language model's capabilities, allowing it to perform actions like fetching data or executing code. ```APIDOC ## Tool Use API ### Description Allows the language model to invoke external tools defined within the `tools` option. The model can call these tools based on the prompt, and the results are fed back to the model. ### Method `LanguageModel.create()` with `tools` option. ### Endpoint N/A (Client-side API) ### Parameters #### Request Body - **tools** (array) - Optional - An array of tool definitions. - **name** (string) - Required - The name of the tool. - **description** (string) - Required - A description of what the tool does. - **inputSchema** (object) - Required - Defines the structure and types of the input arguments for the tool. - **type** (string) - 'object' - Specifies the schema type. - **properties** (object) - Defines the properties (arguments) the tool accepts. - **propertyName** (object) - Describes an argument. - **type** (string) - The data type of the argument (e.g., 'string', 'number'). - **description** (string) - Explanation of the argument. - **required** (array) - List of argument names that are mandatory. - **execute** (function) - Required - An async function that implements the tool's logic. It receives arguments based on `inputSchema` and should return a JSON string. ### Request Example ```javascript const session = await LanguageModel.create({ initialPrompts: [ { role: "system", content: "You are a helpful assistant. You can use tools to help the user." } ], tools: [ { name: "getWeather", description: "Get the weather in a location.", inputSchema: { type: "object", properties: { location: { type: "string", description: "The city to check for the weather condition.", }, }, required: ["location"], }, async execute({ location }) { const res = await fetch("https://weatherapi.example/?location=" + location); return JSON.stringify(await res.json()); }, } ] }); const result = await session.prompt("What is the weather in Seattle?"); ``` ### Response #### Success Response (200) - **result** (string) - The model's final response, potentially incorporating tool results. #### Response Example ```json { "weather": { "location": "Seattle", "temperature": "15°C", "condition": "Cloudy" } } ``` ### Concurrent Tool Use - The model may invoke tools multiple times concurrently (e.g., for multiple locations or different tools). - The system internally uses `Promise.all()` to wait for all tool call results before composing the final response. ``` -------------------------------- ### Customizing Role per Prompt Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Allows for sending multiple messages with specific roles (user, assistant) within a single `prompt()` call, useful for simulating complex dialogues. ```APIDOC ## Customizing Role per Prompt ### Description Enables sending an array of message objects with specified roles to the `prompt()` method, facilitating multi-turn conversations or complex instructions. ### Method `session.prompt()` with an array of `{ role, content }` objects. ### Endpoint N/A (Client-side API) ### Parameters #### Request Body - **prompt_messages** (array) - Required - An array of message objects, each with `role` and `content`. - **role** (string) - Required - The role of the message sender ('user' or 'assistant'). System role is not allowed here. - **content** (string) - Required - The text of the message. ### Request Example ```javascript const multiUserSession = await LanguageModel.create({ initialPrompts: [{ role: "system", content: "You are a mediator in a discussion between two departments." }] }); const result = await multiUserSession.prompt([ { role: "user", content: "Marketing: We need more budget for advertising campaigns." }, { role: "user", content: "Finance: We need to cut costs and advertising is on the list." }, { role: "assistant", content: "Let's explore a compromise that satisfies both departments." } ]); ``` ### Response #### Success Response (200) - **result** (string) - The model's generated response. #### Response Example ``` "We can allocate a portion of the reduced budget to targeted, high-impact advertising campaigns while identifying areas for operational cost savings." ``` ### Notes - System prompts cannot be provided via this method due to their special context window preservation behavior. ``` -------------------------------- ### Aborting a Specific Prompt Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This section details how to abort a specific prompt call using an AbortSignal. It explains the behavior when the prompt is queued, currently being responded to, or already completed. ```APIDOC ## Aborting a Specific Prompt ### Description Specific calls to `prompt()` or `promptStreaming()` can be aborted by passing an `AbortSignal` to them. ### Method `session.prompt(message, options)` or `session.promptStreaming(message, options)` ### Endpoint N/A (Client-side method) ### Parameters #### Request Body - **signal** (AbortSignal) - Required - The signal to abort the prompt operation. ### Request Example ```javascript const controller = new AbortController(); stopButton.onclick = () => controller.abort(); const result = await session.prompt("Write me a poem", { signal: controller.signal }); ``` ### Response #### Success Response (200) - N/A (Promise resolves with the prompt result) #### Error Response - **DOMException** (`"AbortError"`) - Rejected if the prompt is aborted while queued or being responded to. ### Behavior Details - **Queued Prompt:** If the prompt is still queued, it's removed, and the returned promise rejects with an `"AbortError"`. - **In-Progress Prompt:** If the prompt is being responded to, it's aborted, removed from the session, and the promise rejects with an `"AbortError"`. - **Completed Prompt:** If the prompt is already fully responded to, aborting does nothing. ``` -------------------------------- ### Measure Prompt Token Usage with measureInputUsage (JavaScript) Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Calculates the number of tokens a prompt will consume without actually sending it to the model. It supports various input types, including strings and multimodal arrays. ```javascript const stringUsage = await session.measureInputUsage(promptString); ``` ```javascript const audioUsage = await session.measureInputUsage([{ role: "user", content: [ { type: "text", value: "My response to your critique:" }, { type: "audio", value: audioBlob } ] }]); ``` -------------------------------- ### Session Destruction Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This section explains how to destroy language model sessions to free up memory, either by aborting creation or explicitly calling the destroy method. ```APIDOC ## Session Destruction ### Description Destroy a language model session to release its resources and unload it from memory. This can be done by aborting session creation or by calling the `destroy()` method on an active session. ### Method `LanguageModel.create()` with `AbortSignal`. `session.destroy()` to explicitly end the session. ### Endpoint N/A (Client-side API) ### Parameters #### Request Body for `LanguageModel.create()` - **signal** (AbortSignal) - Optional - An `AbortSignal` used to abort session creation. #### Method for `session.destroy()` No parameters required. ### Request Example for `LanguageModel.create()` with AbortSignal ```javascript const controller = new AbortController(); // stopButton.onclick = () => controller.abort(); // Example event handler const session = await LanguageModel.create({ signal: controller.signal }); ``` ### Request Example for `session.destroy()` ```javascript // stopButton.onclick = () => session.destroy(); // Example event handler session.destroy(); ``` ### Effects of Destruction - **Before `create()` settles:** Stops download progress signaling, rejects the `create()` promise with an `"AbortError" DOMException`. - **After `create()` settles:** Rejects ongoing `prompt()` calls, errors `ReadableStream`s from `promptStreaming()`, and allows the user agent to unload the model from memory. ### Error Handling - Rejection reason is an `"AbortError" DOMException` or the provided abort reason. ``` -------------------------------- ### Aborting an Append Operation Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This section explains how to abort an `append()` operation using an AbortSignal, detailing the behavior based on the operation's state (queued, ongoing, or complete). ```APIDOC ## Aborting an Append Operation ### Description The `append()` operation can also be aborted by passing an `AbortSignal`. ### Method `session.append(message, options)` ### Endpoint N/A (Client-side method) ### Parameters #### Request Body - **signal** (AbortSignal) - Required - The signal to abort the append operation. ### Response #### Success Response (200) - N/A (Promise resolves when the append operation is complete) #### Error Response - **DOMException** (`"AbortError"`) - Rejected if the append operation is aborted while queued or ongoing. ### Behavior Details - **Queued Append:** If the append is queued, it's removed, and the promise rejects with an `"AbortError"`. - **Ongoing Append:** If the append is ongoing, it's aborted, any appended part is removed, and the promise rejects with an `"AbortError"`. - **Completed Append:** If the append operation is complete, attempting to abort it does nothing. **Note:** Aborting an operation does not re-introduce overflowed messages if tokenization limits were reached. ``` -------------------------------- ### Constrain Prompt Response with JSON Schema Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This snippet demonstrates how to use a JSON schema to constrain the output of a prompt. The model is asked to summarize feedback into a rating, and the response is expected to be a JSON object with a 'rating' property. It handles parsing the JSON response and potential errors. ```javascript const schema = { type: "object", required: ["rating"], additionalProperties: false, properties: { rating: { type: "number", minimum: 0, maximum: 5, }, }, }; // Prompt the model and wait for the JSON response to come back. const result = await session.prompt("Summarize this feedback into a rating between 0-5: "+ "The food was delicious, service was excellent, will recommend.", { responseConstraint: schema } ); const { rating } = JSON.parse(result); console.log(rating); ``` -------------------------------- ### Omit Response Constraint from Input Prompt Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This snippet illustrates how to prevent the response constraint (JSON schema or RegExp) from being included in the input sent to the language model. This is useful for saving input token quota. When `omitResponseConstraintInput` is true, it's recommended to include guidance within the prompt string itself. ```javascript const result = await session.prompt(` Summarize this feedback into a rating between 0-5, only outputting a JSON object { rating }, with a single property whose value is a number: The food was delicious, service was excellent, will recommend. `, { responseConstraint: schema, omitResponseConstraintInput: true }); ``` -------------------------------- ### Handle Quota Overflow Event on Session (JavaScript) Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README Listens for the 'quotaoverflow' event on the session object, which is triggered when the context window limit is exceeded and older conversation parts are removed. This allows developers to be notified of such events. ```javascript session.addEventListener("quotaoverflow", () => { console.log("We've gone past the quota, and some inputs will be dropped!"); }); ``` -------------------------------- ### Granting API Access to Cross-Origin iframes via Permissions Policy Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This snippet demonstrates how to grant access to the language model API for a cross-origin iframe using the Permissions Policy 'allow' attribute. This ensures that iframes from different origins can utilize the API's capabilities by explicitly listing 'language-model' in the allow attribute. ```html ``` -------------------------------- ### Destroy Language Model Session Source: https://github.com/webmachinelearning/prompt-api/refs/heads/main/README This snippet shows two methods for destroying a language model session: using an AbortSignal during session creation or by explicitly calling the `destroy()` method on the session object. Destroying a session releases the language model from memory. ```javascript const controller = new AbortController(); stopButton.onclick = () => controller.abort(); const session = await LanguageModel.create({ signal: controller.signal }); stopButton.onclick = () => session.destroy(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.