Skip to content
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

community: self-query-lantern template for lantern #16586

Closed
Closed
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
2 changes: 2 additions & 0 deletions templates/self-query-lantern/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
tests
158 changes: 158 additions & 0 deletions templates/self-query-lantern/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@

# self-query-lantern

This template performs [self-querying](https://python.langchain.com/docs/modules/data_connection/retrievers/self_query/)
using Lantern and OpenAI. By default, it uses an artificial dataset of 10 documents, but you can replace it with your own dataset.

## Environment Setup

Set the `OPENAI_API_KEY` environment variable to access the OpenAI models.

Set the `LANTERN_CONNECTION_STRING` to the URL of your Lantern instance. If you do not set any of them,
the template will try to connect a local Lantern instance at `http://localhost:5432`.

```shell
export LANTERN_CONNECTION_STRING=
export OPENAI_API_KEY=
```

## Usage

To use this package, install the LangChain CLI first:

```shell
pip install -U "langchain-cli[serve]"
```

Create a new LangChain project and install this package as the only one:

```shell
langchain app new my-app --package self-query-lantern
```

To add this to an existing project, run:

```shell
langchain app add self-query-lantern
```

### Defaults

Before you launch the server, you need to create a Lantern collection and index the documents.
It can be done by running the following command:

```python
from self_query_lantern.chain import initialize

initialize()
```

Add the following code to your `app/server.py` file:

```python
from self_query_lantern.chain import chain

add_routes(app, chain, path="/self-query-lantern")
```

The default dataset consists 10 documents about dishes, along with their price and restaurant information.
You can find the documents in the `packages/self-query-lantern/self_query_lantern/defaults.py` file.
Here is one of the documents:

```python
from langchain.schema import Document

Document(
page_content="Spaghetti with meatballs and tomato sauce",
metadata={
"price": 12.99,
"restaurant": {
"name": "Olive Garden",
"location": ["New York", "Chicago", "Los Angeles"],
},
},
)
```

The self-querying allows performing semantic search over the documents, with some additional filtering
based on the metadata. For example, you can search for the dishes that cost less than $15 and are served in New York.

### Customization

All the examples above assume that you want to launch the template with just the defaults.
If you want to customize the template, you can do it by passing the parameters to the `create_chain` function
in the `app/server.py` file:

```python
from langchain_community.llms import Cohere
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain.chains.query_constructor.schema import AttributeInfo

from self_query_lantern.chain import create_chain

chain = create_chain(
llm=Cohere(),
embeddings=HuggingFaceEmbeddings(),
document_contents="Descriptions of cats, along with their names and breeds.",
metadata_field_info=[
AttributeInfo(name="name", description="Name of the cat", type="string"),
AttributeInfo(name="breed", description="Cat's breed", type="string"),
],
collection_name="cats",
)
```

The same goes for the `initialize` function that creates a Lantern collection and indexes the documents:

```python
from langchain.schema import Document
from langchain_community.embeddings import HuggingFaceEmbeddings

from self_query_lantern.chain import initialize

initialize(
embeddings=HuggingFaceEmbeddings(),
collection_name="cats",
documents=[
Document(
page_content="A mean lazy old cat who destroys furniture and eats lasagna",
metadata={"name": "Garfield", "breed": "Tabby"},
),
...
]
)
```

The template is flexible and might be used for different sets of documents easily.

### LangSmith

(Optional) If you have access to LangSmith, configure it to help trace, monitor and debug LangChain applications. If you don't have access, skip this section.

```shell
export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_API_KEY=<your-api-key>
export LANGCHAIN_PROJECT=<your-project> # if not specified, defaults to "default"
```

If you are inside this directory, then you can spin up a LangServe instance directly by:

```shell
langchain serve
```

### Local Server

This will start the FastAPI app with a server running locally at
[http://localhost:8000](http://localhost:8000)

You can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs)
Access the playground at [http://127.0.0.1:8000/self-query-lantern/playground](http://127.0.0.1:8000/self-query-lantern/playground)

Access the template from code with:

```python
from langserve.client import RemoteRunnable

runnable = RemoteRunnable("http://localhost:8000/self-query-lantern")
```
Loading
Loading