langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
12.29k stars 2.08k forks source link

Endpoint issue with Gemini Pro 1.5 and Gemini Flash Support #5437

Closed stuntnmore closed 3 months ago

stuntnmore commented 3 months ago

Checked other resources

Example Code

the version should be able to be set from configuration, right now google apis have 2 versions, one called v1, and another called v1beta, the difference is on v1 some of the newest gemini models dont exist on it but on v1beta it does, so being able to set this would allow for more model support of newer generation gemini releases

Error Message and Stack Trace (if applicable)

No response

Description

add the ability to set the google generative api version through config and if not set then default back to v1

System Info

langchain + google community libs

stuntnmore commented 3 months ago

specifically around this area https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-google-common/src/connection.ts#L135

jeasonnow commented 3 months ago

https://github.com/langchain-ai/langchainjs/blob/6dacb4d3b39f1d52e6597bd11e5093146c02329d/libs/langchain-google-common/src/connection.ts#L149

It seems that if you use fields.apiVersion, it will take effect. Would you like to try? @stuntnmore

afirstenberg commented 3 months ago

If you're using any of the packages that use google-common (ie - @langchain/google-gauth, @langchain/google-webauth, @langchain/google-vertexai, or @langchain/google-vertexai-web) you should be able to include apiVersion as a configuration parameter. So something like this should work:

const model = new ChatGoogle({
  modelName: "gemini-1.5-flash-latest",
  apiVersion: "v1beta",
  platformType: "gai",
});
yharaskrik commented 3 months ago

Anyone have a working configuration using an API key? I tried what @afirstenberg suggested (although not with gai) but that did not work, I get

 [GoogleGenerativeAI Error]: Error fetching from https://generativeai.googleapis.com/v1/models/gemini-1.5-flash-latest:generateContent: [404 Not Found]

config:

    apiKey: process.env['API_KEY'],
    modelName: 'gemini-1.5-flash-latest',
    apiVersion: 'v1beta',
afirstenberg commented 3 months ago

@yharaskrik - which package are you using?

What I described works for the packages I specified. If you're using @langchain/google-genai (which it kinda looks like you are), you shouldn't need to set apiVersion, you just need to update to the latest version of the package.

yharaskrik commented 3 months ago

@yharaskrik - which package are you using?

What I described works for the packages I specified. If you're using @langchain/google-genai (which it kinda looks like you are), you shouldn't need to set apiVersion, you just need to update to the latest version of the package.

Ya that's the one I'm using. I think I'm on the latest (since I just installed it today). I'll do some more debugging when I get home.

stuntnmore commented 3 months ago

Just checked up langchain-google-genai and doesnt seem like theres a api version field there and just uses the constructor default of v1 https://github.com/langchain-ai/langchainjs/blob/6dacb4d3b39f1d52e6597bd11e5093146c02329d/libs/langchain-google-genai/src/chat_models.ts#L280

the fix is probably just adding

fields?.apiVersion?? this.apiVersion
stuntnmore commented 3 months ago

opened a pr with change but probably needs more testing to see if its all okay https://github.com/langchain-ai/langchainjs/pull/5480

Just checked up langchain-google-genai and doesnt seem like theres a api version field there and just uses the constructor default of v1

https://github.com/langchain-ai/langchainjs/blob/6dacb4d3b39f1d52e6597bd11e5093146c02329d/libs/langchain-google-genai/src/chat_models.ts#L280

the fix is probably just adding

fields?.apiVersion?? this.apiVersion
afirstenberg commented 3 months ago

This should already be in place: https://github.com/langchain-ai/langchainjs/pull/5115 - Which used the new library that defaults to v1beta https://github.com/langchain-ai/langchainjs/pull/5426 - Which added apiVersion

afirstenberg commented 3 months ago

@bracesproul - Did we get a regression somewhere?

yharaskrik commented 3 months ago

Looks like was a regression it seems and a URL is wrong. This is working for me (obviously its a hack, I will see if I can open a PR with the real fix)

const baseUrl = 'https://generativelanguage.googleapis.com';
const version = 'v1beta';
const genAi = new ChatGoogleGenerativeAI({
    model: 'gemini-1.5-flash-latest',
    apiKey: process.env.GEMINI_KEY,
    apiVersion: version,
    maxRetries: 0,
    baseUrl,
});

(genAi as any).client.requestOptions.apiVersion = version;
(genAi as any).client.requestOptions.baseUrl = baseUrl;
(genAi as any).apiVersion = version;
(genAi as any).baseUrl = baseUrl;
bracesproul commented 3 months ago

This should be fixed by #5490 (@langchain/google-genai v0.0.15)

Please ping if you're still experiencing this issue