Update Anthropic and ChatAnthropic components

This commit is contained in:
anovazzi1 2024-01-16 13:30:31 -03:00
commit 239e4bcb7c
2 changed files with 15 additions and 38 deletions

View file

@ -1,6 +1,7 @@
from langflow import CustomComponent
from typing import Optional, Dict, Any
from langflow.field_typing import BaseLanguageModel
from typing import Optional
from langflow.field_typing import BaseLanguageModel,NestedDict
from langchain_community.llms.anthropic import Anthropic
class AnthropicComponent(CustomComponent):
@ -20,48 +21,25 @@ class AnthropicComponent(CustomComponent):
},
"model_kwargs": {
"display_name": "Model Kwargs",
"field_type": 'dict',
"field_type": 'NestedDict',
"advanced": True,
},
"temperature": {
"display_name": "Temperature",
"type": float,
"field_type": "float",
},
}
def build(
self,
anthropic_api_key: Optional[str],
anthropic_api_url: Optional[str],
model_kwargs: Optional[Dict[str, Any]],
anthropic_api_key: str,
anthropic_api_url: str,
model_kwargs: Optional[NestedDict],
temperature: Optional[float] = None,
) -> BaseLanguageModel:
# The actual builder method should return an instance of the Anthropic class
# Here we are returning a placeholder class as the Anthropic class is not defined
# This is to comply with the type hints required by the CustomComponent
class Anthropic(BaseLanguageModel):
def __init__(
self,
api_key: Optional[str],
api_url: Optional[str],
model_kwargs: Optional[Dict[str, Any]] = None,
temperature: Optional[float] = None,
):
# Initialize Anthropic model with the provided arguments
super().__init__()
self.api_key = api_key
self.api_url = api_url
self.model_kwargs = model_kwargs
self.temperature = temperature
def __call__(self, prompt: str) -> str:
# The logic to call the Anthropic model would go here
# This is a placeholder implementation
return "This is a simulated response from the Anthropic model."
return Anthropic(
api_key=anthropic_api_key,
api_url=anthropic_api_url,
anthropic_api_key=anthropic_api_key,
anthropic_api_url=anthropic_api_url,
model_kwargs=model_kwargs,
temperature=temperature,
)

View file

@ -2,7 +2,7 @@
from langflow import CustomComponent
from typing import Optional, Union, Callable
from langflow.field_typing import BaseLanguageModel
from langchain_community.chat_models.anthropic import ChatAnthropic
class ChatAnthropicComponent(CustomComponent):
display_name = "ChatAnthropic"
description = "`Anthropic` chat large language models."
@ -12,12 +12,12 @@ class ChatAnthropicComponent(CustomComponent):
return {
"anthropic_api_key": {
"display_name": "Anthropic API Key",
"type": str,
"field_type": "str",
"password": True,
},
"anthropic_api_url": {
"display_name": "Anthropic API URL",
"type": str,
"field_type": "str",
},
"model_kwargs": {
"display_name": "Model Kwargs",
@ -26,7 +26,7 @@ class ChatAnthropicComponent(CustomComponent):
},
"temperature": {
"display_name": "Temperature",
"type": float,
"field_type": "float",
},
}
@ -37,10 +37,9 @@ class ChatAnthropicComponent(CustomComponent):
model_kwargs: dict = {},
temperature: Optional[float] = None,
) -> Union[BaseLanguageModel, Callable]:
from langchain.model_io.models.chat.integrations import ChatAnthropic # Importing here due to potential local scope requirements
return ChatAnthropic(
anthropic_api_key=anthropic_api_key.get_secret_value() if anthropic_api_key else None,
anthropic_api_key=anthropic_api_key,
anthropic_api_url=anthropic_api_url,
model_kwargs=model_kwargs,
temperature=temperature,