Skip to content

Commit

Permalink
Merge pull request #88 from langchain-ai/brace/revert-memory
Browse files Browse the repository at this point in the history
chore: Revert what memory graph is called
  • Loading branch information
bracesproul authored Jan 27, 2025
2 parents 3e7443d + 3da350f commit 6330308
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
6 changes: 2 additions & 4 deletions langgraph.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"node_version": "20",
"python_version": "3.12",
"graphs": {
"ingest_data": "./src/agents/ingest-data/ingest-data-graph.ts:graph",
"generate_post": "./src/agents/generate-post/generate-post-graph.ts:generatePostGraph",
Expand All @@ -11,10 +10,9 @@
"verify_reddit_post": "./src/agents/verify-reddit-post/verify-reddit-post-graph.ts:verifyRedditPostGraph",
"verify_tweet": "./src/agents/verify-tweet/verify-tweet-graph.ts:verifyTweetGraph",
"supervisor": "./src/agents/supervisor/supervisor-graph.ts:supervisorGraph",
"generate_report": "./src/agents/generate-report/index.ts:generateReportGraph",
"memory": "./memory-v2/memory_v2/graph.py:graph"
"generate_report": "./src/agents/generate-report/index.ts:generateReportGraph"
},
"env": ".env",
"dependencies": [".", "./memory-v2"],
"dependencies": ["."],
"dockerfile_lines": ["RUN npx -y [email protected] install --with-deps"]
}
12 changes: 9 additions & 3 deletions src/agents/generate-post/nodes/rewrite-post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Listen to your boss closely, and make the necessary changes to the post. You sho

interface RunReflectionsArgs {
originalPost: string;
newPost: string;
userResponse: string;
}

Expand All @@ -30,17 +31,21 @@ interface RunReflectionsArgs {
*/
async function runReflections({
originalPost,
newPost,
userResponse,
}: RunReflectionsArgs) {
const client = new Client({
apiUrl: `http://localhost:${process.env.PORT}`,
});

const thread = await client.threads.create();
await client.runs.create(thread.thread_id, "memory", {
await client.runs.create(thread.thread_id, "reflection", {
input: {
original_post: originalPost,
user_response: userResponse,
// original_post: originalPost,
// user_response: userResponse,
originalPost,
newPost,
userResponse,
},
});
}
Expand Down Expand Up @@ -85,6 +90,7 @@ export async function rewritePost(

await runReflections({
originalPost: state.post,
newPost: revisePostResponse.content as string,
userResponse: state.userResponse,
});

Expand Down
17 changes: 8 additions & 9 deletions src/agents/reflection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import {
import { z } from "zod";
import { ChatAnthropic } from "@langchain/anthropic";
import {
getReflections,
putReflections,
RULESET_KEY,
getReflectionsPrompt,
putReflectionsPrompt,
} from "../../utils/reflections.js";
import { REFLECTION_PROMPT, UPDATE_RULES_PROMPT } from "./prompts.js";

Expand All @@ -20,7 +19,7 @@ const newRuleSchema = z.object({

const updateRulesetSchema = z
.object({
updatedRuleset: z.array(z.string()).describe("The updated ruleset."),
updatedRuleset: z.string().describe("The full updated ruleset."),
})
.describe("The updated ruleset.");

Expand Down Expand Up @@ -86,11 +85,11 @@ async function reflection(
return {};
}

const existingRules = await getReflections(config);
const existingRules = await getReflectionsPrompt(config);

if (!existingRules?.value?.[RULESET_KEY]?.length) {
if (!existingRules?.length) {
// No rules exist yet. Create and return early.
await putReflections(config, [newRule]);
await putReflectionsPrompt(config, newRule);
return {};
}

Expand All @@ -99,7 +98,7 @@ async function reflection(
});
const updateRulesetPrompt = UPDATE_RULES_PROMPT.replace(
"{EXISTING_RULES}",
existingRules?.value[RULESET_KEY].join("\n") || "",
existingRules,
).replace("{NEW_RULE}", newRule);
const updateRulesetResult = await updateRulesetModel.invoke([
{
Expand All @@ -108,7 +107,7 @@ async function reflection(
},
]);

await putReflections(config, updateRulesetResult.updatedRuleset);
await putReflectionsPrompt(config, updateRulesetResult.updatedRuleset);

return {};
}
Expand Down
20 changes: 20 additions & 0 deletions src/utils/reflections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ export async function getReflectionsPrompt(
return reflections?.value?.[PROMPT_KEY] || "";
}

/**
* Stores reflection rules in the store
* @param {LangGraphRunnableConfig} config - Configuration object containing the store
* @param {string} reflections - The reflection rules to store
* @throws {Error} When no store is provided in the config
* @returns {Promise<void>}
*/
export async function putReflectionsPrompt(
config: LangGraphRunnableConfig,
reflections: string,
): Promise<void> {
const { store } = config;
if (!store) {
throw new Error("No store provided");
}
await store.put(NAMESPACE, KEY, {
[PROMPT_KEY]: reflections,
});
}

/**
* Retrieves reflection rules from the store
* @param {LangGraphRunnableConfig} config - Configuration object containing the store
Expand Down

0 comments on commit 6330308

Please sign in to comment.