Skip to content

Commit

Permalink
Templates (langchain-ai#12294)
Browse files Browse the repository at this point in the history
Co-authored-by: Harrison Chase <[email protected]>
Co-authored-by: Lance Martin <[email protected]>
Co-authored-by: Jacob Lee <[email protected]>
  • Loading branch information
4 people authored Oct 26, 2023
1 parent 43257a2 commit ebf998a
Show file tree
Hide file tree
Showing 242 changed files with 53,432 additions and 31 deletions.
2 changes: 1 addition & 1 deletion libs/cli/langchain_cli/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
DEFAULT_GIT_REPO = "https://github.com/langchain-ai/langchain.git"
DEFAULT_GIT_SUBDIRECTORY = "templates"
DEFAULT_GIT_REF = "langserve-templates"
DEFAULT_GIT_REF = "master"
6 changes: 2 additions & 4 deletions libs/langchain/langchain/agents/agent_toolkits/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
from langchain.agents.agent_toolkits.conversational_retrieval.openai_functions import (
create_conversational_retrieval_agent,
)
from langchain.agents.agent_toolkits.conversational_retrieval.tool import (
create_retriever_tool,
)
from langchain.agents.agent_toolkits.csv.base import create_csv_agent
from langchain.agents.agent_toolkits.file_management.toolkit import (
FileManagementToolkit,
Expand Down Expand Up @@ -59,6 +56,7 @@
)
from langchain.agents.agent_toolkits.xorbits.base import create_xorbits_agent
from langchain.agents.agent_toolkits.zapier.toolkit import ZapierToolkit
from langchain.tools.retriever import create_retriever_tool

__all__ = [
"AINetworkToolkit",
Expand Down Expand Up @@ -87,12 +85,12 @@
"create_pbi_agent",
"create_pbi_chat_agent",
"create_python_agent",
"create_retriever_tool",
"create_spark_dataframe_agent",
"create_spark_sql_agent",
"create_sql_agent",
"create_vectorstore_agent",
"create_vectorstore_router_agent",
"create_xorbits_agent",
"create_conversational_retrieval_agent",
"create_retriever_tool",
]
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
from langchain.schema import BaseRetriever
from langchain.tools import Tool
from langchain.tools.retriever import create_retriever_tool


def create_retriever_tool(
retriever: BaseRetriever, name: str, description: str
) -> Tool:
"""Create a tool to do retrieval of documents.
Args:
retriever: The retriever to use for the retrieval
name: The name for the tool. This will be passed to the language model,
so should be unique and somewhat descriptive.
description: The description for the tool. This will be passed to the language
model, so should be descriptive.
Returns:
Tool class to pass to an agent
"""
return Tool(
name=name,
description=description,
func=retriever.get_relevant_documents,
coroutine=retriever.aget_relevant_documents,
)
__all__ = ["create_retriever_tool"]
5 changes: 4 additions & 1 deletion libs/langchain/langchain/retrievers/you.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List
from typing import Any, Dict, List, Optional

from langchain.callbacks.manager import CallbackManagerForRetrieverRun
from langchain.pydantic_v1 import root_validator
Expand All @@ -17,6 +17,7 @@ class YouRetriever(BaseRetriever):
"""

ydc_api_key: str
k: Optional[int] = None
endpoint_type: str = "web"

@root_validator(pre=True)
Expand Down Expand Up @@ -45,6 +46,8 @@ def _get_relevant_documents(
for hit in results["hits"]:
for snippet in hit["snippets"]:
docs.append(Document(page_content=snippet))
if self.k is not None and len(docs) >= self.k:
return docs
return docs
elif self.endpoint_type == "snippet":
results = requests.get(
Expand Down
25 changes: 25 additions & 0 deletions libs/langchain/langchain/tools/retriever.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from langchain.schema import BaseRetriever
from langchain.tools import Tool


def create_retriever_tool(
retriever: BaseRetriever, name: str, description: str
) -> Tool:
"""Create a tool to do retrieval of documents.
Args:
retriever: The retriever to use for the retrieval
name: The name for the tool. This will be passed to the language model,
so should be unique and somewhat descriptive.
description: The description for the tool. This will be passed to the language
model, so should be descriptive.
Returns:
Tool class to pass to an agent
"""
return Tool(
name=name,
description=description,
func=retriever.get_relevant_documents,
coroutine=retriever.aget_relevant_documents,
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ codespell = "^2.2.0"


[tool.codespell]
skip = '.git,*.pdf,*.svg,*.pdf,*.yaml,*.ipynb,poetry.lock,*.min.js,*.css,package-lock.json,example_data,_dist,examples'
skip = '.git,*.pdf,*.svg,*.pdf,*.yaml,*.ipynb,poetry.lock,*.min.js,*.css,package-lock.json,example_data,_dist,examples,templates'
# Ignore latin etc
ignore-regex = '.*(Stati Uniti|Tense=Pres).*'
# whats is a typo but used frequently in queries so kept as is
Expand Down
1 change: 1 addition & 0 deletions templates/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
9 changes: 9 additions & 0 deletions templates/INDEX.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Templates

A list of all template repos

⭐Retrieval Augmented Generation Chatbot: Build a chatbot over your data. Uses OpenAI and Pinecone.
⭐Extraction with OpenAI Functions: Do extraction of structured data from unstructured data. Uses OpenAI function calling.
⭐Local Retrieval Augmented Generation: Build a chatbot over your data. Uses only local tooling: Ollama, GPT4all, Chroma.
⭐OpenAI Functions Agent: Build a chatbot that can take actions. Uses OpenAI function calling and Tavily.
⭐XML Agent: Build a chatbot that can take actions. Uses Anthropic and You.com.
71 changes: 71 additions & 0 deletions templates/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# LangServe Hub

Packages that can be easily hosted by LangServe using the `langserve` cli.

## Using LangServe Hub

You can install the `langservehub` CLI and use it as follows:
```bash
# install langservehub CLI
pip install --upgrade langservehub

langservehub new my-app
cd my-app

poetry install

# if you have problems with poe, use `poetry run poe ...` instead

# add the simple-pirate package
poe add --repo=pingpong-templates/hub simple-pirate

# adding other GitHub repo packages, defaults to repo root
poe add --repo=hwchase17/chain-of-verification

# with a custom api mount point (defaults to `/{package_name}`)
poe add --repo=pingpong-templates/hub simple-translator --api_path=/my/custom/path/translator

poe list

poe start
^C

# remove packages by their api path:
poe remove my/custom/path/translator
```

## Creating New Packages

You can also create new packages with the `langservehub package new` command

```bash
# starting from this directory in langserve-hub
langservehub package new simple-newpackage
```

Now you can edit the chain in `simple-newpackage/simple_newpackage/chain.py` and put up a PR!

Your package will be usable as `poe add --repo=pingpong-templates/hub simple-newpackage` when it's merged in.

## Data Format

What makes these packages work?

- Poetry
- pyproject.toml files

### Installable Packages

Everything is a Poetry package currently. This allows poetry to manage our dependencies for us :).

In addition to normal keys in the `pyproject.toml` file, you'll notice an additional `tool.langserve` key ([link](https://github.com/langchain-ai/langserve-hub/blob/main/simple/pirate/pyproject.toml#L13-L15)).

This allows us to identify which module and attribute to import as the chain/runnable for the langserve `add_routes` call.

### Apps (with installed langserve packages)

Let's say you add the pirate package with `poe add --repo=pingpong-templates/hub simple-pirate`.

First this downloads the simple-pirate package to pirate

Then this adds a `poetry` path dependency, which gets picked up from `add_package_routes`.
21 changes: 21 additions & 0 deletions templates/anthropic-functions/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 LangChain, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 39 additions & 0 deletions templates/anthropic-functions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Function calling with Anthropic

This template enables [Anthropic function calling](https://python.langchain.com/docs/integrations/chat/anthropic_functions).

Function calling can be used for various tasks, such as extraction or tagging.

Specify the function you want to use in `chain.py`

By default, it will tag the input text using the following fields:

* sentiment
* aggressiveness
* language

## LLM

This template will use `Claude2` by default.

Be sure that `ANTHROPIC_API_KEY` is set in your enviorment.

## Adding the template

Create your LangServe app:
```
langchain serve new my-app
cd my-app
```

Add template:
```
langchain serve add anthropic-functions
```

Start server:
```
langchain start
```

See Jupyter notebook `anthropic_functions` for various way to connect to the template.
83 changes: 83 additions & 0 deletions templates/anthropic-functions/anthropic_functions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "4ae4b789",
"metadata": {},
"source": [
"## Document Loading\n",
"\n",
"Load a blog post on agents."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "5d6bd62e",
"metadata": {},
"outputs": [],
"source": [
"from langchain.document_loaders import WebBaseLoader\n",
"loader = WebBaseLoader(\"https://lilianweng.github.io/posts/2023-06-23-agent/\")\n",
"text = loader.load()"
]
},
{
"cell_type": "markdown",
"id": "8e21575d",
"metadata": {},
"source": [
"## Run Template\n",
"\n",
"As shown in the README, add template and start server:\n",
"```\n",
"langchain serve add anthropic-functions\n",
"langchain start\n",
"```\n",
"\n",
"We can now look at the endpoints:\n",
"\n",
"http://127.0.0.1:8000/docs#\n",
"\n",
"And specifically at our loaded template:\n",
"\n",
"http://127.0.0.1:8000/docs#/default/invoke_anthropic-functions_invoke_post\n",
" \n",
"We can also use remote runnable to call it:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "92edba86",
"metadata": {},
"outputs": [],
"source": [
"from langserve.client import RemoteRunnable\n",
"anthropic_function_model = RemoteRunnable('http://localhost:8000/anthropic-functions')\n",
"anthropic_function_model.invoke(text[0].page_content[0:1500])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "langserve",
"language": "python",
"name": "langserve"
},
"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.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
3 changes: 3 additions & 0 deletions templates/anthropic-functions/anthropic_functions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from anthropic_functions.chain import chain

__all__ = ["chain"]
15 changes: 15 additions & 0 deletions templates/anthropic-functions/anthropic_functions/chain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from langchain.chains import create_tagging_chain
from langchain_experimental.llms.anthropic_functions import AnthropicFunctions

model = AnthropicFunctions(model='claude-2')

schema = {
"properties": {
"sentiment": {"type": "string"},
"aggressiveness": {"type": "integer"},
"language": {"type": "string"},
}
}

# This is LLMChain, which implements invoke
chain = create_tagging_chain(schema, model)
Loading

0 comments on commit ebf998a

Please sign in to comment.