Skip to main content

PiyAPI LangChain Adapter

A LangChain VectorStore adapter for PiyAPI Memory, enabling seamless integration with LangChain applications.

PiyAPI by Negentro is the memory layer for AI agents: 84.1% on BEAM-1M, 93.6% on LongMemEval-S and 97% fewer tokens than the full history, with every answer published. Start for free · Docs · Benchmarks

Installation

pip install piyapi-langchain

Quick Start

from piyapi_langchain import PiyAPIVectorStore
from langchain_openai import OpenAIEmbeddings

# Initialize the vector store
vectorstore = PiyAPIVectorStore(
    api_key="your-api-key",
    namespace="my-app",
    embedding=OpenAIEmbeddings()
)

# Add documents
from langchain_core.documents import Document

docs = [
    Document(page_content="PiyAPI Memory is a vector database", metadata={"source": "docs"}),
    Document(page_content="LangChain enables AI applications", metadata={"source": "docs"}),
]
vectorstore.add_documents(docs)

# Search
results = vectorstore.similarity_search("vector database", k=5)
for doc in results:
    print(doc.page_content)

# Search with scores
results_with_scores = vectorstore.similarity_search_with_score("AI applications", k=5)
for doc, score in results_with_scores:
    print(f"{doc.page_content} (score: {score})")

Using as a Retriever

# Create a retriever
retriever = vectorstore.as_retriever(
    search_type="similarity",
    search_kwargs={"k": 5}
)

# Use in a chain
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser

def format_docs(docs):
    # One block, in retrieval order, text only: no scores or metadata that change between calls.
    return "\n\n".join(doc.page_content for doc in docs)

# from_template makes a single human message, so the memories land in the user turn.
template = """Answer based on the context:
{context}

Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)
llm = ChatOpenAI()

chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

answer = chain.invoke("What is PiyAPI Memory?")

In a multi-turn chain, keep the system message fixed, put {context} in the newest human message only (after a MessagesPlaceholder for the history), and store each earlier human message in the history exactly as it was sent, context included. Never put the context in the system message.

Metadata Filtering

# Filter by metadata
results = vectorstore.similarity_search(
    "vector database",
    k=5,
    filter={"source": "docs"}
)

Configuration

Parameter Type Required Description
api_key str Yes Your PiyAPI API key
namespace str No Namespace for organizing memories (default: "default")
embedding Embeddings No LangChain embedding function (uses PiyAPI embeddings if not provided)
base_url str No Custom API base URL

LangGraph store

PiyAPIStore is a LangGraph BaseStore (batch/abatch with Get, Put, Search and ListNamespaces operations). Each item is one PiyAPI memory file, /memories/langgraph/<namespace labels>/<key>.json, holding the value as JSON: versioned, soft-deleted, and searchable. search(..., query=...) uses PiyAPI hybrid search under the namespace prefix; without a query it lists the items. Filters, offset/limit and list_namespaces (prefix, suffix, *, max_depth) behave as in LangGraph's InMemoryStore. index and ttl are accepted and ignored (every item is searchable; items do not expire). Requires langgraph (install it yourself).

from piyapi_memory import PiyAPIClient, AsyncPiyAPIClient
from piyapi_langchain.store import PiyAPIStore

store = PiyAPIStore(PiyAPIClient(), namespace="my-app", async_client=AsyncPiyAPIClient())
graph = builder.compile(store=store)

store.put(("users", "42"), "prefs", {"drink": "tea"})
store.search(("users",), query="what does the user drink?")

License

MIT

Metadata

Release files for piyapi-langchain 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for piyapi-langchain 0.1.0
File Size Uploaded Provenance Actions
piyapi_langchain-0.1.0.tar.gz 16.1 kB Without provenance Details for piyapi_langchain-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for piyapi-langchain 0.1.0
File Interpreter ABI Platform Actions
piyapi_langchain-0.1.0-py3-none-any.whl Python 3 none any Details for piyapi_langchain-0.1.0-py3-none-any.whl

Total release size: 26.6 kB

Release files / piyapi_langchain-0.1.0.tar.gz

Download URL piyapi_langchain-0.1.0.tar.gz
Size 16.1 kB
Tags Source
SHA-256 checksum
How to use checksums
bc7921aea091bbb798769dc956a4e052b497d9d16eb31d003e880952f24079d1
BLAKE2b-256 checksum
How to use checksums
8eb6ababe7f8460bf950ebcdb34eb96b279fb4b5d66d255ca3f35d0e260b533f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.1

Release files / piyapi_langchain-0.1.0-py3-none-any.whl

Download URL piyapi_langchain-0.1.0-py3-none-any.whl
Size 10.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c46623569bb02d5e4f56a7d1e0b66e04256f81306d6d149ce6f81390b8096107
BLAKE2b-256 checksum
How to use checksums
ea05d0df8b97abcce319cb4acd10bf5ef89890847c0054577ef84107c643ac19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.1

Security for piyapi-langchain 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page