Created
July 17, 2024 15:07
-
-
Save AnandPilania/a656680e6d3c641a0b8a15c6b5ff3d59 to your computer and use it in GitHub Desktop.
Google Chrome's experimental (canary build) language model web API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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