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

Partner(google-vertex-ai): Support astream in Vertex LLM and Chart models #17015

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 73 additions & 19 deletions docs/docs/integrations/chat/google_vertex_ai_palm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,35 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install --upgrade --quiet langchain-google-vertexai"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/qiaowang/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020\n",
" warnings.warn(\n"
]
}
],
"source": [
"from langchain_core.prompts import ChatPromptTemplate\n",
"from langchain_google_vertexai import ChatVertexAI"
Expand Down Expand Up @@ -261,21 +278,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'is_blocked': False,\n",
" 'safety_ratings': [{'category': 'HARM_CATEGORY_HARASSMENT',\n",
"{'citation_metadata': ,\n",
" 'is_blocked': False,\n",
" 'safety_ratings': [{'blocked': False,\n",
" 'category': 'HARM_CATEGORY_HARASSMENT',\n",
" 'probability_label': 'NEGLIGIBLE'},\n",
" {'category': 'HARM_CATEGORY_HATE_SPEECH',\n",
" {'blocked': False,\n",
" 'category': 'HARM_CATEGORY_HATE_SPEECH',\n",
" 'probability_label': 'NEGLIGIBLE'},\n",
" {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT',\n",
" {'blocked': False,\n",
" 'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT',\n",
" 'probability_label': 'NEGLIGIBLE'},\n",
" {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT',\n",
" {'blocked': False,\n",
" 'category': 'HARM_CATEGORY_DANGEROUS_CONTENT',\n",
" 'probability_label': 'NEGLIGIBLE'}]}\n"
]
}
Expand Down Expand Up @@ -315,18 +337,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'is_blocked': False,\n",
" 'safety_attributes': {'Derogatory': 0.1,\n",
" 'Finance': 0.3,\n",
" 'Insult': 0.1,\n",
" 'Sexual': 0.1}}\n"
"{'errors': (),\n",
" 'grounding_metadata': {'citations': [], 'search_queries': []},\n",
" 'is_blocked': False,\n",
" 'safety_attributes': {'Derogatory': 0.1, 'Insult': 0.1, 'Sexual': 0.2}}\n"
]
}
],
Expand Down Expand Up @@ -482,15 +503,48 @@
" sys.stdout.write(chunk.content)\n",
" sys.stdout.flush()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'chain' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01masync\u001b[39;00m \u001b[38;5;28;01mfor\u001b[39;00m chunk \u001b[38;5;129;01min\u001b[39;00m chain\u001b[38;5;241m.\u001b[39mastream({}):\n\u001b[1;32m 2\u001b[0m sys\u001b[38;5;241m.\u001b[39mstdout\u001b[38;5;241m.\u001b[39mwrite(chunk\u001b[38;5;241m.\u001b[39mcontent)\n\u001b[1;32m 3\u001b[0m sys\u001b[38;5;241m.\u001b[39mstdout\u001b[38;5;241m.\u001b[39mflush()\n",
"\u001b[0;31mNameError\u001b[0m: name 'chain' is not defined"
]
}
],
"source": [
"async for chunk in chain.astream({}):\n",
" sys.stdout.write(chunk.content)\n",
" sys.stdout.flush()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "",
"name": ""
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down
86 changes: 79 additions & 7 deletions docs/docs/integrations/llms/google_vertex_ai_palm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/qiaowang/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020\n",
" warnings.warn(\n"
]
}
],
"source": [
"from langchain_google_vertexai import VertexAI"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -98,16 +107,16 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'**Pros:**\\n\\n* **Easy to learn and use:** Python is known for its simple syntax and readability, making it a great choice for beginners and experienced programmers alike.\\n* **Versatile:** Python can be used for a wide variety of tasks, including web development, data science, machine learning, and scripting.\\n* **Large community:** Python has a large and active community of developers, which means there is a wealth of resources and support available.\\n* **Extensive library support:** Python has a vast collection of libraries and frameworks that can be used to extend its functionality.\\n* **Cross-platform:** Python is available for a'"
"\"<b>Pros:</b>\\n\\n1.<b>Simplicity and Readability: </b> Python has a concise and easy-to-read syntax, which makes it accessible to new programmers and allows for quick development. Its use of significant whitespace contributes to its readability.\\n\\n2. <b>Extensive Library and Module Ecosystem:</b> Python boasts a comprehensive standard library and a vast repository of third-party libraries and modules. These resources facilitate various programming tasks, including data analysis, machine learning, web development, automation, GUI programming, and more.\\n\\n3. <b>Object-Oriented Programming (OOP):</b> Python supports OOP principles, enabling programmers to structure code in a modular and organized fashion. It allows for the creation of classes, objects, and methods, promoting code reusability and maintainability.\\n\\n4. <b>Rapid Development and Prototyping: </b> Python's rapid development cycle and prototyping capabilities make it suitable for quickly iterating through ideas and testing concepts. Its interactive interpreter allows for quick experimentation and rapid feedback.\\n\\n5. <b>Flexibility and Versatility: </b> Python is a versatile language that finds applications in various domains, including web development, data science, scientific computing, GUI programming, game development, automation, scripting, and DevOps. It provides cross-platform compatibility, allowing programs to run on different operating systems.\\n\\n<b>Cons:</b>\\n\\n1. <b>Performance and Speed:</b> Python is an interpreted language, which inherently makes it slower than compiled languages like C or Java. This speed disadvantage can be a hindrance in applications requiring intensive computational tasks or real-time processing.\\n\\n2. <b>Limited Type Checking and Static Syntax Checking: </b> Python's dynamic type system lacks comprehensive type checking during development. Errors often arise only at runtime, which can lead to difficulties in identifying and resolving issues early on. Additionally, the lack of static syntax checking can introduce errors that go unnoticed until the program is executed.\\n\\n3. <b>GIL (Global Interpreter Lock):</b> Python employs a Global Interpreter Lock (GIL), which allows only one thread to execute Python bytecode at a time. This can limit parallelism and concurrency, impacting the performance of multi-threaded applications.\\n\\n4. <b>Package Management Complexity:</b> Python's package management system, while extensive, can be challenging to manage, particularly for large projects. Dealing with dependencies and version conflicts can sometimes be complex and time-consuming.\\n\\n5. <b>Memory Management: </b> Python's automatic garbage collection, while convenient, can introduce challenges in optimizing memory usage. Developers need to be aware of memory management considerations when working with large datasets or complex algorithms to prevent memory leaks and performance bottlenecks.\""
]
},
"execution_count": null,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -161,6 +170,69 @@
" print(chunk, end=\"\", flush=True)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"**Pros:**\n",
"\n",
"1. **Easy to Learn and Use:**\n",
" - Python is known for its simple syntax, making it easy to learn for beginners.\n",
" - The use of whitespace for indentation adds clarity to the code, making it more readable.\n",
" - Python's extensive documentation and vast online resources contribute to its ease of learning.\n",
"\n",
"2. **Versatile and General-Purpose:**\n",
" - Python can be used for a wide range of tasks, including web development, data science, machine learning, scripting, and automation.\n",
" - Its extensive standard library provides various modules for handling different tasks, reducing the need for external dependencies.\n",
"\n",
"3. **Interpreted Nature:**\n",
" - Python is an interpreted language, meaning the source code is directly executed by the interpreter without being compiled beforehand.\n",
" - This allows for rapid development, as changes can be made and executed quickly without having to recompile the code.\n",
"\n",
"4. **Strong Community Support:**\n",
" - Python has a large and active community of developers who contribute to the language's improvement and offer help and support to others.\n",
" - The continuous development of Python's ecosystem, with new libraries and frameworks being released regularly, contributes to its popularity.\n",
"\n",
"5. **Cross-Platform Availability:**\n",
" - Python is available on various operating systems, including Windows, macOS, and Linux, making it easy to develop and deploy code across different platforms.\n",
" - This portability allows developers to create applications that can run on multiple systems without major modifications.\n",
"\n",
"**Cons:**\n",
"\n",
"1. **Performance:**\n",
" - Compared to compiled languages like C or C++, Python is generally slower in execution speed.\n",
" - However, Python's extensive optimizations and the use of libraries written in compiled languages help mitigate this disadvantage.\n",
"\n",
"2. **Memory Usage:**\n",
" - Python uses a dynamic memory management system, which can lead to higher memory consumption compared to statically typed languages.\n",
" - Optimizing memory usage in Python requires careful consideration of data structures and memory management techniques.\n",
"\n",
"3. **Lack of Strong Typing:**\n",
" - Python is a dynamically typed language, which means data types are not strictly enforced and can change during program execution.\n",
" - This flexibility can lead to errors and make it more difficult to catch type-related issues early on.\n",
" - However, Python does provide mechanisms like type annotations and static type checkers to help with type checking and improving code quality.\n",
"\n",
"4. **Package Management:**\n",
" - Python's package management system, pip, can be complex to use and can sometimes lead to dependency conflicts or version issues.\n",
" - Managing dependencies in Python projects requires careful attention to ensure compatibility and to avoid potential conflicts.\n",
"\n",
"5. **Testing and Debugging:**\n",
" - Python's interpreted nature can make debugging more challenging compared to compiled languages.\n",
" - The dynamic nature of Python allows potential errors to go undetected until runtime, making it more difficult to pinpoint the exact cause of an issue.\n",
" - However, using debugging tools and implementing comprehensive testing can help mitigate these challenges."
]
}
],
"source": [
"async for chunk in model.astream(message):\n",
" print(chunk, end=\"\", flush=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -574,7 +646,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down
Loading
Loading