Skip to content

Commit

Permalink
templates: migrate to langchain_anthropic package to support Claude 3…
Browse files Browse the repository at this point in the history
… models (langchain-ai#19393)

- **Description:** update langchain anthropic templates to support
Claude 3 (iterative search, chain of note, summarization, and XML
response)
- **Issue:** issue # N/A. Stability issues and errors encountered when
trying to use older langchain and anthropic libraries.
- **Dependencies:**
  - langchain_anthropic version 0.1.4\
- anthropic package version in the range ">=0.17.0,<1" to support
langchain_anthropic.
- **Twitter handle:** @d_w_b7


- [ x]**Add tests and docs**: If you're adding a new integration, please
include
  1. used instructions in the README for testing

- [ x] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/

Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to pyproject.toml files (even optional
ones) unless they are required for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.
- If you are adding something to community, do not re-import it in
langchain.

If no one reviews your PR within a few days, please @-mention one of
baskaryan, efriis, eyurtsev, hwchase17.

---------

Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
Co-authored-by: Erick Friis <erick@langchain.dev>
  • Loading branch information
3 people committed Apr 6, 2024
1 parent 5ac0d1f commit de49606
Show file tree
Hide file tree
Showing 15 changed files with 1,002 additions and 808 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from langchain_community.chat_models import ChatAnthropic
from langchain_anthropic import ChatAnthropic
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.pydantic_v1 import BaseModel
Expand All @@ -9,7 +9,9 @@

prompt = ChatPromptTemplate.from_template(answer_prompt)

model = ChatAnthropic(model="claude-2", temperature=0, max_tokens_to_sample=1000)
model = ChatAnthropic(
model="claude-3-sonnet-20240229", temperature=0, max_tokens_to_sample=1000
)

chain = (
{"query": lambda x: x["query"], "information": executor | (lambda x: x["output"])}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from langchain.agents import AgentExecutor
from langchain_community.chat_models import ChatAnthropic
from langchain_anthropic import ChatAnthropic
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
Expand All @@ -17,7 +17,9 @@
)
prompt = prompt.partial(retriever_description=retriever_description)

model = ChatAnthropic(model="claude-2", temperature=0, max_tokens_to_sample=1000)
model = ChatAnthropic(
model="claude-3-sonnet-20240229", temperature=0, max_tokens_to_sample=1000
)

chain = (
RunnablePassthrough.assign(
Expand Down
392 changes: 213 additions & 179 deletions templates/anthropic-iterative-search/poetry.lock

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions templates/anthropic-iterative-search/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ readme = "README.md"
[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
langchain = "^0.1"
anthropic = "^0.5.0"
wikipedia = "^1.4.0"
langchain-anthropic = "^0.1.4"

[tool.poetry.group.dev.dependencies]
langchain-cli = ">=0.0.21"
Expand All @@ -25,7 +25,5 @@ integrations = ["Anthropic", "Wikipedia"]
tags = ["research", "agents"]

[build-system]
requires = [
"poetry-core",
]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
2 changes: 1 addition & 1 deletion templates/chain-of-note-wiki/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Check out the prompt being used here https://smith.langchain.com/hub/bagatur/cha

## Environment Setup

Uses Anthropic claude-2 chat model. Set Anthropic API key:
Uses Anthropic claude-3-sonnet-20240229 chat model. Set Anthropic API key:
```bash
export ANTHROPIC_API_KEY="..."
```
Expand Down
4 changes: 2 additions & 2 deletions templates/chain-of-note-wiki/chain_of_note_wiki/chain.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from langchain import hub
from langchain_community.chat_models import ChatAnthropic
from langchain_anthropic import ChatAnthropic
from langchain_community.utilities import WikipediaAPIWrapper
from langchain_core.output_parsers import StrOutputParser
from langchain_core.pydantic_v1 import BaseModel
Expand All @@ -13,7 +13,7 @@ class Question(BaseModel):
wiki = WikipediaAPIWrapper(top_k_results=5)
prompt = hub.pull("bagatur/chain-of-note-wiki")

llm = ChatAnthropic(model="claude-2")
llm = ChatAnthropic(model="claude-3-sonnet-20240229")


def format_docs(docs):
Expand Down
376 changes: 204 additions & 172 deletions templates/chain-of-note-wiki/poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion templates/chain-of-note-wiki/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ readme = "README.md"
[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
langchain = "^0.1"
anthropic = "^0.7.0"
wikipedia = "^1.4.0"
langchainhub = "^0.1.14"
langchain-anthropic = "^0.1.4"

[tool.poetry.group.dev.dependencies]
langchain-cli = ">=0.0.21"
Expand Down
2 changes: 1 addition & 1 deletion templates/summarize-anthropic/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# summarize-anthropic

This template uses Anthropic's `Claude2` to summarize long documents.
This template uses Anthropic's `claude-3-sonnet-20240229` to summarize long documents.

It leverages a large context window of 100k tokens, allowing for summarization of documents over 100 pages.

Expand Down
370 changes: 199 additions & 171 deletions templates/summarize-anthropic/poetry.lock

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions templates/summarize-anthropic/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
name = "summarize-anthropic"
version = "0.1.0"
description = "This template uses Anthropic's `Claude2` to summarize long documents."
authors = [
"Lance Martin <lance@langchain.dev>",
]
authors = ["Lance Martin <lance@langchain.dev>"]
readme = "README.md"

[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
langchain = "^0.1"
anthropic = ">=0.5.0"
langchainhub = ">=0.1.13"
langchain-anthropic = "^0.1.4"

[tool.poetry.group.dev.dependencies]
langchain-cli = ">=0.0.21"
Expand All @@ -27,7 +25,5 @@ integrations = ["Anthropic"]
tags = ["summarization"]

[build-system]
requires = [
"poetry-core",
]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
4 changes: 2 additions & 2 deletions templates/summarize-anthropic/summarize_anthropic/chain.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from langchain import hub
from langchain_community.chat_models import ChatAnthropic
from langchain_anthropic import ChatAnthropic
from langchain_core.output_parsers import StrOutputParser

# Create chain
prompt = hub.pull("hwchase17/anthropic-paper-qa")
model = ChatAnthropic(model="claude-2", max_tokens=10000)
model = ChatAnthropic(model="claude-3-sonnet-20240229", max_tokens=4096)
chain = prompt | model | StrOutputParser()
616 changes: 361 additions & 255 deletions templates/xml-agent/poetry.lock

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions templates/xml-agent/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
name = "xml-agent"
version = "0.1.0"
description = "Agent that uses XML syntax to communicate its decisions of what actions to take"
authors = [
"Lance Martin <lance@langchain.dev>",
]
authors = ["Lance Martin <lance@langchain.dev>"]
readme = "README.md"

[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
langchain = "^0.1"
anthropic = ">=0.5.0"
langchainhub = ">=0.1.13"
duckduckgo-search = "^3.8.3"
langchain-anthropic = "^0.1.4"

[tool.poetry.group.dev.dependencies]
langchain-cli = ">=0.0.21"
Expand All @@ -28,7 +26,5 @@ integrations = ["Anthropic"]
tags = ["agents"]

[build-system]
requires = [
"poetry-core",
]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
4 changes: 2 additions & 2 deletions templates/xml-agent/xml_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from langchain.agents.format_scratchpad import format_xml
from langchain.tools import DuckDuckGoSearchRun
from langchain.tools.render import render_text_description
from langchain_community.chat_models import ChatAnthropic
from langchain_anthropic import ChatAnthropic
from langchain_core.messages import AIMessage, HumanMessage
from langchain_core.pydantic_v1 import BaseModel, Field

Expand All @@ -19,7 +19,7 @@ def _format_chat_history(chat_history: List[Tuple[str, str]]):
return buffer


model = ChatAnthropic(model="claude-2")
model = ChatAnthropic(model="claude-3-sonnet-20240229")

tools = [DuckDuckGoSearchRun()]

Expand Down

0 comments on commit de49606

Please sign in to comment.