From db1dbd22b493e2869c231326bbdb104480c03e69 Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Tue, 28 Nov 2023 17:24:19 -0500 Subject: [PATCH] x --- examples/configurable_retrieval/client.ipynb | 168 +++++++++++++++++++ examples/retrieval/server.py | 53 +----- 2 files changed, 177 insertions(+), 44 deletions(-) create mode 100644 examples/configurable_retrieval/client.ipynb diff --git a/examples/configurable_retrieval/client.ipynb b/examples/configurable_retrieval/client.ipynb new file mode 100644 index 00000000..2cba4d4c --- /dev/null +++ b/examples/configurable_retrieval/client.ipynb @@ -0,0 +1,168 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Client\n", + "\n", + "Demo of a client interacting with a configurable retriever (see server code)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can interact with this via API directly" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'output': [{'page_content': 'cats like fish',\n", + " 'metadata': {},\n", + " 'type': 'Document'},\n", + " {'page_content': 'dogs like sticks', 'metadata': {}, 'type': 'Document'}],\n", + " 'callback_events': [],\n", + " 'metadata': {'run_id': 'f375cdf6-2848-4976-9565-f69e175c24ce'}}" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import requests\n", + "\n", + "inputs = {\"input\": \"cat\"}\n", + "response = requests.post(\"http://localhost:8000/invoke\", json=inputs)\n", + "\n", + "response.json()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can also interact with this via the RemoteRunnable interface (to use in other chains)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from langserve import RemoteRunnable\n", + "\n", + "remote_runnable = RemoteRunnable(\"http://localhost:8000/\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Remote runnable has the same interface as local runnables" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[Document(page_content='cats like fish'),\n", + " Document(page_content='dogs like sticks')]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "await remote_runnable.ainvoke(\"cat\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[Document(page_content='cats like fish'),\n", + " Document(page_content='dogs like sticks')]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "await remote_runnable.ainvoke(\"cat\", {\"configurable\": {\"collection_name\": \"Index 1\"}})" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[Document(page_content='x_n+1=a * xn * (1-xn)')]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "await remote_runnable.ainvoke(\"cat\", {\"configurable\": {\"collection_name\": \"Index 2\"}})" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/examples/retrieval/server.py b/examples/retrieval/server.py index 5f523abd..12663ed8 100755 --- a/examples/retrieval/server.py +++ b/examples/retrieval/server.py @@ -1,61 +1,26 @@ #!/usr/bin/env python -"""A more complex example that shows""" -from typing import Optional - +"""Example LangChain server exposes a retriever.""" from fastapi import FastAPI from langchain.embeddings import OpenAIEmbeddings -from langchain.schema.retriever import BaseRetriever -from langchain.schema.runnable import Runnable, RunnableConfig -from langchain.schema.runnable.utils import Input, Output -from langchain.vectorstores import FAISS, VectorStore - -vectorstore1 = FAISS.from_texts( - ["cats like fish", "dogs like sticks"], embedding=OpenAIEmbeddings() -) - -vectorstore2 = FAISS.from_texts(["x_n+1=a * xn * (1-xn)"], embedding=OpenAIEmbeddings()) - - -class FakeVectorstore(VectorStore): - """This is a fake vectorstore for demo purposes.""" +from langchain.vectorstores import FAISS - def __init__(self, collection_name: str) -> None: - """Fake vectorstore that has a collection name.""" - self.collection_name = collection_name +from langserve import add_routes - def as_retriever(self) -> BaseRetriever: - if self.collection_name == "index1": - return vectorstore1.as_retriever() - elif self.collection_name == "index2": - return vectorstore2.as_retriever() - else: - raise NotImplementedError( - f"No retriever for collection {self.collection_name}" - ) - - -## Retriever with configuration -vectorstore2 = FAISS.from_texts( +vectorstore = FAISS.from_texts( ["cats like fish", "dogs like sticks"], embedding=OpenAIEmbeddings() ) retriever = vectorstore.as_retriever() - -class ConfigurableVectorstore(Runnable): - vectorstore: FAISS - - def invoke(self, input: Input, config: Optional[RunnableConfig] = None) -> Output: - """Invoke the retriever.""" - retriever = self.vectorstore.as_retriever() - return self.vectorstore.as_retriever() - - app = FastAPI( title="LangChain Server", version="1.0", description="Spin up a simple api server using Langchain's Runnable interfaces", ) - +# Adds routes to the app for using the retriever under: +# /invoke +# /batch +# /stream +add_routes(app, retriever) if __name__ == "__main__": import uvicorn