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

Llama-2-chat integration #9

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c004d7b
llama-2-chat integration [wip]
eryk-dsai Aug 31, 2023
3337d6a
fixing problem with private pipeline
eryk-dsai Aug 31, 2023
26e10fc
_generate returns ChatResult
eryk-dsai Sep 1, 2023
2aba4cd
batch calls example in notebook, handling chat messages
eryk-dsai Sep 1, 2023
82f71f1
_format_messages_as_text docstring
eryk-dsai Sep 1, 2023
933f5f4
you can pass stop words now
eryk-dsai Sep 1, 2023
36519e0
format_messages_as_text test
eryk-dsai Sep 1, 2023
9a92e08
formatter
eryk-dsai Sep 1, 2023
cc53251
fix lint issues
eryk-dsai Sep 1, 2023
6a0cd87
removal of redundant notebook cell
eryk-dsai Sep 1, 2023
cc578b5
refactor: update naming to indicate Hugging Face usage
eryk-dsai Sep 4, 2023
cb33d48
small refactor
eryk-dsai Sep 4, 2023
440571d
fix lint errors, running formatter
eryk-dsai Sep 4, 2023
f860326
moving stopping criteria class out of function, correct typing
eryk-dsai Sep 4, 2023
9b6e79d
code review suggestions
eryk-dsai Sep 4, 2023
900d55c
run formatter and lint
eryk-dsai Sep 4, 2023
3b9c030
StoppingCriteria are correctly placed on the same device as pipeline
eryk-dsai Sep 5, 2023
9715b81
Merge branch 'llama2-chat' of https://github.com/deepsense-ai/langcha…
eryk-dsai Sep 5, 2023
3885de5
run formatter, lint
eryk-dsai Sep 5, 2023
814131d
removal of the redundant notebook cell
eryk-dsai Sep 5, 2023
bd6e2fe
moved StoppingCriteria import to method
eryk-dsai Sep 5, 2023
35b5e09
fixing type annotation
eryk-dsai Sep 5, 2023
1228bfc
fixing Enum tests
eryk-dsai Sep 5, 2023
926c02f
Editing the huggingface llama 2 notebook
eryk-dsai Sep 5, 2023
8a8a03a
Merge branch 'master' into llama2-chat
eryk-dsai Sep 5, 2023
964b579
typos, better name for customg Stopping Critieria subclass
eryk-dsai Sep 13, 2023
87597a1
Generic Hugging Face Pipeline Chat Model
eryk-dsai Sep 13, 2023
7756700
Merge branch 'langchain-ai:master' into llama2-chat
eryk-dsai Oct 6, 2023
d9e9ef3
simplifying HF Chat Model, by making use of HF Chat Templates
eryk-dsai Oct 10, 2023
cf203b9
removing incorrect check from validate_environment method
eryk-dsai Oct 10, 2023
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
296 changes: 296 additions & 0 deletions docs/extras/integrations/chat/huggingface_llama2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Llama-2-Chat Model from Hugging Face"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
eryk-dsai marked this conversation as resolved.
Show resolved Hide resolved
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Hugging Face imports:\n",
"import torch\n",
"from transformers import (\n",
eryk-dsai marked this conversation as resolved.
Show resolved Hide resolved
" AutoModelForCausalLM,\n",
" AutoTokenizer,\n",
" BitsAndBytesConfig,\n",
" pipeline,\n",
")\n",
"\n",
"# LangChain imports:\n",
"from langchain.chat_models import ChatLlama2Hf\n",
"from langchain.schema import AIMessage, HumanMessage, SystemMessage"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook assumes that you were granted with access to the Llama 2 models in the Hugging Face models hub. To use the model locally, you need to be [logged in](https://huggingface.co/docs/huggingface_hub/quick-start#login) with a Hugging Face account."
eryk-dsai marked this conversation as resolved.
Show resolved Hide resolved
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "fdfe7c3faf0c40d0bac0fd22fd9ebd38",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from huggingface_hub import login\n",
"login()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"model_name = \"meta-llama/Llama-2-7b-chat-hf\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"bnb_config = BitsAndBytesConfig(\n",
" load_in_4bit=True,\n",
" bnb_4bit_quant_type=\"nf4\",\n",
" bnb_4bit_use_double_quant=True,\n",
" bnb_4bit_compute_dtype=torch.bfloat16,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4c3b0838de6140019682c5e5d17f1f37",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
"model_4bit = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=bnb_config, device_map=\"auto\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"pipe = pipeline(\n",
" \"text-generation\",\n",
" model=model_4bit,\n",
" tokenizer=tokenizer,\n",
" torch_dtype=torch.float16,\n",
" device_map=\"auto\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"chat = ChatLlama2Hf(pipeline=pipe)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# Generation kwargs:\n",
"pipeline_kwargs = {\n",
" \"do_sample\": True,\n",
" \"top_p\": 0.95,\n",
" \"temperature\": 0.7,\n",
" \"eos_token_id\": tokenizer.eos_token_id,\n",
" \"max_length\": 256, \n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Single calls:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content=' Sure! Here\\'s the translation of \"I love programming\" from English to French:\\nJe adore le programming.\\n\\nI hope that helps! Let me know if you have any other sentences you\\'d like me to translate.', additional_kwargs={}, example=False)"
eryk-dsai marked this conversation as resolved.
Show resolved Hide resolved
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages = [\n",
" SystemMessage(\n",
" content=\"You are a helpful assistant that translates English to French.\"\n",
" ),\n",
" HumanMessage(\n",
" content=\"Translate this sentence from English to French. I love programming.\"\n",
" ),\n",
"]\n",
"chat(messages, **pipeline_kwargs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Single calls with stop words"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content=\" Of course, I'd be happy to help! Artificial\", additional_kwargs={}, example=False)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages = [\n",
" SystemMessage(\n",
" content=\"You are a helpful assistant.\"\n",
" ),\n",
" HumanMessage(\n",
" content=\"Tell me the history of AI.\"\n",
" ),\n",
"]\n",
"chat(messages, stop=[\"Artificial\"], **pipeline_kwargs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Batch calls:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"LLMResult(generations=[[ChatGeneration(text=' Great! \"Programming\" in English can be translated to \"le programming\" in French.\\n\\nSo, you love programming? \"Aimez-vous le programming\" in French.', generation_info=None, message=AIMessage(content=' Great! \"Programming\" in English can be translated to \"le programming\" in French.\\n\\nSo, you love programming? \"Aimez-vous le programming\" in French.', additional_kwargs={}, example=False))], [ChatGeneration(text=' Bonjour! Je suis heureux de vous aider avec la translation de votre phrase en français.\\n\\nVous aimez l\\'intelligence artificielle.\\n\\n(Note: I used the phrase \"Bonjour!\" to greet you in French, as it is a common way to start a conversation in France. \"Je suis heureux de vous aider\" means \"I am happy to help you\" in French.)', generation_info=None, message=AIMessage(content=' Bonjour! Je suis heureux de vous aider avec la translation de votre phrase en français.\\n\\nVous aimez l\\'intelligence artificielle.\\n\\n(Note: I used the phrase \"Bonjour!\" to greet you in French, as it is a common way to start a conversation in France. \"Je suis heureux de vous aider\" means \"I am happy to help you\" in French.)', additional_kwargs={}, example=False))]], llm_output={}, run=[RunInfo(run_id=UUID('844a9416-e941-478b-ac61-82b5c7c15fb7')), RunInfo(run_id=UUID('57636053-2e2c-41a4-89ad-7ef70d38fe12'))])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"batch_messages = [\n",
" [\n",
" SystemMessage(content=\"You are a helpful assistant that translates English to French.\"),\n",
" HumanMessage(content=\"I love programming.\")\n",
" ],\n",
" [\n",
" SystemMessage(content=\"You are a helpful assistant that translates English to French.\"),\n",
" HumanMessage(content=\"I love artificial intelligence.\")\n",
" ],\n",
"]\n",
"result = chat.generate(batch_messages)\n",
"result"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.12 ('langchain_venv': 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.10.12"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "3372ef96e068313d34c91eab0f20d815c93d37110de821968e5d598f73bfb74c"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
2 changes: 2 additions & 0 deletions libs/langchain/langchain/chat_models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from langchain.chat_models.ernie import ErnieBotChat
from langchain.chat_models.fake import FakeListChatModel
from langchain.chat_models.google_palm import ChatGooglePalm
from langchain.chat_models.huggingface_llama2 import ChatLlama2Hf
from langchain.chat_models.human import HumanInputChatModel
from langchain.chat_models.jinachat import JinaChat
from langchain.chat_models.litellm import ChatLiteLLM
Expand All @@ -41,6 +42,7 @@
"ChatGooglePalm",
"ChatMLflowAIGateway",
"ChatOllama",
"ChatLlama2Hf",
"ChatVertexAI",
"JinaChat",
"HumanInputChatModel",
Expand Down
Loading