-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
56aede7
commit fef15c8
Showing
3 changed files
with
57 additions
and
4 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
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; | ||
} | ||
} |