Skip to main content

LangChain integration for Oracle Coherence as a vector store.

Project description

LangChain Coherence Integration

This package integrates Oracle Coherence as a vector store in LangChain.

Installation

pip install langchain_coherence

Usage

Before using LangChain's CoherenceVectorStore you must ensure that a Coherence server (Coherence CE 25.03+ or Oracle Coherence 14.1.2+) is running

For local development, we recommend using the Coherence CE container image:

docker run -d -p 1408:1408 ghcr.io/oracle/coherence-ce:25.03.2

Adding and retrieving Documents

import asyncio

from langchain_coherence import CoherenceVectorStore
from langchain_core.documents import Document
from langchain_core.embeddings import Embeddings
from langchain_huggingface.embeddings import HuggingFaceEmbeddings
from coherence import NamedMap, Session

async def do_run():
    session: Session = await Session.create()
    try:
        named_map: NamedMap[str, Document] = await session.get_map("my-map")
        embedding :Embeddings = HuggingFaceEmbeddings(
            model_name="sentence-transformers/all-MiniLM-l6-v2")
        # this embedding generates vectors of dimension 384
        cvs :CoherenceVectorStore = await CoherenceVectorStore.create(
            named_map,embedding)
        d1 :Document = Document(id="1", page_content="apple")
        d2 :Document = Document(id="2", page_content="orange")
        documents = [d1, d2]
        await cvs.aadd_documents(documents)
    
        ids = [doc.id for doc in documents]
        l = await cvs.aget_by_ids(ids)
        assert len(l) == len(ids)
        print("====")
        for e in l:
            print(e)
    finally:
        await session.close()

asyncio.run(do_run())

SimilaritySearch on Documents

from langchain_core.documents import Document
from langchain_core.embeddings import Embeddings
from langchain_huggingface.embeddings import HuggingFaceEmbeddings

from coherence import NamedMap, Session
from langchain_coherence import CoherenceVectorStore

def test_data():
    d1 :Document = Document(id="1", page_content="apple")
    d2 :Document = Document(id="2", page_content="orange")
    d3 :Document = Document(id="3", page_content="tiger")
    d4 :Document = Document(id="4", page_content="cat")
    d5 :Document = Document(id="5", page_content="dog")
    d6 :Document = Document(id="6", page_content="fox")
    d7 :Document = Document(id="7", page_content="pear")
    d8 :Document = Document(id="8", page_content="banana")
    d9 :Document = Document(id="9", page_content="plum")
    d10 :Document = Document(id="10", page_content="lion")

    documents = [d1, d2, d3, d4, d5, d6, d7, d8, d9, d10]
    return documents

async def test_asimilarity_search():
    documents = test_data()
    session: Session = await Session.create()
    try:
        named_map: NamedMap[str, Document] = await session.get_map("my-map")
        embedding :Embeddings = HuggingFaceEmbeddings(
                model_name="sentence-transformers/all-MiniLM-l6-v2")
        # this embedding generates vectors of dimension 384
        cvs :CoherenceVectorStore = await CoherenceVectorStore.create(
                                            named_map,embedding)
        await cvs.aadd_documents(documents)
        ids = [doc.id for doc in documents]
        l = await cvs.aget_by_ids(ids)
        assert len(l) == 10

        result = await cvs.asimilarity_search("fruit")
        assert len(result) == 4
        print("====")
        for e in result:
            print(e)
    finally:
        await session.close()

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_coherence-0.0.3.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_coherence-0.0.3-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file langchain_coherence-0.0.3.tar.gz.

File metadata

  • Download URL: langchain_coherence-0.0.3.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for langchain_coherence-0.0.3.tar.gz
Algorithm Hash digest
SHA256 4dd516399ed98cca09b57d388c6f545c02720e267c0a6f62612d6b008f110131
MD5 a4eb2f1beb18a0721b2f981056dfa1a7
BLAKE2b-256 178737f3cf9a067eba6d9273796e4b9639a1591bb17af28ab0c6b67f258f49cb

See more details on using hashes here.

File details

Details for the file langchain_coherence-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_coherence-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2d0831138c21932d8ab8ba54879cef846d7872ff333102a419364970510cdee5
MD5 338cfb264cdc3fac3998652062a75c8f
BLAKE2b-256 d196aa493d79489c238c0e9f91722c52b3ba6ce58b37a308cc73875bafc0d3d4

See more details on using hashes here.

Supported by

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