Skip to content

Instantly share code, notes, and snippets.

@AnandPilania
Created July 17, 2024 15:07
Show Gist options
  • Save AnandPilania/a656680e6d3c641a0b8a15c6b5ff3d59 to your computer and use it in GitHub Desktop.
Save AnandPilania/a656680e6d3c641a0b8a15c6b5ff3d59 to your computer and use it in GitHub Desktop.
Google Chrome's experimental (canary build) language model web API
// Assume inputText is a string containing the text you want to prompt the AI with.
const inputText = "What is the capital of India?";
async function useAI() {
try {
// Create a text session with the AI
const session = await window.ai.createTextSession();
// Get a streaming response from the AI
const result = session.promptStreaming(inputText);
// Initialize an empty string to collect the response
let aiResponse = "";
// Iterate over the streamed chunks of the result
for await (const chunk of result) {
// Append each chunk to the response
aiResponse += chunk;
// Optionally, you can log each chunk as it is received
console.log(chunk);
}
// Log the complete response
console.log("AI Response:", aiResponse);
} catch (error) {
console.error("Error using AI:", error);
}
}
// Call the function to use the AI
useAI();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment