langchain-ai/langchainjs

GOOGLE_API_KEY and GOOGLE_APPLICATION_CREDENTIALS conflict in .env

Closed this issue · 2 comments

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

const { VertexAI } = require('@langchain/google-vertexai');
const {
  GoogleCustomSearch,
} = require('@langchain/community/tools/google_custom_search');

// Initialize Vertex AI LLM
const llm = new VertexAI({
  model: process.env.VERTEX_AI_MODEL_NAME,
  modelName: process.env.VERTEX_AI_MODEL_NAME,
  temperature: 1,
  topP: 0.95,
  maxOutputTokens: 8192,
});

// Initialize Google Custom Search
const searchTool = new GoogleCustomSearch();

Error Message and Stack Trace (if applicable)

Error occurred during prompt execution: Error: APIs that require a project ID cannot use an API key

    at ApiKeyGoogleAuth.getProjectId (node_modules/@langchain/google-common/dist/auth.cjs:77:15)
    at GoogleLLMConnection.buildUrlVertex (node_modules/@langchain/google-common/dist/connection.cjs:250:45)
    at GoogleLLMConnection.buildUrl (node_modules/@langchain/google-common/dist/connection.cjs:261:29)
    at GoogleLLMConnection._buildOpts (node_modules/@langchain/google-common/dist/connection.cjs:61:32)
    at GoogleLLMConnection._request (node_modules/@langchain/google-common/dist/connection.cjs:87:33)
    at GoogleLLMConnection.request (node_modules/@langchain/google-common/dist/connection.cjs:284:37)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async VertexAI._call (node_modules/@langchain/google-common/dist/llms.cjs:177:24)
    at async Promise.all (index 0)
    at async VertexAI._generate (node_modules/@langchain/core/dist/language_models/llms.cjs:355:29)

Description

  • GOOGLE_API_KEY seems to be conflicting with the existing GOOGLE_APPLICATION_CREDENTIALS variable present in the .env file.
  • I require both as I am trying to enable GoogleCustomSearch as a tool for the LLM.

System Info


langchain@0.3.7 | MIT | deps: 12 | versions: 302
Typescript bindings for langchain
https://github.com/langchain-ai/langchainjs/tree/main/langchain/

keywords: llm, ai, gpt3, chain, prompt, prompt engineering, chatgpt, machine learning, ml, openai, embeddings, vectorstores

dist
.tarball: https://registry.npmjs.org/langchain/-/langchain-0.3.7.tgz
.shasum: a0d2010a9f2dab473eaebfaac5228fac7c2a6f17
.integrity: sha512-6/Gkk9Zez3HkbsETFxZVo1iKLmaK3OzkDseC5MYFKVmYFDXFAOyJR3srJ9P61xF8heVdsPixqYIsejBn7/9dXg==
.unpackedSize: 2.9 MB

dependencies:
@langchain/openai: >=0.1.0 <0.4.0        jsonpointer: ^5.0.1                      uuid: ^10.0.0                            
@langchain/textsplitters: >=0.0.0 <0.2.0 langsmith: ^0.2.8                        yaml: ^2.2.1                             
js-tiktoken: ^1.0.12                     openapi-types: ^12.1.3                   zod-to-json-schema: ^3.22.3              
js-yaml: ^4.1.0                          p-retry: 4                               zod: ^3.22.4                             

maintainers:
- nfcampos <nuno@boringbits.io>
- jacoblee93 <jacoblee93@gmail.com>
- andrewnguonly <andrewnguonly@gmail.com>
- davidduong <david@duong.cz>
- hwchase17 <hw.chase.17@gmail.com>
- basproul <braceasproul@gmail.com>

dist-tags:
latest: 0.3.7                              tag-for-publishing-older-releases: 0.2.20  
next: 0.3.2-rc.0                           

published a week ago by jacoblee93 <jacoblee93@gmail.com>

Hey @arximughal! I'm here to help you with bug fixes, questions, and becoming a contributor. Let's squash those bugs together! 🤖

The error you're encountering is due to a conflict between using GOOGLE_API_KEY and GOOGLE_APPLICATION_CREDENTIALS for services that require a project ID. In LangChain.js, some Google services, like Vertex AI, require a service account with a project ID, which cannot be used with an API key alone.

To resolve this issue, you should use GOOGLE_APPLICATION_CREDENTIALS for services that require a project ID, such as Vertex AI. This involves setting the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your service account credentials file. This method provides the necessary project ID and other credentials required for authentication.

For the GoogleCustomSearch tool, which uses the GOOGLE_API_KEY, you can continue to use the API key as it does not require a project ID. Ensure that the GOOGLE_API_KEY is set in your environment variables specifically for the Google Custom Search API.

Here's a suggested approach:

  1. Vertex AI: Use GOOGLE_APPLICATION_CREDENTIALS for authentication. Ensure your service account JSON file is correctly set up and the environment variable points to its path.

  2. Google Custom Search: Use GOOGLE_API_KEY and GOOGLE_CSE_ID for authentication. These should be set in your environment variables.

By separating the authentication methods based on the service requirements, you can avoid conflicts and ensure both services work correctly.

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

Well...
If I keep the environment variables named like mentioned above (i.e., GOOGLE_API_KEY & GOOGLE_APPLICATION_CREDENTIALS), it doesn't work.
But if I rename the variables for GOOGLE_API_KEY and GOOGLE_CSE_ID to any other name and then pass them inside the GoogleCustomSearch constructor manually, it seems to work fine:

const searchTool = new GoogleCustomSearch({
  apiKey: process.env.GCP_CUSTOM_SEARCH_API_KEY,
  googleCSEId: process.env.GCP_CUSTOM_SEARCH_ENGINE_ID,
});

I will close the issue. Feel free to open in future if required!