Skip to content

Commit

Permalink
cr
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Oct 10, 2024
1 parent e511e6d commit b60252a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/agent/open-canvas/nodes/generatePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { z } from "zod";
/**
* Routes to the proper node in the graph based on the user's query.
*/
export const generatePath = async (state: typeof OpenCanvasGraphAnnotation.State) => {
export const generatePath = async (
state: typeof OpenCanvasGraphAnnotation.State
) => {
if (state.highlighted) {
return {
next: "updateArtifact",
Expand Down
4 changes: 3 additions & 1 deletion src/agent/open-canvas/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,6 @@ export const OpenCanvasGraphAnnotation = Annotation.Root({
fixBugs: Annotation<boolean | undefined>,
});

export type OpenCanvasGraphReturnType = Partial<typeof OpenCanvasGraphAnnotation.State>;
export type OpenCanvasGraphReturnType = Partial<
typeof OpenCanvasGraphAnnotation.State
>;
43 changes: 27 additions & 16 deletions src/agent/reflection/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ChatAnthropic } from "@langchain/anthropic";
import { type LangGraphRunnableConfig, type BaseStore, StateGraph, START } from "@langchain/langgraph";
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";
Expand All @@ -23,45 +28,51 @@ const formatMemories = (memories: Memory): string => {
- ${memories.content.join("\n- ")}
</user-facts>`;


return styleString + "\n\n" + contentString;
}
};

export const reflect = async (
state: typeof ReflectionGraphAnnotation.State,
config: LangGraphRunnableConfig,
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");``
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 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.")
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 formattedSystemPrompt = REFLECT_SYSTEM_PROMPT.replace(
"{artifact}",
state.artifact?.content ?? "No artifact found."
).replace("{reflections}", memoriesAsString);

const result = await model.invoke([
{
role: "system",
content: formattedSystemPrompt
content: formattedSystemPrompt,
},
...state.messages,
]);
Expand All @@ -74,10 +85,10 @@ export const reflect = async (
await store.put(memoryNamespace, memoryKey, newMemories);

return {};
}
};

const builder = new StateGraph(ReflectionGraphAnnotation)
.addNode("reflect", reflect)
.addEdge(START, "reflect");

export const graph = builder.compile();
export const graph = builder.compile();
2 changes: 1 addition & 1 deletion src/agent/reflection/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ Your job is to take all of the context and existing reflections and re-generate
- 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.`
Finally, use the 'generate_reflections' tool to generate the new, full list of reflections.`;
6 changes: 4 additions & 2 deletions src/agent/reflection/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export const ReflectionGraphAnnotation = Annotation.Root({
/**
* The artifact to reflect on.
*/
artifact: Annotation<Artifact | undefined>
artifact: Annotation<Artifact | undefined>,
});

export type ReflectionGraphReturnType = Partial<typeof ReflectionGraphAnnotation.State>;
export type ReflectionGraphReturnType = Partial<
typeof ReflectionGraphAnnotation.State
>;
7 changes: 1 addition & 6 deletions src/components/artifacts/actions_toolbar/text/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { useEffect, useRef, useState } from "react";
import {
Languages,
BookOpen,
SlidersVertical,
SmilePlus,
} from "lucide-react";
import { Languages, BookOpen, SlidersVertical, SmilePlus } from "lucide-react";
import { cn } from "@/lib/utils";
import { ReadingLevelOptions } from "./ReadingLevelOptions";
import { TranslateOptions } from "./TranslateOptions";
Expand Down
3 changes: 1 addition & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export type ReadingLevelOptions =
| "college"
| "phd";


export interface Memory {
/**
* Style rules to follow for generating content.
Expand All @@ -77,4 +76,4 @@ export interface Memory {
* Key content to remember about the user when generating content.
*/
content: string[];
}
}

0 comments on commit b60252a

Please sign in to comment.