Skip to content

Commit

Permalink
validate key and modularize chat
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasalmeida committed Oct 18, 2024
1 parent 4c2e048 commit 6a6a73b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 43 deletions.
42 changes: 41 additions & 1 deletion chat.mjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
console.log(`stand alone gemini chat being tested at \n${Date()}`)
console.log(`stand alone gemini chat deployed at \n${Date()}`)

const mod = (await import('./gem.mjs'))
const gem = new mod.GEM(mod.validKey())
const post = gem.post
post('hello') // testing


//export{
// post
//}

//get post from instance of GEM
//post = (new (await import('./gem.mjs')).GEM).post

/*
this.chat=async function(div,url='https://episphere.github.io/gemini/connectStudy.txt'){ // target div and context url
console.log(`chatting ...`)
let txt = await (await fetch(url)).text()
if(typeof(div)=='string'){
div = document.getElementById(div)
}
if(!div){
div = document.createElement('div')
document.body.appendChild(div)
}
// scafold
div.innerHTML=`<div id="divQuestion"></div><hr><textarea id="txtPrompt">experimental bot, don't take seriously</textarea>`
// operate on textarea
let txtPrompt = div.querySelector('#txtPrompt')
let that=this
txtPrompt.onkeyup=async function(ev){
if(ev.key=='Enter'){
console.log(ev.target.value,that)
let ans = await that.post(ev.target.value)
console.log(ans)
}
}
4
}
*/

65 changes: 23 additions & 42 deletions gem.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
console.log(`gem.mjs (gemini SDK) imported\n${Date()}`);

function validKey(key) {
let vKey = key || localStorage.gemKey || localStorage._gemApiKey
if (typeof (vKey) == 'undefined') {
vKey = false
} else {
if (vKey.length < 10) {
vKey = false
}
}
if (!vKey) {
vKey = prompt(`please provide your API key, you can find it at https://aistudio.google.com/app/apikey`)
localStorage.gemKey = vKey
}
return vKey
}
class GEM {
constructor(key) {
this.loadedAt = Date()
this.key = key || localStorage.gemKey || localStorage._gemApiKey
if(typeof(this.key)=='undefined'){
delete this.key
}else{
if(this.key.length<10){delete this.key}
}
if (!this.key) {
this.key = prompt(`please provide your API key, you can find it at https://aistudio.google.com/app/apikey`)
localStorage.gemKey = this.key
}
this.key = validKey(key)
let thisKey=this.key
this.post = async function(txt="how to best grill sardines") {
let res = await (await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-goog-api-key': `${this.key}`
'x-goog-api-key': `${thisKey}`
},
body: JSON.stringify({
"contents": [{
Expand All @@ -28,19 +35,19 @@ class GEM {
}]
})
})).json()
console.log('res:',res.candidates[0].content.parts[0].text)
console.log('res:', res.candidates[0].content.parts[0].text)
return res
}
this.embed=async function(txt="how to best grill sardines",dim=768){
this.embed = async function(txt="how to best grill sardines", dim=768) {
let res = await (await fetch(`https://generativelanguage.googleapis.com/v1beta/models/text-embedding-004:embedContent`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-goog-api-key': `${this.key}`
'x-goog-api-key': `${thisKey}`
},
body: JSON.stringify({
"model": "models/text-embedding-004",
"output_dimensionality":dim,
"output_dimensionality": dim,
"content": {
"parts": [{
"text": txt
Expand All @@ -51,33 +58,7 @@ class GEM {
console.log(res)
return res.embedding.values
}
/*
this.chat=async function(div,url='https://episphere.github.io/gemini/connectStudy.txt'){ // target div and context url
console.log(`chatting ...`)
let txt = await (await fetch(url)).text()
if(typeof(div)=='string'){
div = document.getElementById(div)
}
if(!div){
div = document.createElement('div')
document.body.appendChild(div)
}
// scafold
div.innerHTML=`<div id="divQuestion"></div><hr><textarea id="txtPrompt">experimental bot, don't take seriously</textarea>`
// operate on textarea
let txtPrompt = div.querySelector('#txtPrompt')
let that=this
txtPrompt.onkeyup=async function(ev){
if(ev.key=='Enter'){
console.log(ev.target.value,that)
let ans = await that.post(ev.target.value)
console.log(ans)
}
}
4
}
*/
}
}

export {GEM}
export {GEM,validKey}

0 comments on commit 6a6a73b

Please sign in to comment.