Skip to content

Commit

Permalink
fix: Parse intermediate steps from LangChain into JSON.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 627444864
  • Loading branch information
Yeesian Ng authored and Copybara-Service committed Apr 23, 2024
1 parent 76c5d6d commit 754c89d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from langchain_core import messages
from langchain_core import outputs
from langchain_core import tools as lc_tools
from langchain.load import dump as langchain_load_dump
from langchain.tools.base import StructuredTool


Expand Down Expand Up @@ -77,6 +78,12 @@ def vertexai_init_mock():
yield vertexai_init_mock


@pytest.fixture
def langchain_dump_mock():
with mock.patch.object(langchain_load_dump, "dumpd") as langchain_dump_mock:
yield langchain_dump_mock


@pytest.mark.usefixtures("google_auth_mock")
class TestLangchainAgent:
def setup_method(self):
Expand Down Expand Up @@ -114,7 +121,7 @@ def test_set_up(self, vertexai_init_mock):
agent.set_up()
assert agent._runnable is not None

def test_query(self):
def test_query(self, langchain_dump_mock):
agent = reasoning_engines.LangchainAgent(model=_TEST_MODEL)
agent._runnable = mock.Mock()
mocks = mock.Mock()
Expand Down
8 changes: 6 additions & 2 deletions vertexai/preview/reasoning_engines/templates/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
TYPE_CHECKING,
Any,
Callable,
Dict,
List,
Mapping,
Optional,
Expand Down Expand Up @@ -418,7 +419,7 @@ def query(
input: Union[str, Mapping[str, Any]],
config: Optional["RunnableConfig"] = None,
**kwargs: Any,
) -> Mapping[str, Any]:
) -> Dict[str, Any]:
"""Queries the Agent with the given input and config.
Args:
Expand All @@ -433,8 +434,11 @@ def query(
Returns:
The output of querying the Agent with the given input and config.
"""
from langchain.load import dump as langchain_load_dump
if isinstance(input, str):
input = {"input": input}
if not self._runnable:
self.set_up()
return self._runnable.invoke(input=input, config=config, **kwargs)
return langchain_load_dump.dumpd(
self._runnable.invoke(input=input, config=config, **kwargs)
)

0 comments on commit 754c89d

Please sign in to comment.