diff --git a/docs/docs/integrations/chat/ollama.ipynb b/docs/docs/integrations/chat/ollama.ipynb index 054666d8076..d0df5b4b99d 100644 --- a/docs/docs/integrations/chat/ollama.ipynb +++ b/docs/docs/integrations/chat/ollama.ipynb @@ -15,45 +15,32 @@ "source": [ "# ChatOllama\n", "\n", - "[Ollama](https://ollama.ai/) allows you to run open-source large language models, such as LLaMA2, locally.\n", + "[Ollama](https://ollama.ai/) allows you to run open-source large language models, such as Llama 2, locally.\n", "\n", "Ollama bundles model weights, configuration, and data into a single package, defined by a Modelfile. \n", "\n", "It optimizes setup and configuration details, including GPU usage.\n", "\n", - "For a complete list of supported models and model variants, see the [Ollama model library](https://ollama.ai/library).\n", + "For a complete list of supported models and model variants, see the [Ollama model library](https://github.com/jmorganca/ollama#model-library).\n", "\n", "## Setup\n", "\n", "First, follow [these instructions](https://github.com/jmorganca/ollama) to set up and run a local Ollama instance:\n", "\n", - "* [Download](https://ollama.ai/download)\n", - "* Fetch a model via `ollama pull `\n", - "* e.g., for `Llama-7b`: `ollama pull llama2`\n", - "* This will download the most basic version of the model (e.g., minimum # parameters and 4-bit quantization)\n", - "* On Mac, it will download to:\n", + "* [Download](https://ollama.ai/download) and install Ollama onto the available supported platforms (including Windows Subsystem for Linux)\n", + "* Fetch available LLM model via `ollama pull `\n", + " * View a list of available models via the [model library](https://ollama.ai/library)\n", + " * e.g., for `Llama-7b`: `ollama pull llama2`\n", + "* This will download the default tagged version of the model. Typically, the default points to the latest, smallest sized-parameter model.\n", "\n", - "`~/.ollama/models/manifests/registry.ollama.ai/library//latest`\n", - "\n", - "* And we can specify a particular version, e.g., for `ollama pull vicuna:13b-v1.5-16k-q4_0`\n", - "* The file is here with the model version in place of `latest`\n", - "\n", - "`~/.ollama/models/manifests/registry.ollama.ai/library/vicuna/13b-v1.5-16k-q4_0`\n", - "\n", - "You can easily access models in a few ways:\n", - "\n", - "1/ if the app is running:\n", - "\n", - "* All of your local models are automatically served on `localhost:11434`\n", - "* Select your model when setting `llm = Ollama(..., model=\":\")`\n", - "* If you set `llm = Ollama(..., model=\" On Mac, the models will be download to `~/.ollama/models`\n", + "> \n", + "> On Linux (or WSL), the models will be stored at `/usr/share/ollama/.ollama/models`\n", "\n", + "* Specify the exact version of the model of interest as such `ollama pull vicuna:13b-v1.5-16k-q4_0` (View the [various tags for the `Vicuna`](https://ollama.ai/library/vicuna/tags) model in this instance)\n", + "* To view all pulled models, use `ollama list`\n", + "* To chat directly with a model from the command line, use `ollama run `\n", + "* View the [Ollama documentation](https://github.com/jmorganca/ollama) for more commands. Run `ollama help` in the terminal to see available commands too.\n", "\n", "## Usage\n", "\n", @@ -61,59 +48,198 @@ "\n", "If you are using a LLaMA `chat` model (e.g., `ollama pull llama2:7b-chat`) then you can use the `ChatOllama` interface.\n", "\n", - "This includes [special tokens](https://huggingface.co/blog/llama2#how-to-prompt-llama-2) for system message and user input." + "This includes [special tokens](https://huggingface.co/blog/llama2#how-to-prompt-llama-2) for system message and user input.\n", + "\n", + "## Interacting with Models \n", + "\n", + "Here are a few ways to interact with pulled local models\n", + "\n", + "#### directly in the terminal:\n", + "\n", + "* All of your local models are automatically served on `localhost:11434`\n", + "* Run `ollama run ` to start interacting via the command line directly\n", + "\n", + "### via an API\n", + "\n", + "Send an `application/json` request to the API endpoint of Ollama to interact.\n", + "\n", + "```bash\n", + "curl http://localhost:11434/api/generate -d '{\n", + " \"model\": \"llama2\",\n", + " \"prompt\":\"Why is the sky blue?\"\n", + "}'\n", + "```\n", + "\n", + "See the Ollama [API documentation](https://github.com/jmorganca/ollama/blob/main/docs/api.md) for all endpoints.\n", + "\n", + "#### via LangChain\n", + "\n", + "See a typical basic example of using Ollama via the `ChatOllama` chat model in your LangChain application." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Sure, here's a fun space-themed joke for you:\n", + "\n", + "Why don't astronauts like broccoli? \n", + "Because it has too many \"crisps\" in it!\n", + "\n" + ] + } + ], "source": [ - "from langchain.callbacks.manager import CallbackManager\n", - "from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n", + "# LangChain supports many other chat models. Here, we're using Ollama\n", "from langchain_community.chat_models import ChatOllama\n", + "from langchain_core.output_parsers import StrOutputParser\n", + "from langchain_core.prompts import ChatPromptTemplate\n", "\n", - "chat_model = ChatOllama(\n", - " model=\"llama2:7b-chat\",\n", - ")" + "# supports many more optional parameters. Hover on your `ChatOllama(...)`\n", + "# class to view the latest available supported parameters\n", + "llm = ChatOllama(model=\"llama2\")\n", + "prompt = ChatPromptTemplate.from_template(\"Tell me a short joke about {topic}\")\n", + "\n", + "# using LangChain Expressive Language chain syntax\n", + "# learn more about the LCEL on\n", + "# https://python.langchain.com/docs/expression_language/why\n", + "chain = prompt | llm | StrOutputParser()\n", + "\n", + "# for brevity, response is printed in terminal\n", + "# You can use LangServe to deploy your application for\n", + "# production\n", + "print(chain.invoke({\"topic\": \"Space travel\"}))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Optionally, pass `StreamingStdOutCallbackHandler` to stream tokens:\n", - "\n", - "```\n", - "chat_model = ChatOllama(\n", - " model=\"llama2:7b-chat\",\n", - " callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),\n", - ")\n", - "```" + "LCEL chains, out of the box, provide extra functionalities, such as streaming of responses, and async support" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 12, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "AIMessage(content='\\nArtificial intelligence (AI) has a rich and diverse history that spans several decades. Here is a brief overview of the major milestones and events in the development of AI:\\n\\n1. 1950s: The Dartmouth Conference: The field of AI was officially launched at a conference held at Dartmouth College in 1956. Attendees included computer scientists, mathematicians, and cognitive scientists who were interested in exploring the possibilities of creating machines that could simulate human intelligence.\\n2. 1951: The Turing Test: British mathematician Alan Turing proposed a test to measure a machine\\'s ability to exhibit intelligent behavior equivalent to, or indistinguishable from, that of a human. The Turing Test has since become a benchmark for measuring the success of AI systems.\\n3. 1956: The First AI Program: Computer scientist John McCarthy created the first AI program, called the Logical Theorist, which was designed to reason and solve problems using logical deduction.\\n4. 1960s: Rule-Based Expert Systems: The development of rule-based expert systems, which used a set of rules to reason and make decisions, marked a significant milestone in the history of AI. These systems were widely used in industries such as banking, healthcare, and transportation.\\n5. 1970s: Machine Learning: Machine learning, which enables machines to learn from data without being explicitly programmed, emerged as a major area of research in AI. This led to the development of algorithms such as decision trees and neural networks.\\n6. 1980s: Expert Systems: The development of expert systems, which were designed to mimic the decision-making abilities of human experts, reached its peak in the 1980s. These systems were widely used in industries such as banking and healthcare.\\n7. 1990s: AI Winter: Despite the progress that had been made in AI research, the field experienced a decline in funding and interest in the 1990s, which became known as the \"AI winter.\"\\n8. 2000s: Machine Learning Resurgence: The resurgence of machine learning, driven by advances in computational power and data storage, led to a new wave of AI research and applications.\\n9. 2010s: Deep Learning: The development of deep learning algorithms, which are capable of learning complex patterns in large datasets, marked a significant breakthrough in AI research. These algorithms have been used in applications such as image and speech recognition, natural language processing, and autonomous vehicles.\\n10. Present Day: AI is now being applied to a wide range of industries and domains, including healthcare, finance, transportation, and education. The field is continuing to evolve, with new technologies and applications emerging all the time.\\n\\nOverall, the history of AI reflects a long-standing interest in creating machines that can simulate human intelligence. While the field has experienced periods of progress and setbacks, it continues to evolve and expand into new areas of research and application.')" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + " Sure\n", + ",\n", + " here\n", + "'s\n", + " a\n", + " joke\n", + ":\n", + " Why\n", + " did\n", + " the\n", + " astronaut\n", + " break\n", + " up\n", + " with\n", + " his\n", + " girlfriend\n", + "?\n", + " Because\n", + " he\n", + " needed\n", + " more\n", + " space\n", + " to\n", + " explore\n", + ".\n", + "\n", + "\n", + "\n" + ] } ], "source": [ - "from langchain.schema import HumanMessage\n", + "topic = {\"topic\": \"Space travel\"}\n", "\n", - "messages = [HumanMessage(content=\"Tell me about the history of AI\")]\n", - "chat_model(messages)" + "for chunks in chain.stream(topic):\n", + " print(chunks)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For streaming async support, here's an example - all possible via the single chain created above." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Sure\n", + ",\n", + " here\n", + "'s\n", + " a\n", + " little\n", + " one\n", + ":\n", + " Why\n", + " did\n", + " the\n", + " rocket\n", + " scientist\n", + " break\n", + " up\n", + " with\n", + " her\n", + " partner\n", + "?\n", + " Because\n", + " he\n", + " couldn\n", + "'t\n", + " handle\n", + " all\n", + " her\n", + " \"\n", + "space\n", + "y\n", + "\"\n", + " jokes\n", + ".\n", + "\n", + "\n", + "\n" + ] + } + ], + "source": [ + "topic = {\"topic\": \"Space travel\"}\n", + "\n", + "async for chunks in chain.astream(topic):\n", + " print(chunks)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Take a look at the [LangChain Expressive Language (LCEL) Interface](https://python.langchain.com/docs/expression_language/interface) for the other available interfaces for use when a chain is created.\n", + "\n", + "## Building from source\n", + "\n", + "For up to date instructions on building from source, check the Ollama documentation on [Building from Source](https://github.com/jmorganca/ollama?tab=readme-ov-file#building)" ] }, { @@ -122,42 +248,32 @@ "source": [ "## Extraction\n", " \n", - "Update your version of Ollama and supply the [`format`](https://github.com/jmorganca/ollama/blob/main/docs/api.md#json-mode) flag.\n", + "Use the latest version of Ollama and supply the [`format`](https://github.com/jmorganca/ollama/blob/main/docs/api.md#json-mode) flag. The `format` flag will force the model to produce the response in JSON.\n", "\n", - "We can enforce the model to produce JSON.\n", - "\n", - "**Note:** You can also try out the experimental [OllamaFunctions](https://python.langchain.com/docs/integrations/chat/ollama_functions) wrapper for convenience." + "> **Note:** You can also try out the experimental [OllamaFunctions](https://python.langchain.com/docs/integrations/chat/ollama_functions) wrapper for convenience." ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "from langchain.callbacks.manager import CallbackManager\n", - "from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n", "from langchain_community.chat_models import ChatOllama\n", "\n", - "chat_model = ChatOllama(\n", - " model=\"llama2\",\n", - " format=\"json\",\n", - " callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),\n", - ")" + "llm = ChatOllama(model=\"llama2\", format=\"json\", temperature=0)" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{\"morning\": {\"sky\": \"pink\", \"sun\": \"rise\"}, \"daytime\": {\"sky\": \"blue\", \"sun\": \"high\"}, \"afternoon\": {\"sky\": \"gray\", \"sun\": \"peak\"}, \"evening\": {\"sky\": \"orange\", \"sun\": \"set\"}}\n", - " \t\n", - "\n" + "content='{\\n\"morning\": {\\n\"color\": \"light blue\"\\n},\\n\"noon\": {\\n\"color\": \"blue\"\\n},\\n\"afternoon\": {\\n\"color\": \"grayish-blue\"\\n},\\n\"evening\": {\\n\"color\": \"pinkish-orange\"\\n}\\n}'\n" ] } ], @@ -170,37 +286,27 @@ " )\n", "]\n", "\n", - "chat_model_response = chat_model(messages)" + "chat_model_response = llm.invoke(messages)\n", + "print(chat_model_response)" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "\n", "{\n", - " \"name\": \"John\",\n", - " \"age\": 35,\n", - " \"fav_food\": \"pizza\"\n", - "}\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n" + "\"name\": \"John\",\n", + "\"age\": 35,\n", + "\"interests\": [\n", + "\"pizza\"\n", + "]\n", + "}\n" ] } ], @@ -208,6 +314,9 @@ "import json\n", "\n", "from langchain.schema import HumanMessage\n", + "from langchain_community.chat_models import ChatOllama\n", + "from langchain_core.output_parsers import StrOutputParser\n", + "from langchain_core.prompts import ChatPromptTemplate\n", "\n", "json_schema = {\n", " \"title\": \"Person\",\n", @@ -225,17 +334,24 @@ " \"required\": [\"name\", \"age\"],\n", "}\n", "\n", + "llm = ChatOllama(model=\"llama2\")\n", + "\n", "messages = [\n", " HumanMessage(\n", " content=\"Please tell me about a person using the following JSON schema:\"\n", " ),\n", - " HumanMessage(content=json.dumps(json_schema, indent=2)),\n", + " HumanMessage(content=\"{dumps}\"),\n", " HumanMessage(\n", " content=\"Now, considering the schema, tell me about a person named John who is 35 years old and loves pizza.\"\n", " ),\n", "]\n", "\n", - "chat_model_response = chat_model(messages)" + "prompt = ChatPromptTemplate.from_messages(messages)\n", + "dumps = json.dumps(json_schema, indent=2)\n", + "\n", + "chain = prompt | llm | StrOutputParser()\n", + "\n", + "print(chain.invoke({\"dumps\": dumps}))" ] }, { @@ -246,25 +362,32 @@ "\n", "Ollama has support for multi-modal LLMs, such as [bakllava](https://ollama.ai/library/bakllava) and [llava](https://ollama.ai/library/llava).\n", "\n", - "Browse the full set of versions for models with `tags`, such as [here](https://ollama.ai/library/llava/tags).\n", + "Browse the full set of versions for models with `tags`, such as [Llava](https://ollama.ai/library/llava/tags).\n", "\n", - "Download the desired LLM:\n", - "```\n", - "ollama pull bakllava\n", - "```\n", + "Download the desired LLM via `ollama pull bakllava`\n", "\n", - "Be sure to update Ollama so that you have the most recent version to support multi-modal." + "Be sure to update Ollama so that you have the most recent version to support multi-modal.\n", + "\n", + "Check out the typical example of how to use ChatOllama multi-modal support below:" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], "source": [ - "%pip install --upgrade --quiet pillow" + "pip install --upgrade --quiet pillow" ] }, { @@ -275,7 +398,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -319,7 +442,7 @@ " display(HTML(image_html))\n", "\n", "\n", - "file_path = \"/Users/rlm/Desktop/Eval_Sets/multi_modal_presentations/DDOG/img_23.jpg\"\n", + "file_path = \"../../../static/img/ollama_example_img.jpg\"\n", "pil_image = Image.open(file_path)\n", "\n", "image_b64 = convert_to_base64(pil_image)\n", @@ -328,40 +451,52 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "AIMessage(content='90%')" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "90%\n" + ] } ], "source": [ + "from langchain.schema import HumanMessage\n", "from langchain_community.chat_models import ChatOllama\n", - "from langchain_core.messages import HumanMessage\n", "\n", - "chat_model = ChatOllama(\n", - " model=\"bakllava\",\n", + "llm = ChatOllama(model=\"bakllava\", temperature=0)\n", + "\n", + "\n", + "def prompt_func(data):\n", + " text = data[\"text\"]\n", + " image = data[\"image\"]\n", + "\n", + " image_part = {\n", + " \"type\": \"image_url\",\n", + " \"image_url\": f\"data:image/jpeg;base64,{image}\",\n", + " }\n", + "\n", + " content_parts = []\n", + "\n", + " text_part = {\"type\": \"text\", \"text\": text}\n", + "\n", + " content_parts.append(image_part)\n", + " content_parts.append(text_part)\n", + "\n", + " return [HumanMessage(content=content_parts)]\n", + "\n", + "\n", + "from langchain_core.output_parsers import StrOutputParser\n", + "\n", + "chain = prompt_func | llm | StrOutputParser()\n", + "\n", + "query_chain = chain.invoke(\n", + " {\"text\": \"What is the Dollar-based gross retention rate?\", \"image\": image_b64}\n", ")\n", "\n", - "# Call the chat model with both messages and images\n", - "content_parts = []\n", - "image_part = {\n", - " \"type\": \"image_url\",\n", - " \"image_url\": f\"data:image/jpeg;base64,{image_b64}\",\n", - "}\n", - "text_part = {\"type\": \"text\", \"text\": \"What is the Daollar-based gross retention rate?\"}\n", - "\n", - "content_parts.append(image_part)\n", - "content_parts.append(text_part)\n", - "prompt = [HumanMessage(content=content_parts)]\n", - "chat_model(prompt)" + "print(query_chain)" ] } ], @@ -381,7 +516,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.16" + "version": "3.9.1" } }, "nbformat": 4, diff --git a/docs/docs/integrations/llms/ollama.ipynb b/docs/docs/integrations/llms/ollama.ipynb index 9637c965ccd..79ea7589878 100644 --- a/docs/docs/integrations/llms/ollama.ipynb +++ b/docs/docs/integrations/llms/ollama.ipynb @@ -18,64 +18,54 @@ "\n", "First, follow [these instructions](https://github.com/jmorganca/ollama) to set up and run a local Ollama instance:\n", "\n", - "* [Download](https://ollama.ai/download)\n", - "* Fetch a model via `ollama pull `\n", - "* e.g., for `Llama-7b`: `ollama pull llama2` (see full list [here](https://ollama.ai/library)\n", - "* This will download the most basic version of the model typically (e.g., smallest # parameters)\n", - "* On Mac, it will download to \n", + "* [Download](https://ollama.ai/download) and install Ollama onto the available supported platforms (including Windows Subsystem for Linux)\n", + "* Fetch available LLM model via `ollama pull `\n", + " * View a list of available models via the [model library](https://ollama.ai/library)\n", + " * e.g., for `Llama-7b`: `ollama pull llama2`\n", + "* This will download the default tagged version of the model. Typically, the default points to the latest, smallest sized-parameter model.\n", "\n", - "`~/.ollama/models/manifests/registry.ollama.ai/library//latest`\n", - "\n", - "* And we specify a particular version, e.g., for `ollama pull vicuna:13b-v1.5-16k-q4_0`\n", - "* The file is here with the model version in place of `latest`\n", - "\n", - "`~/.ollama/models/manifests/registry.ollama.ai/library/vicuna/13b-v1.5-16k-q4_0`\n", - "\n", - "You can easily access models in a few ways:\n", - "\n", - "1/ if the app is running:\n", - "\n", - "* All of your local models are automatically served on `localhost:11434`\n", - "* Select your model when setting `llm = Ollama(..., model=\":\")`\n", - "* If you set `llm = Ollama(..., model=\" On Mac, the models will be download to `~/.ollama/models`\n", + "> \n", + "> On Linux (or WSL), the models will be stored at `/usr/share/ollama/.ollama/models`\n", "\n", + "* Specify the exact version of the model of interest as such `ollama pull vicuna:13b-v1.5-16k-q4_0` (View the [various tags for the `Vicuna`](https://ollama.ai/library/vicuna/tags) model in this instance)\n", + "* To view all pulled models, use `ollama list`\n", + "* To chat directly with a model from the command line, use `ollama run `\n", + "* View the [Ollama documentation](https://github.com/jmorganca/ollama) for more commands. Run `ollama help` in the terminal to see available commands too.\n", "\n", "## Usage\n", "\n", - "You can see a full list of supported parameters on the [API reference page](https://api.python.langchain.com/en/latest/llms/langchain.llms.ollama.Ollama.html)." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "from langchain.callbacks.manager import CallbackManager\n", - "from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n", - "from langchain_community.llms import Ollama\n", + "You can see a full list of supported parameters on the [API reference page](https://api.python.langchain.com/en/latest/llms/langchain.llms.ollama.Ollama.html).\n", "\n", - "llm = Ollama(model=\"llama2\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Optionally, pass `StreamingStdOutCallbackHandler` to stream tokens:\n", + "If you are using a LLaMA `chat` model (e.g., `ollama pull llama2:7b-chat`) then you can use the `ChatOllama` interface.\n", "\n", + "This includes [special tokens](https://huggingface.co/blog/llama2#how-to-prompt-llama-2) for system message and user input.\n", + "\n", + "## Interacting with Models \n", + "\n", + "Here are a few ways to interact with pulled local models\n", + "\n", + "#### directly in the terminal:\n", + "\n", + "* All of your local models are automatically served on `localhost:11434`\n", + "* Run `ollama run ` to start interacting via the command line directly\n", + "\n", + "### via an API\n", + "\n", + "Send an `application/json` request to the API endpoint of Ollama to interact.\n", + "\n", + "```bash\n", + "curl http://localhost:11434/api/generate -d '{\n", + " \"model\": \"llama2\",\n", + " \"prompt\":\"Why is the sky blue?\"\n", + "}'\n", "```\n", - "llm = Ollama(\n", - " model=\"llama2\",\n", - " callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),\n", - ")\n", - "```" + "\n", + "See the Ollama [API documentation](https://github.com/jmorganca/ollama/blob/main/docs/api.md) for all endpoints.\n", + "\n", + "#### via LangChain\n", + "\n", + "See a typical basic example of using Ollama chat model in your LangChain application." ] }, { @@ -86,7 +76,7 @@ { "data": { "text/plain": [ - "' Artificial intelligence (AI) has a rich and varied history that spans several decades. październik 1950s and has evolved significantly over time. Here is a brief overview of the major milestones in the history of AI:\\n\\n1. 1950s: The Dartmouth Conference - Considered the birthplace of AI, this conference brought together computer scientists, mathematicians, and cognitive scientists to discuss the possibilities of creating machines that could simulate human intelligence. Attendees included John McCarthy, Marvin Minsky, Nathaniel Rochester, and Claude Shannon.\\n2. 1951: The Turing Test - Alan Turing proposed a test to measure a machine\\'s ability to exhibit intelligent behavior equivalent to, or indistinguishable from, that of a human. The Turing Test has since become a benchmark for measuring the success of AI systems.\\n3. 1956: The First AI Program - John McCarthy created the first AI program, called the Logical Theorist, which was designed to reason and solve problems using logical deduction.\\n4. 1960s: Rule-Based Expert Systems - Researchers developed rule-based expert systems, which used a set of rules to reason and make decisions. These systems were widely used in industries such as banking and healthcare.\\n5. 1970s: Machine Learning -Machine learning, a subfield of AI, emerged as a way for machines to learn from data without being explicitly programmed. This led to the development of algorithms such as decision trees and neural networks.\\n6. 1980s: Expert Systems - The development of expert systems, which were designed to mimic the decision-making abilities of human experts, reached its peak in the 1980s. These systems were widely used in industries such as banking and healthcare.\\n7. 1990s: AI Winter - Despite the progress made in AI research, the field experienced a decline in funding and interest in the 1990s, known as the \"AI winter.\"\\n8. 2000s: AI Resurgence - The resurgence of AI began in the early 2000s with the development of new algorithms and techniques, such as support vector machines and deep learning. This led to a renewed interest in AI research and applications.\\n9. 2010s: Rise of Deep Learning - The development of deep learning algorithms, which are capable of learning and improving on their own by analyzing large amounts of data, has been a major factor in the recent progress made in AI. These algorithms have been used in applications such as image recognition, natural language processing, and autonomous vehicles.\\n10. Present Day: AI Continues to Advance - AI is continuing to advance at a rapid pace, with new techniques and applications emerging all the time. Areas of research include natural language processing, computer vision, robotics, and more.\\n\\nSome notable people who have made significant contributions to the field of AI include:\\n\\n1. Alan Turing - Considered one of the pioneers of AI, Turing proposed the Turing Test and developed the concept of a universal machine.\\n2. John McCarthy - McCarthy is known as the \"father of AI\" for his work in developing the field of AI. He coined the term \"Artificial Intelligence\" and was instrumental in organizing the Dartmouth Conference.\\n3. Marvin Minsky - Minsky was a pioneer in the field of neural networks and co-founder of the MIT AI Laboratory.\\n4. Nathaniel Rochester - Rochester was a computer scientist and cognitive scientist who worked on early AI projects, including the development of the Logical Theorist.\\n5. Claude Shannon - Shannon was a mathematician and electrical engineer who is known for his work on information theory, which has had a significant impact on the field of AI.\\n6. Yann LeCun - LeCun is a computer scientist and the director of AI Research at Facebook. He is also the Silver Professor of Computer Science at New York University, and a professor at the Courant Institute of Mathematical Sciences.\\n7. Geoffrey Hinton - Hinton is a computer scientist and cognitive psychologist who is known for his work on artificial neural networks. He is a pioneer in the field of deep learning and has made significant contributions to the development of convolutional neural networks (CNNs).\\n8. Yoshua Bengio - Bengio is a computer scientist and a pioneer in the field of deep learning. He is known for his work on recurrent neural networks (RNNs) and has made significant contributions to the development of CNNs and RNNs.\\n9. Andrew Ng - Ng is a computer scientist and entrepreneur who has made significant contributions to the field of AI. He is known for his work on deep learning and has worked at Google, where he founded the Google Brain deep learning project, and at Baidu, where he led the company\\'s AI group.\\n10. Demis Hassabis - Hassabis is a computer scientist and entrepreneur who is known for his work on deep learning and artificial intelligence. He is the co-founder of DeepMind, which was acquired by Alphabet in 2014, and has made significant contributions to the field of AI.\\n\\nThese are just a few examples of notable people who have made significant contributions to the field of AI. There are many other researchers and scientists who have also made important advancements in the field.'" + "\"Sure! Here's a quick one:\\n\\nWhy don't scientists trust atoms?\\nBecause they make up everything!\\n\\nI hope that brought a smile to your face!\"" ] }, "execution_count": 2, @@ -95,7 +85,97 @@ } ], "source": [ - "llm(\"Tell me about the history of AI\")" + "from langchain_community.llms import Ollama\n", + "\n", + "llm = Ollama(model=\"llama2\")\n", + "\n", + "llm.invoke(\"Tell me a joke\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To stream tokens, use the `.stream(...)` method:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "S\n", + "ure\n", + ",\n", + " here\n", + "'\n", + "s\n", + " one\n", + ":\n", + "\n", + "\n", + "\n", + "\n", + "Why\n", + " don\n", + "'\n", + "t\n", + " scient\n", + "ists\n", + " trust\n", + " atoms\n", + "?\n", + "\n", + "\n", + "B\n", + "ecause\n", + " they\n", + " make\n", + " up\n", + " everything\n", + "!\n", + "\n", + "\n", + "\n", + "\n", + "I\n", + " hope\n", + " you\n", + " found\n", + " that\n", + " am\n", + "using\n", + "!\n", + " Do\n", + " you\n", + " want\n", + " to\n", + " hear\n", + " another\n", + " one\n", + "?\n", + "\n" + ] + } + ], + "source": [ + "query = \"Tell me a joke\"\n", + "\n", + "for chunks in llm.stream(query):\n", + " print(chunks)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To learn more about the LangChain Expressive Language and the available methods on an LLM, see the [LCEL Interface](https://python.langchain.com/docs/expression_language/interface)" ] }, { @@ -106,16 +186,14 @@ "\n", "Ollama has support for multi-modal LLMs, such as [bakllava](https://ollama.ai/library/bakllava) and [llava](https://ollama.ai/library/llava).\n", "\n", - "```\n", - "ollama pull bakllava\n", - "```\n", + "`ollama pull bakllava`\n", "\n", "Be sure to update Ollama so that you have the most recent version to support multi-modal." ] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -132,7 +210,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -176,7 +254,7 @@ " display(HTML(image_html))\n", "\n", "\n", - "file_path = \"/Users/rlm/Desktop/Eval_Sets/multi_modal_presentations/DDOG/img_23.jpg\"\n", + "file_path = \"../../../static/img/ollama_example_img.jpg\"\n", "pil_image = Image.open(file_path)\n", "image_b64 = convert_to_base64(pil_image)\n", "plt_img_base64(image_b64)" @@ -184,7 +262,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -193,7 +271,7 @@ "'90%'" ] }, - "execution_count": 5, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -220,7 +298,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.1" + "version": "3.9.1" } }, "nbformat": 4, diff --git a/docs/static/img/ollama_example_img.jpg b/docs/static/img/ollama_example_img.jpg new file mode 100644 index 00000000000..86149b79844 Binary files /dev/null and b/docs/static/img/ollama_example_img.jpg differ