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,384)
        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,384)
        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.2.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.2-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: langchain_coherence-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 32ee7b37fe430216483e2070592b26bb41062deee77827fd3f1756905aad23f4
MD5 0bbc97344d019779e4725b35b4e91ba3
BLAKE2b-256 99c3662007ab18a92a32b277d63f509e71fe6b93679085d53d9b3d56db5458a0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for langchain_coherence-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4c67129a454d858b1c37401805aa1e17ed47335f5a790440d8e61d6f24719835
MD5 fb1de79e4151502de570189aa1800626
BLAKE2b-256 fe81a9c673ea9b4ce515d1e87d8d9485f58e7759c3b30713b140b845f4aeaca1

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