Skip to content

The agent engineering platform.

templates: Added template for JaguarDB (#16757)

daniel ungedf9d1c90522
diff --git a/templates/rag-jaguardb/LICENSE b/templates/rag-jaguardb/LICENSE
new file mode 100644
index 0000000000..426b650903
--- /dev/null
+++ b/templates/rag-jaguardb/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2023 LangChain, Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/templates/rag-jaguardb/README.md b/templates/rag-jaguardb/README.md
new file mode 100644
index 0000000000..e99eefb1d0
--- /dev/null
+++ b/templates/rag-jaguardb/README.md
@@ -0,0 +1,91 @@
+
+# rag-jaguardb
+
+This template performs RAG using JaguarDB and OpenAI.
+
+## Environment Setup
+
+You should export two environment variables, one being your Jaguar URI, the other being your OpenAI API KEY.
+If you do not have JaguarDB set up, see the `Setup Jaguar` section at the bottom for instructions on how to do so.
+
+```shell
+export JAGUAR_API_KEY=...
+export OPENAI_API_KEY=...
+```
+
+## Usage
+
+To use this package, you should first have the LangChain CLI installed:
+
+```shell
+pip install -U langchain-cli
+```
+
+To create a new LangChain project and install this as the only package, you can do:
+
+```shell
+langchain app new my-app --package rag-jaguardb
+```
+
+If you want to add this to an existing project, you can just run:
+
+```shell
+langchain app add rag-jagaurdb
+```
+
+And add the following code to your `server.py` file:
+```python
+from rag_jaguardb import chain as rag_jaguardb
+
+add_routes(app, rag_jaguardb_chain, path="/rag-jaguardb")
+```
+
+(Optional) Let's now configure LangSmith.
+LangSmith will help us trace, monitor and debug LangChain applications.
+LangSmith is currently in private beta, you can sign up [here](https://smith.langchain.com/).
+If you don't have access, you can skip this section
+
+
+```shell
+export LANGCHAIN_TRACING_V2=true
+export LANGCHAIN_API_KEY=<your-api-key>
+export LANGCHAIN_PROJECT=<your-project> # if not specified, defaults to "default"
+```
+
+
+If you are inside this directory, then you can spin up a LangServe instance directly by:
+
+```shell
+langchain serve
+```
+
+This will start the FastAPI app with a server is running locally at
+[http://localhost:8000](http://localhost:8000)
+
+We can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs)
+We can access the playground at [http://127.0.0.1:8000/rag-jaguardb/playground](http://127.0.0.1:8000/rag-jaguardb/playground)
+
+We can access the template from code with:
+
+```python
+from langserve.client import RemoteRunnable
+
+runnable = RemoteRunnable("http://localhost:8000/rag-jaguardb")
+```
+
+## JaguarDB Setup
+
+To utilize JaguarDB, you can use docker pull and docker run commands to quickly setup JaguarDB.
+
+```shell
+docker pull jaguardb/jaguardb
+docker run -d -p 8888:8888 --name jaguardb jaguardb/jaguardb
+```
+
+To launch the JaguarDB client terminal to interact with JaguarDB server:
+
+```shell
+docker exec -it jaguardb /home/jaguar/jaguar/bin/jag
+```
+
+Another option is to download an already-built binary package of JaguarDB on Linux, and deploy the database on a single node or in a cluster of nodes. The streamlined process enables you to quickly start using JaguarDB and leverage its powerful features and functionalities. [here](http://www.jaguardb.com/download.html).
\ No newline at end of file
diff --git a/templates/rag-jaguardb/pyproject.toml b/templates/rag-jaguardb/pyproject.toml
new file mode 100644
index 0000000000..f1ebc5e70b
--- /dev/null
+++ b/templates/rag-jaguardb/pyproject.toml
@@ -0,0 +1,34 @@
+[tool.poetry]
+name = "rag-jaguardb"
+version = "0.1.0"
+description = "RAG w/ JaguarDB"
+authors = [
+ "Daniel Ung <[email protected]>",
+]
+readme = "README.md"
+
+[tool.poetry.dependencies]
+python = ">=3.8.1,<4.0"
+langchain = "^0.1"
+openai = "<2"
+tiktoken = ">=0.5.1"
+jaguar = ">=3.4"
+
+[tool.poetry.group.dev.dependencies]
+langchain-cli = ">=0.0.15"
+
+[tool.langserve]
+export_module = "rag_jaguardb"
+export_attr = "chain"
+
+[tool.templates-hub]
+use-case = "rag"
+author = "LangChain"
+integrations = ["JaguarDB", "OpenAI"]
+tags = ["vectordbs"]
+
+[build-system]
+requires = [
+ "poetry-core",
+]
+build-backend = "poetry.core.masonry.api"
diff --git a/templates/rag-jaguardb/rag_jaguardb.ipynb b/templates/rag-jaguardb/rag_jaguardb.ipynb
new file mode 100644
index 0000000000..34782c40df
--- /dev/null
+++ b/templates/rag-jaguardb/rag_jaguardb.ipynb
@@ -0,0 +1,51 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "681a5d1e",
+ "metadata": {},
+ "source": [
+ "## Run Template\n",
+ "\n",
+ "In `server.py`, set -\n",
+ "```\n",
+ "add_routes(app, rag_jaguardb_chain, path=\"/rag-jaguardb\")\n",
+ "```"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d774be2a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from langserve.client import RemoteRunnable\n",
+ "\n",
+ "rag_app = RemoteRunnable(\"http://localhost:8001/rag-jaguardb\")\n",
+ "rag_app.invoke(\"hello!\")"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "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.9.16"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/templates/rag-jaguardb/rag_jaguardb/__init__.py b/templates/rag-jaguardb/rag_jaguardb/__init__.py
new file mode 100644
index 0000000000..81c930be74
--- /dev/null
+++ b/templates/rag-jaguardb/rag_jaguardb/__init__.py
@@ -0,0 +1,3 @@
+from rag_jaguardb import chain
+
+__all__ = ["chain"]
diff --git a/templates/rag-jaguardb/rag_jaguardb/chain.py b/templates/rag-jaguardb/rag_jaguardb/chain.py
new file mode 100644
index 0000000000..b4450cf401
--- /dev/null
+++ b/templates/rag-jaguardb/rag_jaguardb/chain.py
@@ -0,0 +1,64 @@
+import os
+
+from langchain_community.chat_models import ChatOpenAI
+from langchain_community.embeddings import OpenAIEmbeddings
+from langchain_community.vectorstores.jaguar import Jaguar
+from langchain_core.output_parsers import StrOutputParser
+from langchain_core.prompts import ChatPromptTemplate
+from langchain_core.pydantic_v1 import BaseModel
+from langchain_core.runnables import (
+ RunnableParallel,
+ RunnablePassthrough,
+)
+
+if os.environ.get("JAGUAR_API_KEY", None) is None:
+ raise Exception("Missing `JAGUAR_API_KEY` environment variable.")
+JAGUAR_API_KEY = os.environ["JAGUAR_API_KEY"]
+
+url = "http://192.168.3.88:8080/fwww/"
+pod = "vdb"
+store = "langchain_test_store"
+vector_index = "v"
+vector_type = "cosine_fraction_float"
+vector_dimension = 1536
+embeddings = OpenAIEmbeddings()
+vectorstore = Jaguar(
+ pod, store, vector_index, vector_type, vector_dimension, url, embeddings
+)
+
+retriever = vectorstore.as_retriever()
+
+vectorstore.login()
+"""
+Create vector store on the JaguarDB database server.
+This should be done only once.
+"""
+
+metadata = "category char(16)"
+text_size = 4096
+vectorstore.create(metadata, text_size)
+
+# RAG prompt
+template = """Answer the question based only on the following context:
+{context}
+Question: {question}
+"""
+prompt = ChatPromptTemplate.from_template(template)
+
+
+# RAG
+model = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
+chain = (
+ RunnableParallel({"context": retriever, "question": RunnablePassthrough()})
+ | prompt
+ | model
+ | StrOutputParser()
+)
+
+
+# Add typing for input
+class Question(BaseModel):
+ __root__: str
+
+
+chain = chain.with_types(input_type=Question)
diff --git a/templates/rag-jaguardb/tests/__init__.py b/templates/rag-jaguardb/tests/__init__.py
new file mode 100644
index 0000000000..e69de29bb2