From 42c686bc28a22d99394b7afd9314f9a560fea7d0 Mon Sep 17 00:00:00 2001 From: Jacob Lee Date: Wed, 31 Jul 2024 18:01:55 -0700 Subject: [PATCH] docs[patch]: Update local model how-to guide (#24911) Updates to use `langchain_ollama`, new models, chat model example --- docs/docs/how_to/index.mdx | 3 +- docs/docs/how_to/local_llms.ipynb | 98 +++++++++++++++++++++---------- 2 files changed, 69 insertions(+), 32 deletions(-) diff --git a/docs/docs/how_to/index.mdx b/docs/docs/how_to/index.mdx index c3964d892e..de9000095f 100644 --- a/docs/docs/how_to/index.mdx +++ b/docs/docs/how_to/index.mdx @@ -88,6 +88,7 @@ These are the core building blocks you can use when building applications. - [How to: few shot prompt tool behavior](/docs/how_to/tools_few_shot) - [How to: bind model-specific formatted tools](/docs/how_to/tools_model_specific) - [How to: force a specific tool call](/docs/how_to/tool_choice) +- [How to: work with local models](/docs/how_to/local_llms) - [How to: init any model in one line](/docs/how_to/chat_models_universal_init/) ### Messages @@ -106,7 +107,7 @@ What LangChain calls [LLMs](/docs/concepts/#llms) are older forms of language mo - [How to: create a custom LLM class](/docs/how_to/custom_llm) - [How to: stream a response back](/docs/how_to/streaming_llm) - [How to: track token usage](/docs/how_to/llm_token_usage_tracking) -- [How to: work with local LLMs](/docs/how_to/local_llms) +- [How to: work with local models](/docs/how_to/local_llms) ### Output parsers diff --git a/docs/docs/how_to/local_llms.ipynb b/docs/docs/how_to/local_llms.ipynb index 795e8832a3..aa3c0a7f9d 100644 --- a/docs/docs/how_to/local_llms.ipynb +++ b/docs/docs/how_to/local_llms.ipynb @@ -5,11 +5,11 @@ "id": "b8982428", "metadata": {}, "source": [ - "# Run LLMs locally\n", + "# Run models locally\n", "\n", "## Use case\n", "\n", - "The popularity of projects like [PrivateGPT](https://github.com/imartinez/privateGPT), [llama.cpp](https://github.com/ggerganov/llama.cpp), [Ollama](https://github.com/ollama/ollama), [GPT4All](https://github.com/nomic-ai/gpt4all), [llamafile](https://github.com/Mozilla-Ocho/llamafile), and others underscore the demand to run LLMs locally (on your own device).\n", + "The popularity of projects like [llama.cpp](https://github.com/ggerganov/llama.cpp), [Ollama](https://github.com/ollama/ollama), [GPT4All](https://github.com/nomic-ai/gpt4all), [llamafile](https://github.com/Mozilla-Ocho/llamafile), and others underscore the demand to run LLMs locally (on your own device).\n", "\n", "This has at least two important benefits:\n", "\n", @@ -66,6 +66,12 @@ "\n", "![Image description](../../static/img/llama_t_put.png)\n", "\n", + "### Formatting prompts\n", + "\n", + "Some providers have [chat model](/docs/concepts/#chat-models) wrappers that takes care of formatting your input prompt for the specific local model you're using. However, if you are prompting local models with a [text-in/text-out LLM](/docs/concepts/#llms) wrapper, you may need to use a prompt tailed for your specific model.\n", + "\n", + "This can [require the inclusion of special tokens](https://huggingface.co/blog/llama2#how-to-prompt-llama-2). [Here's an example for LLaMA 2](https://smith.langchain.com/hub/rlm/rag-prompt-llama).\n", + "\n", "## Quickstart\n", "\n", "[`Ollama`](https://ollama.ai/) is one way to easily run inference on macOS.\n", @@ -73,10 +79,20 @@ "The instructions [here](https://github.com/jmorganca/ollama?tab=readme-ov-file#ollama) provide details, which we summarize:\n", " \n", "* [Download and run](https://ollama.ai/download) the app\n", - "* From command line, fetch a model from this [list of options](https://github.com/jmorganca/ollama): e.g., `ollama pull llama2`\n", + "* From command line, fetch a model from this [list of options](https://github.com/jmorganca/ollama): e.g., `ollama pull llama3.1:8b`\n", "* When the app is running, all models are automatically served on `localhost:11434`\n" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "29450fc9", + "metadata": {}, + "outputs": [], + "source": [ + "%pip install -qU langchain_ollama" + ] + }, { "cell_type": "code", "execution_count": 2, @@ -86,7 +102,7 @@ { "data": { "text/plain": [ - "' The first man on the moon was Neil Armstrong, who landed on the moon on July 20, 1969 as part of the Apollo 11 mission. obviously.'" + "'...Neil Armstrong!\\n\\nOn July 20, 1969, Neil Armstrong became the first person to set foot on the lunar surface, famously declaring \"That\\'s one small step for man, one giant leap for mankind\" as he stepped off the lunar module Eagle onto the Moon\\'s surface.\\n\\nWould you like to know more about the Apollo 11 mission or Neil Armstrong\\'s achievements?'" ] }, "execution_count": 2, @@ -95,51 +111,78 @@ } ], "source": [ - "from langchain_community.llms import Ollama\n", + "from langchain_ollama import OllamaLLM\n", + "\n", + "llm = OllamaLLM(model=\"llama3.1:8b\")\n", "\n", - "llm = Ollama(model=\"llama2\")\n", "llm.invoke(\"The first man on the moon was ...\")" ] }, { "cell_type": "markdown", - "id": "343ab645", + "id": "674cc672", "metadata": {}, "source": [ - "Stream tokens as they are being generated." + "Stream tokens as they are being generated:" ] }, { "cell_type": "code", - "execution_count": 40, - "id": "9cd83603", + "execution_count": 3, + "id": "1386a852", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - " The first man to walk on the moon was Neil Armstrong, an American astronaut who was part of the Apollo 11 mission in 1969. февруари 20, 1969, Armstrong stepped out of the lunar module Eagle and onto the moon's surface, famously declaring \"That's one small step for man, one giant leap for mankind\" as he took his first steps. He was followed by fellow astronaut Edwin \"Buzz\" Aldrin, who also walked on the moon during the mission." + "...|" ] }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Neil| Armstrong|,| an| American| astronaut|.| He| stepped| out| of| the| lunar| module| Eagle| and| onto| the| surface| of| the| Moon| on| July| |20|,| |196|9|,| famously| declaring|:| \"|That|'s| one| small| step| for| man|,| one| giant| leap| for| mankind|.\"||" + ] + } + ], + "source": [ + "for chunk in llm.stream(\"The first man on the moon was ...\"):\n", + " print(chunk, end=\"|\", flush=True)" + ] + }, + { + "cell_type": "markdown", + "id": "e5731060", + "metadata": {}, + "source": [ + "Ollama also includes a chat model wrapper that handles formatting conversation turns:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "f14a778a", + "metadata": {}, + "outputs": [ { "data": { "text/plain": [ - "' The first man to walk on the moon was Neil Armstrong, an American astronaut who was part of the Apollo 11 mission in 1969. февруари 20, 1969, Armstrong stepped out of the lunar module Eagle and onto the moon\\'s surface, famously declaring \"That\\'s one small step for man, one giant leap for mankind\" as he took his first steps. He was followed by fellow astronaut Edwin \"Buzz\" Aldrin, who also walked on the moon during the mission.'" + "AIMessage(content='The answer is a historic one!\\n\\nThe first man to walk on the Moon was Neil Armstrong, an American astronaut and commander of the Apollo 11 mission. On July 20, 1969, Armstrong stepped out of the lunar module Eagle onto the surface of the Moon, famously declaring:\\n\\n\"That\\'s one small step for man, one giant leap for mankind.\"\\n\\nArmstrong was followed by fellow astronaut Edwin \"Buzz\" Aldrin, who also walked on the Moon during the mission. Michael Collins remained in orbit around the Moon in the command module Columbia.\\n\\nNeil Armstrong passed away on August 25, 2012, but his legacy as a pioneering astronaut and engineer continues to inspire people around the world!', response_metadata={'model': 'llama3.1:8b', 'created_at': '2024-08-01T00:38:29.176717Z', 'message': {'role': 'assistant', 'content': ''}, 'done_reason': 'stop', 'done': True, 'total_duration': 10681861417, 'load_duration': 34270292, 'prompt_eval_count': 19, 'prompt_eval_duration': 6209448000, 'eval_count': 141, 'eval_duration': 4432022000}, id='run-7bed57c5-7f54-4092-912c-ae49073dcd48-0', usage_metadata={'input_tokens': 19, 'output_tokens': 141, 'total_tokens': 160})" ] }, - "execution_count": 40, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "from langchain_core.callbacks import CallbackManager, StreamingStdOutCallbackHandler\n", + "from langchain_ollama import ChatOllama\n", "\n", - "llm = Ollama(\n", - " model=\"llama2\", callback_manager=CallbackManager([StreamingStdOutCallbackHandler()])\n", - ")\n", - "llm.invoke(\"The first man on the moon was ...\")" + "chat_model = ChatOllama(model=\"llama3.1:8b\")\n", + "\n", + "chat_model.invoke(\"Who was the first man on the moon?\")" ] }, { @@ -199,7 +242,7 @@ "\n", "With [Ollama](https://github.com/jmorganca/ollama), fetch a model via `ollama pull :`:\n", "\n", - "* E.g., for Llama-7b: `ollama pull llama2` will download the most basic version of the model (e.g., smallest # parameters and 4 bit quantization)\n", + "* E.g., for Llama 2 7b: `ollama pull llama2` will download the most basic version of the model (e.g., smallest # parameters and 4 bit quantization)\n", "* We can also specify a particular version from the [model list](https://github.com/jmorganca/ollama?tab=readme-ov-file#model-library), e.g., `ollama pull llama2:13b`\n", "* See the full set of parameters on the [API reference page](https://api.python.langchain.com/en/latest/llms/langchain_community.llms.ollama.Ollama.html)" ] @@ -222,9 +265,7 @@ } ], "source": [ - "from langchain_community.llms import Ollama\n", - "\n", - "llm = Ollama(model=\"llama2:13b\")\n", + "llm = OllamaLLM(model=\"llama2:13b\")\n", "llm.invoke(\"The first man on the moon was ... think step by step\")" ] }, @@ -268,11 +309,7 @@ "cell_type": "code", "execution_count": null, "id": "5eba38dc", - "metadata": { - "vscode": { - "languageId": "plaintext" - } - }, + "metadata": {}, "outputs": [], "source": [ "%env CMAKE_ARGS=\"-DLLAMA_METAL=on\"\n", @@ -542,7 +579,6 @@ } ], "source": [ - "from langchain.chains import LLMChain\n", "from langchain.chains.prompt_selector import ConditionalPromptSelector\n", "from langchain_core.prompts import PromptTemplate\n", "\n", @@ -613,9 +649,9 @@ ], "source": [ "# Chain\n", - "llm_chain = LLMChain(prompt=prompt, llm=llm)\n", + "chain = prompt | llm\n", "question = \"What NFL team won the Super Bowl in the year that Justin Bieber was born?\"\n", - "llm_chain.run({\"question\": question})" + "chain.invoke({\"question\": question})" ] }, { @@ -666,7 +702,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.7" + "version": "3.10.5" } }, "nbformat": 4,