#How to access env variables?
36 messages · Page 1 of 1 (latest)
You need to pass the context in when using env() https://hono.dev/helpers/adapter#env
https://github.com/honojs/hono/issues/799#issuecomment-1990977003
Can you check this? I'm struggling to figure it out.
You are extracting GEMINI_AI, but then trying to use GEMINI_API
I corrected it
Is there a way to use it wihout using (c)?
The thread it corrected, read one it GEMINI_API
No, you are supposed to use c
Environment Variables are not accessible in a global context
import { env } from 'hono/adapter';
const app = new Hono();
app.get('/', (c) => {
const {GEMINI_AI} = env(c)
console.log("my key:" + GEMINI_API)
return c.json({
message: 'Live!',
env: GEMINI_API
})
})
export default app;
Ok, so in this code I want to access the env outsite the get? so ho do I do that?
You don't. Environment variables are only defined within the get
Or whatever other handler functions you define
Ok, so I can do this right? let output = await run(c); ??
Like I need the value of env in my run()
Yes, you can do that
import { env, getRuntimeKey } from 'hono/adapter'
import { GoogleGenerativeAI } from '@google/generative-ai';
const app = new Hono();
async function run(content: any) {
// For text-only input, use the gemini-pro model
const {GEMINI_API} = env(content)
const genAI = new GoogleGenerativeAI(GEMINI_API);
const model = genAI.getGenerativeModel({ model: "gemini-pro"});
const prompt = "Write a story about a magic backpack."
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
return text
}
app.get('/', async (c) => {
// const {GEMINI_API} = env(c)
const ans = await run(c);
console.log(ans)
console.log("my key:")
return c.json({
message: 'Live!',
Quote: ans
})
})
export default app;```
Am I doing it right, it's working, but sometimes give internal server error.
Looks right to me. What does the error show?
Just internal server error is there a console on on Cloudflare, let me check
Now, After the second deployment the errors vanished now no more errors.
Does it work locally?
Yes, it working fine now.
Just a little slow
I will enable streaming now, so it gives words one by one.
Let's see how can I do that
I didn't had cors in it, so was working with my IP only. Couldn't open the site from anyohter place
import { cors } from 'hono/cors'
import { env, getRuntimeKey } from 'hono/adapter'
import { GoogleGenerativeAI } from '@google/generative-ai';
const app = new Hono();
app.use('/*', cors())
async function run(content: any) {
// For text-only input, use the gemini-pro model
const {GEMINI_API} = env(content)
const genAI = new GoogleGenerativeAI(GEMINI_API);
const model = genAI.getGenerativeModel({ model: "gemini-pro"});
const prompt = "Write a line."
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
return text
}
app.get('/', async (c) => {
const ans = await run(c);
return c.json({
message: 'Live!',
Quote: ans
})
})
app.get('/v1', async (c) => {
// const ans = await run(c);
return c.text("ans")
})
export default app;```
I have this code, and I can't access it outside my home network. I don't know why
/v1 route is working but not the / route
ANS: it's not wokers it GEMINI API: giving this output.
"Error: [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent: [400 Bad Request] User location is not supported for the API use."
That sounds like an issue from Google’s end
They might have locked their APIs down to certain locations by IP
It works with my HOME network but not with the mobile
LOL What a pain.
Need to find some other free LLM api
Nope, finally I found the issue was with the way CloudFlare handled the reqeust, since Google is geo-blocking in some countries, When I was using fiber internet it worked, but when I switched to mobile internet I don't know how cloudflare did it's routing, but it might have went to some geo-blocked country like EU etc.