Skip to content

Commit

Permalink
fix wsa
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Apr 4, 2024
1 parent 56aede7 commit fef15c8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
1 change: 0 additions & 1 deletion langchain-core/src/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ export abstract class BaseMessage
}
super(fields);
this.name = fields.name;
console.log("Assigning content", fields.content);
this.content = fields.content;
this.additional_kwargs = fields.additional_kwargs;
this.response_metadata = fields.response_metadata;
Expand Down
6 changes: 3 additions & 3 deletions libs/langchain-anthropic/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import {
import { StructuredToolInterface } from "@langchain/core/tools";
import { zodToJsonSchema } from "zod-to-json-schema";
import { BaseLLMOutputParser } from "@langchain/core/output_parsers";
import { JsonOutputKeyToolsParser } from "@langchain/core/output_parsers/openai_tools";
import {
Runnable,
RunnablePassthrough,
RunnableSequence,
} from "@langchain/core/runnables";
import { isZodSchema } from "@langchain/core/utils/types";
import { z } from "zod";
import { AnthropicToolsOutputParser } from "./output_parsers.js";

type AnthropicTool = {
name: string;
Expand Down Expand Up @@ -779,7 +779,7 @@ export class ChatAnthropicMessages<
input_schema: jsonSchema,
},
];
outputParser = new JsonOutputKeyToolsParser({
outputParser = new AnthropicToolsOutputParser({
returnSingle: true,
keyName: functionName,
zodSchema: schema,
Expand All @@ -802,7 +802,7 @@ export class ChatAnthropicMessages<
};
}
tools = [anthropicTools];
outputParser = new JsonOutputKeyToolsParser<RunOutput>({
outputParser = new AnthropicToolsOutputParser<RunOutput>({
returnSingle: true,
keyName: functionName,
});
Expand Down
54 changes: 54 additions & 0 deletions libs/langchain-anthropic/src/output_parsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { BaseLLMOutputParser } from "@langchain/core/output_parsers";
import { JsonOutputKeyToolsParserParams } from "@langchain/core/output_parsers/openai_tools";
import { ChatGeneration } from "@langchain/core/outputs";
import { AnthropicToolResponse } from "./chat_models.js";

interface AnthropicToolsOutputParserParams
extends JsonOutputKeyToolsParserParams {}

export class AnthropicToolsOutputParser<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
T extends Record<string, any> = Record<string, any>
> extends BaseLLMOutputParser<T> {
static lc_name() {
return "AnthropicToolsOutputParser";
}

lc_namespace = ["langchain", "anthropic", "output_parsers"];

returnId = false;

/** The type of tool calls to return. */
keyName: string;

/** Whether to return only the first tool call. */
returnSingle = false;

constructor(params: AnthropicToolsOutputParserParams) {
super(params);
this.keyName = params.keyName;
this.returnSingle = params.returnSingle ?? this.returnSingle;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async parseResult(generations: ChatGeneration[]): Promise<T> {
const tools = generations.flatMap((generation) => {
const { message } = generation;
if (typeof message === "string") {
return [];
}
if (!Array.isArray(message.content)) {
return [];
}
const tool = message.content.find((item) => item.type === "tool_use") as
| AnthropicToolResponse
| undefined;
return tool;
});
if (tools.length === 0 || !tools[0]) {
throw new Error("No tools provided to AnthropicToolsOutputParser.");
}
const [tool] = tools;
return tool.input as T;
}
}

0 comments on commit fef15c8

Please sign in to comment.