Skip to content

Commit

Permalink
Merge pull request langchain-ai#146 from langchain-ai/brace/fix-custo…
Browse files Browse the repository at this point in the history
…m-model

fix: Customize model for all requests
  • Loading branch information
bracesproul authored Oct 28, 2024
2 parents d9e935a + a5e1765 commit c11e50d
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ yarn-error.log*
*.tsbuildinfo
next-env.d.ts

credentials.json
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@langchain/anthropic": "^0.3.6",
"@langchain/community": "^0.3.9",
"@langchain/core": "^0.3.14",
"@langchain/google-genai": "^0.1.1",
"@langchain/google-genai": "^0.1.2",
"@langchain/langgraph": "^0.2.18",
"@langchain/langgraph-sdk": "^0.0.17",
"@langchain/openai": "^0.3.11",
Expand Down
8 changes: 4 additions & 4 deletions src/agent/open-canvas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { generateArtifact } from "./nodes/generateArtifact";
import { rewriteArtifact } from "./nodes/rewriteArtifact";
import { rewriteArtifactTheme } from "./nodes/rewriteArtifactTheme";
import { updateArtifact } from "./nodes/updateArtifact";
import { respondToQuery } from "./nodes/respondToQuery";
import { replyToGeneralInput } from "./nodes/replyToGeneralInput";
import { rewriteCodeArtifactTheme } from "./nodes/rewriteCodeArtifactTheme";
import { reflectNode } from "./nodes/reflect";
import { customAction } from "./nodes/customAction";
Expand Down Expand Up @@ -34,7 +34,7 @@ const builder = new StateGraph(OpenCanvasGraphAnnotation)
.addNode("generatePath", generatePath)
.addEdge(START, "generatePath")
// Nodes
.addNode("respondToQuery", respondToQuery)
.addNode("replyToGeneralInput", replyToGeneralInput)
.addNode("rewriteArtifact", rewriteArtifact)
.addNode("rewriteArtifactTheme", rewriteArtifactTheme)
.addNode("rewriteCodeArtifactTheme", rewriteCodeArtifactTheme)
Expand All @@ -50,7 +50,7 @@ const builder = new StateGraph(OpenCanvasGraphAnnotation)
"updateArtifact",
"rewriteArtifactTheme",
"rewriteCodeArtifactTheme",
"respondToQuery",
"replyToGeneralInput",
"generateArtifact",
"rewriteArtifact",
"customAction",
Expand All @@ -65,7 +65,7 @@ const builder = new StateGraph(OpenCanvasGraphAnnotation)
.addEdge("rewriteCodeArtifactTheme", "generateFollowup")
.addEdge("customAction", "generateFollowup")
// End edges
.addEdge("respondToQuery", "cleanState")
.addEdge("replyToGeneralInput", "cleanState")
// Only reflect if an artifact was generated/updated.
.addEdge("generateFollowup", "reflect")
.addEdge("reflect", "cleanState")
Expand Down
2 changes: 1 addition & 1 deletion src/agent/open-canvas/nodes/generatePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const generatePath = async (
const modelWithTool = model.withStructuredOutput(
z.object({
route: z
.enum(["respondToQuery", artifactRoute])
.enum(["replyToGeneralInput", artifactRoute])
.describe("The route to take based on the user's query."),
}),
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { OpenCanvasGraphAnnotation, OpenCanvasGraphReturnType } from "../state";
/**
* Generate responses to questions. Does not generate artifacts.
*/
export const respondToQuery = async (
export const replyToGeneralInput = async (
state: typeof OpenCanvasGraphAnnotation.State,
config: LangGraphRunnableConfig
): Promise<OpenCanvasGraphReturnType> => {
Expand Down
29 changes: 17 additions & 12 deletions src/agent/open-canvas/nodes/rewriteArtifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,25 @@ export const rewriteArtifact = async (
.describe(
"The new title to give the artifact. ONLY update this if the user is making a request which changes the subject/topic of the artifact."
),
programmingLanguage: z
language: z
.enum(
PROGRAMMING_LANGUAGES.map((lang) => lang.language) as [
string,
...string[],
]
)
.optional()
.describe(
"The programming language of the code artifact. ONLY update this if the user is making a request which changes the programming language of the code artifact, or is asking for a code artifact to be generated."
"The language of the code artifact. This should be populated with the programming language if the user is requesting code to be written, or 'other', in all other cases."
),
});
const { modelName, modelProvider } =
getModelNameAndProviderFromConfig(config);
const model = await initChatModel(modelName, {
temperature: 0,
modelProvider,
});
const toolCallingModel = model
const toolCallingModel = (
await initChatModel(modelName, {
temperature: 0,
modelProvider,
})
)
.bindTools(
[
{
Expand All @@ -71,6 +71,15 @@ export const rewriteArtifact = async (
)
.withConfig({ runName: "optionally_update_artifact_meta" });

const smallModelWithConfig = (
await initChatModel(modelName, {
temperature: 0,
modelProvider,
})
).withConfig({
runName: "rewrite_artifact_model_call",
});

const store = ensureStoreInConfig(config);
const assistantId = config.configurable?.assistant_id;
if (!assistantId) {
Expand Down Expand Up @@ -136,10 +145,6 @@ export const rewriteArtifact = async (
: ""
);

const smallModelWithConfig = model.withConfig({
runName: "rewrite_artifact_model_call",
});

const newArtifactResponse = await smallModelWithConfig.invoke([
{ role: "system", content: formattedPrompt },
recentHumanMessage,
Expand Down
4 changes: 2 additions & 2 deletions src/agent/open-canvas/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@ Rules and guidelines:
export const ROUTE_QUERY_OPTIONS_HAS_ARTIFACTS = `
- 'rewriteArtifact': The user has requested some sort of change, or revision to the artifact, or to write a completely new artifact independent of the current artifact. Use their recent message and the currently selected artifact (if any) to determine what to do. You should ONLY select this if the user has clearly requested a change to the artifact, otherwise you should lean towards either generating a new artifact or responding to their query.
It is very important you do not edit the artifact unless clearly requested by the user.
- 'respondToQuery': The user has asked a question, or has submitted a general message which requires a response, but does not require updating or generating an entirely new artifact.`;
- 'replyToGeneralInput': The user submitted a general input which does not require making an update, edit or generating a new artifact. This should ONLY be used if you are ABSOLUTELY sure the user does NOT want to make an edit, update or generate a new artifact.`;

export const ROUTE_QUERY_OPTIONS_NO_ARTIFACTS = `
- 'generateArtifact': The user has inputted a request which requires generating an artifact.
- 'respondToQuery': The user has asked a question, or has submitted a general message which requires a response, but does not require generating a artifact.`;
- 'replyToGeneralInput': The user submitted a general input which does not require making an update, edit or generating a new artifact. This should ONLY be used if you are ABSOLUTELY sure the user does NOT want to make an edit, update or generate a new artifact.`;

export const CURRENT_ARTIFACT_PROMPT = `This artifact is the one the user is currently viewing.
<artifact>
Expand Down
2 changes: 1 addition & 1 deletion src/components/artifacts/ArtifactRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export function ArtifactRenderer(props: ArtifactRendererProps) {
? getArtifactContent(props.artifact)
: undefined;

if (!props.artifact || (!currentArtifactContent && props.isStreaming)) {
if (!props.artifact && props.isStreaming) {
return <ArtifactLoading />;
}

Expand Down
18 changes: 11 additions & 7 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const LANGGRAPH_API_URL =
process.env.LANGGRAPH_API_URL ?? "http://localhost:52375";
process.env.LANGGRAPH_API_URL ?? "http://localhost:53529";
// v2 is tied to the 'open-canvas-prod' deployment.
export const ASSISTANT_ID_COOKIE = "oc_assistant_id_v2";
// export const ASSISTANT_ID_COOKIE = "oc_assistant_id";
Expand Down Expand Up @@ -32,23 +32,27 @@ export const ANTHROPIC_MODELS = [
name: "claude-3-haiku-20240307",
label: "Claude 3 Haiku",
},
// {
// name: "claude-3-5-sonnet-20240620",
// label: "Claude 3.5 Sonnet",
// },
];
export const FIREWORKS_MODELS = [
{
name: "accounts/fireworks/models/llama-v3p1-70b-instruct",
label: "Fireworks Llama 70B",
},
{
name: "accounts/fireworks/models/llama-v3p1-8b-instruct",
label: "Fireworks Llama 8B",
name: "accounts/fireworks/models/llama-v3p1-405b-instruct",
label: "Fireworks Llama 405B",
},
];

export const GEMINI_MODELS = [
{
name: "gemini-1.5-flash",
label: "Gemini 1.5 Flash",
},
// {
// name: "gemini-1.5-flash",
// label: "Gemini 1.5 Flash",
// },
];
export const DEFAULT_MODEL_NAME: ALL_MODEL_NAMES = "gpt-4o-mini";
export type OPENAI_MODEL_NAMES = (typeof OPENAI_MODELS)[number]["name"];
Expand Down
9 changes: 4 additions & 5 deletions src/hooks/use-graph/useGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export function useGraph(useGraphInput: UseGraphInput) {
if (chunk.data.event === "on_chat_model_stream") {
// These are generating new messages to insert to the chat window.
if (
["generateFollowup", "respondToQuery"].includes(
["generateFollowup", "replyToGeneralInput"].includes(
chunk.data.metadata.langgraph_node
)
) {
Expand Down Expand Up @@ -534,7 +534,6 @@ export function useGraph(useGraphInput: UseGraphInput) {
chunk.data.name === "rewrite_artifact_model_call" &&
rewriteArtifactMeta
) {
console.log("IN THIS NODE");
if (!artifact) {
toast({
title: "Error",
Expand All @@ -552,12 +551,12 @@ export function useGraph(useGraphInput: UseGraphInput) {
if (
!artifactLanguage &&
rewriteArtifactMeta.type === "code" &&
rewriteArtifactMeta.programmingLanguage
rewriteArtifactMeta.language
) {
// If the type is `code` we should have a programming language populated
// in the rewriteArtifactMeta and can use that.
artifactLanguage =
rewriteArtifactMeta.programmingLanguage as ProgrammingLanguageOptions;
rewriteArtifactMeta.language as ProgrammingLanguageOptions;
} else if (!artifactLanguage) {
artifactLanguage =
(prevCurrentContent?.title as ProgrammingLanguageOptions) ??
Expand Down Expand Up @@ -652,7 +651,7 @@ export function useGraph(useGraphInput: UseGraphInput) {
rewriteArtifactMeta: {
type: artifactType,
title: prevCurrentContent.title,
programmingLanguage: artifactLanguage,
language: artifactLanguage,
},
prevCurrentContent,
newArtifactIndex,
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,10 @@ export type RewriteArtifactMetaToolResponse =
| {
type: "text";
title?: string;
language: ProgrammingLanguageOptions;
}
| {
type: "code";
title: string;
programmingLanguage: string;
language: ProgrammingLanguageOptions;
};
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,10 @@
zod "^3.22.4"
zod-to-json-schema "^3.22.3"

"@langchain/google-genai@^0.1.1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@langchain/google-genai/-/google-genai-0.1.1.tgz#85528c64b518a285ec7d22a8793296f27cec27ff"
integrity sha512-uuZJXWXTTTx8FGmhr/utkpiKgOZU4EXvkPIq+opLhpzCN7hYQXXMAIykjZPhAyZjazga1jxH8YOiGonM/3jhDA==
"@langchain/google-genai@^0.1.2":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@langchain/google-genai/-/google-genai-0.1.2.tgz#23387d7027b4a787b542fc5cfb7a2469e9f4c845"
integrity sha512-oePFjTurY4O2tJiU4cJ3Wu9L+JGVwYib2LovI+SxGJImVyVlQQ1HV2SVek03vqG4d0kiX0XLQTEC7mJ7EBySkg==
dependencies:
"@google/generative-ai" "^0.7.0"
zod-to-json-schema "^3.22.4"
Expand Down

0 comments on commit c11e50d

Please sign in to comment.