Skip to content

Commit

Permalink
Migrate to langchain_anthropic and add ChatAnthropic enhancements (#1551
Browse files Browse the repository at this point in the history
)

Migrate to langchain_anthropic and enhance ChatAnthropic functionality

- Replace langchain_community with langchain_anthropic for ChatAnthropic
- Add support for selecting the Claude model in ChatAnthropic
- Implement top_p and top_k parameter support in ChatAnthropic
  • Loading branch information
daikiad committed Mar 22, 2024
1 parent be94153 commit 0b380af
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 44 deletions.
69 changes: 33 additions & 36 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Expand Up @@ -60,7 +60,6 @@ cohere = "^4.47.0"
python-multipart = "^0.0.7"
sqlmodel = "^0.0.14"
faiss-cpu = "^1.7.4"
anthropic = "^0.15.0"
orjson = "3.9.15"
multiprocess = "^0.70.14"
cachetools = "^5.3.1"
Expand Down Expand Up @@ -105,6 +104,8 @@ elasticsearch = "^8.12.0"
pytube = "^15.0.0"
llama-index = "0.9.48"
langchain-openai = "^0.0.6"
urllib3 = "<2"
langchain-anthropic = "^0.1.4"

[tool.poetry.group.dev.dependencies]
pytest-asyncio = "^0.23.1"
Expand Down
33 changes: 26 additions & 7 deletions src/backend/langflow/components/llms/ChatAnthropic.py
Expand Up @@ -2,7 +2,9 @@
from langflow import CustomComponent
from typing import Optional, Union, Callable
from langflow.field_typing import BaseLanguageModel
from langchain_community.chat_models.anthropic import ChatAnthropic

# from langchain_community.chat_models.anthropic import ChatAnthropic
from langchain_anthropic import ChatAnthropic


class ChatAnthropicComponent(CustomComponent):
Expand All @@ -17,31 +19,48 @@ def build_config(self):
"field_type": "str",
"password": True,
},
"anthropic_api_url": {
"display_name": "Anthropic API URL",
"field_type": "str",
},
"model_kwargs": {
"display_name": "Model Kwargs",
"field_type": "dict",
"advanced": True,
},
"model_name": {
"display_name": "Model Name",
"field_type": "str",
"advanced": False,
"required": False,
"options": ["claude-3-opus-20240229", "claude-3-sonnet-20240229", "claude-3-haiku-20240307"],
},
"temperature": {
"display_name": "Temperature",
"field_type": "float",
},
"max_tokens": {
"display_name": "Max Tokens",
"field_type": "int",
"advanced": False,
"required": False,
},
"top_k": {"display_name": "Top K", "field_type": "int", "advanced": True},
"top_p": {"display_name": "Top P", "field_type": "float", "advanced": True},
}

def build(
self,
anthropic_api_key: str,
anthropic_api_url: Optional[str] = None,
model_kwargs: dict = {},
model_name: str = "claude-3-opus-20240229",
temperature: Optional[float] = None,
max_tokens: Optional[int] = 1024,
top_k: Optional[int] = None,
top_p: Optional[float] = None,
) -> Union[BaseLanguageModel, Callable]:
return ChatAnthropic(
anthropic_api_key=SecretStr(anthropic_api_key),
anthropic_api_url=anthropic_api_url,
model_kwargs=model_kwargs,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
top_k=top_k,
top_p=top_p,
)

0 comments on commit 0b380af

Please sign in to comment.