Skip to main content

Integration package connecting ObjectBox and LangChain

Project description

langchain-objectbox

About

This package contains the ObjectBox integrations for LangChain.

Getting Started

Install the langchain-objectbox package from PyPI via pip.

pip install langchain-objectbox

In Python import the ObjectBox vector store which is available under fully qualified class path langchain_objectbox.vectorstores.ObjectBox, e.g.:

from langchain_objectbox.vectorstores import ObjectBox

Create an ObjectBox VectorStore using e.g. one of the from_ class methods e.g. from_texts class method.

NOTE: Ensure to set argument embedding_dimensions along with the dimensions used in your embeddings model.

obx_vectorstore = ObjectBox.from_texts(texts, embeddings, embedding_dimensions=768)

Example 1: A very simple example using DeterministicFakeEmbedding

from langchain_core.embeddings.fake import DeterministicFakeEmbedding
from langchain_objectbox.vectorstores import ObjectBox

texts = ["foo", "bar", "baz"]
obx_vectorstore = ObjectBox.from_texts(
    texts, 
    DeterministicFakeEmbedding(size=10),
    embedding_dimensions=10,
)
result = obx_vectorstore.similarity_search("foo",k=1)
print(result)

Example 2: A more complex example using web retrieval chain.

Prerequisites:

from langchain_objectbox.vectorstores import ObjectBox

from langchain_community.llms import Ollama
llm = Ollama(model="llama2")

from langchain_community.document_loaders import WebBaseLoader
loader = WebBaseLoader("https://docs.smith.langchain.com/user_guide")

docs = loader.load()

from langchain_community.embeddings import OllamaEmbeddings

embeddings = OllamaEmbeddings()

from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts.chat import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template("""Answer the following question based only on the provided context:

<context>
{context}
</context>

Question: {input}""")

from langchain_text_splitters import RecursiveCharacterTextSplitter

text_splitter = RecursiveCharacterTextSplitter()
documents = text_splitter.split_documents(docs)

vector = ObjectBox.from_documents(documents, embeddings, embedding_dimensions=768)
document_chain = create_stuff_documents_chain(llm, prompt)
from langchain_core.documents import Document
from langchain.chains import create_retrieval_chain

retriever = vector.as_retriever()
retrieval_chain = create_retrieval_chain(retriever, document_chain)

response = retrieval_chain.invoke({"input": "how can langsmith help with testing?"})
print(response["answer"])

LICENSE

MIT License

Copyright (c) 2024 ObjectBox, Ltd.

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_objectbox-0.1.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

langchain_objectbox-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file langchain_objectbox-0.1.0.tar.gz.

File metadata

  • Download URL: langchain_objectbox-0.1.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.12 Linux/6.5.0-10036-tuxedo

File hashes

Hashes for langchain_objectbox-0.1.0.tar.gz
Algorithm Hash digest
SHA256 672d2457d51e73b5714ac583e65f6450de5ccff793a6583fec55119d628bc382
MD5 5f78cf47445e255a61744c3aecca1363
BLAKE2b-256 157c7b10fd550bc193d4cec715fcc93ae44675ea2af847dad8da084b880af7fe

See more details on using hashes here.

File details

Details for the file langchain_objectbox-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_objectbox-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e516a007a6f6e07c747138d40eac3237fa776a178d93d084445531f212258759
MD5 778c209075e4c02365467c38cf81cec8
BLAKE2b-256 2d27fb79470372e8f23d079e9fa6b1c362835ad33ebb454b6a0e24a879747f0f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page