The language model API allows you to create a conversation with the language model.
This API is compatible with the Prompt API proposal shipping with Google Chrome
constsession=awaitwindow.aibrow.languageModel.create();// Prompt the model and wait for the whole result to come back.constresult=awaitsession.prompt("Write me a poem.");console.log(result);// Prompt the model and stream the result:conststream=awaitsession.promptStreaming("Write me an extra-long poem.");forawait (constchunkof stream) {console.log(chunk);}
Continuing a conversation
constsession=awaitwindow.aibrow.languageModel.create();// Ask the intial questionconstresult=awaitsession.prompt("Tell me about the weather");console.log(result);// Prompt the same session again to continue the same conversationconstresult2=awaitsession.prompt("Can you expand on this?");console.log(result2);
System prompts & initial prompts
You can use system prompts to customize the behaviour of the model as well as initial prompts from a previous conversation
constsession=awaitwindow.aibrow.languageModel.create({ initialPrompts: [ { role:"system", content:"Speak like a pirate" }, { role:"user", content:"What's your favourite word?" }, { role:"assistant", content:"Swashbuckling" } ]});asyncfunctionrandomWord () {// Clone the sessionconstfreshSession=awaitsession.clone()returnawaitfreshSessiong.prompt("Give me another")}// Continue the conversation from the initial promptsconsole.log(awaitrandomWord())console.log(awaitrandomWord())