Skip to content

Commit

Permalink
Fixed support for custom tool
Browse files Browse the repository at this point in the history
  • Loading branch information
moiz-stri committed Mar 14, 2024
1 parent 9c6e7bc commit eb057dd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
8 changes: 7 additions & 1 deletion libs/vertexai/langchain_google_vertexai/functions_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,14 @@ def _format_tools_to_vertex_tool(
for tool in tools:
if isinstance(tool, BaseTool):
func = _format_tool_to_vertex_function(tool)
else:
elif isinstance(tool, type) and issubclass(tool, BaseModel):
func = _format_pydantic_to_vertex_function(tool)
else:
func = {
"name": tool["name"],
"description": tool.get("description"),
"parameters": _get_parameters_from_schema(tool["parameters"]),
}
function_declarations.append(FunctionDeclaration(**func))

return [VertexTool(function_declarations=function_declarations)]
Expand Down
32 changes: 32 additions & 0 deletions libs/vertexai/tests/integration_tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,38 @@ def test_tools() -> None:
assert round(float(just_numbers), 2) == 2.16


@pytest.mark.extended
def test_custom_tool() -> None:
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain.agents import tool

Check failure on line 100 in libs/vertexai/tests/integration_tests/test_tools.py

View workflow job for this annotation

GitHub Actions / cd libs/vertexai / - / make lint #3.11

Ruff (I001)

tests/integration_tests/test_tools.py:98:1: I001 Import block is un-sorted or un-formatted
@tool
def search(query: str) -> str:
"""Look up things online."""
return "LangChain"

tools = [search]

llm = ChatVertexAI(model_name="gemini-pro", temperature=0.0, convert_system_message_to_human=True)

Check failure on line 108 in libs/vertexai/tests/integration_tests/test_tools.py

View workflow job for this annotation

GitHub Actions / cd libs/vertexai / - / make lint #3.11

Ruff (E501)

tests/integration_tests/test_tools.py:108:89: E501 Line too long (102 > 88)
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful assistant"),
MessagesPlaceholder("chat_history", optional=True),
("human", "{input}"),
MessagesPlaceholder("agent_scratchpad"),
]
)

agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

response = agent_executor.invoke({"input": "What is LangChain?"})
assert isinstance(response, dict)
assert response["input"] == "What is LangChain?"

assert "LangChain" in response["output"]


@pytest.mark.extended
def test_stream() -> None:
from langchain.chains import LLMMathChain
Expand Down

0 comments on commit eb057dd

Please sign in to comment.