langchain.js
    Preparing search index...

    Integration with Google Vertex AI chat models in web environments.

    Setup: Install @langchain/google-vertexai-web and set your stringified Vertex AI credentials as an environment variable named GOOGLE_VERTEX_AI_WEB_CREDENTIALS.

    npm install @langchain/google-vertexai-web
    export GOOGLE_VERTEX_AI_WEB_CREDENTIALS={"type":"service_account","project_id":"YOUR_PROJECT-12345",...}

    Runtime args can be passed as the second argument to any of the base runnable methods .invoke. .stream, .batch, etc. They can also be passed via .withConfig, or the second arg in .bindTools, like shown in the examples below:

    // If binding tools along with other options, chain `.bindTools` and `.withConfig`
    const llmWithArgsBound = llm.bindTools([...]) // tools array
    .withConfig({
    stop: ["\n"], // other call options
    });

    // When calling `.bindTools`, call options should be passed via the second argument
    const llmWithTools = llm.bindTools(
    [...],
    {
    tool_choice: "auto",
    }
    );
    Instantiate
    import { ChatVertexAI } from '@langchain/google-vertexai-web';

    const llm = new ChatVertexAI({
    model: "gemini-1.5-pro",
    temperature: 0,
    authOptions: {
    credentials: process.env.GOOGLE_VERTEX_AI_WEB_CREDENTIALS,
    },
    // other params...
    });

    Invoking
    const input = `Translate "I love programming" into French.`;

    // Models also accept a list of chat messages or a formatted prompt
    const result = await llm.invoke(input);
    console.log(result);
    AIMessageChunk {
      "content": "\"J'adore programmer\" \n\nHere's why this is the best translation:\n\n* **J'adore** means \"I love\" and conveys a strong passion.\n* **Programmer** is the French verb for \"to program.\"\n\nThis translation is natural and idiomatic in French. \n",
      "additional_kwargs": {},
      "response_metadata": {},
      "tool_calls": [],
      "tool_call_chunks": [],
      "invalid_tool_calls": [],
      "usage_metadata": {
        "input_tokens": 9,
        "output_tokens": 63,
        "total_tokens": 72
      }
    }
    

    Streaming Chunks
    for await (const chunk of await llm.stream(input)) {
    console.log(chunk);
    }
    AIMessageChunk {
      "content": "\"",
      "additional_kwargs": {},
      "response_metadata": {},
      "tool_calls": [],
      "tool_call_chunks": [],
      "invalid_tool_calls": []
    }
    AIMessageChunk {
      "content": "J'adore programmer\" \n",
      "additional_kwargs": {},
      "response_metadata": {},
      "tool_calls": [],
      "tool_call_chunks": [],
      "invalid_tool_calls": []
    }
    AIMessageChunk {
      "content": "",
      "additional_kwargs": {},
      "response_metadata": {},
      "tool_calls": [],
      "tool_call_chunks": [],
      "invalid_tool_calls": []
    }
    AIMessageChunk {
      "content": "",
      "additional_kwargs": {},
      "response_metadata": {
        "finishReason": "stop"
      },
      "tool_calls": [],
      "tool_call_chunks": [],
      "invalid_tool_calls": [],
      "usage_metadata": {
        "input_tokens": 9,
        "output_tokens": 8,
        "total_tokens": 17
      }
    }
    

    Aggregate Streamed Chunks
    import { AIMessageChunk } from '@langchain/core/messages';
    import { concat } from '@langchain/core/utils/stream';

    const stream = await llm.stream(input);
    let full: AIMessageChunk | undefined;
    for await (const chunk of stream) {
    full = !full ? chunk : concat(full, chunk);
    }
    console.log(full);
    AIMessageChunk {
      "content": "\"J'adore programmer\" \n",
      "additional_kwargs": {},
      "response_metadata": {
        "finishReason": "stop"
      },
      "tool_calls": [],
      "tool_call_chunks": [],
      "invalid_tool_calls": [],
      "usage_metadata": {
        "input_tokens": 9,
        "output_tokens": 8,
        "total_tokens": 17
      }
    }
    

    Bind tools
    import { z } from 'zod';

    const GetWeather = {
    name: "GetWeather",
    description: "Get the current weather in a given location",
    schema: z.object({
    location: z.string().describe("The city and state, e.g. San Francisco, CA")
    }),
    }

    const GetPopulation = {
    name: "GetPopulation",
    description: "Get the current population in a given location",
    schema: z.object({
    location: z.string().describe("The city and state, e.g. San Francisco, CA")
    }),
    }

    const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);
    const aiMsg = await llmWithTools.invoke(
    "Which city is hotter today and which is bigger: LA or NY?"
    );
    console.log(aiMsg.tool_calls);
    [
      {
        name: 'GetPopulation',
        args: { location: 'New York City, NY' },
        id: '33c1c1f47e2f492799c77d2800a43912',
        type: 'tool_call'
      }
    ]
    

    Structured Output
    import { z } from 'zod';

    const Joke = z.object({
    setup: z.string().describe("The setup of the joke"),
    punchline: z.string().describe("The punchline to the joke"),
    rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
    }).describe('Joke to tell user.');

    const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
    const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
    console.log(jokeResult);
    {
      setup: 'What do you call a cat that loves to bowl?',
      punchline: 'An alley cat!'
    }
    

    Usage Metadata
    const aiMsgForMetadata = await llm.invoke(input);
    console.log(aiMsgForMetadata.usage_metadata);
    { input_tokens: 9, output_tokens: 8, total_tokens: 17 }
    

    Stream Usage Metadata
    const streamForMetadata = await llm.stream(
    input,
    {
    streamUsage: true
    }
    );
    let fullForMetadata: AIMessageChunk | undefined;
    for await (const chunk of streamForMetadata) {
    fullForMetadata = !fullForMetadata ? chunk : concat(fullForMetadata, chunk);
    }
    console.log(fullForMetadata?.usage_metadata);
    { input_tokens: 9, output_tokens: 8, total_tokens: 17 }
    

    Hierarchy

    • ChatGoogle
      • ChatVertexAI
    Index

    Constructors

    Properties

    Methods

    Constructors

    Properties

    lc_namespace: string[] = ...

    Methods

    • Returns string