LangChain
LangChain is a popular library for building LLM applications. Search plays a very important role in Retreival Augmented Generation (RAG) systems. The LangChain building block for retrieval is the Retriever. Below are resources for leveraging Objective within your LangChain workflows in both Python and TypeScript.
Python Retriever
Below is a Custom Retriever implemented using the Objective Python SDK.
import json
from typing import List
from langchain_core.callbacks import CallbackManagerForRetrieverRun
from langchain_core.documents import Document
from langchain_core.retrievers import BaseRetriever
from objective import Objective
class ObjectiveRetriever(BaseRetriever):
limit: int
object_fields: str
index_id: str
objective_api_key: str
ranking_expr: str | None = None
logger = logging.getLogger(__name__)
def _get_relevant_documents(
self, query: any, *, run_manager: CallbackManagerForRetrieverRun
) -> List[Document]:
client = Objective(api_key=self.objective_api_key)
matching_documents = []
if not isinstance(query, str):
raise ValueError("Query is not a string")
results = client.indexes.search(
index_id=self.index_id,
query=query,
limit=self.limit,
object_fields=self.object_fields,
ranking_expr=self.ranking_expr
).to_dict()
matching_documents = []
for result in results['results']:
content = ''
for f in self.object_fields.split(","):
if result.get("object") is None:
continue
if isinstance(result['object'][f], str):
content += f + ": " + result['object'][f] + "\n"
elif isinstance(result['object'][f], list):
content += f + ": " + "\n\t".join(result['object'][f]) + "\n"
elif isinstance(result['object'][f], dict):
content += f + ": " + json.dumps(result['object'][f]) + "\n"
else:
content += f + ": " + str(result['object'][f]) + "\n"
document = Document(
page_content=content,
)
matching_documents.append(document)
return matching_documents
Below is a simple example of how to create and invoke an ObjectiveRetriever. It can be used just like any other LangChain Retriever.
retriever = ObjectiveRetriever(
limit=3,
object_fields="id,title,ingredients,instructions",
index_id=index_id,
objective_api_key=api_key,
ranking_expr="uncalibrated_relevance * (object.popularity_score ^ 0.01)"
)
retriever.invoke("What are the best recipes for chicken?")
Below is a more complex example, using the ObjectiveRetriver within a MultiQueryRetriever chain.
retriever = ObjectiveRetriever(
limit=3,
object_fields="id,title,ingredients,instructions",
index_id=index_id,
objective_api_key=api_key,
ranking_expr="uncalibrated_relevance * (object.popularity_score ^ 0.01)"
)
query_chain = QUERY_PROMPT | model | output_parser
mq_retriever = MultiQueryRetriever(
retriever=retriever, llm_chain=query_chain, parser_key="lines"
)
chain = (
{
"context": lambda x: mq_retriever.invoke(x["question"]),
"question": itemgetter("question")
}
| prompt
| model
| StrOutputParser()
)
TypeScript Retriever
We have published a Typescript LangChain Retriever to NPM. Please see the README for instructions and example usage.