Skip to content

Add README, pydoc and notebook #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions langchain/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
# langchain-vectorize

This package contains the LangChain integrations for using Vectorize.

## Installation and Setup

Installation of this package:

```bash
pip install langchain-vectorize
```

## Integrations overview

### Retriever

See the [LangChain Retriever documentation](https://python.langchain.com/docs/concepts/retrievers/) for more information.
```python
from langchain_vectorize import VectorizeRetriever

retriever = VectorizeRetriever(
api_token="...",
organization="...",
pipeline_id="...",
)
retriever.invoke("query")
```
See an example notebook [here](https://github.com/vectorize-io/integrations-python/tree/main/notebooks/langchain_retriever.ipynb).
4 changes: 4 additions & 0 deletions langchain/langchain_vectorize/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
"""Vectorize integrations with LangChain."""

from langchain_vectorize.retrievers import VectorizeRetriever

__all__ = ["VectorizeRetriever"]
74 changes: 72 additions & 2 deletions langchain/langchain_vectorize/retrievers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,76 @@


class VectorizeRetriever(BaseRetriever):
"""Vectorize retriever."""
"""Vectorize retriever.

Setup:
Install package ``langchain-vectorize``

.. code-block:: bash

pip install -U langchain-vectorize

Init args:
api_token: str
The Vectorize API token.
environment: Literal["prod", "dev", "local", "staging"]
The Vectorize API environment. Defaults to "prod".
organization: Optional[str]
The Vectorize organization ID. Defaults to None.
pipeline_id: Optional[str]
The Vectorize pipeline ID. Defaults to None.
num_results: int
Number of documents to return. Defaults to 5.
rerank: bool
Whether to rerank the results. Defaults to False.
metadata_filters: list[dict[str, Any]]
The metadata filters to apply when retrieving the documents. Defaults to [].

Instantiate:
.. code-block:: python

from langchain_vectorize import VectorizeRetriever

retriever = VectorizeRetriever(
api_token="xxxxx", "organization"="1234", "pipeline_id"="5678"
)

Usage:
.. code-block:: python

query = "what year was breath of the wild released?"
retriever.invoke(query)

Use within a chain:
.. code-block:: python

from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI

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

Context: {context}

Question: {question}\"\"\"
)

llm = ChatOpenAI(model="gpt-4o")

def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)

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

chain.invoke("how many units did breath of the wild sell in 2020")
""" # noqa: D301

api_token: str
"""The Vectorize API token."""
Expand Down Expand Up @@ -146,7 +215,8 @@ def invoke(

.. code-block:: python

retriever.invoke("query")
query = "what year was breath of the wild released?"
docs = retriever.invoke(query, num_results=2)
"""
kwargs = {}
if organization:
Expand Down
Loading