forked from langchain-ai/open-canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d38cc9
commit e511e6d
Showing
20 changed files
with
181 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
src/agent/nodes/generateArtifact.ts → ...ent/open-canvas/nodes/generateArtifact.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
src/agent/nodes/generateFollowup.ts → ...ent/open-canvas/nodes/generateFollowup.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/agent/nodes/generatePath.ts → src/agent/open-canvas/nodes/generatePath.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
src/agent/nodes/respondToQuery.ts → ...agent/open-canvas/nodes/respondToQuery.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
src/agent/nodes/rewriteArtifact.ts → ...gent/open-canvas/nodes/rewriteArtifact.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
src/agent/nodes/updateArtifact.ts → ...agent/open-canvas/nodes/updateArtifact.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { ChatAnthropic } from "@langchain/anthropic"; | ||
import { type LangGraphRunnableConfig, type BaseStore, StateGraph, START } from "@langchain/langgraph"; | ||
import { ReflectionGraphAnnotation, ReflectionGraphReturnType } from "./state"; | ||
import { Memory } from "../../types"; | ||
import { REFLECT_SYSTEM_PROMPT } from "./prompts"; | ||
import { z } from "zod"; | ||
|
||
const ensureStoreFromConfig = (config: LangGraphRunnableConfig): BaseStore => { | ||
if (!config.store) { | ||
throw new Error("`store` not found in config"); | ||
} | ||
|
||
return config.store; | ||
}; | ||
|
||
const formatMemories = (memories: Memory): string => { | ||
const styleString = `The following is a list of style guidelines previously generated by you: | ||
<style-guidelines> | ||
- ${memories.styleRules.join("\n- ")} | ||
</style-guidelines>`; | ||
const contentString = `The following is a list of memories/facts you previously generated about the user: | ||
<user-facts> | ||
- ${memories.content.join("\n- ")} | ||
</user-facts>`; | ||
|
||
|
||
return styleString + "\n\n" + contentString; | ||
} | ||
|
||
export const reflect = async ( | ||
state: typeof ReflectionGraphAnnotation.State, | ||
config: LangGraphRunnableConfig, | ||
): Promise<ReflectionGraphReturnType> => { | ||
const store = ensureStoreFromConfig(config); | ||
const assistantId = config.configurable?.assistant_id; | ||
if (!assistantId) { | ||
throw new Error("`assistant_id` not found in configurable");`` | ||
} | ||
const memoryNamespace = ["memories", assistantId]; | ||
const memoryKey = "reflection"; | ||
const memories = await store.get(memoryNamespace, memoryKey); | ||
|
||
const memoriesAsString = memories?.value ? formatMemories(memories.value as Memory) : "No memories found."; | ||
|
||
const generateReflectionsSchema = z.object({ | ||
styleRules: z.array(z.string()).describe("The complete new list of style rules and guidelines."), | ||
content: z.array(z.string()).describe("The complete new list of memories/facts about the user.") | ||
}); | ||
|
||
const model = new ChatAnthropic({ | ||
model: "claude-3-5-sonnet-20240620", | ||
temperature: 0, | ||
}).withStructuredOutput(generateReflectionsSchema, { | ||
name: "generate_reflections", | ||
}) | ||
|
||
const formattedSystemPrompt = REFLECT_SYSTEM_PROMPT | ||
.replace("{artifact}", state.artifact?.content ?? "No artifact found.") | ||
.replace("{reflections}", memoriesAsString); | ||
|
||
const result = await model.invoke([ | ||
{ | ||
role: "system", | ||
content: formattedSystemPrompt | ||
}, | ||
...state.messages, | ||
]); | ||
|
||
const newMemories = { | ||
styleRules: result.styleRules, | ||
content: result.content, | ||
}; | ||
|
||
await store.put(memoryNamespace, memoryKey, newMemories); | ||
|
||
return {}; | ||
} | ||
|
||
const builder = new StateGraph(ReflectionGraphAnnotation) | ||
.addNode("reflect", reflect) | ||
.addEdge(START, "reflect"); | ||
|
||
export const graph = builder.compile(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export const REFLECT_SYSTEM_PROMPT = `You are an expert assistant, and writer. You are tasked with reflecting on the following conversation between a user and an AI assistant. | ||
You are also provided with an 'artifact' the user and assistant worked together on to write. Artifacts can be code, creative writing, emails, or any other form of written content. | ||
<artifact> | ||
{artifact} | ||
</artifact> | ||
You have also previously generated the following reflections about the user. Your reflections are broken down into two categories: | ||
1. Style Guidelines: These are the style guidelines you have generated for the user. Style guidelines can be anything from writing style, to code style, to design style. | ||
They should be general, and apply to the all the users work, including the conversation and artifact generated. | ||
2. Content: These are general memories, facts, and insights you generate about the user. These can be anything from the users interests, to their goals, to their personality traits. | ||
Ensure you think carefully about what goes in here, as the assistant will use these when generating future responses or artifacts for the user. | ||
<reflections> | ||
{reflections} | ||
</reflections> | ||
Your job is to take all of the context and existing reflections and re-generate all. Use these guidelines when generating the new set of reflections: | ||
<system-guidelines> | ||
- Ensure your reflections are relevant to the conversation and artifact. | ||
- Remove duplicate reflections, or combine multiple reflections into one if they are duplicating content. | ||
- Do not remove reflections unless the conversation/artifact clearly demonstrates they should no longer be included. | ||
This does NOT mean remove reflections if you see no evidence of them in the conversation/artifact, but instead remove them if the user indicates they are no longer relevant. | ||
- Think of the deeper meaning behind a users messages, or artifact to generate content reflections that will be relevant and useful for future interactions. | ||
- Keep the rules you list high signal-to-noise - don't include unnecessary reflections, but make sure the ones you do add are descriptive. | ||
- Your reflections should be very descriptive and detailed, ensuring they are clear and will not be misinterpreted. | ||
</system-guidelines> | ||
Finally, use the 'generate_reflections' tool to generate the new, full list of reflections.` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Artifact } from "@/types"; | ||
import { Annotation, MessagesAnnotation } from "@langchain/langgraph"; | ||
|
||
export const ReflectionGraphAnnotation = Annotation.Root({ | ||
/** | ||
* The chat history to reflect on. | ||
*/ | ||
...MessagesAnnotation.spec, | ||
/** | ||
* The artifact to reflect on. | ||
*/ | ||
artifact: Annotation<Artifact | undefined> | ||
}); | ||
|
||
export type ReflectionGraphReturnType = Partial<typeof ReflectionGraphAnnotation.State>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters