Created
July 20, 2024 07:19
-
-
Save Nithur-M/b5a95bbd43bf1e70e2aed1cde7a536ed to your computer and use it in GitHub Desktop.
Send user queries to Gemini Nano
This file contains hidden or 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
... | |
const handleSendMessage = async () => { | |
const canCreate = await window.ai.canCreateTextSession(); | |
if (canCreate !== "no") { | |
const session = await window.ai.createTextSession(); | |
const stream = session.promptStreaming(`You are a friendly assistant designed to help users with job searching. You need to answer this question: \n\n${question} based on this text: ${newtext}. If you couldn't find answers from the given text, say there is no answer.`); | |
for await (const chunk of stream) { | |
setAnswer(chunk); | |
} | |
} | |
} | |
const handleKeyDown = (event) => { | |
if (event.key === 'Enter') { | |
event.preventDefault(); // Prevent form submission | |
handleSendMessage(); | |
} | |
}; | |
return ( | |
... | |
{show && | |
<div className="fixed flex flex-col bottom-28 right-12 p-4 rounded-md bg-foreground min-h-64 w-96"> | |
<h3 className="text-background font-semibold border-b">Ask any question about {title}</h3> | |
<div className="max-h-96 overflow-scroll"> | |
{answer && | |
<div className="text-background rounded-md bg-gray-200 p-4 mt-3 mb-3"> | |
<ReactMarkdown>{answer}</ReactMarkdown> | |
</div> | |
} | |
</div> | |
<Input className="mt-auto text-background" onChange={(e) => setQuestion(e.target.value)} onKeyDown={handleKeyDown} placeholder="What is the salary?" /> | |
</div> | |
} | |
... | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment