Created
June 6, 2023 01:52
-
-
Save mabry1985/54c10fa4594fb5a5edcf65c1db55b44b to your computer and use it in GitHub Desktop.
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
let { GoogleAuth } = await import("google-auth-library"); | |
let { DiscussServiceClient } = await import("@google-ai/generativelanguage"); | |
import "@johnlindquist/kit"; | |
const MODEL_NAME = "models/chat-bison-001"; | |
const API_KEY = await env("PALM_API_KEY", { | |
hint: `Signup for waitlist here <a href="https://developers.generativeai.google/">here</a>`, | |
}); | |
const client = new DiscussServiceClient({ | |
authClient: new GoogleAuth().fromAPIKey(API_KEY), | |
}); | |
const config = { | |
model: MODEL_NAME, | |
temperature: 0.75, | |
candidateCount: 1, | |
top_k: 40, | |
top_p: 0.95, | |
}; | |
const chatHistory = []; | |
const generateText = async (text) => { | |
chatHistory.push({ content: text }); | |
const response = await client.generateMessage({ | |
...config, | |
prompt: { | |
context: "You are a funny and helpful assistant.", | |
messages: chatHistory, | |
}, | |
}); | |
log(response); | |
log(response[0].filters); | |
if (response[0].filters.length > 0) { | |
return `The model has rejected your input. Reason: ${response[0].filters[0].reason}`; | |
} else { | |
chatHistory.push({ content: response[0].candidates[0].content }); | |
return response[0].candidates[0].content; | |
} | |
}; | |
await chat({ | |
onSubmit: async (input) => { | |
setLoading(true); | |
try { | |
const response = await generateText(input); | |
let message = md(response); | |
chat.addMessage(""); | |
chat.setMessage(-1, message); | |
} catch (e) { | |
console.log(e); | |
chat.addMessage(""); | |
chat.setMessage(-1, md("Error: " + e.message)); | |
} | |
setLoading(false); | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment