|
36 | 36 |
|
37 | 37 |
|
38 | 38 | 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 |
40 | 109 |
|
41 | 110 | api_token: str
|
42 | 111 | """The Vectorize API token."""
|
@@ -146,7 +215,8 @@ def invoke(
|
146 | 215 |
|
147 | 216 | .. code-block:: python
|
148 | 217 |
|
149 |
| - retriever.invoke("query") |
| 218 | + query = "what year was breath of the wild released?" |
| 219 | + docs = retriever.invoke(query, num_results=2) |
150 | 220 | """
|
151 | 221 | kwargs = {}
|
152 | 222 | if organization:
|
|
0 commit comments