forked from langchain-ai/langchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Harrison Chase <[email protected]> Co-authored-by: Lance Martin <[email protected]> Co-authored-by: Jacob Lee <[email protected]>
- Loading branch information
1 parent
43257a2
commit ebf998a
Showing
242 changed files
with
53,432 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 2 additions & 24 deletions
26
libs/langchain/langchain/agents/agent_toolkits/conversational_retrieval/tool.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
3
templates/anthropic-functions/anthropic_functions/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
templates/anthropic-functions/anthropic_functions/chain.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.