mirror of
https://github.com/langchain-ai/retrieval-agent-template-js.git
synced 2026-07-01 15:24:18 -04:00
Initial commit
This commit is contained in:
+19
@@ -0,0 +1,19 @@
|
||||
index.cjs
|
||||
index.js
|
||||
index.d.ts
|
||||
node_modules
|
||||
dist
|
||||
.yarn/*
|
||||
!.yarn/patches
|
||||
!.yarn/plugins
|
||||
!.yarn/releases
|
||||
!.yarn/sdks
|
||||
!.yarn/versions
|
||||
|
||||
.turbo
|
||||
**/.turbo
|
||||
**/.eslintcache
|
||||
|
||||
.env
|
||||
.ipynb_checkpoints
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 LangChain
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,25 @@
|
||||
# LangGraph Starter Template
|
||||
|
||||
This repo offers the basic code structure to get started building LangGraph workflows in JavaScript within LangGraph studio.
|
||||
|
||||
|
||||
## Repo Structure
|
||||
|
||||
```txt
|
||||
├── LICENSE
|
||||
├── README.md
|
||||
├── jest.config.js # Test configuration
|
||||
├── .env # Define environment variables. Can copy over from .env.example
|
||||
├── langgraph.json # LangGraph studio configuration
|
||||
├── my-app
|
||||
│ ├── graph.ts # Graph / workflow definition
|
||||
│ └── index.ts
|
||||
├── package.json # Define the project dependencies
|
||||
├── tests # Add any tests you'd like here
|
||||
│ ├── integration
|
||||
│ │ └── graph.int.test.ts
|
||||
│ └── unit
|
||||
│ └── graph.test.ts
|
||||
├── tsconfig.json # Typescript-specific configuration
|
||||
└── yarn.lock
|
||||
```
|
||||
@@ -0,0 +1,15 @@
|
||||
export default {
|
||||
preset: "ts-jest/presets/default-esm",
|
||||
moduleNameMapper: {
|
||||
"^(\\.{1,2}/.*)\\.js$": "$1",
|
||||
},
|
||||
transform: {
|
||||
"^.+\\.tsx?$": [
|
||||
"ts-jest",
|
||||
{
|
||||
useESM: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
extensionsToTreatAsEsm: [".ts"],
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"node_version": "v20.16.0",
|
||||
"dockerfile_lines": [],
|
||||
"dependencies": ["."],
|
||||
"graphs": {
|
||||
"agent": "./graph.ts:graph"
|
||||
},
|
||||
"env": ".env"
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Empty LangGraph Template
|
||||
*
|
||||
* Make this code your own!
|
||||
*/
|
||||
|
||||
import { StateGraph, Annotation } from "@langchain/langgraph";
|
||||
|
||||
/**
|
||||
* The State defines three things:
|
||||
* 1. The structure of the graph's state (which "channels" are available to read/write)
|
||||
* 2. The default values for the state's channels
|
||||
* 3. The reducers for the state's channels. Reducers are functions that determine how to apply updates to the state.
|
||||
* See [Reducers](https://langchain-ai.github.io/langgraphjs/concepts/low_level/#reducers) for more information.
|
||||
*/
|
||||
const State = Annotation.Root({
|
||||
// Example of a channel that will be replaced any time
|
||||
// it is written to
|
||||
input: Annotation<string>(),
|
||||
// Example of a channel that is "append-only"
|
||||
stringList: Annotation<string[]>({
|
||||
reducer: (x: string[], y: string[]) => x.concat(y),
|
||||
default: () => [],
|
||||
}),
|
||||
});
|
||||
|
||||
type StateT = typeof State.State;
|
||||
|
||||
// Define nodes, these do the work:
|
||||
|
||||
const callModel = async (state: StateT) => {
|
||||
// Do some work... (e.g. call an LLM)
|
||||
return { stringList: [state.input, "b", "c"] };
|
||||
};
|
||||
|
||||
// Define conditional edge logic:
|
||||
|
||||
/**
|
||||
* Routing function: Determines whether to continue research or end the workflow.
|
||||
* This function decides if the gathered information is satisfactory or if more research is needed.
|
||||
*
|
||||
* @param state - The current state of the research workflow
|
||||
* @returns Either "callModel" to continue research or END to finish the workflow
|
||||
*/
|
||||
export const _route = (state: StateT): "__end__" | "callModel" => {
|
||||
if (state.stringList.length > 0) {
|
||||
return "__end__";
|
||||
}
|
||||
// Loop back
|
||||
return "callModel";
|
||||
};
|
||||
|
||||
// Finally, create the graph itself.
|
||||
const workflow = new StateGraph(State)
|
||||
// Add the nodes to do the work.
|
||||
// Chaining the nodes together in this way
|
||||
// updates the types of the StateGraph instance
|
||||
// so you have static type checking when it comes time
|
||||
// to add the edges.
|
||||
.addNode("callModel", callModel)
|
||||
// Regular edges mean "always transition to node B after node A is done"
|
||||
// The "__start__" and "__end__" nodes are "virtual" nodes that are always present
|
||||
// and represent the beginning and end of the workflow.
|
||||
.addEdge("__start__", "callModel")
|
||||
.addConditionalEdges("callModel", _route);
|
||||
|
||||
export const graph: any = workflow.compile();
|
||||
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "example-graph",
|
||||
"version": "0.0.1",
|
||||
"description": "A starter template for creating a LangGraph workflow.",
|
||||
"main": "graph.ts",
|
||||
"author": "Your Name",
|
||||
"license": "MIT",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"clean": "rm -rf dist",
|
||||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --testPathPattern=\\.test\\.ts$ --testPathIgnorePatterns=\\.int\\.test\\.ts$",
|
||||
"test:int": "node --experimental-vm-modules node_modules/jest/bin/jest.js --testPathPattern=\\.int\\.test\\.ts$",
|
||||
"format": "prettier --write ."
|
||||
},
|
||||
"dependencies": {
|
||||
"@langchain/langgraph": "^0.0.34",
|
||||
"ts-node": "^10.9.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@langchain/openai": "^0.2.7",
|
||||
"@tsconfig/recommended": "^1.0.7",
|
||||
"@types/jest": "^29.5.0",
|
||||
"jest": "^29.7.0",
|
||||
"prettier": "^3.3.3",
|
||||
"ts-jest": "^29.1.0",
|
||||
"typescript": "^5.3.3"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { describe, it, expect } from "@jest/globals";
|
||||
import { graph } from "../../my-app/graph.js";
|
||||
describe("Researcher", () => {
|
||||
it("Simple runthrough", async () => {
|
||||
const res = await graph.invoke({ input: "ExampleInput" });
|
||||
expect(res.stringList).toEqual(["ExampleInput", "b", "c"]);
|
||||
}, 100_000);
|
||||
});
|
||||
@@ -0,0 +1,8 @@
|
||||
import { describe, it, expect } from "@jest/globals";
|
||||
import { _route } from "../../my-app/graph.js";
|
||||
describe("Routers", () => {
|
||||
it("Test route", async () => {
|
||||
const res = _route({ input: "ExampleInput", stringList: [] });
|
||||
expect(res).toEqual("callModel");
|
||||
}, 100_000);
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"extends": "@tsconfig/recommended",
|
||||
"compilerOptions": {
|
||||
"target": "ES2021",
|
||||
"lib": ["ES2021", "ES2022.Object", "DOM"],
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "nodenext",
|
||||
"esModuleInterop": true,
|
||||
"declaration": true,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"useDefineForClassFields": true,
|
||||
"strictPropertyInitialization": false,
|
||||
"allowJs": true,
|
||||
"strict": true,
|
||||
"strictFunctionTypes": false,
|
||||
"outDir": "dist",
|
||||
"types": ["jest", "node"],
|
||||
"resolveJsonModule": true
|
||||
},
|
||||
"include": ["**/*.ts", "**/*.js"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
Reference in New Issue
Block a user