Ollama + SingleStore – LangChain = :-(

In a previous article, we used Ollama with LangChain and SingleStore. LangChain provided an efficient and compact solution for integrating Ollama with SingleStore. However, what if we were to remove LangChain? In this article, we’ll demonstrate an example of using Ollama with SingleStore without relying on LangChain. We’ll see that while we can achieve the same results described in the previous article, the number of code increases, requiring us to manage more of the plumbing that LangChain normally handles.

The notebook file used in this article is available on GitHub.

Introduction

From the previous article, we’ll follow the same steps to set up our test environment as described in these sections:

  • Introduction
    • Use a Virtual Machine or venv.
  • Create a SingleStoreDB Cloud account
    • Use Ollama Demo Group as the Workspace Group Name and ollama-demo as the Workspace Name. Make a note of the password and host name. Temporarily allow access from anywhere by configuring the firewall under Ollama Demo Group > Firewall.
  • Create a Database 
  • CREATE DATABASE IF NOT EXISTS ollama_demo;
  • Install Jupyter
    • pip install notebook
  • Install Ollama
    • curl -fsSL https://ollama.com/install.sh | sh
  • Environment Variable
    • export SINGLESTOREDB_URL="admin:<password>@<host>:3306/ollama_demo"
      Replace <password> and <host> with the values for your environment.
  • Launch Jupyter
    • jupyter notebook

Fill out the Notebook

First, some packages:

Shell

 

Next, we’ll import some libraries:

Python

 

We’ll create embeddings using all-minilm (45 MB at the time of writing):

Python

 

Example output:

Plain Text

 

For our LLM we’ll use llama2 (3.8 GB at the time of writing): 

Python

 

Example output:

Plain Text

 

Next, we’ll use the example text from the Ollama website:

Python

 

We’ll set the embeddings to all-minilm and iterate through each document to build up the content for a Pandas DataFrame. Additionally, we’ll convert the embeddings to a 32-bit format as this is SingleStore’s default for the VECTOR data type. Lastly, we’ll determine the number of embedding dimensions for the first document in the Pandas DataFrame. 

Next, we’ll create a connection to our SingleStore instance:

Python

 

Now we’ll create a table with the vector column using the dimensions we previously determined:

Python

 

We’ll now write the Pandas DataFrame to the table:

Python

 

Example output:

Plain Text

 

We’ll now create an index to match the one we created in the previous article:

Python

 

We’ll now ask a question, as follows:

Python

 

We’ll convert the prompt to embeddings, ensure that the embeddings are converted to a 32-bit format, and then execute the SQL query which uses the infix notation <-> for Euclidean Distance.

Example output:

Plain Text

 

Next, we’ll use the LLM, as follows:

Python

 

Example output:

Plain Text

 

Summary

In this article, we’ve replicated the steps we followed in the previous article and achieved similar results. However, we had to write a series of SQL statements and manage several steps that LangChain would have handled for us. Additionally, there may be more time and cost involved in maintaining the code base long-term compared to the LangChain solution. 

Using LangChain instead of writing custom code for database access provides several advantages, such as efficiency, scalability, and reliability. 

LangChain offers a library of prebuilt modules for database interaction, reducing development time and effort. Developers can use these modules to quickly implement various database operations without starting from scratch.

LangChain abstracts many of the complexities involved in database management, allowing developers to focus on high-level tasks rather than low-level implementation details. This improves productivity and time-to-market for database-driven applications.

LangChain has a large, active, and growing community of developers, is available on GitHub, and provides extensive documentation and examples. 

In summary, LangChain offers developers a powerful, efficient, and reliable platform for building database-driven applications, enabling them to focus on business problems using higher-level abstractions rather than reinventing the wheel with custom code. Comparing the example in this article with the example we used in the previous article, we can see the benefits.

Source:
https://dzone.com/articles/ollama-plus-singlestore-minus-langchain