From 2670e6ea6a81dca4830b057cef77d3a63176bc34 Mon Sep 17 00:00:00 2001 From: Erick Friis Date: Wed, 6 Dec 2023 16:25:41 -0800 Subject: [PATCH 1/6] langchain[patch]: fix ChatVertexAI streaming --- libs/langchain/langchain/chat_models/vertexai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/langchain/langchain/chat_models/vertexai.py b/libs/langchain/langchain/chat_models/vertexai.py index d6010e27fc776..2902945cc2c60 100644 --- a/libs/langchain/langchain/chat_models/vertexai.py +++ b/libs/langchain/langchain/chat_models/vertexai.py @@ -242,7 +242,7 @@ def _stream( ) -> Iterator[ChatGenerationChunk]: question = _get_question(messages) history = _parse_chat_history(messages[:-1]) - params = self._prepare_params(stop=stop, **kwargs) + params = self._prepare_params(stop=stop, stream=True, **kwargs) examples = kwargs.get("examples", None) if examples: params["examples"] = _parse_examples(examples) From d96e08a8a67069bb634aa2cbe1d1e7f5c745fba6 Mon Sep 17 00:00:00 2001 From: Erick Friis Date: Wed, 6 Dec 2023 16:34:15 -0800 Subject: [PATCH 2/6] documentation update --- .../chat/google_vertex_ai_palm.ipynb | 248 +++++++++--------- 1 file changed, 121 insertions(+), 127 deletions(-) diff --git a/docs/docs/integrations/chat/google_vertex_ai_palm.ipynb b/docs/docs/integrations/chat/google_vertex_ai_palm.ipynb index 436e2fd14246e..3d62cb81e0e21 100644 --- a/docs/docs/integrations/chat/google_vertex_ai_palm.ipynb +++ b/docs/docs/integrations/chat/google_vertex_ai_palm.ipynb @@ -38,9 +38,47 @@ "metadata": { "tags": [] }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: google-cloud-aiplatform in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (1.37.0)\n", + "Requirement already satisfied: google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-aiplatform) (2.14.0)\n", + "Requirement already satisfied: proto-plus<2.0.0dev,>=1.22.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-aiplatform) (1.22.3)\n", + "Requirement already satisfied: protobuf!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.19.5 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-aiplatform) (4.25.0)\n", + "Requirement already satisfied: packaging>=14.3 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-aiplatform) (23.2)\n", + "Requirement already satisfied: google-cloud-storage<3.0.0dev,>=1.32.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-aiplatform) (2.13.0)\n", + "Requirement already satisfied: google-cloud-bigquery<4.0.0dev,>=1.15.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-aiplatform) (3.13.0)\n", + "Requirement already satisfied: google-cloud-resource-manager<3.0.0dev,>=1.3.3 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-aiplatform) (1.10.4)\n", + "Requirement already satisfied: shapely<3.0.0dev in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-aiplatform) (1.8.5.post1)\n", + "Requirement already satisfied: googleapis-common-protos<2.0.dev0,>=1.56.2 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (1.61.0)\n", + "Requirement already satisfied: google-auth<3.0.dev0,>=2.14.1 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (2.23.4)\n", + "Requirement already satisfied: requests<3.0.0.dev0,>=2.18.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (2.31.0)\n", + "Requirement already satisfied: grpcio<2.0dev,>=1.33.2 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (1.59.3)\n", + "Requirement already satisfied: grpcio-status<2.0.dev0,>=1.33.2 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (1.59.3)\n", + "Requirement already satisfied: google-cloud-core<3.0.0dev,>=1.6.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-bigquery<4.0.0dev,>=1.15.0->google-cloud-aiplatform) (2.3.3)\n", + "Requirement already satisfied: google-resumable-media<3.0dev,>=0.6.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-bigquery<4.0.0dev,>=1.15.0->google-cloud-aiplatform) (2.6.0)\n", + "Requirement already satisfied: python-dateutil<3.0dev,>=2.7.2 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-bigquery<4.0.0dev,>=1.15.0->google-cloud-aiplatform) (2.8.2)\n", + "Requirement already satisfied: grpc-google-iam-v1<1.0.0dev,>=0.12.4 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-resource-manager<3.0.0dev,>=1.3.3->google-cloud-aiplatform) (0.12.7)\n", + "Requirement already satisfied: google-crc32c<2.0dev,>=1.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-storage<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (1.5.0)\n", + "Requirement already satisfied: cachetools<6.0,>=2.0.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-auth<3.0.dev0,>=2.14.1->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (5.3.2)\n", + "Requirement already satisfied: pyasn1-modules>=0.2.1 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-auth<3.0.dev0,>=2.14.1->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (0.3.0)\n", + "Requirement already satisfied: rsa<5,>=3.1.4 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-auth<3.0.dev0,>=2.14.1->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (4.9)\n", + "Requirement already satisfied: six>=1.5 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from python-dateutil<3.0dev,>=2.7.2->google-cloud-bigquery<4.0.0dev,>=1.15.0->google-cloud-aiplatform) (1.16.0)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from requests<3.0.0.dev0,>=2.18.0->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (3.3.2)\n", + "Requirement already satisfied: idna<4,>=2.5 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from requests<3.0.0.dev0,>=2.18.0->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (3.4)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from requests<3.0.0.dev0,>=2.18.0->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (1.26.18)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from requests<3.0.0.dev0,>=2.18.0->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (2023.7.22)\n", + "Requirement already satisfied: pyasn1<0.6.0,>=0.4.6 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from pyasn1-modules>=0.2.1->google-auth<3.0.dev0,>=2.14.1->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (0.5.0)\n", + "\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.1\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n" + ] + } + ], "source": [ - "#!pip install langchain google-cloud-aiplatform" + "!pip install -U google-cloud-aiplatform" ] }, { @@ -57,41 +95,27 @@ "cell_type": "code", "execution_count": 3, "metadata": {}, - "outputs": [], - "source": [ - "chat = ChatVertexAI()" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": {}, - "outputs": [], - "source": [ - "system = \"You are a helpful assistant who translate English to French\"\n", - "human = \"Translate this sentence from English to French. I love programming.\"\n", - "prompt = ChatPromptTemplate.from_messages([(\"system\", system), (\"human\", human)])\n", - "messages = prompt.format_messages()" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "AIMessage(content=\" J'aime la programmation.\", additional_kwargs={}, example=False)" + "AIMessage(content=\" J'aime la programmation.\")" ] }, - "execution_count": 9, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "chat(messages)" + "system = \"You are a helpful assistant who translate English to French\"\n", + "human = \"Translate this sentence from English to French. I love programming.\"\n", + "prompt = ChatPromptTemplate.from_messages([(\"system\", system), (\"human\", human)])\n", + "\n", + "chat = ChatVertexAI()\n", + "\n", + "chain = prompt | chat\n", + "chain.invoke({})" ] }, { @@ -103,35 +127,29 @@ }, { "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [], - "source": [ - "system = (\n", - " \"You are a helpful assistant that translates {input_language} to {output_language}.\"\n", - ")\n", - "human = \"{text}\"\n", - "prompt = ChatPromptTemplate.from_messages([(\"system\", system), (\"human\", human)])" - ] - }, - { - "cell_type": "code", - "execution_count": 13, + "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "AIMessage(content=' 私はプログラミングが大好きです。', additional_kwargs={}, example=False)" + "AIMessage(content=' プログラミングが大好きです')" ] }, - "execution_count": 13, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ + "system = (\n", + " \"You are a helpful assistant that translates {input_language} to {output_language}.\"\n", + ")\n", + "human = \"{text}\"\n", + "prompt = ChatPromptTemplate.from_messages([(\"system\", system), (\"human\", human)])\n", + "\n", "chain = prompt | chat\n", + "\n", "chain.invoke(\n", " {\n", " \"input_language\": \"English\",\n", @@ -162,20 +180,7 @@ }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "chat = ChatVertexAI(\n", - " model_name=\"codechat-bison\", max_output_tokens=1000, temperature=0.5\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 20, + "execution_count": 5, "metadata": { "tags": [] }, @@ -185,20 +190,39 @@ "output_type": "stream", "text": [ " ```python\n", - "def is_prime(x): \n", - " if (x <= 1): \n", + "def is_prime(n):\n", + " if n <= 1:\n", " return False\n", - " for i in range(2, x): \n", - " if (x % i == 0): \n", + " for i in range(2, n):\n", + " if n % i == 0:\n", " return False\n", " return True\n", + "\n", + "def find_prime_numbers(n):\n", + " prime_numbers = []\n", + " for i in range(2, n + 1):\n", + " if is_prime(i):\n", + " prime_numbers.append(i)\n", + " return prime_numbers\n", + "\n", + "print(find_prime_numbers(100))\n", + "```\n", + "\n", + "Output:\n", + "\n", + "```\n", + "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]\n", "```\n" ] } ], "source": [ - "# For simple string in string out usage, we can use the `predict` method:\n", - "print(chat.predict(\"Write a Python function to identify all prime numbers\"))" + "chat = ChatVertexAI(\n", + " model_name=\"codechat-bison\", max_output_tokens=1000, temperature=0.5\n", + ")\n", + "\n", + "message = chat.invoke(\"Write a Python function to identify all prime numbers\")\n", + "print(message.content)" ] }, { @@ -207,66 +231,41 @@ "source": [ "## Asynchronous calls\n", "\n", - "We can make asynchronous calls via the `agenerate` and `ainvoke` methods." + "We can make asynchronous calls via the Runnables [Async Interface](/docs/expression_language/interface)" ] }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ + "# for running these examples in the notebook:\n", "import asyncio\n", "\n", - "# import nest_asyncio\n", - "# nest_asyncio.apply()" + "import nest_asyncio\n", + "nest_asyncio.apply()" ] }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "LLMResult(generations=[[ChatGeneration(text=\" J'aime la programmation.\", generation_info=None, message=AIMessage(content=\" J'aime la programmation.\", additional_kwargs={}, example=False))]], llm_output={}, run=[RunInfo(run_id=UUID('223599ef-38f8-4c79-ac6d-a5013060eb9d'))])" + "AIMessage(content=' Why do you love programming?')" ] }, - "execution_count": 35, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "chat = ChatVertexAI(\n", - " model_name=\"chat-bison\",\n", - " max_output_tokens=1000,\n", - " temperature=0.7,\n", - " top_p=0.95,\n", - " top_k=40,\n", - ")\n", + "chain = prompt | chat\n", "\n", - "asyncio.run(chat.agenerate([messages]))" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "AIMessage(content=' अहं प्रोग्रामिंग प्रेमामि', additional_kwargs={}, example=False)" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ "asyncio.run(\n", " chain.ainvoke(\n", " {\n", @@ -289,56 +288,51 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import sys" - ] - }, - { - "cell_type": "code", - "execution_count": 32, + "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - " 1. China (1,444,216,107)\n", - "2. India (1,393,409,038)\n", - "3. United States (332,403,650)\n", - "4. Indonesia (273,523,615)\n", - "5. Pakistan (220,892,340)\n", - "6. Brazil (212,559,409)\n", - "7. Nigeria (206,139,589)\n", - "8. Bangladesh (164,689,383)\n", - "9. Russia (145,934,462)\n", - "10. Mexico (128,932,488)\n", - "11. Japan (126,476,461)\n", - "12. Ethiopia (115,063,982)\n", - "13. Philippines (109,581,078)\n", - "14. Egypt (102,334,404)\n", - "15. Vietnam (97,338,589)" + " The five most populous countries in the world are:\n", + "1. China (1.4 billion)\n", + "2. India (1.3 billion)\n", + "3. United States (331 million)\n", + "4. Indonesia (273 million)\n", + "5. Pakistan (220 million)" ] } ], "source": [ + "import sys\n", + "\n", "prompt = ChatPromptTemplate.from_messages(\n", - " [(\"human\", \"List out the 15 most populous countries in the world\")]\n", + " [(\"human\", \"List out the 5 most populous countries in the world\")]\n", ")\n", - "messages = prompt.format_messages()\n", - "for chunk in chat.stream(messages):\n", + "\n", + "chat = ChatVertexAI()\n", + "\n", + "chain = prompt | chat\n", + "\n", + "for chunk in chain.stream({}):\n", " sys.stdout.write(chunk.content)\n", " sys.stdout.flush()" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "poetry-venv", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "poetry-venv" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -350,7 +344,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.1" + "version": "3.11.4" }, "vscode": { "interpreter": { From b2de08f26505454b047844a23e2fc9a77146fd05 Mon Sep 17 00:00:00 2001 From: Erick Friis Date: Wed, 6 Dec 2023 16:34:46 -0800 Subject: [PATCH 3/6] remove pip output --- .../chat/google_vertex_ai_palm.ipynb | 42 +------------------ 1 file changed, 2 insertions(+), 40 deletions(-) diff --git a/docs/docs/integrations/chat/google_vertex_ai_palm.ipynb b/docs/docs/integrations/chat/google_vertex_ai_palm.ipynb index 3d62cb81e0e21..e37cac52fa6b2 100644 --- a/docs/docs/integrations/chat/google_vertex_ai_palm.ipynb +++ b/docs/docs/integrations/chat/google_vertex_ai_palm.ipynb @@ -34,49 +34,11 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Requirement already satisfied: google-cloud-aiplatform in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (1.37.0)\n", - "Requirement already satisfied: google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-aiplatform) (2.14.0)\n", - "Requirement already satisfied: proto-plus<2.0.0dev,>=1.22.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-aiplatform) (1.22.3)\n", - "Requirement already satisfied: protobuf!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.19.5 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-aiplatform) (4.25.0)\n", - "Requirement already satisfied: packaging>=14.3 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-aiplatform) (23.2)\n", - "Requirement already satisfied: google-cloud-storage<3.0.0dev,>=1.32.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-aiplatform) (2.13.0)\n", - "Requirement already satisfied: google-cloud-bigquery<4.0.0dev,>=1.15.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-aiplatform) (3.13.0)\n", - "Requirement already satisfied: google-cloud-resource-manager<3.0.0dev,>=1.3.3 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-aiplatform) (1.10.4)\n", - "Requirement already satisfied: shapely<3.0.0dev in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-aiplatform) (1.8.5.post1)\n", - "Requirement already satisfied: googleapis-common-protos<2.0.dev0,>=1.56.2 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (1.61.0)\n", - "Requirement already satisfied: google-auth<3.0.dev0,>=2.14.1 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (2.23.4)\n", - "Requirement already satisfied: requests<3.0.0.dev0,>=2.18.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (2.31.0)\n", - "Requirement already satisfied: grpcio<2.0dev,>=1.33.2 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (1.59.3)\n", - "Requirement already satisfied: grpcio-status<2.0.dev0,>=1.33.2 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (1.59.3)\n", - "Requirement already satisfied: google-cloud-core<3.0.0dev,>=1.6.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-bigquery<4.0.0dev,>=1.15.0->google-cloud-aiplatform) (2.3.3)\n", - "Requirement already satisfied: google-resumable-media<3.0dev,>=0.6.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-bigquery<4.0.0dev,>=1.15.0->google-cloud-aiplatform) (2.6.0)\n", - "Requirement already satisfied: python-dateutil<3.0dev,>=2.7.2 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-bigquery<4.0.0dev,>=1.15.0->google-cloud-aiplatform) (2.8.2)\n", - "Requirement already satisfied: grpc-google-iam-v1<1.0.0dev,>=0.12.4 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-resource-manager<3.0.0dev,>=1.3.3->google-cloud-aiplatform) (0.12.7)\n", - "Requirement already satisfied: google-crc32c<2.0dev,>=1.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-cloud-storage<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (1.5.0)\n", - "Requirement already satisfied: cachetools<6.0,>=2.0.0 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-auth<3.0.dev0,>=2.14.1->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (5.3.2)\n", - "Requirement already satisfied: pyasn1-modules>=0.2.1 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-auth<3.0.dev0,>=2.14.1->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (0.3.0)\n", - "Requirement already satisfied: rsa<5,>=3.1.4 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from google-auth<3.0.dev0,>=2.14.1->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (4.9)\n", - "Requirement already satisfied: six>=1.5 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from python-dateutil<3.0dev,>=2.7.2->google-cloud-bigquery<4.0.0dev,>=1.15.0->google-cloud-aiplatform) (1.16.0)\n", - "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from requests<3.0.0.dev0,>=2.18.0->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (3.3.2)\n", - "Requirement already satisfied: idna<4,>=2.5 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from requests<3.0.0.dev0,>=2.18.0->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (3.4)\n", - "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from requests<3.0.0.dev0,>=2.18.0->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (1.26.18)\n", - "Requirement already satisfied: certifi>=2017.4.17 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from requests<3.0.0.dev0,>=2.18.0->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (2023.7.22)\n", - "Requirement already satisfied: pyasn1<0.6.0,>=0.4.6 in /Users/erickfriis/langchain/oss-pr-review/.venv/lib/python3.11/site-packages (from pyasn1-modules>=0.2.1->google-auth<3.0.dev0,>=2.14.1->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform) (0.5.0)\n", - "\n", - "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.1\u001b[0m\n", - "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n" - ] - } - ], + "outputs": [], "source": [ "!pip install -U google-cloud-aiplatform" ] From 4a43b093ec6ad758dee278ce1830451aaa2de12e Mon Sep 17 00:00:00 2001 From: Erick Friis Date: Wed, 6 Dec 2023 17:51:06 -0800 Subject: [PATCH 4/6] scheduled stream test --- .../chat_models/test_vertexai.py | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/libs/langchain/tests/integration_tests/chat_models/test_vertexai.py b/libs/langchain/tests/integration_tests/chat_models/test_vertexai.py index 47cd280b8e821..7a38dd020d551 100644 --- a/libs/langchain/tests/integration_tests/chat_models/test_vertexai.py +++ b/libs/langchain/tests/integration_tests/chat_models/test_vertexai.py @@ -11,7 +11,12 @@ from unittest.mock import MagicMock, Mock, patch import pytest -from langchain_core.messages import AIMessage, HumanMessage, SystemMessage +from langchain_core.messages import ( + AIMessage, + AIMessageChunk, + HumanMessage, + SystemMessage, +) from langchain_core.outputs import LLMResult from langchain.chat_models import ChatVertexAI @@ -41,6 +46,7 @@ def test_vertexai_single_call(model_name: str) -> None: assert isinstance(response.content, str) +@pytest.mark.scheduled def test_candidates() -> None: model = ChatVertexAI(model_name="chat-bison@001", temperature=0.3, n=2) message = HumanMessage(content="Hello") @@ -62,6 +68,20 @@ async def test_vertexai_agenerate() -> None: assert response.generations[0][0] == sync_response.generations[0][0] +@pytest.mark.scheduled +async def test_vertexai_stream() -> None: + model = ChatVertexAI(temperature=0) + message = HumanMessage(content="Hello") + response = model.astream([message]) + + async for chunk in response: + assert isinstance(chunk, AIMessageChunk) + + sync_response = model.stream([message]) + for chunk in sync_response: + assert isinstance(chunk, AIMessageChunk) + + @pytest.mark.scheduled def test_vertexai_single_call_with_context() -> None: model = ChatVertexAI() From 76ae737c436e83116f6a9f590ea201a5491c1147 Mon Sep 17 00:00:00 2001 From: Erick Friis Date: Wed, 6 Dec 2023 17:59:11 -0800 Subject: [PATCH 5/6] format --- docs/docs/integrations/chat/google_vertex_ai_palm.ipynb | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/docs/integrations/chat/google_vertex_ai_palm.ipynb b/docs/docs/integrations/chat/google_vertex_ai_palm.ipynb index e37cac52fa6b2..0858988f0dcda 100644 --- a/docs/docs/integrations/chat/google_vertex_ai_palm.ipynb +++ b/docs/docs/integrations/chat/google_vertex_ai_palm.ipynb @@ -206,6 +206,7 @@ "import asyncio\n", "\n", "import nest_asyncio\n", + "\n", "nest_asyncio.apply()" ] }, From 0f6b066da24d2853a20d06849fe39a45c157073b Mon Sep 17 00:00:00 2001 From: Erick Friis Date: Thu, 7 Dec 2023 09:38:52 -0800 Subject: [PATCH 6/6] scheduled test --- .../tests/integration_tests/chat_models/test_vertexai.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/libs/langchain/tests/integration_tests/chat_models/test_vertexai.py b/libs/langchain/tests/integration_tests/chat_models/test_vertexai.py index 7a38dd020d551..d8da1e31eb7e6 100644 --- a/libs/langchain/tests/integration_tests/chat_models/test_vertexai.py +++ b/libs/langchain/tests/integration_tests/chat_models/test_vertexai.py @@ -72,10 +72,6 @@ async def test_vertexai_agenerate() -> None: async def test_vertexai_stream() -> None: model = ChatVertexAI(temperature=0) message = HumanMessage(content="Hello") - response = model.astream([message]) - - async for chunk in response: - assert isinstance(chunk, AIMessageChunk) sync_response = model.stream([message]) for chunk in sync_response: