Skip to content

Commit

Permalink
feat: Add support for BaseModels in LangChain templates
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 630232213
  • Loading branch information
Yeesian Ng authored and Copybara-Service committed May 3, 2024
1 parent d1e2012 commit 5eb885e
Show file tree
Hide file tree
Showing 28 changed files with 183 additions and 288 deletions.
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "1.50.0"
".": "1.49.0"
}
24 changes: 0 additions & 24 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
# Changelog

## [1.50.0](https://github.com/googleapis/python-aiplatform/compare/v1.49.0...v1.50.0) (2024-05-02)


### Features

* Add `Candidate.grounding_metadata` property ([b22a8b8](https://github.com/googleapis/python-aiplatform/commit/b22a8b847e3b299b828e37405e3678093486de28))
* Add option to not include time_series_metrics in get_experiment_df call. This will improve execution time for Experiments with large number of runs. ([78a95c5](https://github.com/googleapis/python-aiplatform/commit/78a95c52d0e7bd9ec5b656ce67044b2f01677156))
* Add tune_model and deploy_tuned_model for TextEmbeddingModel. ([42f5d6f](https://github.com/googleapis/python-aiplatform/commit/42f5d6f7cd13d51c4a73113c59e8b3c728cfc08b))
* Automatically populate parents for full resource name in Vertex RAG SDK ([26657ff](https://github.com/googleapis/python-aiplatform/commit/26657ffd25ecb91882ca764e513c2e952833257f))
* Deploy a tuned text embedding model -- it doesn't matter, if it's tuned using Node.js, or curl. ([8ca9cdf](https://github.com/googleapis/python-aiplatform/commit/8ca9cdf3576e3ce3b373ace4cd6ab0e9c54aa9f2))
* Make get_embeddings work both for foundational & tuned models. ([b8b589c](https://github.com/googleapis/python-aiplatform/commit/b8b589ce9fff29d1721450d32b4a84a7f69413c3))
* Python SDK for Vertex Model Monitoring V2. ([021d59f](https://github.com/googleapis/python-aiplatform/commit/021d59f1487e4e16c847d4135899d6845c0210aa))
* Support public endpoint for Ray Client ([57a5f78](https://github.com/googleapis/python-aiplatform/commit/57a5f7815ffb8523e91d900da4ff7cfd0c344fe4))


### Bug Fixes

* Add deprecation warnings when using Ray v2.4 ([3a36784](https://github.com/googleapis/python-aiplatform/commit/3a367843840513e3257610c8ab38e9f79d3bcea0))
* Append allowed_plugins in tb-gcp-uploader to default allowed plugins ([aab9c3e](https://github.com/googleapis/python-aiplatform/commit/aab9c3e41b92a1d60090e3d1d594390a5e9f3ff6))
* LLM - Added missing parameters to the no-op `_TunableTextEmbeddingModelMixin.get_tuned_model` method ([eb05ac4](https://github.com/googleapis/python-aiplatform/commit/eb05ac421f186441a92c6e3b6a010d74caf14782))
* LVM - Fixed the typo in the VisionModel aspect ratio type annotation ([2d19137](https://github.com/googleapis/python-aiplatform/commit/2d1913773cf9f4a4f8a2c8c8f45680c3ea97f68e))
* Move torch import ([e6d34df](https://github.com/googleapis/python-aiplatform/commit/e6d34df7da7508c655eb17ee694e1ab2160fc8aa))
* Ray - Fixed exception when using Ray 2.4 ([2661f52](https://github.com/googleapis/python-aiplatform/commit/2661f52fd08169e5d29b58f2afce9702b30101ae))

## [1.49.0](https://github.com/googleapis/python-aiplatform/compare/v1.48.0...v1.49.0) (2024-04-27)


Expand Down
2 changes: 1 addition & 1 deletion google/cloud/aiplatform/gapic_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
2 changes: 1 addition & 1 deletion google/cloud/aiplatform/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# limitations under the License.
#

__version__ = "1.50.0"
__version__ = "1.49.0"
2 changes: 1 addition & 1 deletion google/cloud/aiplatform_v1/gapic_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
2 changes: 1 addition & 1 deletion google/cloud/aiplatform_v1beta1/gapic_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.50.0" # {x-release-please-version}
__version__ = "1.49.0" # {x-release-please-version}
2 changes: 1 addition & 1 deletion pypi/_vertex_ai_placeholder/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# limitations under the License.
#

__version__ = "1.50.0"
__version__ = "1.49.0"
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"language": "PYTHON",
"name": "google-cloud-aiplatform",
"version": "1.50.0"
"version": "1.49.0"
},
"snippets": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"language": "PYTHON",
"name": "google-cloud-aiplatform",
"version": "1.50.0"
"version": "1.49.0"
},
"snippets": [
{
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@
]

langchain_extra_require = [
"langchain >= 0.1.13, < 0.2",
"langchain >= 0.1.16, < 0.2",
"langchain-core < 0.2",
"langchain-google-vertexai < 0.2",
"langchain-google-vertexai < 2",
]

langchain_testing_extra_require = list(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.
#
import importlib
import json
from typing import Optional
from unittest import mock

Expand All @@ -25,11 +24,9 @@
import pytest


from langchain_core import agents
from langchain_core import messages
from langchain_core import outputs
from langchain_core import tools as lc_tools
from langchain_core import prompts
from langchain.load import dump as langchain_load_dump
from langchain.agents.format_scratchpad import format_to_openai_function_messages
from langchain.tools.base import StructuredTool


Expand Down Expand Up @@ -93,6 +90,18 @@ def setup_method(self):
project=_TEST_PROJECT,
location=_TEST_LOCATION,
)
self.prompt = {
"input": lambda x: x["input"],
"agent_scratchpad": (
lambda x: format_to_openai_function_messages(x["intermediate_steps"])
),
} | prompts.ChatPromptTemplate.from_messages(
[
("user", "{input}"),
prompts.MessagesPlaceholder(variable_name="agent_scratchpad"),
]
)
self.output_parser = mock.Mock()

def teardown_method(self):
initializer.global_pool.shutdown(wait=True)
Expand All @@ -105,24 +114,33 @@ def test_initialization(self):
assert agent._runnable is None

def test_initialization_with_tools(self):
tools = [
place_tool_query,
StructuredTool.from_function(place_photo_query),
]
agent = reasoning_engines.LangchainAgent(
model=_TEST_MODEL,
tools=[
place_tool_query,
StructuredTool.from_function(place_photo_query),
],
tools=tools,
)
for tool in agent._tools:
assert isinstance(tool, lc_tools.BaseTool)
for tool, agent_tool in zip(tools, agent._tools):
assert isinstance(agent_tool, type(tool))

def test_set_up(self, vertexai_init_mock):
agent = reasoning_engines.LangchainAgent(model=_TEST_MODEL)
agent = reasoning_engines.LangchainAgent(
model=_TEST_MODEL,
prompt=self.prompt,
output_parser=self.output_parser,
)
assert agent._runnable is None
agent.set_up()
assert agent._runnable is not None

def test_query(self, langchain_dump_mock):
agent = reasoning_engines.LangchainAgent(model=_TEST_MODEL)
agent = reasoning_engines.LangchainAgent(
model=_TEST_MODEL,
prompt=self.prompt,
output_parser=self.output_parser,
)
agent._runnable = mock.Mock()
mocks = mock.Mock()
mocks.attach_mock(mock=agent._runnable, attribute="invoke")
Expand All @@ -132,63 +150,6 @@ def test_query(self, langchain_dump_mock):
)


class TestDefaultOutputParser:
def test_parse_result_function_call(self, vertexai_init_mock):
agent = reasoning_engines.LangchainAgent(model=_TEST_MODEL)
agent.set_up()
tool_input = {
"photo_reference": "abcd1234",
"maxwidth": _DEFAULT_PLACE_PHOTO_MAXWIDTH,
}
result = agent._output_parser.parse_result(
[
outputs.ChatGeneration(
message=messages.AIMessage(
content="",
additional_kwargs={
"function_call": {
"name": "place_tool_query",
"arguments": json.dumps(tool_input),
},
},
)
)
]
)
assert isinstance(result, agents.AgentActionMessageLog)
assert result.tool == "place_tool_query"
assert result.tool_input == tool_input

def test_parse_result_not_function_call(self, vertexai_init_mock):
agent = reasoning_engines.LangchainAgent(model=_TEST_MODEL)
agent.set_up()
content = "test content"
result = agent._output_parser.parse_result(
[
outputs.ChatGeneration(
message=messages.AIMessage(content=content),
)
]
)
assert isinstance(result, agents.AgentFinish)
assert result.return_values == {"output": content}
assert result.log == content


class TestDefaultOutputParserErrors:
def test_parse_result_non_chat_generation_errors(self, vertexai_init_mock):
agent = reasoning_engines.LangchainAgent(model=_TEST_MODEL)
agent.set_up()
with pytest.raises(ValueError, match=r"only works on ChatGeneration"):
agent._output_parser.parse_result(["text"])

def test_parse_text_errors(self, vertexai_init_mock):
agent = reasoning_engines.LangchainAgent(model=_TEST_MODEL)
agent.set_up()
with pytest.raises(ValueError, match=r"Can only parse messages"):
agent._output_parser.parse("text")


class TestConvertToolsOrRaise:
def test_convert_tools_or_raise(self, vertexai_init_mock):
pass
Expand Down

0 comments on commit 5eb885e

Please sign in to comment.