Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vertexai[patch]: enable agents for vertex #137

Merged
merged 3 commits into from
Apr 11, 2024
Merged

vertexai[patch]: enable agents for vertex #137

merged 3 commits into from
Apr 11, 2024

Conversation

ccurme
Copy link
Contributor

@ccurme ccurme commented Apr 11, 2024

from langchain.agents import AgentExecutor, create_tool_calling_agent, tool
from langchain_google_vertexai import ChatVertexAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder


prompt = ChatPromptTemplate.from_messages(
    [
        ("human", "{input}"),
        MessagesPlaceholder("agent_scratchpad"),
    ]
)
model = ChatVertexAI(model_name="gemini-pro", temperature=0)


@tool
def magic_function(input: int) -> str:
    """Applies a magic function to an input."""
    return str(input + 2)



tools = [magic_function]

agent = create_tool_calling_agent(model, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

agent_executor.invoke({"input": "what is the value of magic_function(3)?"})
> Entering new AgentExecutor chain...

Invoking: `magic_function` with `{'input': 3.0}`


5The value of magic_function(3) is {"content": "5"}.

> Finished chain.
{'input': 'what is the value of magic_function(3)?',
 'output': 'The value of magic_function(3) is {"content": "5"}.'}

@baskaryan baskaryan changed the title (WIP): enable agents for vertex vertexai[patch]: enable agents for vertex Apr 11, 2024
@baskaryan baskaryan marked this pull request as ready for review April 11, 2024 11:18
@baskaryan baskaryan requested a review from lkuligin April 11, 2024 12:11
# message.name can be null for ToolMessage
if history[i - 1].tool_calls: # type: ignore
name = history[i - 1].tool_calls[0]["name"] # type: ignore
else:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits: I believe this else block is not needed

@lkuligin lkuligin merged commit 5523121 into main Apr 11, 2024
13 checks passed
@lkuligin lkuligin deleted the cc/vertex_agents branch April 11, 2024 13:04
@ccurme
Copy link
Contributor Author

ccurme commented Apr 11, 2024

@lkuligin @baskaryan @efriis we have at least two options to make this work with agents. The underlying issue is that Langchain requires tool_call_ids on instances of ToolMessage (i.e., when passing the result of a tool execution back to the model).

Following the example in the vertex docs here, from what I can tell tool calls and responses are identified with a "name" attribute:

role: "model"
parts {
  function_call {
    name: "get_current_weather"
    args {
      fields {
        key: "location"
        value {
          string_value: "boston"
        }
      }
    }
  }
}

parts {
  function_response {
    name: "get_current_weather"
    response {
      fields {
        key: "content"
        value {
          string_value: "response"
        }
      }
    }
  }
}

Option 1 (implemented here): generate IDs on tool invocations. Then when processing the ToolMessage, we fetch the name from the most recent tool invocation.

Option 2 (implemented locally): use the tool name as the ID. This "just works", but tool calls may look funny with id and name the same. Let me know what you think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants