[PR #307] [MERGED] Add Annotation.Root to make it easier to access State, Update and Node types #649

Closed
opened 2026-02-15 19:15:22 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langchain-ai/langgraphjs/pull/307
Author: @nfcampos
Created: 8/11/2024
Status: Merged
Merged: 8/12/2024
Merged by: @jacoblee93

Base: mainHead: nc/11aug/annotation


📝 Commits (5)

📊 Changes

9 files changed (+239 additions, -97 deletions)

View changed files

📝 langgraph/.eslintrc.cjs (+2 -1)
📝 langgraph/package.json (+1 -0)
langgraph/src/graph/annotation.ts (+104 -0)
📝 langgraph/src/graph/index.ts (+1 -3)
📝 langgraph/src/graph/message.ts (+1 -1)
📝 langgraph/src/graph/state.ts (+23 -81)
📝 langgraph/src/tests/graph.test.ts (+13 -5)
📝 langgraph/src/tests/pregel.test.ts (+5 -5)
📝 yarn.lock (+89 -1)

📄 Description

// useful to generate SQL query
const modelLowTemp = new ChatOpenAI({ temperature: 0.1 });
// useful to generate natural language outputs
const modelHighTemp = new ChatOpenAI({ temperature: 0.7 });

const annotation = Annotation.Root({
  messages: Annotation({ reducer: messagesStateReducer, default: () => [] }),
  user_query: Annotation<string>(),
  sql_query: Annotation<string>(),
  sql_explanation: Annotation<string>(),
});

const generatePrompt = new SystemMessage(
  "You are a helpful data analyst, who generates SQL queries for users based on their questions."
);

async function generateSql(state: typeof annotation.State) {
  const userMessage = new HumanMessage(state.user_query);
  const messages = [generatePrompt, ...state.messages, userMessage];
  const res = await modelLowTemp.invoke(messages);
  return {
    sql_query: res.content as string,
    // update conversation history
    messages: [userMessage, res],
  };
}

const explainPrompt = new SystemMessage(
  "You are a helpful data analyst, who explains SQL queries to users."
);

async function explainSql(state: typeof annotation.State) {
  const messages = [explainPrompt, ...state.messages];
  const res = await modelHighTemp.invoke(messages);
  return {
    sql_explanation: res.content as string,
    // update conversation history
    messages: res,
  };
}

const builder = new StateGraph(annotation)
  .addNode("generate_sql", generateSql)
  .addNode("explain_sql", explainSql)
  .addEdge(START, "generate_sql")
  .addEdge("generate_sql", "explain_sql")
  .addEdge("explain_sql", END);

const graph = builder.compile();

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langchain-ai/langgraphjs/pull/307 **Author:** [@nfcampos](https://github.com/nfcampos) **Created:** 8/11/2024 **Status:** ✅ Merged **Merged:** 8/12/2024 **Merged by:** [@jacoblee93](https://github.com/jacoblee93) **Base:** `main` ← **Head:** `nc/11aug/annotation` --- ### 📝 Commits (5) - [`2d4b9b8`](https://github.com/langchain-ai/langgraphjs/commit/2d4b9b86cdd26d340c480857df251e155791c90c) Add Annotation.Root to make it easier to access State, Update and Node types - [`248e2d5`](https://github.com/langchain-ai/langgraphjs/commit/248e2d590943e5ee5722ad2710d30bc6df1cdc6b) Use declare to prevent overwriting, add test - [`bd4a74a`](https://github.com/langchain-ai/langgraphjs/commit/bd4a74ad31e98cbf4a8dffacd87dcf794886048e) Lint - [`470d42f`](https://github.com/langchain-ai/langgraphjs/commit/470d42ff68a5b96652b70f61293017bb520b1251) Merge branch 'main' of https://github.com/langchain-ai/langgraphjs into nc/11aug/annotation - [`d35d434`](https://github.com/langchain-ai/langgraphjs/commit/d35d43415fbcf18f997ad5e4b2f7e7d61e48c920) Update test ### 📊 Changes **9 files changed** (+239 additions, -97 deletions) <details> <summary>View changed files</summary> 📝 `langgraph/.eslintrc.cjs` (+2 -1) 📝 `langgraph/package.json` (+1 -0) ➕ `langgraph/src/graph/annotation.ts` (+104 -0) 📝 `langgraph/src/graph/index.ts` (+1 -3) 📝 `langgraph/src/graph/message.ts` (+1 -1) 📝 `langgraph/src/graph/state.ts` (+23 -81) 📝 `langgraph/src/tests/graph.test.ts` (+13 -5) 📝 `langgraph/src/tests/pregel.test.ts` (+5 -5) 📝 `yarn.lock` (+89 -1) </details> ### 📄 Description ```ts // useful to generate SQL query const modelLowTemp = new ChatOpenAI({ temperature: 0.1 }); // useful to generate natural language outputs const modelHighTemp = new ChatOpenAI({ temperature: 0.7 }); const annotation = Annotation.Root({ messages: Annotation({ reducer: messagesStateReducer, default: () => [] }), user_query: Annotation<string>(), sql_query: Annotation<string>(), sql_explanation: Annotation<string>(), }); const generatePrompt = new SystemMessage( "You are a helpful data analyst, who generates SQL queries for users based on their questions." ); async function generateSql(state: typeof annotation.State) { const userMessage = new HumanMessage(state.user_query); const messages = [generatePrompt, ...state.messages, userMessage]; const res = await modelLowTemp.invoke(messages); return { sql_query: res.content as string, // update conversation history messages: [userMessage, res], }; } const explainPrompt = new SystemMessage( "You are a helpful data analyst, who explains SQL queries to users." ); async function explainSql(state: typeof annotation.State) { const messages = [explainPrompt, ...state.messages]; const res = await modelHighTemp.invoke(messages); return { sql_explanation: res.content as string, // update conversation history messages: res, }; } const builder = new StateGraph(annotation) .addNode("generate_sql", generateSql) .addNode("explain_sql", explainSql) .addEdge(START, "generate_sql") .addEdge("generate_sql", "explain_sql") .addEdge("explain_sql", END); const graph = builder.compile(); ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-15 19:15:22 -05:00
yindo closed this issue 2026-02-15 19:15:22 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraphjs#649