mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-14 17:07:25 +00:00
langchain: add partial parsing support to JsonOutputToolsParser (#17035)
- **Description:** Add partial parsing support to JsonOutputToolsParser - **Issue:** [16736](https://github.com/langchain-ai/langchain/issues/16736) --------- Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
This commit is contained in:
parent
dcf973c22c
commit
e022bfaa7d
@ -4,9 +4,8 @@ from json import JSONDecodeError
|
||||
from typing import Any, List, Type
|
||||
|
||||
from langchain_core.exceptions import OutputParserException
|
||||
from langchain_core.output_parsers import (
|
||||
BaseGenerationOutputParser,
|
||||
)
|
||||
from langchain_core.output_parsers import BaseGenerationOutputParser
|
||||
from langchain_core.output_parsers.json import parse_partial_json
|
||||
from langchain_core.outputs import ChatGeneration, Generation
|
||||
from langchain_core.pydantic_v1 import BaseModel
|
||||
|
||||
@ -42,6 +41,11 @@ class JsonOutputToolsParser(BaseGenerationOutputParser[Any]):
|
||||
if "function" not in tool_call:
|
||||
continue
|
||||
try:
|
||||
if partial:
|
||||
function_args = parse_partial_json(
|
||||
tool_call["function"]["arguments"], strict=self.strict
|
||||
)
|
||||
else:
|
||||
function_args = json.loads(
|
||||
tool_call["function"]["arguments"], strict=self.strict
|
||||
)
|
||||
@ -77,7 +81,7 @@ class JsonOutputKeyToolsParser(JsonOutputToolsParser):
|
||||
super().__init__(key_name=key_name, **kwargs)
|
||||
|
||||
def parse_result(self, result: List[Generation], *, partial: bool = False) -> Any:
|
||||
results = super().parse_result(result)
|
||||
results = super().parse_result(result, partial=partial)
|
||||
results = [res for res in results if res["type"] == self.key_name]
|
||||
if not self.return_id:
|
||||
results = [res["args"] for res in results]
|
||||
@ -92,6 +96,6 @@ class PydanticToolsParser(JsonOutputToolsParser):
|
||||
tools: List[Type[BaseModel]]
|
||||
|
||||
def parse_result(self, result: List[Generation], *, partial: bool = False) -> Any:
|
||||
results = super().parse_result(result)
|
||||
results = super().parse_result(result, partial=partial)
|
||||
name_dict = {tool.__name__: tool for tool in self.tools}
|
||||
return [name_dict[res["type"]](**res["args"]) for res in results]
|
||||
|
Loading…
Reference in New Issue
Block a user