Skip to content

Commit 0e870cc

Browse files
authored
Add README, pydoc and notebook (#3)
1 parent 3dbe573 commit 0e870cc

File tree

4 files changed

+389
-2
lines changed

4 files changed

+389
-2
lines changed

langchain/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,28 @@
11
# langchain-vectorize
22

3+
This package contains the LangChain integrations for using Vectorize.
4+
5+
## Installation and Setup
6+
7+
Installation of this package:
8+
9+
```bash
10+
pip install langchain-vectorize
11+
```
12+
13+
## Integrations overview
14+
15+
### Retriever
16+
17+
See the [LangChain Retriever documentation](https://python.langchain.com/docs/concepts/retrievers/) for more information.
18+
```python
19+
from langchain_vectorize import VectorizeRetriever
20+
21+
retriever = VectorizeRetriever(
22+
api_token="...",
23+
organization="...",
24+
pipeline_id="...",
25+
)
26+
retriever.invoke("query")
27+
```
28+
See an example notebook [here](https://github.com/vectorize-io/integrations-python/tree/main/notebooks/langchain_retriever.ipynb).
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
"""Vectorize integrations with LangChain."""
2+
3+
from langchain_vectorize.retrievers import VectorizeRetriever
4+
5+
__all__ = ["VectorizeRetriever"]

langchain/langchain_vectorize/retrievers.py

+72-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,76 @@
3636

3737

3838
class VectorizeRetriever(BaseRetriever):
39-
"""Vectorize retriever."""
39+
"""Vectorize retriever.
40+
41+
Setup:
42+
Install package ``langchain-vectorize``
43+
44+
.. code-block:: bash
45+
46+
pip install -U langchain-vectorize
47+
48+
Init args:
49+
api_token: str
50+
The Vectorize API token.
51+
environment: Literal["prod", "dev", "local", "staging"]
52+
The Vectorize API environment. Defaults to "prod".
53+
organization: Optional[str]
54+
The Vectorize organization ID. Defaults to None.
55+
pipeline_id: Optional[str]
56+
The Vectorize pipeline ID. Defaults to None.
57+
num_results: int
58+
Number of documents to return. Defaults to 5.
59+
rerank: bool
60+
Whether to rerank the results. Defaults to False.
61+
metadata_filters: list[dict[str, Any]]
62+
The metadata filters to apply when retrieving the documents. Defaults to [].
63+
64+
Instantiate:
65+
.. code-block:: python
66+
67+
from langchain_vectorize import VectorizeRetriever
68+
69+
retriever = VectorizeRetriever(
70+
api_token="xxxxx", "organization"="1234", "pipeline_id"="5678"
71+
)
72+
73+
Usage:
74+
.. code-block:: python
75+
76+
query = "what year was breath of the wild released?"
77+
retriever.invoke(query)
78+
79+
Use within a chain:
80+
.. code-block:: python
81+
82+
from langchain_core.output_parsers import StrOutputParser
83+
from langchain_core.prompts import ChatPromptTemplate
84+
from langchain_core.runnables import RunnablePassthrough
85+
from langchain_openai import ChatOpenAI
86+
87+
prompt = ChatPromptTemplate.from_template(
88+
\"\"\"Answer the question based only on the context provided.
89+
90+
Context: {context}
91+
92+
Question: {question}\"\"\"
93+
)
94+
95+
llm = ChatOpenAI(model="gpt-4o")
96+
97+
def format_docs(docs):
98+
return "\n\n".join(doc.page_content for doc in docs)
99+
100+
chain = (
101+
{"context": retriever | format_docs, "question": RunnablePassthrough()}
102+
| prompt
103+
| llm
104+
| StrOutputParser()
105+
)
106+
107+
chain.invoke("how many units did breath of the wild sell in 2020")
108+
""" # noqa: D301
40109

41110
api_token: str
42111
"""The Vectorize API token."""
@@ -146,7 +215,8 @@ def invoke(
146215
147216
.. code-block:: python
148217
149-
retriever.invoke("query")
218+
query = "what year was breath of the wild released?"
219+
docs = retriever.invoke(query, num_results=2)
150220
"""
151221
kwargs = {}
152222
if organization:

0 commit comments

Comments
 (0)