From 0325c4168a82d9ba30633e919787cc0f8fc4c00a Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Fri, 16 Feb 2024 14:07:55 -0300 Subject: [PATCH] Add Anthropic model component --- .../langflow/components/models/Anthropic.py | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/backend/langflow/components/models/Anthropic.py diff --git a/src/backend/langflow/components/models/Anthropic.py b/src/backend/langflow/components/models/Anthropic.py new file mode 100644 index 000000000..b49e9e5c4 --- /dev/null +++ b/src/backend/langflow/components/models/Anthropic.py @@ -0,0 +1,52 @@ +from typing import Optional + +from langchain_community.llms.anthropic import Anthropic +from pydantic.v1 import SecretStr + +from langflow import CustomComponent +from langflow.field_typing import BaseLanguageModel, NestedDict + + +class AnthropicComponent(CustomComponent): + display_name = "Anthropic Model" + description = "Anthropic large language models." + + def build_config(self): + return { + "anthropic_api_key": { + "display_name": "Anthropic API Key", + "type": str, + "password": True, + }, + "anthropic_api_url": { + "display_name": "Anthropic API URL", + "type": str, + }, + "model_kwargs": { + "display_name": "Model Kwargs", + "field_type": "NestedDict", + "advanced": True, + }, + "temperature": { + "display_name": "Temperature", + "field_type": "float", + }, + "inputs": {"display_name": "Input"}, + + } + + def build( + self, + anthropic_api_key: str, + anthropic_api_url: str, + model_kwargs: NestedDict = {}, + temperature: Optional[float] = None, + inputs: str = None, + ) -> BaseLanguageModel: + llm = Anthropic( + anthropic_api_key=SecretStr(anthropic_api_key), + anthropic_api_url=anthropic_api_url, + model_kwargs=model_kwargs, + temperature=temperature, + ) + return llm.invoke(input=inputs)