diff --git a/DAY-5/demo_langchain.ipynb b/DAY-5/demo_langchain.ipynb new file mode 100644 index 0000000..512c9aa --- /dev/null +++ b/DAY-5/demo_langchain.ipynb @@ -0,0 +1,864 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "5p-Pz_2CXmPj", + "outputId": "3df7c033-d758-42d3-8a61-d88a6ba9c68c" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Collecting langchain\n", + " Downloading langchain-0.2.11-py3-none-any.whl.metadata (7.1 kB)\n", + "Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.10/dist-packages (from langchain) (6.0.1)\n", + "Requirement already satisfied: SQLAlchemy<3,>=1.4 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.0.31)\n", + "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /usr/local/lib/python3.10/dist-packages (from langchain) (3.9.5)\n", + "Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in /usr/local/lib/python3.10/dist-packages (from langchain) (4.0.3)\n", + "Collecting langchain-core<0.3.0,>=0.2.23 (from langchain)\n", + " Downloading langchain_core-0.2.24-py3-none-any.whl.metadata (6.2 kB)\n", + "Collecting langchain-text-splitters<0.3.0,>=0.2.0 (from langchain)\n", + " Downloading langchain_text_splitters-0.2.2-py3-none-any.whl.metadata (2.1 kB)\n", + "Collecting langsmith<0.2.0,>=0.1.17 (from langchain)\n", + " Downloading langsmith-0.1.93-py3-none-any.whl.metadata (13 kB)\n", + "Requirement already satisfied: numpy<2,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain) (1.26.4)\n", + "Requirement already satisfied: pydantic<3,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.8.2)\n", + "Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.31.0)\n", + "Requirement already satisfied: tenacity!=8.4.0,<9.0.0,>=8.1.0 in /usr/local/lib/python3.10/dist-packages (from langchain) (8.5.0)\n", + "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)\n", + "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (23.2.0)\n", + "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.4.1)\n", + "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.5)\n", + "Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.4)\n", + "Collecting jsonpatch<2.0,>=1.33 (from langchain-core<0.3.0,>=0.2.23->langchain)\n", + " Downloading jsonpatch-1.33-py2.py3-none-any.whl.metadata (3.0 kB)\n", + "Requirement already satisfied: packaging<25,>=23.2 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.3.0,>=0.2.23->langchain) (24.1)\n", + "Collecting orjson<4.0.0,>=3.9.14 (from langsmith<0.2.0,>=0.1.17->langchain)\n", + " Downloading orjson-3.10.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (50 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.4/50.4 kB\u001b[0m \u001b[31m2.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hRequirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (0.7.0)\n", + "Requirement already satisfied: pydantic-core==2.20.1 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (2.20.1)\n", + "Requirement already satisfied: typing-extensions>=4.6.1 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (4.12.2)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (3.3.2)\n", + "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (3.7)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (2.0.7)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (2024.7.4)\n", + "Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.10/dist-packages (from SQLAlchemy<3,>=1.4->langchain) (3.0.3)\n", + "Collecting jsonpointer>=1.9 (from jsonpatch<2.0,>=1.33->langchain-core<0.3.0,>=0.2.23->langchain)\n", + " Downloading jsonpointer-3.0.0-py2.py3-none-any.whl.metadata (2.3 kB)\n", + "Downloading langchain-0.2.11-py3-none-any.whl (990 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m990.3/990.3 kB\u001b[0m \u001b[31m14.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading langchain_core-0.2.24-py3-none-any.whl (377 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m377.3/377.3 kB\u001b[0m \u001b[31m24.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading langchain_text_splitters-0.2.2-py3-none-any.whl (25 kB)\n", + "Downloading langsmith-0.1.93-py3-none-any.whl (139 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m139.8/139.8 kB\u001b[0m \u001b[31m12.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading jsonpatch-1.33-py2.py3-none-any.whl (12 kB)\n", + "Downloading orjson-3.10.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (141 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m141.1/141.1 kB\u001b[0m \u001b[31m11.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading jsonpointer-3.0.0-py2.py3-none-any.whl (7.6 kB)\n", + "Installing collected packages: orjson, jsonpointer, jsonpatch, langsmith, langchain-core, langchain-text-splitters, langchain\n", + "Successfully installed jsonpatch-1.33 jsonpointer-3.0.0 langchain-0.2.11 langchain-core-0.2.24 langchain-text-splitters-0.2.2 langsmith-0.1.93 orjson-3.10.6\n" + ] + } + ], + "source": [ + "!pip install langchain" + ] + }, + { + "cell_type": "code", + "source": [ + "!export LANGCHAIN_TRACING_V2=\"true\"\n", + "!export LANGCHAIN_API_KEY=\"...\"" + ], + "metadata": { + "id": "Ix7QbN7CXvIy" + }, + "execution_count": 2, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "import getpass\n", + "import os\n", + "\n", + "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "4OvJ7svxX1lX", + "outputId": "78e0c2ac-9f2a-4179-f27c-5b16a132976d" + }, + "execution_count": 2, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "··········\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "!pip install -qU langchain-google-vertexai\n", + "\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "LRXF7ugHX366", + "outputId": "466919ad-bb6b-4fed-c07b-4846b90ac409" + }, + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/75.3 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[90m╺\u001b[0m\u001b[90m━\u001b[0m \u001b[32m71.7/75.3 kB\u001b[0m \u001b[31m2.7 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.3/75.3 kB\u001b[0m \u001b[31m1.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25h\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/130.5 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m130.5/130.5 kB\u001b[0m \u001b[31m5.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25h" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "os.environ[\"GOOGLE_CLOUD_PROJECT_ID\"] = \"mystical-atlas-430903-v1\"\n" + ], + "metadata": { + "id": "scouFlm9juET" + }, + "execution_count": 4, + "outputs": [] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "LJtIhkrElwJU" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "import os\n", + "from google.oauth2 import service_account" + ], + "metadata": { + "id": "epVGc0AImZOO" + }, + "execution_count": 5, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "service_account_path = \"/content/mystical-atlas-430903-v1-12c96e73df92.json\"\n", + "os.environ[\"GOOGLE_APPLICATION_CREDENTIALS\"] = service_account_path\n" + ], + "metadata": { + "id": "vLn6joeUmaOg" + }, + "execution_count": 8, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "import getpass\n", + "import os\n", + "\n", + "#os.environ[\"GOOGLE_API_KEY\"] = getpass.getpass()\n", + "\n", + "from langchain_google_vertexai import ChatVertexAI\n", + "\n", + "model = ChatVertexAI(model=\"gemini-1.5-flash\", project_id=\"mystical-atlas-430903-v1\")" + ], + "metadata": { + "id": "NAFG8emoYYvI" + }, + "execution_count": 9, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "from langchain_core.messages import HumanMessage, SystemMessage\n", + "\n", + "messages = [\n", + " SystemMessage(content=\"Translate the following from English into Italian\"),\n", + " HumanMessage(content=\"hi!\"),\n", + "]\n", + "\n", + "model.invoke(messages)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "BVM72qFSYhBY", + "outputId": "412bf981-d479-4943-c3fe-bbcc374dda91" + }, + "execution_count": 10, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "AIMessage(content='Ciao! \\n', response_metadata={'is_blocked': False, 'safety_ratings': [{'category': 'HARM_CATEGORY_HATE_SPEECH', 'probability_label': 'NEGLIGIBLE', 'blocked': False, 'severity': 'HARM_SEVERITY_NEGLIGIBLE'}, {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False, 'severity': 'HARM_SEVERITY_NEGLIGIBLE'}, {'category': 'HARM_CATEGORY_HARASSMENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False, 'severity': 'HARM_SEVERITY_NEGLIGIBLE'}, {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability_label': 'NEGLIGIBLE', 'blocked': False, 'severity': 'HARM_SEVERITY_NEGLIGIBLE'}], 'usage_metadata': {'prompt_token_count': 9, 'candidates_token_count': 4, 'total_token_count': 13}}, id='run-5a6eac4e-3ee7-48f4-a951-573418c7463d-0', usage_metadata={'input_tokens': 9, 'output_tokens': 4, 'total_tokens': 13})" + ] + }, + "metadata": {}, + "execution_count": 10 + } + ] + }, + { + "cell_type": "code", + "source": [ + "from langchain_core.output_parsers import StrOutputParser\n", + "\n", + "parser = StrOutputParser()" + ], + "metadata": { + "id": "voM5JrBmbmdA" + }, + "execution_count": 11, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "result = model.invoke(messages)" + ], + "metadata": { + "id": "zZqkV-6r05bW" + }, + "execution_count": 12, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "parser.invoke(result)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "q36aYPpk07WP", + "outputId": "72c0e4a8-b834-4429-d0db-1f5d7af01224" + }, + "execution_count": 13, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'Ciao! \\n'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 13 + } + ] + }, + { + "cell_type": "code", + "source": [ + "chain = model | parser" + ], + "metadata": { + "id": "P-maDSr909lZ" + }, + "execution_count": 14, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "chain.invoke(messages)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "IOGHYgXh1KD6", + "outputId": "a10c3568-935c-4218-c0b1-df35ffd98067" + }, + "execution_count": 15, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'Ciao! \\n'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 15 + } + ] + }, + { + "cell_type": "code", + "source": [ + "from langchain_core.prompts import ChatPromptTemplate" + ], + "metadata": { + "id": "acqEtnzk1L9h" + }, + "execution_count": 16, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "system_template = \"Translate the following into {language}:\"" + ], + "metadata": { + "id": "RloY4dAG15Wy" + }, + "execution_count": 17, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "prompt_template = ChatPromptTemplate.from_messages(\n", + " [(\"system\", system_template), (\"user\", \"{text}\")]\n", + ")" + ], + "metadata": { + "id": "ZohL88AF162w" + }, + "execution_count": 18, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "result = prompt_template.invoke({\"language\": \"italian\", \"text\": \"hi\"})\n", + "\n", + "result" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "tqgwmvJZ19zm", + "outputId": "d6820d87-e599-4757-a136-295f21086d79" + }, + "execution_count": 19, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "ChatPromptValue(messages=[SystemMessage(content='Translate the following into italian:'), HumanMessage(content='hi')])" + ] + }, + "metadata": {}, + "execution_count": 19 + } + ] + }, + { + "cell_type": "code", + "source": [ + "result.to_messages()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "5wZm_y8R2CXL", + "outputId": "6eeb6afd-a396-47fd-d07c-6ae8869872a8" + }, + "execution_count": 20, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[SystemMessage(content='Translate the following into italian:'),\n", + " HumanMessage(content='hi')]" + ] + }, + "metadata": {}, + "execution_count": 20 + } + ] + }, + { + "cell_type": "code", + "source": [ + "chain = prompt_template | model | parser" + ], + "metadata": { + "id": "YERfCtQR2IbS" + }, + "execution_count": 21, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "chain.invoke({\"language\": \"italian\", \"text\": \"hi\"})" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "6bfe6vdf2Ljw", + "outputId": "ab40e661-e4c0-4731-af45-0cea4b982901" + }, + "execution_count": 22, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'Ciao! \\n'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 22 + } + ] + }, + { + "cell_type": "code", + "source": [ + "!pip install \"langserve[all]\"" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "2grXHPoC2NU-", + "outputId": "1e87af73-6560-4ac5-cec9-290d67e6f35f" + }, + "execution_count": 23, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Collecting langserve[all]\n", + " Downloading langserve-0.2.2-py3-none-any.whl.metadata (39 kB)\n", + "Collecting httpx>=0.23.0 (from langserve[all])\n", + " Downloading httpx-0.27.0-py3-none-any.whl.metadata (7.2 kB)\n", + "Requirement already satisfied: langchain-core<0.3,>=0.1 in /usr/local/lib/python3.10/dist-packages (from langserve[all]) (0.2.24)\n", + "Requirement already satisfied: orjson>=2 in /usr/local/lib/python3.10/dist-packages (from langserve[all]) (3.10.6)\n", + "Requirement already satisfied: pydantic>=1 in /usr/local/lib/python3.10/dist-packages (from langserve[all]) (2.8.2)\n", + "Collecting pyproject-toml<0.0.11,>=0.0.10 (from langserve[all])\n", + " Downloading pyproject_toml-0.0.10-py3-none-any.whl.metadata (642 bytes)\n", + "Collecting fastapi<1,>=0.90.1 (from langserve[all])\n", + " Downloading fastapi-0.111.1-py3-none-any.whl.metadata (26 kB)\n", + "Collecting sse-starlette<2.0.0,>=1.3.0 (from langserve[all])\n", + " Downloading sse_starlette-1.8.2-py3-none-any.whl.metadata (5.4 kB)\n", + "Collecting starlette<0.38.0,>=0.37.2 (from fastapi<1,>=0.90.1->langserve[all])\n", + " Downloading starlette-0.37.2-py3-none-any.whl.metadata (5.9 kB)\n", + "Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from fastapi<1,>=0.90.1->langserve[all]) (4.12.2)\n", + "Collecting fastapi-cli>=0.0.2 (from fastapi<1,>=0.90.1->langserve[all])\n", + " Downloading fastapi_cli-0.0.4-py3-none-any.whl.metadata (7.0 kB)\n", + "Requirement already satisfied: jinja2>=2.11.2 in /usr/local/lib/python3.10/dist-packages (from fastapi<1,>=0.90.1->langserve[all]) (3.1.4)\n", + "Collecting python-multipart>=0.0.7 (from fastapi<1,>=0.90.1->langserve[all])\n", + " Downloading python_multipart-0.0.9-py3-none-any.whl.metadata (2.5 kB)\n", + "Collecting email_validator>=2.0.0 (from fastapi<1,>=0.90.1->langserve[all])\n", + " Downloading email_validator-2.2.0-py3-none-any.whl.metadata (25 kB)\n", + "Collecting uvicorn>=0.12.0 (from uvicorn[standard]>=0.12.0->fastapi<1,>=0.90.1->langserve[all])\n", + " Downloading uvicorn-0.30.3-py3-none-any.whl.metadata (6.5 kB)\n", + "Requirement already satisfied: anyio in /usr/local/lib/python3.10/dist-packages (from httpx>=0.23.0->langserve[all]) (3.7.1)\n", + "Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx>=0.23.0->langserve[all]) (2024.7.4)\n", + "Collecting httpcore==1.* (from httpx>=0.23.0->langserve[all])\n", + " Downloading httpcore-1.0.5-py3-none-any.whl.metadata (20 kB)\n", + "Requirement already satisfied: idna in /usr/local/lib/python3.10/dist-packages (from httpx>=0.23.0->langserve[all]) (3.7)\n", + "Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from httpx>=0.23.0->langserve[all]) (1.3.1)\n", + "Collecting h11<0.15,>=0.13 (from httpcore==1.*->httpx>=0.23.0->langserve[all])\n", + " Downloading h11-0.14.0-py3-none-any.whl.metadata (8.2 kB)\n", + "Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.3,>=0.1->langserve[all]) (6.0.1)\n", + "Requirement already satisfied: jsonpatch<2.0,>=1.33 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.3,>=0.1->langserve[all]) (1.33)\n", + "Requirement already satisfied: langsmith<0.2.0,>=0.1.75 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.3,>=0.1->langserve[all]) (0.1.93)\n", + "Requirement already satisfied: packaging<25,>=23.2 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.3,>=0.1->langserve[all]) (24.1)\n", + "Requirement already satisfied: tenacity!=8.4.0,<9.0.0,>=8.1.0 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.3,>=0.1->langserve[all]) (8.5.0)\n", + "Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic>=1->langserve[all]) (0.7.0)\n", + "Requirement already satisfied: pydantic-core==2.20.1 in /usr/local/lib/python3.10/dist-packages (from pydantic>=1->langserve[all]) (2.20.1)\n", + "Requirement already satisfied: setuptools>=42 in /usr/local/lib/python3.10/dist-packages (from pyproject-toml<0.0.11,>=0.0.10->langserve[all]) (71.0.4)\n", + "Requirement already satisfied: wheel in /usr/local/lib/python3.10/dist-packages (from pyproject-toml<0.0.11,>=0.0.10->langserve[all]) (0.43.0)\n", + "Requirement already satisfied: toml in /usr/local/lib/python3.10/dist-packages (from pyproject-toml<0.0.11,>=0.0.10->langserve[all]) (0.10.2)\n", + "Requirement already satisfied: jsonschema in /usr/local/lib/python3.10/dist-packages (from pyproject-toml<0.0.11,>=0.0.10->langserve[all]) (4.23.0)\n", + "Collecting dnspython>=2.0.0 (from email_validator>=2.0.0->fastapi<1,>=0.90.1->langserve[all])\n", + " Downloading dnspython-2.6.1-py3-none-any.whl.metadata (5.8 kB)\n", + "Requirement already satisfied: typer>=0.12.3 in /usr/local/lib/python3.10/dist-packages (from fastapi-cli>=0.0.2->fastapi<1,>=0.90.1->langserve[all]) (0.12.3)\n", + "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2>=2.11.2->fastapi<1,>=0.90.1->langserve[all]) (2.1.5)\n", + "Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.10/dist-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3,>=0.1->langserve[all]) (3.0.0)\n", + "Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.10/dist-packages (from langsmith<0.2.0,>=0.1.75->langchain-core<0.3,>=0.1->langserve[all]) (2.31.0)\n", + "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio->httpx>=0.23.0->langserve[all]) (1.2.2)\n", + "Requirement already satisfied: click>=7.0 in /usr/local/lib/python3.10/dist-packages (from uvicorn>=0.12.0->uvicorn[standard]>=0.12.0->fastapi<1,>=0.90.1->langserve[all]) (8.1.7)\n", + "Collecting httptools>=0.5.0 (from uvicorn[standard]>=0.12.0->fastapi<1,>=0.90.1->langserve[all])\n", + " Downloading httptools-0.6.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (3.6 kB)\n", + "Collecting python-dotenv>=0.13 (from uvicorn[standard]>=0.12.0->fastapi<1,>=0.90.1->langserve[all])\n", + " Downloading python_dotenv-1.0.1-py3-none-any.whl.metadata (23 kB)\n", + "Collecting uvloop!=0.15.0,!=0.15.1,>=0.14.0 (from uvicorn[standard]>=0.12.0->fastapi<1,>=0.90.1->langserve[all])\n", + " Downloading uvloop-0.19.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.9 kB)\n", + "Collecting watchfiles>=0.13 (from uvicorn[standard]>=0.12.0->fastapi<1,>=0.90.1->langserve[all])\n", + " Downloading watchfiles-0.22.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.9 kB)\n", + "Collecting websockets>=10.4 (from uvicorn[standard]>=0.12.0->fastapi<1,>=0.90.1->langserve[all])\n", + " Downloading websockets-12.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.6 kB)\n", + "Requirement already satisfied: attrs>=22.2.0 in /usr/local/lib/python3.10/dist-packages (from jsonschema->pyproject-toml<0.0.11,>=0.0.10->langserve[all]) (23.2.0)\n", + "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.10/dist-packages (from jsonschema->pyproject-toml<0.0.11,>=0.0.10->langserve[all]) (2023.12.1)\n", + "Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.10/dist-packages (from jsonschema->pyproject-toml<0.0.11,>=0.0.10->langserve[all]) (0.35.1)\n", + "Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from jsonschema->pyproject-toml<0.0.11,>=0.0.10->langserve[all]) (0.19.1)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langsmith<0.2.0,>=0.1.75->langchain-core<0.3,>=0.1->langserve[all]) (3.3.2)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langsmith<0.2.0,>=0.1.75->langchain-core<0.3,>=0.1->langserve[all]) (2.0.7)\n", + "Requirement already satisfied: shellingham>=1.3.0 in /usr/local/lib/python3.10/dist-packages (from typer>=0.12.3->fastapi-cli>=0.0.2->fastapi<1,>=0.90.1->langserve[all]) (1.5.4)\n", + "Requirement already satisfied: rich>=10.11.0 in /usr/local/lib/python3.10/dist-packages (from typer>=0.12.3->fastapi-cli>=0.0.2->fastapi<1,>=0.90.1->langserve[all]) (13.7.1)\n", + "Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer>=0.12.3->fastapi-cli>=0.0.2->fastapi<1,>=0.90.1->langserve[all]) (3.0.0)\n", + "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer>=0.12.3->fastapi-cli>=0.0.2->fastapi<1,>=0.90.1->langserve[all]) (2.16.1)\n", + "Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer>=0.12.3->fastapi-cli>=0.0.2->fastapi<1,>=0.90.1->langserve[all]) (0.1.2)\n", + "Downloading fastapi-0.111.1-py3-none-any.whl (92 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m92.2/92.2 kB\u001b[0m \u001b[31m4.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading httpx-0.27.0-py3-none-any.whl (75 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m6.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading httpcore-1.0.5-py3-none-any.whl (77 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m6.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading pyproject_toml-0.0.10-py3-none-any.whl (6.9 kB)\n", + "Downloading sse_starlette-1.8.2-py3-none-any.whl (8.9 kB)\n", + "Downloading langserve-0.2.2-py3-none-any.whl (1.2 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m18.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading email_validator-2.2.0-py3-none-any.whl (33 kB)\n", + "Downloading fastapi_cli-0.0.4-py3-none-any.whl (9.5 kB)\n", + "Downloading python_multipart-0.0.9-py3-none-any.whl (22 kB)\n", + "Downloading starlette-0.37.2-py3-none-any.whl (71 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m71.9/71.9 kB\u001b[0m \u001b[31m6.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading uvicorn-0.30.3-py3-none-any.whl (62 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.8/62.8 kB\u001b[0m \u001b[31m5.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading dnspython-2.6.1-py3-none-any.whl (307 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m307.7/307.7 kB\u001b[0m \u001b[31m20.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading h11-0.14.0-py3-none-any.whl (58 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m4.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading httptools-0.6.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (341 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m341.4/341.4 kB\u001b[0m \u001b[31m22.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading python_dotenv-1.0.1-py3-none-any.whl (19 kB)\n", + "Downloading uvloop-0.19.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.4 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.4/3.4 MB\u001b[0m \u001b[31m47.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading watchfiles-0.22.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m47.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading websockets-12.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (130 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m130.2/130.2 kB\u001b[0m \u001b[31m11.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hInstalling collected packages: websockets, uvloop, python-multipart, python-dotenv, httptools, h11, dnspython, watchfiles, uvicorn, starlette, httpcore, email_validator, httpx, pyproject-toml, fastapi-cli, langserve, fastapi, sse-starlette\n", + "Successfully installed dnspython-2.6.1 email_validator-2.2.0 fastapi-0.111.1 fastapi-cli-0.0.4 h11-0.14.0 httpcore-1.0.5 httptools-0.6.1 httpx-0.27.0 langserve-0.2.2 pyproject-toml-0.0.10 python-dotenv-1.0.1 python-multipart-0.0.9 sse-starlette-1.8.2 starlette-0.37.2 uvicorn-0.30.3 uvloop-0.19.0 watchfiles-0.22.0 websockets-12.0\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "!pip install langchain-openai\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "MbSwgPw424dk", + "outputId": "54b9f248-2e2c-46d9-9b65-c0696f362531" + }, + "execution_count": 25, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Collecting langchain-openai\n", + " Downloading langchain_openai-0.1.19-py3-none-any.whl.metadata (2.6 kB)\n", + "Requirement already satisfied: langchain-core<0.3.0,>=0.2.24 in /usr/local/lib/python3.10/dist-packages (from langchain-openai) (0.2.24)\n", + "Collecting openai<2.0.0,>=1.32.0 (from langchain-openai)\n", + " Downloading openai-1.37.1-py3-none-any.whl.metadata (22 kB)\n", + "Collecting tiktoken<1,>=0.7 (from langchain-openai)\n", + " Downloading tiktoken-0.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.6 kB)\n", + "Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.3.0,>=0.2.24->langchain-openai) (6.0.1)\n", + "Requirement already satisfied: jsonpatch<2.0,>=1.33 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.3.0,>=0.2.24->langchain-openai) (1.33)\n", + "Requirement already satisfied: langsmith<0.2.0,>=0.1.75 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.3.0,>=0.2.24->langchain-openai) (0.1.93)\n", + "Requirement already satisfied: packaging<25,>=23.2 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.3.0,>=0.2.24->langchain-openai) (24.1)\n", + "Requirement already satisfied: pydantic<3,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.3.0,>=0.2.24->langchain-openai) (2.8.2)\n", + "Requirement already satisfied: tenacity!=8.4.0,<9.0.0,>=8.1.0 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.3.0,>=0.2.24->langchain-openai) (8.5.0)\n", + "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.32.0->langchain-openai) (3.7.1)\n", + "Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai<2.0.0,>=1.32.0->langchain-openai) (1.7.0)\n", + "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.32.0->langchain-openai) (0.27.0)\n", + "Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.32.0->langchain-openai) (1.3.1)\n", + "Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.32.0->langchain-openai) (4.66.4)\n", + "Requirement already satisfied: typing-extensions<5,>=4.7 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.32.0->langchain-openai) (4.12.2)\n", + "Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.10/dist-packages (from tiktoken<1,>=0.7->langchain-openai) (2024.5.15)\n", + "Requirement already satisfied: requests>=2.26.0 in /usr/local/lib/python3.10/dist-packages (from tiktoken<1,>=0.7->langchain-openai) (2.31.0)\n", + "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai<2.0.0,>=1.32.0->langchain-openai) (3.7)\n", + "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai<2.0.0,>=1.32.0->langchain-openai) (1.2.2)\n", + "Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.32.0->langchain-openai) (2024.7.4)\n", + "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.32.0->langchain-openai) (1.0.5)\n", + "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai<2.0.0,>=1.32.0->langchain-openai) (0.14.0)\n", + "Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.10/dist-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3.0,>=0.2.24->langchain-openai) (3.0.0)\n", + "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in /usr/local/lib/python3.10/dist-packages (from langsmith<0.2.0,>=0.1.75->langchain-core<0.3.0,>=0.2.24->langchain-openai) (3.10.6)\n", + "Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain-core<0.3.0,>=0.2.24->langchain-openai) (0.7.0)\n", + "Requirement already satisfied: pydantic-core==2.20.1 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain-core<0.3.0,>=0.2.24->langchain-openai) (2.20.1)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken<1,>=0.7->langchain-openai) (3.3.2)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken<1,>=0.7->langchain-openai) (2.0.7)\n", + "Downloading langchain_openai-0.1.19-py3-none-any.whl (47 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m47.1/47.1 kB\u001b[0m \u001b[31m1.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading openai-1.37.1-py3-none-any.whl (337 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m337.0/337.0 kB\u001b[0m \u001b[31m7.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading tiktoken-0.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.1/1.1 MB\u001b[0m \u001b[31m31.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hInstalling collected packages: tiktoken, openai, langchain-openai\n", + "Successfully installed langchain-openai-0.1.19 openai-1.37.1 tiktoken-0.7.0\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "!pip install localhost-on-colab\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "uvGNbruE9UHY", + "outputId": "6a0bebe5-2eb4-4539-a802-0954c2f63db6" + }, + "execution_count": 37, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Collecting localhost-on-colab\n", + " Downloading localhost_on_colab-0.0.0-py3-none-any.whl.metadata (1.7 kB)\n", + "Requirement already satisfied: pyngrok>=5.0.5 in /usr/local/lib/python3.10/dist-packages (from localhost-on-colab) (7.2.0)\n", + "Requirement already satisfied: PyYAML>=5.4.1 in /usr/local/lib/python3.10/dist-packages (from localhost-on-colab) (6.0.1)\n", + "Downloading localhost_on_colab-0.0.0-py3-none-any.whl (2.9 kB)\n", + "Installing collected packages: localhost-on-colab\n", + "Successfully installed localhost-on-colab-0.0.0\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "from fastapi import FastAPI\n", + "import uvicorn\n", + "from langchain_core.prompts import ChatPromptTemplate\n", + "from langchain_core.output_parsers import StrOutputParser\n", + "from langserve import add_routes\n", + "\n", + "# Create FastAPI app\n", + "app = FastAPI(\n", + " title=\"LangChain Server\",\n", + " version=\"1.0\",\n", + " description=\"A simple API server using LangChain's Runnable interfaces\",\n", + ")\n", + "\n", + "# Define your prompt template\n", + "system_template = \"Translate the following into {language}:\"\n", + "prompt_template = ChatPromptTemplate.from_messages([\n", + " ('system', system_template),\n", + " ('user', '{text}')\n", + "])\n", + "\n", + "# Define your Gemini model and parser\n", + "model = ChatVertexAI(model=\"gemini-1.5-flash\", project_id=\"mystical-atlas-430903-v1\")\n", + "parser = StrOutputParser()\n", + "\n", + "# Create the chain\n", + "chain = prompt_template | model | parser\n", + "\n", + "# Add routes\n", + "add_routes(app, chain, path=\"/chain\")\n", + "\n", + "# Function to run the server\n", + "def run_server():\n", + " uvicorn.run(app, host=\"localhost\", port=8001)\n" + ], + "metadata": { + "id": "7msMdtzD2R6Z" + }, + "execution_count": 54, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "!pip install localhost-on-colab" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "7PXv6FPA-5JS", + "outputId": "fa002703-1847-468a-dc1a-10d306cdb00d" + }, + "execution_count": 41, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Requirement already satisfied: localhost-on-colab in /usr/local/lib/python3.10/dist-packages (0.0.0)\n", + "Requirement already satisfied: pyngrok>=5.0.5 in /usr/local/lib/python3.10/dist-packages (from localhost-on-colab) (7.2.0)\n", + "Requirement already satisfied: PyYAML>=5.4.1 in /usr/local/lib/python3.10/dist-packages (from localhost-on-colab) (6.0.1)\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "!pip install pyngrok\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "h_viEsoB_QJo", + "outputId": "519275a5-7ba3-4d09-b0b8-7039bf2d8c75" + }, + "execution_count": 43, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Requirement already satisfied: pyngrok in /usr/local/lib/python3.10/dist-packages (7.2.0)\n", + "Requirement already satisfied: PyYAML>=5.1 in /usr/local/lib/python3.10/dist-packages (from pyngrok) (6.0.1)\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "import getpass\n", + "\n", + "from pyngrok import ngrok, conf\n", + "\n", + "print(\"Enter your authtoken, which can be copied from https://dashboard.ngrok.com/auth\")\n", + "conf.get_default().auth_token = getpass.getpass()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "m79tgBNS_X4C", + "outputId": "18663de8-c8b3-46f2-fb69-cc1b18a6c435" + }, + "execution_count": 49, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter your authtoken, which can be copied from https://dashboard.ngrok.com/auth\n", + "··········\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "from localhost_on_colab.core import LocalHostOnColab\n", + "\n", + "# Define the function to run\n", + "def some_function():\n", + " import uvicorn\n", + " uvicorn.run(app, host=\"localhost\", port=8000)\n", + "\n", + "# Create an instance of LocalHostOnColab\n", + "bridge = LocalHostOnColab()\n", + "\n", + "# Run the function and get the URL\n", + "url = bridge.run(\n", + " function=some_function,\n", + " kwargs={},\n", + " port=8000\n", + ")\n", + "\n", + "print(\"Server is running at:\", url)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Nkio_bj833Ds", + "outputId": "5952b7a1-37fd-4cb8-e848-80b750b87f4c" + }, + "execution_count": 55, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Server is running at: https://553b-34-106-255-66.ngrok-free.app\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "mDP62b1b-9zQ" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file