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 2f079d5 commit 0d8eb36
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@codemirror/lang-php": "^6.0.1",
"@codemirror/lang-python": "^6.1.6",
"@langchain/anthropic": "^0.3.3",
"@langchain/core": "^0.3.3",
"@langchain/core": "^0.3.9",
"@langchain/langgraph": "^0.2.10",
"@langchain/langgraph-sdk": "^0.0.14",
"@langchain/openai": "^0.3.5",
Expand Down
3 changes: 2 additions & 1 deletion src/agent/open-canvas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { updateArtifact } from "./nodes/updateArtifact";
import { respondToQuery } from "./nodes/respondToQuery";
import { rewriteCodeArtifactTheme } from "./nodes/rewriteCodeArtifactTheme";
import { reflect } from "../reflection";
import { reflectNode } from "./nodes/reflect";

const defaultInputs: Omit<
typeof OpenCanvasGraphAnnotation.State,
Expand Down Expand Up @@ -58,7 +59,7 @@ const builder = new StateGraph(OpenCanvasGraphAnnotation)
.addNode("generateArtifact", generateArtifact)
.addNode("generateFollowup", generateFollowup)
.addNode("cleanState", cleanState)
.addNode("reflect", reflect)
.addNode("reflect", reflectNode)
// Edges
.addEdge("generateArtifact", "generateFollowup")
.addEdge("updateArtifact", "generateFollowup")
Expand Down
1 change: 1 addition & 0 deletions src/agent/open-canvas/nodes/generateFollowup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const generateFollowup = async (
recentArtifact.content
).replace("{reflections}", memoriesAsString);

// TODO: Include the chat history as well.
const response = await smallModel.invoke([
{ role: "user", content: formattedPrompt },
]);
Expand Down
10 changes: 6 additions & 4 deletions src/agent/open-canvas/nodes/reflect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { Client } from "@langchain/langgraph-sdk";
import { OpenCanvasGraphAnnotation } from "../state";
import { LangGraphRunnableConfig } from "@langchain/langgraph";

