From d3ee1329e9630a4bb08a9210b2f693afe8107543 Mon Sep 17 00:00:00 2001 From: Matt Williams Date: Thu, 10 Aug 2023 21:27:37 -0700 Subject: [PATCH] Add tutorials for using Langchain with ollama Signed-off-by: Matt Williams --- docs/README.md | 1 + docs/tutorials.md | 5 +++ docs/tutorials/langchainjs.md | 73 +++++++++++++++++++++++++++++++ docs/tutorials/langchainpy.md | 81 +++++++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+) create mode 100644 docs/tutorials.md create mode 100644 docs/tutorials/langchainjs.md create mode 100644 docs/tutorials/langchainpy.md diff --git a/docs/README.md b/docs/README.md index 84f7dd8a..20d445ca 100644 --- a/docs/README.md +++ b/docs/README.md @@ -3,3 +3,4 @@ - [Modelfile](./modelfile.md) - [How to develop Ollama](./development.md) - [API](./api.md) +- [Tutorials](./tutorials.md) diff --git a/docs/tutorials.md b/docs/tutorials.md new file mode 100644 index 00000000..bdaf16d2 --- /dev/null +++ b/docs/tutorials.md @@ -0,0 +1,5 @@ +# Tutorials + +Here is a list of ways you can use Ollama with other tools to build interesting applications. + +- [Using LangChain with Ollama using JavaScript](./tutorials/langchainjs.md) \ No newline at end of file diff --git a/docs/tutorials/langchainjs.md b/docs/tutorials/langchainjs.md new file mode 100644 index 00000000..b3eb03df --- /dev/null +++ b/docs/tutorials/langchainjs.md @@ -0,0 +1,73 @@ +# Using LangChain with Ollama using JavaScript + +In this tutorial, we are going to use JavaScript with LangChain and Ollama to learn about something just a touch more recent. In August 2023, there was a series of wildfires on Maui. There is no way any LLM can know about this, since their training data doesn't include anything as recent as that. So we can find the [Wikipedia article about the fires](https://en.wikipedia.org/wiki/2023_Hawaii_wildfires) and ask questions about the contents. + +To get started, let's just use **LangChain** to ask a simple question to a model. To do this with JavaScript, we need to install **LangChain**: + +```bash +npm install langchain +``` + +Now we can start building out our JavaScript: + +```javascript +import { Ollama } from "langchain/llms/ollama"; + +const ollama = new Ollama({ + baseUrl: "http://localhost:11434", + model: "llama2", +}); + +const answer = await ollama.call(`why is the sky blue?`); + +console.log(answer); +``` + +That will get us the same thing as if we ran `ollama run llama2 "why is the sky blue"`. But we want to load a document from the web to ask a question against. **Cheerio** is a great library for ingesting a webpage, and **LangChain** uses it in their **CheerioWebBaseLoader**. So let's build that part of the app. + +```javascript +import { CheerioWebBaseLoader } from "langchain/document_loaders/web/cheerio"; + +const loader = new CheerioWebBaseLoader("https://en.wikipedia.org/wiki/2023_Hawaii_wildfires"); +const data = loader.load(); +``` + +That will load the document. Although this page is smaller than the Odyssey, it is certainly bigger than the context size for most LLMs. So we are going to need to split into smaller pieces, and then select just the pieces relevant to our question. This is a great use for a vector datastore. In this example, we will use the **MemoryVectorStore** that is part of **LangChain**. But there is one more thing we need to get the content into the datastore. We have to run an embeddings process that converts the tokens in the text into a series of vectors. And for that, we are going to use **Tensorflow**. There is a lot of stuff going on in this one. First, install the **Tensorflow** components that we need. + +```javascript +npm install @tensorflow/tfjs-core@3.6.0 @tensorflow/tfjs-converter@3.6.0 @tensorflow-models/universal-sentence-encoder@1.3.3 @tensorflow/tfjs-node@4.10.0 +``` + +If you just install those components without the version numbers, it will install the latest versions, but there are conflicts within **Tensorflow**, so you need to install the compatible versions. + +```javascript +import { RecursiveCharacterTextSplitter } from "langchain/text_splitter" +import { MemoryVectorStore } from "langchain/vectorstores/memory"; +import "@tensorflow/tfjs-node"; +import { TensorFlowEmbeddings } from "langchain/embeddings/tensorflow"; + +// Split the text into 500 character chunks. And overlap each chunk by 20 characters +const textSplitter = new RecursiveCharacterTextSplitter({ + chunkSize: 500, + chunkOverlap: 20 +}); +const splitDocs = await textSplitter.splitDocuments(data); + +// Then use the TensorFlow Embedding to store these chunks in the datastore +const vectorStore = await MemoryVectorStore.fromDocuments(splitDocs, new TensorFlowEmbeddings()); +``` + +To connect the datastore to a question asked to a LLM, we need to use the concept at the heart of **LangChain**: the chain. Chains are a way to connect a number of activities together to accomplish a particular tasks. There are a number of chain types available, but for this tutorial we are using the **RetrievalQAChain**. + +```javascript +import { RetrievalQAChain } from "langchain/chains"; + +const retriever = vectorStore.asRetriever(); +const chain = RetrievalQAChain.fromLLM(ollama, retriever); +const result = await chain.call({query: "When was Hawaii's request for a major disaster declaration approved?"}); +console.log(result.text) +``` + +So we created a retriever, which is a way to return the chunks that match a query from a datastore. And then connect the retriever and the model via a chain. Finally, we send a query to the chain, which results in an answer using our document as a source. The answer it returned was correct, August 10, 2023. + +And that is a simple introduction to what you can do with **LangChain** and **Ollama.** diff --git a/docs/tutorials/langchainpy.md b/docs/tutorials/langchainpy.md new file mode 100644 index 00000000..c340ea62 --- /dev/null +++ b/docs/tutorials/langchainpy.md @@ -0,0 +1,81 @@ +# Using LangChain with Ollama in Python + +Let's imagine we are studying the classics, such as **the Odyssey** by **Homer**. We might have a question about Neleus and his family. If you ask llama2 for that info, you may get something like: + +> I apologize, but I'm a large language model, I cannot provide information on individuals or families that do not exist in reality. Neleus is not a real person or character, and therefore does not have a family or any other personal details. My apologies for any confusion. Is there anything else I can help you with? + +This sounds like a typical censored response, but even llama2-uncensored gives a mediocre answer: + +> Neleus was a legendary king of Pylos and the father of Nestor, one of the Argonauts. His mother was Clymene, a sea nymph, while his father was Neptune, the god of the sea. + +So let's figure out how we can use **LangChain** with Ollama to ask our question to the actual document, the Odyssey by Homer, using Python. + +Let's start by asking a simple question that we can get an answer to from the **Llama2** model using **Ollama**. First, we need to install the **LangChain** package: + +`pip install langchain` + +Then we can create a model and ask the question: + +```python +from langchain.llms import Ollama +ollama = Ollama(base_url='[http://localhost:11434](http://localhost:11434/)', +model="llama2") +print(ollama("why is the sky blue")) +``` + +Notice that we are defining the model and the base URL for Ollama. + +Now let's load a document to ask questions against. I'll load up the Odyssey by Homer, which you can find at Project Gutenberg. We will need **WebBaseLoader** which is part of **LangChain** and loads text from any webpage. On my machine, I also needed to install **bs4** to get that to work, so run `pip install bs4`. + +```python +from langchain.document_loaders import WebBaseLoader +loader = WebBaseLoader("https://www.gutenberg.org/files/1727/1727-h/1727-h.htm") +data = loader.load() +``` + +This file is pretty big. Just the preface is 3000 tokens. Which means the full document won't fit into the context for the model. So we need to split it up into smaller pieces. + +```python +from langchain.text_splitter import RecursiveCharacterTextSplitter + +text_splitter=RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0) +all_splits = text_splitter.split_documents(data) +``` + +It's split up, but we have to find the relevant splits and then submit those to the model. We can do this by creating embeddings and storing them in a vector database. For now, we don't have embeddings built in to Ollama, though we will be adding that soon, so for now, we can use the GPT4All library for that. We will use ChromaDB in this example for a vector database. `pip install GPT4All chromadb` + +```python +from langchain.embeddings import GPT4AllEmbeddings +from langchain.vectorstores import Chroma +vectorstore = Chroma.from_documents(documents=all_splits, embedding=GPT4AllEmbeddings()) +``` + +Now let's ask a question from the document. **Who was Neleus, and who is in his family?** Neleus is a character in the Odyssey, and the answer can be found in our text. + +```python +question="Who is Neleus and who is in Neleus' family?" +docs = vectorstore.similarity_search(question) +len(docs) +``` + +This will output the number of matches for chunks of data similar to the search. + +The next thing is to send the question and the relevant parts of the docs to the model to see if we can get a good answer. But we are stitching two parts of the process together, and that is called a chain. This means we need to define a chain: + +```python +from langchain.chains import RetrievalQA +qachain=RetrievalQA.from_chain_type(ollama, retriever=vectorstore.as_retriever()) +qachain({"query": question}) +``` + +The answer received from this chain was: + +> Neleus is a character in Homer's "Odyssey" and is mentioned in the context of Penelope's suitors. Neleus is the father of Chloris, who is married to Neleus and bears him several children, including Nestor, Chromius, Periclymenus, and Pero. Amphinomus, the son of Nisus, is also mentioned as a suitor of Penelope and is known for his good natural disposition and agreeable conversation. + +It's not a perfect answer, as it implies Neleus married his daughter when actually Chloris "was the youngest daughter to Amphion son of Iasus and king of Minyan Orchomenus, and was Queen in Pylos". + +I updated the chunk_overlap for the text splitter to 20 and tried again and got a much better answer: + +> Neleus is a character in Homer's epic poem "The Odyssey." He is the husband of Chloris, who is the youngest daughter of Amphion son of Iasus and king of Minyan Orchomenus. Neleus has several children with Chloris, including Nestor, Chromius, Periclymenus, and Pero. + +And that is a much better answer.