langchain/templates
Stefano Lottini 040597e832
community: init signature revision for Cassandra LLM cache classes + small maintenance (#17765)
This PR improves on the `CassandraCache` and `CassandraSemanticCache`
classes, mainly in the constructor signature, and also introduces
several minor improvements around these classes.

### Init signature

A (sigh) breaking change is tentatively introduced to the constructor.
To me, the advantages outweigh the possible discomfort: the new syntax
places the DB-connection objects `session` and `keyspace` later in the
param list, so that they can be given a default value. This is what
enables the pattern of _not_ specifying them, provided one has
previously initialized the Cassandra connection through the versatile
utility method `cassio.init(...)`.

In this way, a much less unwieldy instantiation can be done, such as
`CassandraCache()` and `CassandraSemanticCache(embedding=xyz)`,
everything else falling back to defaults.

A downside is that, compared to the earlier signature, this might turn
out to be breaking for those doing positional instantiation. As a way to
mitigate this problem, this PR typechecks its first argument trying to
detect the legacy usage.
(And to make this point less tricky in the future, most arguments are
left to be keyword-only).

If this is considered too harsh, I'd like guidance on how to further
smoothen this transition. **Our plan is to make the pattern of optional
session/keyspace a standard across all Cassandra classes**, so that a
repeatable strategy would be ideal. A possibility would be to keep
positional arguments for legacy reasons but issue a deprecation warning
if any of them is actually used, to later remove them with 0.2 - please
advise on this point.

### Other changes

- class docstrings: enriched, completely moved to class level, added
note on `cassio.init(...)` pattern, added tiny sample usage code.
- semantic cache: revised terminology to never mention "distance" (it is
in fact a similarity!). Kept the legacy constructor param with a
deprecation warning if used.
- `llm_caching` notebook: uniform flow with the Cassandra and Astra DB
separate cases; better and Cassandra-first description; all imports made
explicit and from community where appropriate.
- cache integration tests moved to community (incl. the imported tools),
env var bugfix for `CASSANDRA_CONTACT_POINTS`.

---------

Co-authored-by: Erick Friis <erick@langchain.dev>
2024-05-16 17:22:24 +00:00
..
anthropic-iterative-search patch: deprecate (a)get_relevant_documents (#20477) 2024-04-22 11:14:53 -04:00
basic-critique-revise templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
bedrock-jcvd docs: aws docs updates (#20571) 2024-04-17 23:32:00 +00:00
cassandra-entomology-rag templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
cassandra-synonym-caching community: init signature revision for Cassandra LLM cache classes + small maintenance (#17765) 2024-05-16 17:22:24 +00:00
chain-of-note-wiki templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
chat-bot-feedback templates, cli: more security deps (#19006) 2024-03-12 20:48:56 -07:00
cohere-librarian patch: deprecate (a)get_relevant_documents (#20477) 2024-04-22 11:14:53 -04:00
csv-agent templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
docs pinecone[patch], docs: PineconeVectorStore, release 0.0.3 (#17896) 2024-02-22 08:24:08 -08:00
elastic-query-generator templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
extraction-anthropic-functions templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
extraction-openai-functions templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
gemini-functions-agent templates: utilities import fix (#20679) 2024-04-19 21:41:15 -04:00
guardrails-output-parser templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
hybrid-search-weaviate templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
hyde templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
intel-rag-xeon templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
llama2-functions templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
mongo-parent-document-retrieval templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
neo4j-advanced-rag templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
neo4j-cypher Update neo4j cypher templates to the function callback (#20515) 2024-04-19 18:33:32 +00:00
neo4j-cypher-ft Update neo4j cypher templates to the function callback (#20515) 2024-04-19 18:33:32 +00:00
neo4j-cypher-memory Update neo4j cypher templates to the function callback (#20515) 2024-04-19 18:33:32 +00:00
neo4j-generation templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
neo4j-parent templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
neo4j-semantic-layer templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
neo4j-semantic-ollama templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
neo4j-vector-memory templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
nvidia-rag-canonical templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
openai-functions-agent templates: utilities import fix (#20679) 2024-04-19 21:41:15 -04:00
openai-functions-agent-gmail templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
openai-functions-tool-retrieval-agent patch: deprecate (a)get_relevant_documents (#20477) 2024-04-22 11:14:53 -04:00
pii-protected-chatbot templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
pirate-speak templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
pirate-speak-configurable templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
plate-chain templates, cli: more security deps (#19006) 2024-03-12 20:48:56 -07:00
propositional-retrieval templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
python-lint templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-astradb templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-aws-bedrock templates: bump lockfile deps (#19001) 2024-03-13 01:25:45 +00:00
rag-aws-kendra templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-azure-search templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-chroma templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-chroma-multi-modal templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-chroma-multi-modal-multi-vector templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-chroma-private templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-codellama-fireworks templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-conversation templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-conversation-zep templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-elasticsearch templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-fusion templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-gemini-multi-modal templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-google-cloud-sensitive-data-protection templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-google-cloud-vertexai-search templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-gpt-crawler templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-jaguardb templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-lancedb templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-lantern templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-matching-engine templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-momento-vector-index templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-mongo templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-multi-index-fusion templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-multi-index-router templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-multi-modal-local templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-multi-modal-mv-local templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-ollama-multi-query templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-opensearch templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-pinecone templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-pinecone-multi-query templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-pinecone-rerank templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-redis templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-redis-multi-modal-multi-vector templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-self-query templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-semi-structured templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-singlestoredb templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-supabase templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-timescale-conversation templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-timescale-hybrid-search-time templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-vectara templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-vectara-multiquery templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rag-weaviate templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
research-assistant templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
retrieval-agent templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
retrieval-agent-fireworks templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
rewrite-retrieve-read templates: utilities import fix (#20679) 2024-04-19 21:41:15 -04:00
robocorp-action-server templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
self-query-qdrant templates: update qdrant self query (#19218) 2024-03-18 19:59:08 +00:00
self-query-supabase templates: bump lockfile deps (#19001) 2024-03-13 01:25:45 +00:00
shopping-assistant templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
skeleton-of-thought templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
solo-performance-prompting-agent templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
sql-llama2 templates: utilities import fix (#20679) 2024-04-19 21:41:15 -04:00
sql-llamacpp templates: utilities import fix (#20679) 2024-04-19 21:41:15 -04:00
sql-ollama templates: utilities import fix (#20679) 2024-04-19 21:41:15 -04:00
sql-pgvector templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
sql-research-assistant templates: utilities import fix (#20679) 2024-04-19 21:41:15 -04:00
stepback-qa-prompting templates: utilities import fix (#20679) 2024-04-19 21:41:15 -04:00
summarize-anthropic templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
vertexai-chuck-norris templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
xml-agent templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00
.gitignore
Makefile core[patch], langchain[patch]: fix required deps (#14373) 2023-12-07 14:24:58 -08:00
poetry.lock templates: bump lockfile deps (#19001) 2024-03-13 01:25:45 +00:00
pyproject.toml infra, multiple: Upgrade configuration for Ruff v0.2.0 (#16905) 2024-02-09 14:28:02 -08:00
README.md templates: readme langsmith not private beta (#20173) 2024-04-12 13:08:10 -07:00

LangChain Templates

LangChain Templates are the easiest and fastest way to build a production-ready LLM application. These templates serve as a set of reference architectures for a wide variety of popular LLM use cases. They are all in a standard format which make it easy to deploy them with LangServe.

🚩 We will be releasing a hosted version of LangServe for one-click deployments of LangChain applications. Sign up here to get on the waitlist.

Quick Start

To use, first install the LangChain CLI.

pip install -U langchain-cli

Next, create a new LangChain project:

langchain app new my-app

This will create a new directory called my-app with two folders:

  • app: This is where LangServe code will live
  • packages: This is where your chains or agents will live

To pull in an existing template as a package, you first need to go into your new project:

cd my-app

And you can the add a template as a project. In this getting started guide, we will add a simple pirate-speak project. All this project does is convert user input into pirate speak.

langchain app add pirate-speak

This will pull in the specified template into packages/pirate-speak

You will then be prompted if you want to install it. This is the equivalent of running pip install -e packages/pirate-speak. You should generally accept this (or run that same command afterwards). We install it with -e so that if you modify the template at all (which you likely will) the changes are updated.

After that, it will ask you if you want to generate route code for this project. This is code you need to add to your app to start using this chain. If we accept, we will see the following code generated:

from pirate_speak.chain import chain as pirate_speak_chain

add_routes(app, pirate_speak_chain, path="/pirate-speak")

You can now edit the template you pulled down. You can change the code files in packages/pirate-speak to use a different model, different prompt, different logic. Note that the above code snippet always expects the final chain to be importable as from pirate_speak.chain import chain, so you should either keep the structure of the package similar enough to respect that or be prepared to update that code snippet.

Once you have done as much of that as you want, it is In order to have LangServe use this project, you then need to modify app/server.py. Specifically, you should add the above code snippet to app/server.py so that file looks like:

from fastapi import FastAPI
from langserve import add_routes
from pirate_speak.chain import chain as pirate_speak_chain

app = FastAPI()

add_routes(app, pirate_speak_chain, path="/pirate-speak")

(Optional) Let's now configure LangSmith. LangSmith will help us trace, monitor and debug LangChain applications. You can sign up for LangSmith here. If you don't have access, you can skip this section

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

For this particular application, we will use OpenAI as the LLM, so we need to export our OpenAI API key:

export OPENAI_API_KEY=sk-...

You can then spin up production-ready endpoints, along with a playground, by running:

langchain serve

This now gives a fully deployed LangServe application. For example, you get a playground out-of-the-box at http://127.0.0.1:8000/pirate-speak/playground/:

Screenshot of the LangServe Playground interface with input and output fields demonstrating pirate speak conversion.

Access API documentation at http://127.0.0.1:8000/docs

Screenshot of the API documentation interface showing available endpoints for the pirate-speak application.

Use the LangServe python or js SDK to interact with the API as if it were a regular Runnable.

from langserve import RemoteRunnable

api = RemoteRunnable("http://127.0.0.1:8000/pirate-speak")
api.invoke({"text": "hi"})

That's it for the quick start! You have successfully downloaded your first template and deployed it with LangServe.

Additional Resources

Index of Templates

Explore the many templates available to use - from advanced RAG to agents.

Contributing

Want to contribute your own template? It's pretty easy! These instructions walk through how to do that.

Launching LangServe from a Package

You can also launch LangServe from a package directly (without having to create a new project). These instructions cover how to do that.