Add code and env info
This commit is contained in:
152
chapter01/Chapter 1 - Introduction to Language Models.ipynb
Normal file
152
chapter01/Chapter 1 - Introduction to Language Models.ipynb
Normal file
@@ -0,0 +1,152 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "EDe7DsPWmEBV"
|
||||
},
|
||||
"source": [
|
||||
"<h1>Chapter 1 - Introduction to Language Models</h1>\n",
|
||||
"<i>Exploring the exciting field of Language AI</i>\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"<a href=\"https://www.amazon.com/Hands-Large-Language-Models-Understanding/dp/1098150961\"><img src=\"https://img.shields.io/badge/Buy%20the%20Book!-grey?logo=amazon\"></a>\n",
|
||||
"<a href=\"https://www.oreilly.com/library/view/hands-on-large-language/9781098150952/\"><img src=\"https://img.shields.io/badge/O'Reilly-white.svg?logo=data:image/svg%2bxml;base64,PHN2ZyB3aWR0aD0iMzQiIGhlaWdodD0iMjciIHZpZXdCb3g9IjAgMCAzNCAyNyIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPGNpcmNsZSBjeD0iMTMiIGN5PSIxNCIgcj0iMTEiIHN0cm9rZT0iI0Q0MDEwMSIgc3Ryb2tlLXdpZHRoPSI0Ii8+CjxjaXJjbGUgY3g9IjMwLjUiIGN5PSIzLjUiIHI9IjMuNSIgZmlsbD0iI0Q0MDEwMSIvPgo8L3N2Zz4K\"></a>\n",
|
||||
"<a href=\"https://github.com/HandsOnLLM/Hands-On-Large-Language-Models\"><img src=\"https://img.shields.io/badge/GitHub%20Repository-black?logo=github\"></a>\n",
|
||||
"[](https://colab.research.google.com/drive/1NOo_hNGE8MuXAUa6UXh3eHzt9X_1mYvc?usp=sharing)\n",
|
||||
"\n",
|
||||
"---\n",
|
||||
"\n",
|
||||
"This notebook is for Chapter 1 of the [Hands-On Large Language Models](https://www.amazon.com/Hands-Large-Language-Models-Understanding/dp/1098150961) book by [Jay Alammar](https://www.linkedin.com/in/jalammar) and [Maarten Grootendorst](https://www.linkedin.com/in/mgrootendorst/).\n",
|
||||
"\n",
|
||||
"---\n",
|
||||
"\n",
|
||||
"<a href=\"https://www.amazon.com/Hands-Large-Language-Models-Understanding/dp/1098150961\">\n",
|
||||
"<img src=\"https://raw.githubusercontent.com/HandsOnLLM/Hands-On-Large-Language-Models/main/images/book_cover.png\" width=\"350\"/></a>\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "hXp09JFsFBXi"
|
||||
},
|
||||
"source": [
|
||||
"# Phi-3\n",
|
||||
"\n",
|
||||
"The first step is to load our model onto the GPU for faster inference. Note that we load the model and tokenizer separately (although that isn't always necessary)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"id": "RSNalRXZyTTk"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from transformers import AutoModelForCausalLM, AutoTokenizer\n",
|
||||
"\n",
|
||||
"# Load model and tokenizer\n",
|
||||
"model = AutoModelForCausalLM.from_pretrained(\n",
|
||||
" \"microsoft/Phi-3-mini-4k-instruct\",\n",
|
||||
" device_map=\"cuda\",\n",
|
||||
" torch_dtype=\"auto\",\n",
|
||||
" trust_remote_code=True,\n",
|
||||
")\n",
|
||||
"tokenizer = AutoTokenizer.from_pretrained(\"microsoft/Phi-3-mini-4k-instruct\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "qdyYYS0E5fEU"
|
||||
},
|
||||
"source": [
|
||||
"Although we can now use the model and tokenizer directly, it's much easier to wrap it in a `pipeline` object:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"id": "DiUi4Wu1FCyN"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from transformers import pipeline\n",
|
||||
"\n",
|
||||
"# Create a pipeline\n",
|
||||
"generator = pipeline(\n",
|
||||
" \"text-generation\",\n",
|
||||
" model=model,\n",
|
||||
" tokenizer=tokenizer,\n",
|
||||
" return_full_text=False,\n",
|
||||
" max_new_tokens=500,\n",
|
||||
" do_sample=False\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "mD49kysT5mMY"
|
||||
},
|
||||
"source": [
|
||||
"Finally, we create our prompt as a user and give it to the model:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"id": "hkR7LBmiyXmY"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" Why did the chicken join the band? Because it had the drumsticks!\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# The prompt (user input / query)\n",
|
||||
"messages = [\n",
|
||||
" {\"role\": \"user\", \"content\": \"Create a funny joke about chickens.\"}\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"# Generate output\n",
|
||||
"output = generator(messages)\n",
|
||||
"print(output[0][\"generated_text\"])"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"accelerator": "GPU",
|
||||
"colab": {
|
||||
"authorship_tag": "ABX9TyPCWg08aO4e8NWQuYCK5ppF",
|
||||
"gpuType": "T4",
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.10.14"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
Binary file not shown.
21135
chapter02/Chapter 2 - Tokens and Token Embeddings.ipynb
Normal file
21135
chapter02/Chapter 2 - Tokens and Token Embeddings.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
Binary file not shown.
5662
chapter03/Chapter 3 - Looking Inside LLMs.ipynb
Normal file
5662
chapter03/Chapter 3 - Looking Inside LLMs.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
Binary file not shown.
1763
chapter04/Chapter 4 - Text Classification.ipynb
Normal file
1763
chapter04/Chapter 4 - Text Classification.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
Binary file not shown.
5871
chapter05/Chapter 5 - Text Clustering and Topic Modeling.ipynb
Normal file
5871
chapter05/Chapter 5 - Text Clustering and Topic Modeling.ipynb
Normal file
File diff suppressed because one or more lines are too long
Binary file not shown.
1618
chapter06/Chapter 6 - Prompt Engineering.ipynb
Normal file
1618
chapter06/Chapter 6 - Prompt Engineering.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
5331
chapter08/Chapter 8 - Semantic Search.ipynb
Normal file
5331
chapter08/Chapter 8 - Semantic Search.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
Binary file not shown.
13128
chapter09/Chapter 9 - Multimodal Large Language Models.ipynb
Normal file
13128
chapter09/Chapter 9 - Multimodal Large Language Models.ipynb
Normal file
File diff suppressed because one or more lines are too long
Binary file not shown.
10581
chapter10/Chapter 10 - Creating Text Embedding Models.ipynb
Normal file
10581
chapter10/Chapter 10 - Creating Text Embedding Models.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
Binary file not shown.
12839
chapter11/Chapter 11 - Fine-Tuning BERT.ipynb
Normal file
12839
chapter11/Chapter 11 - Fine-Tuning BERT.ipynb
Normal file
File diff suppressed because one or more lines are too long
Binary file not shown.
12908
chapter12/Chapter 12 - Fine-tuning Generation Models.ipynb
Normal file
12908
chapter12/Chapter 12 - Fine-tuning Generation Models.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
Binary file not shown.
248
environment.yml
Normal file
248
environment.yml
Normal file
@@ -0,0 +1,248 @@
|
||||
name: thellmbook
|
||||
channels:
|
||||
- conda-forge
|
||||
- defaults
|
||||
dependencies:
|
||||
- bzip2=1.0.8
|
||||
- ca-certificates=2024.8.30
|
||||
- libffi=3.4.2
|
||||
- libsqlite=3.46.1
|
||||
- libzlib=1.3.1
|
||||
- openssl=3.3.2
|
||||
- pip=24.2
|
||||
- python=3.10.14
|
||||
- setuptools=73.0.1
|
||||
- tk=8.6.13
|
||||
- ucrt=10.0.22621.0
|
||||
- vc=14.3
|
||||
- vc14_runtime=14.40.33810
|
||||
- vs2015_runtime=14.40.33810
|
||||
- wheel=0.44.0
|
||||
- xz=5.2.6
|
||||
- pip:
|
||||
- accelerate==0.31.0
|
||||
- aiohappyeyeballs==2.4.0
|
||||
- aiohttp==3.10.5
|
||||
- aiosignal==1.3.1
|
||||
- annotated-types==0.7.0
|
||||
- annoy==1.17.3
|
||||
- anyio==4.4.0
|
||||
- argon2-cffi==23.1.0
|
||||
- argon2-cffi-bindings==21.2.0
|
||||
- arrow==1.3.0
|
||||
- asttokens==2.4.1
|
||||
- async-lru==2.0.4
|
||||
- async-timeout==4.0.3
|
||||
- attrs==24.2.0
|
||||
- babel==2.16.0
|
||||
- beautifulsoup4==4.12.3
|
||||
- bertopic==0.16.3
|
||||
- bitsandbytes==0.43.1
|
||||
- bleach==6.1.0
|
||||
- boto3==1.35.15
|
||||
- botocore==1.35.15
|
||||
- certifi==2024.8.30
|
||||
- cffi==1.17.1
|
||||
- charset-normalizer==3.3.2
|
||||
- click==8.1.7
|
||||
- cloudpickle==3.0.0
|
||||
- cohere==5.5.8
|
||||
- colorama==0.4.6
|
||||
- colorcet==3.1.0
|
||||
- colorspacious==1.1.2
|
||||
- comm==0.2.2
|
||||
- contourpy==1.3.0
|
||||
- cycler==0.12.1
|
||||
- dask==2024.8.2
|
||||
- dataclasses-json==0.6.7
|
||||
- datamapplot==0.3.0
|
||||
- datasets==2.20.0
|
||||
- datashader==0.16.3
|
||||
- debugpy==1.8.5
|
||||
- decorator==5.1.1
|
||||
- defusedxml==0.7.1
|
||||
- dill==0.3.8
|
||||
- diskcache==5.6.3
|
||||
- distro==1.9.0
|
||||
- docstring-parser==0.16
|
||||
- duckduckgo-search==6.1.6
|
||||
- eval-type-backport==0.2.0
|
||||
- evaluate==0.4.2
|
||||
- exceptiongroup==1.2.2
|
||||
- executing==2.1.0
|
||||
- fastavro==1.9.7
|
||||
- fastjsonschema==2.20.0
|
||||
- filelock==3.16.0
|
||||
- fonttools==4.53.1
|
||||
- fqdn==1.5.1
|
||||
- frozenlist==1.4.1
|
||||
- fsspec==2024.5.0
|
||||
- gensim==4.3.2
|
||||
- greenlet==3.0.3
|
||||
- h11==0.14.0
|
||||
- hdbscan==0.8.38.post1
|
||||
- httpcore==1.0.5
|
||||
- httpx==0.27.2
|
||||
- httpx-sse==0.4.0
|
||||
- huggingface-hub==0.24.6
|
||||
- idna==3.8
|
||||
- imageio==2.35.1
|
||||
- importlib-metadata==8.4.0
|
||||
- intel-openmp==2021.4.0
|
||||
- ipykernel==6.29.5
|
||||
- ipython==8.27.0
|
||||
- ipywidgets==8.1.3
|
||||
- isoduration==20.11.0
|
||||
- jedi==0.19.1
|
||||
- jinja2==3.1.4
|
||||
- jmespath==1.0.1
|
||||
- joblib==1.4.2
|
||||
- json5==0.9.25
|
||||
- jsonpatch==1.33
|
||||
- jsonpointer==3.0.0
|
||||
- jsonschema==4.23.0
|
||||
- jsonschema-specifications==2023.12.1
|
||||
- jupyter-client==8.6.2
|
||||
- jupyter-core==5.7.2
|
||||
- jupyter-events==0.10.0
|
||||
- jupyter-lsp==2.2.5
|
||||
- jupyter-server==2.14.2
|
||||
- jupyter-server-terminals==0.5.3
|
||||
- jupyterlab==4.2.2
|
||||
- jupyterlab-pygments==0.3.0
|
||||
- jupyterlab-server==2.27.3
|
||||
- jupyterlab-widgets==3.0.13
|
||||
- kiwisolver==1.4.7
|
||||
- langchain==0.2.5
|
||||
- langchain-community==0.2.5
|
||||
- langchain-core==0.2.38
|
||||
- langchain-openai==0.1.8
|
||||
- langchain-text-splitters==0.2.4
|
||||
- langsmith==0.1.117
|
||||
- lazy-loader==0.4
|
||||
- llama-cpp-python==0.2.78
|
||||
- llvmlite==0.43.0
|
||||
- locket==1.0.0
|
||||
- markdown-it-py==3.0.0
|
||||
- markupsafe==2.1.5
|
||||
- marshmallow==3.22.0
|
||||
- matplotlib==3.9.0
|
||||
- matplotlib-inline==0.1.7
|
||||
- mdurl==0.1.2
|
||||
- mistune==3.0.2
|
||||
- mkl==2021.4.0
|
||||
- mpmath==1.3.0
|
||||
- mteb==1.12.39
|
||||
- multidict==6.1.0
|
||||
- multipledispatch==1.0.0
|
||||
- multiprocess==0.70.16
|
||||
- mypy-extensions==1.0.0
|
||||
- nbclient==0.10.0
|
||||
- nbconvert==7.16.4
|
||||
- nbformat==5.10.4
|
||||
- nest-asyncio==1.6.0
|
||||
- networkx==3.3
|
||||
- nltk==3.8.1
|
||||
- notebook-shim==0.2.4
|
||||
- numba==0.60.0
|
||||
- numexpr==2.10.0
|
||||
- numpy==1.26.4
|
||||
- openai==1.34.0
|
||||
- orjson==3.10.7
|
||||
- overrides==7.7.0
|
||||
- packaging==24.1
|
||||
- pandas==2.2.2
|
||||
- pandocfilters==1.5.1
|
||||
- param==2.1.1
|
||||
- parameterized==0.9.0
|
||||
- parso==0.8.4
|
||||
- partd==1.4.2
|
||||
- peft==0.11.1
|
||||
- pillow==10.4.0
|
||||
- platformdirs==4.3.2
|
||||
- plotly==5.24.0
|
||||
- polars==1.6.0
|
||||
- prometheus-client==0.20.0
|
||||
- prompt-toolkit==3.0.47
|
||||
- psutil==6.0.0
|
||||
- pure-eval==0.2.3
|
||||
- pyarrow==17.0.0
|
||||
- pyarrow-hotfix==0.6
|
||||
- pycparser==2.22
|
||||
- pyct==0.5.0
|
||||
- pydantic==2.9.1
|
||||
- pydantic-core==2.23.3
|
||||
- pygments==2.18.0
|
||||
- pylabeladjust==0.1.13
|
||||
- pynndescent==0.5.13
|
||||
- pyparsing==3.1.4
|
||||
- pyqtree==1.0.0
|
||||
- pyreqwest-impersonate==0.5.3
|
||||
- python-dateutil==2.9.0.post0
|
||||
- python-json-logger==2.0.7
|
||||
- pytrec-eval-terrier==0.5.6
|
||||
- pytz==2024.1
|
||||
- pywin32==306
|
||||
- pywinpty==2.0.13
|
||||
- pyyaml==6.0.2
|
||||
- pyzmq==26.2.0
|
||||
- referencing==0.35.1
|
||||
- regex==2024.7.24
|
||||
- requests==2.32.3
|
||||
- rfc3339-validator==0.1.4
|
||||
- rfc3986-validator==0.1.1
|
||||
- rich==13.8.1
|
||||
- rpds-py==0.20.0
|
||||
- s3transfer==0.10.2
|
||||
- safetensors==0.4.5
|
||||
- scikit-image==0.24.0
|
||||
- scikit-learn==1.5.0
|
||||
- send2trash==1.8.3
|
||||
- sentence-transformers==3.0.1
|
||||
- sentencepiece==0.2.0
|
||||
- seqeval==1.2.2
|
||||
- setfit==1.0.3
|
||||
- shtab==1.7.1
|
||||
- six==1.16.0
|
||||
- smart-open==7.0.4
|
||||
- sniffio==1.3.1
|
||||
- soupsieve==2.6
|
||||
- sqlalchemy==2.0.34
|
||||
- stack-data==0.6.3
|
||||
- sympy==1.13.2
|
||||
- tbb==2021.13.1
|
||||
- tenacity==8.5.0
|
||||
- terminado==0.18.1
|
||||
- threadpoolctl==3.5.0
|
||||
- tifffile==2024.8.30
|
||||
- tiktoken==0.7.0
|
||||
- tinycss2==1.3.0
|
||||
- tokenizers==0.19.1
|
||||
- tomli==2.0.1
|
||||
- toolz==0.12.1
|
||||
- torch==2.3.1
|
||||
- tornado==6.4.1
|
||||
- tqdm==4.66.5
|
||||
- traitlets==5.14.3
|
||||
- transformers==4.41.2
|
||||
- trl==0.9.4
|
||||
- types-python-dateutil==2.9.0.20240906
|
||||
- types-requests==2.32.0.20240907
|
||||
- typing-extensions==4.12.2
|
||||
- typing-inspect==0.9.0
|
||||
- tyro==0.8.10
|
||||
- tzdata==2024.1
|
||||
- umap-learn==0.5.6
|
||||
- uri-template==1.3.0
|
||||
- urllib3==2.2.2
|
||||
- wcwidth==0.2.13
|
||||
- webcolors==24.8.0
|
||||
- webencodings==0.5.1
|
||||
- websocket-client==1.8.0
|
||||
- widgetsnbextension==4.0.13
|
||||
- wrapt==1.16.0
|
||||
- xarray==2024.7.0
|
||||
- xxhash==3.5.0
|
||||
- yarl==1.11.1
|
||||
- zipp==3.20.1
|
||||
prefix: C:\ProgramData\miniconda3\envs\thellmbook
|
||||
43
requirements.txt
Normal file
43
requirements.txt
Normal file
@@ -0,0 +1,43 @@
|
||||
# Data handling
|
||||
numpy == 1.26.4
|
||||
pandas == 2.2.2
|
||||
datasets == 2.20.0
|
||||
|
||||
# Environment
|
||||
jupyterlab == 4.2.2
|
||||
ipywidgets == 8.1.3
|
||||
|
||||
# Hard dependencies throughout the entire book
|
||||
torch == 2.3.1
|
||||
transformers == 4.41.2
|
||||
sentence-transformers == 3.0.1
|
||||
matplotlib == 3.9.0
|
||||
scikit-learn == 1.5.0
|
||||
sentencepiece == 0.2.0
|
||||
nltk == 3.8.1
|
||||
evaluate == 0.4.2
|
||||
scipy == 1.12.0 # TEMPORARY DEP BECAUSE OF https://github.com/piskvorky/gensim/issues/3525
|
||||
|
||||
# Cloud providers
|
||||
openai == 1.34.0
|
||||
cohere == 5.5.8
|
||||
|
||||
# Chapter-specific dependencies
|
||||
datamapplot == 0.3.0
|
||||
faiss-cpu==1.8.0
|
||||
bertopic == 0.16.3
|
||||
annoy == 1.17.3
|
||||
llama_cpp_python == 0.2.78 -C cmake.args="-DLLAMA_BLAS=ON"
|
||||
numexpr == 2.10.0
|
||||
langchain == 0.2.5
|
||||
langchain-community == 0.2.5
|
||||
langchain-openai==0.1.8
|
||||
duckduckgo-search == 6.1.6
|
||||
gensim == 4.3.2
|
||||
setfit == 1.0.3
|
||||
seqeval == 1.2.2
|
||||
trl == 0.9.4
|
||||
peft == 0.11.1
|
||||
accelerate == 0.31.0
|
||||
bitsandbytes == 0.43.1
|
||||
mteb == 1.12.39
|
||||
43
requirements_min.txt
Normal file
43
requirements_min.txt
Normal file
@@ -0,0 +1,43 @@
|
||||
# Data handling
|
||||
numpy >= 1.26.4
|
||||
pandas >= 2.2.2
|
||||
datasets >= 2.20.0
|
||||
|
||||
# Environment
|
||||
jupyterlab >= 4.2.2
|
||||
ipywidgets >= 8.1.3
|
||||
|
||||
# Hard dependencies throughout the entire book
|
||||
torch >= 2.3.1
|
||||
transformers >= 4.41.2
|
||||
sentence-transformers >= 3.0.1
|
||||
matplotlib >= 3.9.0
|
||||
scikit-learn >= 1.5.0
|
||||
sentencepiece >= 0.2.0
|
||||
nltk >= 3.8.1
|
||||
evaluate >= 0.4.2
|
||||
scipy == 1.12.0 # TEMPORARY DEP BECAUSE OF https://github.com/piskvorky/gensim/issues/3525
|
||||
|
||||
# Cloud providers
|
||||
openai >= 1.34.0
|
||||
cohere >= 5.5.8
|
||||
|
||||
# Chapter-specific dependencies
|
||||
datamapplot >= 0.3.0
|
||||
faiss-cpu >= 1.8.0
|
||||
bertopic >= 0.16.3
|
||||
annoy >= 1.17.3
|
||||
llama_cpp_python >= 0.2.78 -C cmake.args="-DLLAMA_BLAS=ON"
|
||||
numexpr >= 2.10.0
|
||||
langchain >= 0.2.5
|
||||
langchain-community >= 0.2.5
|
||||
langchain-openai >= 0.1.8
|
||||
duckduckgo-search >= 6.1.6
|
||||
gensim >= 4.3.2
|
||||
setfit >= 1.0.3
|
||||
seqeval >= 1.2.2
|
||||
trl >= 0.9.4
|
||||
peft >= 0.11.1
|
||||
accelerate >= 0.31.0
|
||||
bitsandbytes >= 0.43.1
|
||||
mteb >= 1.12.39
|
||||
Reference in New Issue
Block a user