export const reflect = async (
export const reflectNode = async (
state: typeof OpenCanvasGraphAnnotation.State,
config: LangGraphRunnableConfig
) => {
const langGraphClient = new Client();

const langGraphClient = new Client({
apiUrl: `http://localhost:${process.env.PORT}`,
});
console.log("reflectNode", state);
const selectedArtifact = state.selectedArtifactId
? state.artifacts.find((art) => art.id === state.selectedArtifactId)
: state.artifacts[state.artifacts.length - 1];
Expand All @@ -19,7 +21,7 @@ export const reflect = async (
configurable: {
// Ensure we pass in the current graph's assistant ID as this is
// how we fetch & store the memories.
assistant_id: config.configurable?.assistant_id,
open_canvas_assistant_id: config.configurable?.assistant_id,
},
};

Expand Down
6 changes: 0 additions & 6 deletions src/agent/open-canvas/nodes/rewriteArtifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,5 @@ export const rewriteArtifact = async (

return {
artifacts: [newArtifact],
selectedArtifactId: undefined,
highlighted: undefined,
language: undefined,
artifactLength: undefined,
regenerateWithEmojis: undefined,
readingLevel: undefined,
};
};
6 changes: 0 additions & 6 deletions src/agent/open-canvas/nodes/rewriteArtifactTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,5 @@ export const rewriteArtifactTheme = async (

return {
artifacts: [newArtifact],
selectedArtifactId: undefined,
highlighted: undefined,
language: undefined,
artifactLength: undefined,
regenerateWithEmojis: undefined,
readingLevel: undefined,
};
};
6 changes: 0 additions & 6 deletions src/agent/open-canvas/nodes/rewriteCodeArtifactTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,5 @@ export const rewriteCodeArtifactTheme = async (

return {
artifacts: [newArtifact],
selectedArtifactId: undefined,
highlighted: undefined,
language: undefined,
artifactLength: undefined,
regenerateWithEmojis: undefined,
readingLevel: undefined,
};
};
18 changes: 14 additions & 4 deletions src/agent/reflection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "@langchain/langgraph";
import { ReflectionGraphAnnotation, ReflectionGraphReturnType } from "./state";
import { Reflections } from "../../types";
import { REFLECT_SYSTEM_PROMPT } from "./prompts";
import { REFLECT_SYSTEM_PROMPT, REFLECT_USER_PROMPT } from "./prompts";
import { z } from "zod";
import { ensureStoreInConfig, formatReflections } from "../utils";

Expand All @@ -15,9 +15,9 @@ export const reflect = async (
config: LangGraphRunnableConfig
): Promise<ReflectionGraphReturnType> => {
const store = ensureStoreInConfig(config);
const assistantId = config.configurable?.assistant_id;
const assistantId = config.configurable?.open_canvas_assistant_id;
if (!assistantId) {
throw new Error("`assistant_id` not found in configurable");
throw new Error("`open_canvas_assistant_id` not found in configurable");
}
const memoryNamespace = ["memories", assistantId];
const memoryKey = "reflection";
Expand Down Expand Up @@ -48,12 +48,22 @@ export const reflect = async (
state.artifact?.content ?? "No artifact found."
).replace("{reflections}", memoriesAsString);

const formattedUserPrompt = REFLECT_USER_PROMPT.replace(
"{conversation}",
state.messages
.map((msg) => `<${msg.getType()}>\n${msg.content}\n</${msg.getType()}>`)
.join("\n\n")
);

const result = await model.invoke([
{
role: "system",
content: formattedSystemPrompt,
},
...state.messages,
{
role: "user",
content: formattedUserPrompt,
},
]);

const newMemories = {
Expand Down
4 changes: 4 additions & 0 deletions src/agent/reflection/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ Your job is to take all of the context and existing reflections and re-generate
</system-guidelines>
Finally, use the 'generate_reflections' tool to generate the new, full list of reflections.`;

export const REFLECT_USER_PROMPT = `Here is my conversation:
{conversation}`;
2 changes: 1 addition & 1 deletion src/app/api/[..._path]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function handleRequest(req: NextRequest, method: string) {
options.body = await req.text();
}

const apiUrl = process.env.LANGGRAPH_API_URL ?? "http://localhost:50014";
const apiUrl = process.env.LANGGRAPH_API_URL ?? "http://localhost:50930";
const res = await fetch(`${apiUrl}/${path}${queryString}`, options);

const headers = new Headers({
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { parsePartialJson } from "@langchain/core/output_parsers";
import { useRuns } from "./useRuns";
import { reverseCleanContent } from "@/lib/normalize_string";
import { getCookie } from "@/lib/cookies";
import { getCookie, setCookie } from "@/lib/cookies";
// import { DEFAULT_ARTIFACTS, DEFAULT_MESSAGES } from "@/lib/dummy";

interface ArtifactToolResponse {
Expand Down Expand Up @@ -97,6 +97,7 @@ export function useGraph() {
graphId: "agent",
});
setAssistantId(assistant.assistant_id);
setCookie("oc_assistant_id", assistant.assistant_id);
};

const streamMessage = async (params: GraphInput) => {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,10 @@
zod "^3.22.4"
zod-to-json-schema "^3.22.4"

"@langchain/core@^0.3.3":
version "0.3.3"
resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.3.3.tgz#af12fd767ff2fcedb0a71bd79e6588d7dd52b6b6"
integrity sha512-WAtkmhbdl2T41qzimTzhb3pXCHQxO4onqxzPxgdf3KftQdTwLq0YYBDhozRMZLNAd/+cfH0ymZGaZSsnc9Ogsg==
"@langchain/core@^0.3.9":
version "0.3.9"
resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.3.9.tgz#1fa75be84a22d951946277bc4ad3ea656c77fafc"
integrity sha512-Rttr9FuFwU+CWIoEyuLqQUPYg+3pKL1YpDgo3nvoDVhinoHqwGQ7aNGzZ/Sf+qASMi76sPSLm+75pHMJwwOiWg==
dependencies:
ansi-styles "^5.0.0"
camelcase "6"
Expand Down

0 comments on commit 0d8eb36

Please sign in to comment.