Files
2025-07-03 20:56:38 +03:00

599 lines
37 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "c3ab171c-90f3-4481-afb8-d7420ae8f042",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: transformers in ./.venv/lib/python3.9/site-packages (4.50.0)\n",
"Collecting langchain\n",
" Downloading langchain-0.3.21-py3-none-any.whl.metadata (7.8 kB)\n",
"Requirement already satisfied: torch in ./.venv/lib/python3.9/site-packages (2.6.0)\n",
"Collecting langchain-community\n",
" Downloading langchain_community-0.3.20-py3-none-any.whl.metadata (2.4 kB)\n",
"Collecting langchain-huggingface\n",
" Downloading langchain_huggingface-0.1.2-py3-none-any.whl.metadata (1.3 kB)\n",
"Requirement already satisfied: filelock in ./.venv/lib/python3.9/site-packages (from transformers) (3.18.0)\n",
"Requirement already satisfied: huggingface-hub<1.0,>=0.26.0 in ./.venv/lib/python3.9/site-packages (from transformers) (0.29.3)\n",
"Requirement already satisfied: numpy>=1.17 in ./.venv/lib/python3.9/site-packages (from transformers) (2.0.2)\n",
"Requirement already satisfied: packaging>=20.0 in ./.venv/lib/python3.9/site-packages (from transformers) (24.2)\n",
"Requirement already satisfied: pyyaml>=5.1 in ./.venv/lib/python3.9/site-packages (from transformers) (6.0.2)\n",
"Requirement already satisfied: regex!=2019.12.17 in ./.venv/lib/python3.9/site-packages (from transformers) (2024.11.6)\n",
"Requirement already satisfied: requests in ./.venv/lib/python3.9/site-packages (from transformers) (2.32.3)\n",
"Requirement already satisfied: tokenizers<0.22,>=0.21 in ./.venv/lib/python3.9/site-packages (from transformers) (0.21.1)\n",
"Requirement already satisfied: safetensors>=0.4.3 in ./.venv/lib/python3.9/site-packages (from transformers) (0.5.3)\n",
"Requirement already satisfied: tqdm>=4.27 in ./.venv/lib/python3.9/site-packages (from transformers) (4.67.1)\n",
"Collecting langchain-core<1.0.0,>=0.3.45 (from langchain)\n",
" Downloading langchain_core-0.3.47-py3-none-any.whl.metadata (5.9 kB)\n",
"Collecting langchain-text-splitters<1.0.0,>=0.3.7 (from langchain)\n",
" Downloading langchain_text_splitters-0.3.7-py3-none-any.whl.metadata (1.9 kB)\n",
"Collecting langsmith<0.4,>=0.1.17 (from langchain)\n",
" Downloading langsmith-0.3.18-py3-none-any.whl.metadata (15 kB)\n",
"Collecting pydantic<3.0.0,>=2.7.4 (from langchain)\n",
" Downloading pydantic-2.10.6-py3-none-any.whl.metadata (30 kB)\n",
"Collecting SQLAlchemy<3,>=1.4 (from langchain)\n",
" Downloading sqlalchemy-2.0.39-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (9.6 kB)\n",
"Collecting async-timeout<5.0.0,>=4.0.0 (from langchain)\n",
" Downloading async_timeout-4.0.3-py3-none-any.whl.metadata (4.2 kB)\n",
"Requirement already satisfied: typing-extensions>=4.10.0 in ./.venv/lib/python3.9/site-packages (from torch) (4.12.2)\n",
"Requirement already satisfied: networkx in ./.venv/lib/python3.9/site-packages (from torch) (3.2.1)\n",
"Requirement already satisfied: jinja2 in ./.venv/lib/python3.9/site-packages (from torch) (3.1.6)\n",
"Requirement already satisfied: fsspec in ./.venv/lib/python3.9/site-packages (from torch) (2024.12.0)\n",
"Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.4.127 in ./.venv/lib/python3.9/site-packages (from torch) (12.4.127)\n",
"Requirement already satisfied: nvidia-cuda-runtime-cu12==12.4.127 in ./.venv/lib/python3.9/site-packages (from torch) (12.4.127)\n",
"Requirement already satisfied: nvidia-cuda-cupti-cu12==12.4.127 in ./.venv/lib/python3.9/site-packages (from torch) (12.4.127)\n",
"Requirement already satisfied: nvidia-cudnn-cu12==9.1.0.70 in ./.venv/lib/python3.9/site-packages (from torch) (9.1.0.70)\n",
"Requirement already satisfied: nvidia-cublas-cu12==12.4.5.8 in ./.venv/lib/python3.9/site-packages (from torch) (12.4.5.8)\n",
"Requirement already satisfied: nvidia-cufft-cu12==11.2.1.3 in ./.venv/lib/python3.9/site-packages (from torch) (11.2.1.3)\n",
"Requirement already satisfied: nvidia-curand-cu12==10.3.5.147 in ./.venv/lib/python3.9/site-packages (from torch) (10.3.5.147)\n",
"Requirement already satisfied: nvidia-cusolver-cu12==11.6.1.9 in ./.venv/lib/python3.9/site-packages (from torch) (11.6.1.9)\n",
"Requirement already satisfied: nvidia-cusparse-cu12==12.3.1.170 in ./.venv/lib/python3.9/site-packages (from torch) (12.3.1.170)\n",
"Requirement already satisfied: nvidia-cusparselt-cu12==0.6.2 in ./.venv/lib/python3.9/site-packages (from torch) (0.6.2)\n",
"Requirement already satisfied: nvidia-nccl-cu12==2.21.5 in ./.venv/lib/python3.9/site-packages (from torch) (2.21.5)\n",
"Requirement already satisfied: nvidia-nvtx-cu12==12.4.127 in ./.venv/lib/python3.9/site-packages (from torch) (12.4.127)\n",
"Requirement already satisfied: nvidia-nvjitlink-cu12==12.4.127 in ./.venv/lib/python3.9/site-packages (from torch) (12.4.127)\n",
"Requirement already satisfied: triton==3.2.0 in ./.venv/lib/python3.9/site-packages (from torch) (3.2.0)\n",
"Requirement already satisfied: sympy==1.13.1 in ./.venv/lib/python3.9/site-packages (from torch) (1.13.1)\n",
"Requirement already satisfied: mpmath<1.4,>=1.1.0 in ./.venv/lib/python3.9/site-packages (from sympy==1.13.1->torch) (1.3.0)\n",
"Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in ./.venv/lib/python3.9/site-packages (from langchain-community) (3.11.14)\n",
"Collecting tenacity!=8.4.0,<10,>=8.1.0 (from langchain-community)\n",
" Downloading tenacity-9.0.0-py3-none-any.whl.metadata (1.2 kB)\n",
"Collecting dataclasses-json<0.7,>=0.5.7 (from langchain-community)\n",
" Downloading dataclasses_json-0.6.7-py3-none-any.whl.metadata (25 kB)\n",
"Collecting pydantic-settings<3.0.0,>=2.4.0 (from langchain-community)\n",
" Downloading pydantic_settings-2.8.1-py3-none-any.whl.metadata (3.5 kB)\n",
"Collecting httpx-sse<1.0.0,>=0.4.0 (from langchain-community)\n",
" Downloading httpx_sse-0.4.0-py3-none-any.whl.metadata (9.0 kB)\n",
"Collecting sentence-transformers>=2.6.0 (from langchain-huggingface)\n",
" Downloading sentence_transformers-3.4.1-py3-none-any.whl.metadata (10 kB)\n",
"Requirement already satisfied: aiohappyeyeballs>=2.3.0 in ./.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (2.6.1)\n",
"Requirement already satisfied: aiosignal>=1.1.2 in ./.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.3.2)\n",
"Requirement already satisfied: attrs>=17.3.0 in ./.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (25.3.0)\n",
"Requirement already satisfied: frozenlist>=1.1.1 in ./.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.5.0)\n",
"Requirement already satisfied: multidict<7.0,>=4.5 in ./.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (6.2.0)\n",
"Requirement already satisfied: propcache>=0.2.0 in ./.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (0.3.0)\n",
"Requirement already satisfied: yarl<2.0,>=1.17.0 in ./.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.18.3)\n",
"Collecting marshmallow<4.0.0,>=3.18.0 (from dataclasses-json<0.7,>=0.5.7->langchain-community)\n",
" Downloading marshmallow-3.26.1-py3-none-any.whl.metadata (7.3 kB)\n",
"Collecting typing-inspect<1,>=0.4.0 (from dataclasses-json<0.7,>=0.5.7->langchain-community)\n",
" Downloading typing_inspect-0.9.0-py3-none-any.whl.metadata (1.5 kB)\n",
"Collecting jsonpatch<2.0,>=1.33 (from langchain-core<1.0.0,>=0.3.45->langchain)\n",
" Downloading jsonpatch-1.33-py2.py3-none-any.whl.metadata (3.0 kB)\n",
"Collecting httpx<1,>=0.23.0 (from langsmith<0.4,>=0.1.17->langchain)\n",
" Downloading httpx-0.28.1-py3-none-any.whl.metadata (7.1 kB)\n",
"Collecting orjson<4.0.0,>=3.9.14 (from langsmith<0.4,>=0.1.17->langchain)\n",
" Downloading orjson-3.10.15-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (41 kB)\n",
"Collecting requests-toolbelt<2.0.0,>=1.0.0 (from langsmith<0.4,>=0.1.17->langchain)\n",
" Downloading requests_toolbelt-1.0.0-py2.py3-none-any.whl.metadata (14 kB)\n",
"Collecting zstandard<0.24.0,>=0.23.0 (from langsmith<0.4,>=0.1.17->langchain)\n",
" Downloading zstandard-0.23.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (3.0 kB)\n",
"Collecting annotated-types>=0.6.0 (from pydantic<3.0.0,>=2.7.4->langchain)\n",
" Downloading annotated_types-0.7.0-py3-none-any.whl.metadata (15 kB)\n",
"Collecting pydantic-core==2.27.2 (from pydantic<3.0.0,>=2.7.4->langchain)\n",
" Downloading pydantic_core-2.27.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.6 kB)\n",
"Collecting python-dotenv>=0.21.0 (from pydantic-settings<3.0.0,>=2.4.0->langchain-community)\n",
" Downloading python_dotenv-1.0.1-py3-none-any.whl.metadata (23 kB)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in ./.venv/lib/python3.9/site-packages (from requests->transformers) (3.4.1)\n",
"Requirement already satisfied: idna<4,>=2.5 in ./.venv/lib/python3.9/site-packages (from requests->transformers) (3.10)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in ./.venv/lib/python3.9/site-packages (from requests->transformers) (2.3.0)\n",
"Requirement already satisfied: certifi>=2017.4.17 in ./.venv/lib/python3.9/site-packages (from requests->transformers) (2025.1.31)\n",
"Collecting scikit-learn (from sentence-transformers>=2.6.0->langchain-huggingface)\n",
" Downloading scikit_learn-1.6.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (18 kB)\n",
"Collecting scipy (from sentence-transformers>=2.6.0->langchain-huggingface)\n",
" Downloading scipy-1.13.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (60 kB)\n",
"Collecting Pillow (from sentence-transformers>=2.6.0->langchain-huggingface)\n",
" Downloading pillow-11.1.0-cp39-cp39-manylinux_2_28_x86_64.whl.metadata (9.1 kB)\n",
"Collecting greenlet!=0.4.17 (from SQLAlchemy<3,>=1.4->langchain)\n",
" Downloading greenlet-3.1.1-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.metadata (3.8 kB)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in ./.venv/lib/python3.9/site-packages (from jinja2->torch) (3.0.2)\n",
"Collecting anyio (from httpx<1,>=0.23.0->langsmith<0.4,>=0.1.17->langchain)\n",
" Downloading anyio-4.9.0-py3-none-any.whl.metadata (4.7 kB)\n",
"Collecting httpcore==1.* (from httpx<1,>=0.23.0->langsmith<0.4,>=0.1.17->langchain)\n",
" Downloading httpcore-1.0.7-py3-none-any.whl.metadata (21 kB)\n",
"Collecting h11<0.15,>=0.13 (from httpcore==1.*->httpx<1,>=0.23.0->langsmith<0.4,>=0.1.17->langchain)\n",
" Downloading h11-0.14.0-py3-none-any.whl.metadata (8.2 kB)\n",
"Collecting jsonpointer>=1.9 (from jsonpatch<2.0,>=1.33->langchain-core<1.0.0,>=0.3.45->langchain)\n",
" Downloading jsonpointer-3.0.0-py2.py3-none-any.whl.metadata (2.3 kB)\n",
"Collecting mypy-extensions>=0.3.0 (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain-community)\n",
" Downloading mypy_extensions-1.0.0-py3-none-any.whl.metadata (1.1 kB)\n",
"Collecting joblib>=1.2.0 (from scikit-learn->sentence-transformers>=2.6.0->langchain-huggingface)\n",
" Downloading joblib-1.4.2-py3-none-any.whl.metadata (5.4 kB)\n",
"Collecting threadpoolctl>=3.1.0 (from scikit-learn->sentence-transformers>=2.6.0->langchain-huggingface)\n",
" Downloading threadpoolctl-3.6.0-py3-none-any.whl.metadata (13 kB)\n",
"Requirement already satisfied: exceptiongroup>=1.0.2 in ./.venv/lib/python3.9/site-packages (from anyio->httpx<1,>=0.23.0->langsmith<0.4,>=0.1.17->langchain) (1.2.2)\n",
"Collecting sniffio>=1.1 (from anyio->httpx<1,>=0.23.0->langsmith<0.4,>=0.1.17->langchain)\n",
" Downloading sniffio-1.3.1-py3-none-any.whl.metadata (3.9 kB)\n",
"Downloading langchain-0.3.21-py3-none-any.whl (1.0 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m2.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hDownloading langchain_community-0.3.20-py3-none-any.whl (2.5 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.5/2.5 MB\u001b[0m \u001b[31m3.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hDownloading langchain_huggingface-0.1.2-py3-none-any.whl (21 kB)\n",
"Downloading async_timeout-4.0.3-py3-none-any.whl (5.7 kB)\n",
"Downloading dataclasses_json-0.6.7-py3-none-any.whl (28 kB)\n",
"Downloading httpx_sse-0.4.0-py3-none-any.whl (7.8 kB)\n",
"Downloading langchain_core-0.3.47-py3-none-any.whl (417 kB)\n",
"Downloading langchain_text_splitters-0.3.7-py3-none-any.whl (32 kB)\n",
"Downloading langsmith-0.3.18-py3-none-any.whl (351 kB)\n",
"Downloading pydantic-2.10.6-py3-none-any.whl (431 kB)\n",
"Downloading pydantic_core-2.27.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m7.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hDownloading pydantic_settings-2.8.1-py3-none-any.whl (30 kB)\n",
"Downloading sentence_transformers-3.4.1-py3-none-any.whl (275 kB)\n",
"Downloading sqlalchemy-2.0.39-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.1 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.1/3.1 MB\u001b[0m \u001b[31m7.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0mm\n",
"\u001b[?25hDownloading tenacity-9.0.0-py3-none-any.whl (28 kB)\n",
"Downloading annotated_types-0.7.0-py3-none-any.whl (13 kB)\n",
"Downloading greenlet-3.1.1-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (597 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m597.4/597.4 kB\u001b[0m \u001b[31m4.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading httpx-0.28.1-py3-none-any.whl (73 kB)\n",
"Downloading httpcore-1.0.7-py3-none-any.whl (78 kB)\n",
"Downloading jsonpatch-1.33-py2.py3-none-any.whl (12 kB)\n",
"Downloading marshmallow-3.26.1-py3-none-any.whl (50 kB)\n",
"Downloading orjson-3.10.15-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (130 kB)\n",
"Downloading python_dotenv-1.0.1-py3-none-any.whl (19 kB)\n",
"Downloading requests_toolbelt-1.0.0-py2.py3-none-any.whl (54 kB)\n",
"Downloading typing_inspect-0.9.0-py3-none-any.whl (8.8 kB)\n",
"Downloading zstandard-0.23.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.4 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.4/5.4 MB\u001b[0m \u001b[31m8.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0mm\n",
"\u001b[?25hDownloading pillow-11.1.0-cp39-cp39-manylinux_2_28_x86_64.whl (4.5 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m4.5/4.5 MB\u001b[0m \u001b[31m10.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hDownloading scikit_learn-1.6.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.5 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m13.5/13.5 MB\u001b[0m \u001b[31m9.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0mta \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hDownloading scipy-1.13.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (38.6 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m38.6/38.6 MB\u001b[0m \u001b[31m13.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hDownloading joblib-1.4.2-py3-none-any.whl (301 kB)\n",
"Downloading jsonpointer-3.0.0-py2.py3-none-any.whl (7.6 kB)\n",
"Downloading mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB)\n",
"Downloading threadpoolctl-3.6.0-py3-none-any.whl (18 kB)\n",
"Downloading anyio-4.9.0-py3-none-any.whl (100 kB)\n",
"Downloading h11-0.14.0-py3-none-any.whl (58 kB)\n",
"Downloading sniffio-1.3.1-py3-none-any.whl (10 kB)\n",
"Installing collected packages: zstandard, threadpoolctl, tenacity, sniffio, scipy, python-dotenv, pydantic-core, Pillow, orjson, mypy-extensions, marshmallow, jsonpointer, joblib, httpx-sse, h11, greenlet, async-timeout, annotated-types, typing-inspect, SQLAlchemy, scikit-learn, requests-toolbelt, pydantic, jsonpatch, httpcore, anyio, pydantic-settings, httpx, dataclasses-json, langsmith, sentence-transformers, langchain-core, langchain-text-splitters, langchain-huggingface, langchain, langchain-community\n",
" Attempting uninstall: async-timeout\n",
" Found existing installation: async-timeout 5.0.1\n",
" Uninstalling async-timeout-5.0.1:\n",
" Successfully uninstalled async-timeout-5.0.1\n",
"Successfully installed Pillow-11.1.0 SQLAlchemy-2.0.39 annotated-types-0.7.0 anyio-4.9.0 async-timeout-4.0.3 dataclasses-json-0.6.7 greenlet-3.1.1 h11-0.14.0 httpcore-1.0.7 httpx-0.28.1 httpx-sse-0.4.0 joblib-1.4.2 jsonpatch-1.33 jsonpointer-3.0.0 langchain-0.3.21 langchain-community-0.3.20 langchain-core-0.3.47 langchain-huggingface-0.1.2 langchain-text-splitters-0.3.7 langsmith-0.3.18 marshmallow-3.26.1 mypy-extensions-1.0.0 orjson-3.10.15 pydantic-2.10.6 pydantic-core-2.27.2 pydantic-settings-2.8.1 python-dotenv-1.0.1 requests-toolbelt-1.0.0 scikit-learn-1.6.1 scipy-1.13.1 sentence-transformers-3.4.1 sniffio-1.3.1 tenacity-9.0.0 threadpoolctl-3.6.0 typing-inspect-0.9.0 zstandard-0.23.0\n"
]
}
],
"source": [
"!pip install transformers langchain torch langchain-community langchain-huggingface"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "4156b8a6-585a-4a16-aa00-270270a33f44",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Device set to use cuda:0\n"
]
}
],
"source": [
"from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline\n",
"from transformers import T5Tokenizer, T5ForConditionalGeneration\n",
"from langchain_huggingface import HuggingFacePipeline\n",
"\n",
"model_name = \"gpt2\"\n",
"\n",
"if model_name.startswith(\"t5\"):\n",
" tokenizer = T5Tokenizer.from_pretrained(model_name)\n",
" model = T5ForConditionalGeneration.from_pretrained(model_name)\n",
"\n",
" hf_pipeline = pipeline(\n",
" \"text2text-generation\",\n",
" model=model,\n",
" tokenizer=tokenizer,\n",
" max_length=1024,\n",
" max_new_tokens=50,\n",
" truncation=True\n",
" )\n",
"else:\n",
" tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
" model = AutoModelForCausalLM.from_pretrained(model_name)\n",
"\n",
" tokenizer.pad_token = tokenizer.eos_token\n",
"\n",
" hf_pipeline = pipeline(\n",
" \"text-generation\",\n",
" model=model,\n",
" tokenizer=tokenizer,\n",
" max_length=512,\n",
" max_new_tokens=50,\n",
" truncation=True\n",
" )\n",
"\n",
"llm = HuggingFacePipeline(pipeline=hf_pipeline)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "bb2f39eb-228a-4296-a13d-a644ecc1e92f",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_93712/2625397836.py:4: LangChainDeprecationWarning: The method `BaseLLM.__call__` was deprecated in langchain-core 0.1.7 and will be removed in 1.0. Use :meth:`~invoke` instead.\n",
" output = llm(prompt)\n"
]
},
{
"data": {
"text/plain": [
"\"How are you? Where are you from? What are you doing? Why do you work here? How else can you tell us? When or where are you staying? If you are not sure, and you come here before 10pm, please call 0845 2138 (please allow 2-5 business hours to be used as this is all private). Please note this is a small place, of small-sized population, so if you have any trouble coming to our place of worship, it will be welcomed with open arms. You never have to worry about that we are not here to do any work for you, you may go and stay here as you please. And if you find that you aren't well, please call us, we will fix it! Don't forget to pay the staff when they are available.\\n\\nIt is really nice to welcome some of the best local restaurants and cafes in Newcastle. We are very sorry that you are not enjoying these wonderful places or services the way you do.\""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"### Try this for gpt2 and t5-small\n",
"prompt = \"How are you?\"\n",
"\n",
"output = llm(prompt)\n",
"\n",
"output"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a2ca590c-5615-4936-a60c-306935f0e0be",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"translate English to French: How are you?\\n\\n(It appears, however, that this wasn't quite the answer)\\n\\nWhy is my French so much better?\""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain.prompts import PromptTemplate, ChatPromptTemplate\n",
"\n",
"template = PromptTemplate(\n",
" input_variables=[\"text\"],\n",
" template=\"translate English to French: {text}\"\n",
")\n",
"\n",
"### Try this for gpt2 and t5-small\n",
"prompt_text = template.format(text=\"How are you?\")\n",
"\n",
"response = llm(prompt_text)\n",
"\n",
"response"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "90fbf82e-9f50-41b3-b4d2-83b41f2ed33e",
"metadata": {},
"outputs": [],
"source": [
"template_string = \"\"\"You be tasked with takin' the followin' text \\\n",
"and transformin' it into a joke in the style of {style}. \\\n",
"Make sure it stays true to the humor and tone of the given style. \\\n",
"If the text ain't naturally a joke, twist it into somethin' funny! \n",
"\n",
"text: ```{text}```\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "7e318633-7238-4f1b-bae6-8e346ad48cb3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"PromptTemplate(input_variables=['style', 'text'], input_types={}, partial_variables={}, template=\"You be tasked with takin' the followin' text and transformin' it into a joke in the style of {style}. Make sure it stays true to the humor and tone of the given style. If the text ain't naturally a joke, twist it into somethin' funny! \\n\\ntext: ```{text}```\\n\")"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt_template = PromptTemplate.from_template(template_string)\n",
"\n",
"prompt_template"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9c5f95c9-f205-4bd1-b5d6-1cb29b66f93d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['style', 'text']"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt_template.input_variables"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d8875a55-bf80-4132-b6ac-b53fd8f61c7b",
"metadata": {},
"outputs": [],
"source": [
"prompt_style = \"\"\"pirate\"\"\"\n",
"\n",
"prompt_input = \"\"\"Why do programmers prefer dark mode?\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "189f60ca-f228-4d7f-8bba-9951e56a90f7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'You be tasked with takin\\' the followin\\' text and transformin\\' it into a joke in the style of pirate. Make sure it stays true to the humor and tone of the given style. If the text ain\\'t naturally a joke, twist it into somethin\\' funny! \\n\\ntext: ```Why do programmers prefer dark mode?```\\n\\nIf the text you create needs to be a joke rather than a joke in the style of a pirate you\\'ve set up some parameters to control the output. If you decide the result of a certain method is too hard to follow then just add it (the name of the method to be used) to the end of the text.\\n\\nThe easiest way to start converting this is to run: python c.py convertrtext.py \"text: \"``\"\\n\\nThat will bring up a terminal window to start formatting you text.\\n\\n\\nNote The font will be highlighted and changed if you change the font, and also change the text of the message in your text window.\\n\\nCancellations\\n\\nIf you make any changes to a text object during its rendering, your file will be removed, or there will be no output.\\n\\nIf the text object fails to receive an event from the terminal, you can tell it to call a function from the terminal and then use the callback to get back the event and perform the formatting.\\n\\nThe callback, called when the specified text is replaced by another text, creates a callback in your function that tells the terminal it needs to show the text instead of the callback.\\n\\nAs long as it has done so it doesn\\'t need to do anything, because the terminal will keep adding a new button and returning the same text.\\n\\nThis approach works really well for small files, but for large files, it\\'s not particularly good.\\n\\nCustomization\\n\\nSo what\\'s the use?\\n\\nIt takes long enough to do all the formatting stuff for every text that you want, but a few lines of code in your function should let you easily generate different styles to fit your needs. So use this for your own code.\\n\\nThe syntax for the function to be used changes (add, remove) to your variable name. But don\\'t go and do everything yourself, be aware sometimes text is going to be corrupted.\\n\\nYour main argument(s) for the format is where it belongs. It\\'s always important to know what your local variables are, so that if something'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"message = prompt_template.format(\n",
" style=prompt_style,\n",
" text=prompt_input)\n",
"\n",
"response = llm(message)\n",
"\n",
"response"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "0e332795-ec3f-4a1b-9026-209c990096c6",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_93712/3882055530.py:7: LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 1.0. Use :meth:`~RunnableSequence, e.g., `prompt | llm`` instead.\n",
" chain = LLMChain(llm=llm, prompt=prompt)\n"
]
},
{
"data": {
"text/plain": [
"{'topic': 'why pirates love gold',\n",
" 'style': 'pirate',\n",
" 'text': 'Human: Tell a joke about why pirates love gold in the style of pirate.\\n\\nAthletic: A slang for getting \"up next to a woman for the first time.\"\\n\\nAstros: The original English abbreviation for the \"Astros league.\" Originally a slang term used to describe amateur sailors from coast-to-coast. Also known as \"The Astroglios.\"\\n\\nAlaska: A Native American word for \"the land in which cattle and human beings and fish breed.\" Its name was derived from the Indian name which is derived from the Hawaiian word for \"meadow.\"'}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain.chains import LLMChain\n",
"\n",
"prompt = ChatPromptTemplate.from_template(\n",
" \"Tell a joke about {topic} in the style of {style}.\"\n",
")\n",
"\n",
"chain = LLMChain(llm=llm, prompt=prompt)\n",
"\n",
"# Example Input\n",
"topic = \"why pirates love gold\"\n",
"style = \"pirate\"\n",
"\n",
"chain.invoke({\"topic\": topic, \"style\": style})"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "cc8f4433-219c-4b42-a317-b93545bc5b20",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Both `max_new_tokens` (=50) and `max_length`(=512) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Both `max_new_tokens` (=50) and `max_length`(=512) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)\n",
"Both `max_new_tokens` (=50) and `max_length`(=512) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)\n",
"Both `max_new_tokens` (=50) and `max_length`(=512) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"{'Review': \"Je trouve le goût médiocre. La mousse ne tient pas, c'est bizarre. J'achète les mêmes dans le commerce et le goût est bien meilleur...\\nVieux lot ou contrefaçon !?\",\n",
" 'English_Review': 'Human: Translate the following review to english:\\n\\nJe trouve le goût médiocre. La mousse ne tient pas, c\\'est bizarre. J\\'achète les mêmes dans le commerce et le goût est bien meilleur...\\nVieux lot ou contrefaçon !? (If we can get two, we want an extra.) And the \"fairy tales\" are, of course, very much a part of what I love about this story. They\\'re not that difficult to write. They\\'re not that hard to',\n",
" 'summary': 'Human: Can you summarize the following review in 1 sentence:\\n\\nHuman: Translate the following review to english:\\n\\nJe trouve le goût médiocre. La mousse ne tient pas, c\\'est bizarre. J\\'achète les mêmes dans le commerce et le goût est bien meilleur...\\nVieux lot ou contrefaçon !? (If we can get two, we want an extra.) And the \"fairy tales\" are, of course, very much a part of what I love about this story. They\\'re not that difficult to write. They\\'re not that hard to translate. I know people like to write these, but it isn\\'t like I can\\'t imagine some sort of magic. (And I know it\\'s not just that we don\\'t have time to take the time to translate the stories.)\\n\\nI',\n",
" 'followup_message': 'Human: Write a follow up response to the following summary in the specified language:\\n\\nSummary: Human: Can you summarize the following review in 1 sentence:\\n\\nHuman: Translate the following review to english:\\n\\nJe trouve le goût médiocre. La mousse ne tient pas, c\\'est bizarre. J\\'achète les mêmes dans le commerce et le goût est bien meilleur...\\nVieux lot ou contrefaçon !? (If we can get two, we want an extra.) And the \"fairy tales\" are, of course, very much a part of what I love about this story. They\\'re not that difficult to write. They\\'re not that hard to translate. I know people like to write these, but it isn\\'t like I can\\'t imagine some sort of magic. (And I know it\\'s not just that we don\\'t have time to take the time to translate the stories.)\\n\\nI\\n\\nLanguage: Human: What language is the following review:\\n\\nJe trouve le goût médiocre. La mousse ne tient pas, c\\'est bizarre. J\\'achète les mêmes dans le commerce et le goût est bien meilleur...\\nVieux lot ou contrefaçon !?\\n\\nThe following conversation takes place in Paris (the first time I\\'ve been here, in fact, I had been here, my French life was mostly a miserable situation, but my French language was far from perfect, so I had no problems finding words and words that went across the top of me when writing it; sometimes I couldn\\'t even hear how it sounded at least not very bad) and the French man is a really nice conversationalist. The entire conversation begins like this:\\n'}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"## SequentialChain\n",
"\n",
"from langchain.chains import SequentialChain,LLMChain\n",
"\n",
"first_prompt = ChatPromptTemplate.from_template(\n",
" \"Translate the following review to english:\"\n",
" \"\\n\\n{Review}\"\n",
")\n",
"\n",
"chain_one = LLMChain(llm=llm, prompt=first_prompt, \n",
" output_key=\"English_Review\"\n",
" )\n",
"second_prompt = ChatPromptTemplate.from_template(\n",
" \"Can you summarize the following review in 1 sentence:\"\n",
" \"\\n\\n{English_Review}\"\n",
")\n",
"\n",
"chain_two = LLMChain(llm=llm, prompt=second_prompt, \n",
" output_key=\"summary\"\n",
" )\n",
"\n",
"third_prompt = ChatPromptTemplate.from_template(\n",
" \"What language is the following review:\\n\\n{Review}\"\n",
")\n",
"\n",
"chain_three = LLMChain(llm=llm, prompt=third_prompt,\n",
" output_key=\"language\"\n",
" )\n",
"\n",
"fourth_prompt = ChatPromptTemplate.from_template(\n",
" \"Write a follow up response to the following \"\n",
" \"summary in the specified language:\"\n",
" \"\\n\\nSummary: {summary}\\n\\nLanguage: {language}\"\n",
")\n",
"\n",
"chain_four = LLMChain(llm=llm, prompt=fourth_prompt,\n",
" output_key=\"followup_message\"\n",
" )\n",
"\n",
"overall_chain = SequentialChain(\n",
" chains=[chain_one, chain_two, chain_three, chain_four],\n",
" input_variables=[\"Review\"],\n",
" output_variables=[\"English_Review\", \"summary\",\"followup_message\"],\n",
" verbose=True\n",
")\n",
"\n",
"review = \"Je trouve le goût médiocre. La mousse ne tient pas, c'est bizarre. J'achète les mêmes dans le commerce et le goût est bien meilleur...\\nVieux lot ou contrefaçon !?\"\n",
"\n",
"overall_chain(review)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "379e9e00-5fae-46a2-ad79-376abeb21f08",
"metadata": {},
"outputs": [],
"source": [
"## Router Chain\n",
"from langchain.chains.router import MultiPromptChain\n",
"from langchain.chains.router.llm_router import LLMRouterChain, RouterOutputParser\n",
"\n",
"## .... "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "610eebe2-8b6f-493e-a248-6e7b43e28157",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.21"
}
},
"nbformat": 4,
"nbformat_minor": 5
}