[GH-ISSUE #17] How can I use this with bedrock? #7

Closed
opened 2026-02-16 08:17:11 -05:00 by yindo · 1 comment
Owner

Originally created by @absolutemadla8 on GitHub (Mar 19, 2025).
Original GitHub issue: https://github.com/langchain-ai/create-agent-chat-app/issues/17

import { AIMessage } from "@langchain/core/messages";
import { RunnableConfig } from "@langchain/core/runnables";
import { MessagesAnnotation, StateGraph } from "@langchain/langgraph";
import { ToolNode } from "@langchain/langgraph/prebuilt";
import { BedrockChat } from "@langchain/community/chat_models/bedrock";

import { ConfigurationSchema } from "./configuration.js";
import { TOOLS } from "./tools.js";
import { SYSTEM_PROMPT_TEMPLATE } from "./prompts.js";

// Define the function that calls the model
async function callModel(
  state: typeof MessagesAnnotation.State,
  config: RunnableConfig,
): Promise<typeof MessagesAnnotation.Update> {
  /** Call the LLM powering our agent. **/
  const configurable = config.configurable ?? {};
  
  // Create the Bedrock model directly instead of using loadChatModel
  const model = new BedrockChat({
    model: "anthropic.claude-3-sonnet-20240229-v1:0", // Use the model directly
    region: configurable.region ?? process.env.AWS_REGION ?? "us-west-2",
    // Use environment variables for credentials in production
    credentials: configurable.credentials ?? {
      accessKeyId: process.env.AWS_ACCESS_KEY_ID,
      secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
      sessionToken: process.env.AWS_SESSION_TOKEN,
    },
    temperature: 0,
    maxTokens: 4096,
  });
  
  // Bind tools to the model
  const modelWithTools = model.bindTools(TOOLS);

  const response = await modelWithTools.invoke([
    {
      role: "system",
      content: configurable.systemPromptTemplate ?? SYSTEM_PROMPT_TEMPLATE.replace(
        "{system_time}",
        new Date().toISOString(),
      ),
    },
    ...state.messages,
  ]);

  // We return a list, because this will get added to the existing list
  return { messages: [response] };
}

// Define the function that determines whether to continue or not
function routeModelOutput(state: typeof MessagesAnnotation.State): string {
  const messages = state.messages;
  const lastMessage = messages[messages.length - 1];
  // If the LLM is invoking tools, route there.
  if ((lastMessage as AIMessage)?.tool_calls?.length || 0 > 0) {
    return "tools";
  }
  // Otherwise end the graph.
  else {
    return "__end__";
  }
}

// Define a new graph with MessagesAnnotation
const workflow = new StateGraph(MessagesAnnotation, ConfigurationSchema)
  // Define the two nodes we will cycle between
  .addNode("callModel", callModel)
  .addNode("tools", new ToolNode(TOOLS))
  // Set the entrypoint as `callModel`
  .addEdge("__start__", "callModel")
  .addConditionalEdges(
    "callModel",
    routeModelOutput,
  )
  .addEdge("tools", "callModel");

// Compile the graph
export const graph = workflow.compile({
  interruptBefore: [],
  interruptAfter: [],
});

Getting Error

[1] agents:dev: (node:86677) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 21 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
[1] agents:dev: (Use `node --trace-warnings ...` to show where the warning was created)
[1] agents:dev: info:    ┏ Background run succeeded
[1] agents:dev: info:    ┃ [1] {
[1] agents:dev: info:    ┃ [2]   run_id: '0f53dd97-b56c-4c6f-8b43-f39ebeeeb615',
[1] agents:dev: info:    ┃ [3]   run_attempt: 1,
[1] agents:dev: info:    ┃ [4]   run_created_at: 2025-03-19T09:23:58.738Z,
[1] agents:dev: info:    ┃ [5]   run_started_at: 2025-03-19T09:23:58.797Z,
[1] agents:dev: info:    ┃ [6]   run_ended_at: 2025-03-19T09:24:03.988Z,
[1] agents:dev: info:    ┃ [7]   run_exec_ms: 5191
[1] agents:dev: info:    ┗ [8] }
Originally created by @absolutemadla8 on GitHub (Mar 19, 2025). Original GitHub issue: https://github.com/langchain-ai/create-agent-chat-app/issues/17 ```typescript import { AIMessage } from "@langchain/core/messages"; import { RunnableConfig } from "@langchain/core/runnables"; import { MessagesAnnotation, StateGraph } from "@langchain/langgraph"; import { ToolNode } from "@langchain/langgraph/prebuilt"; import { BedrockChat } from "@langchain/community/chat_models/bedrock"; import { ConfigurationSchema } from "./configuration.js"; import { TOOLS } from "./tools.js"; import { SYSTEM_PROMPT_TEMPLATE } from "./prompts.js"; // Define the function that calls the model async function callModel( state: typeof MessagesAnnotation.State, config: RunnableConfig, ): Promise<typeof MessagesAnnotation.Update> { /** Call the LLM powering our agent. **/ const configurable = config.configurable ?? {}; // Create the Bedrock model directly instead of using loadChatModel const model = new BedrockChat({ model: "anthropic.claude-3-sonnet-20240229-v1:0", // Use the model directly region: configurable.region ?? process.env.AWS_REGION ?? "us-west-2", // Use environment variables for credentials in production credentials: configurable.credentials ?? { accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, sessionToken: process.env.AWS_SESSION_TOKEN, }, temperature: 0, maxTokens: 4096, }); // Bind tools to the model const modelWithTools = model.bindTools(TOOLS); const response = await modelWithTools.invoke([ { role: "system", content: configurable.systemPromptTemplate ?? SYSTEM_PROMPT_TEMPLATE.replace( "{system_time}", new Date().toISOString(), ), }, ...state.messages, ]); // We return a list, because this will get added to the existing list return { messages: [response] }; } // Define the function that determines whether to continue or not function routeModelOutput(state: typeof MessagesAnnotation.State): string { const messages = state.messages; const lastMessage = messages[messages.length - 1]; // If the LLM is invoking tools, route there. if ((lastMessage as AIMessage)?.tool_calls?.length || 0 > 0) { return "tools"; } // Otherwise end the graph. else { return "__end__"; } } // Define a new graph with MessagesAnnotation const workflow = new StateGraph(MessagesAnnotation, ConfigurationSchema) // Define the two nodes we will cycle between .addNode("callModel", callModel) .addNode("tools", new ToolNode(TOOLS)) // Set the entrypoint as `callModel` .addEdge("__start__", "callModel") .addConditionalEdges( "callModel", routeModelOutput, ) .addEdge("tools", "callModel"); // Compile the graph export const graph = workflow.compile({ interruptBefore: [], interruptAfter: [], }); ``` Getting Error ``` [1] agents:dev: (node:86677) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 21 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit [1] agents:dev: (Use `node --trace-warnings ...` to show where the warning was created) [1] agents:dev: info: ┏ Background run succeeded [1] agents:dev: info: ┃ [1] { [1] agents:dev: info: ┃ [2] run_id: '0f53dd97-b56c-4c6f-8b43-f39ebeeeb615', [1] agents:dev: info: ┃ [3] run_attempt: 1, [1] agents:dev: info: ┃ [4] run_created_at: 2025-03-19T09:23:58.738Z, [1] agents:dev: info: ┃ [5] run_started_at: 2025-03-19T09:23:58.797Z, [1] agents:dev: info: ┃ [6] run_ended_at: 2025-03-19T09:24:03.988Z, [1] agents:dev: info: ┃ [7] run_exec_ms: 5191 [1] agents:dev: info: ┗ [8] } ```
yindo closed this issue 2026-02-16 08:17:11 -05:00
Author
Owner

@bracesproul commented on GitHub (Mar 19, 2025):

@absolutemadla8 the logs you included are not showing any reason bedrock wouldn't work, in fact they say Background run succeeded. From your code, I don't see any reason it wouldn't work. I would also suggest moving to AWS Converse via our @langchain/aws package, instead of the community package.

@bracesproul commented on GitHub (Mar 19, 2025): @absolutemadla8 the logs you included are not showing any reason bedrock wouldn't work, in fact they say `Background run succeeded`. From your code, I don't see any reason it wouldn't work. I would also suggest moving to AWS Converse via our `@langchain/aws` package, instead of the community package.
yindo changed title from How can I use this with bedrock? to [GH-ISSUE #17] How can I use this with bedrock? 2026-06-05 17:18:23 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/create-agent-chat-app#7