Update AmazonBedrock and Anthropic models

This commit is contained in:
anovazzi1 2024-02-16 14:46:00 -03:00
commit 964f2ca7ca
2 changed files with 64 additions and 32 deletions

View file

@ -1,7 +1,8 @@
from typing import Optional from typing import Optional
from langchain.llms.base import BaseLLM from langchain_community.chat_models.bedrock import BedrockChat
from langchain.llms.bedrock import Bedrock from langflow.field_typing import Text
from langflow import CustomComponent from langflow import CustomComponent
@ -46,9 +47,9 @@ class AmazonBedrockComponent(CustomComponent):
endpoint_url: Optional[str] = None, endpoint_url: Optional[str] = None,
streaming: bool = False, streaming: bool = False,
cache: Optional[bool] = None, cache: Optional[bool] = None,
) -> BaseLLM: ) -> Text:
try: try:
output = Bedrock( output = BedrockChat(
credentials_profile_name=credentials_profile_name, credentials_profile_name=credentials_profile_name,
model_id=model_id, model_id=model_id,
region_name=region_name, region_name=region_name,
@ -59,4 +60,6 @@ class AmazonBedrockComponent(CustomComponent):
) # type: ignore ) # type: ignore
except Exception as e: except Exception as e:
raise ValueError("Could not connect to AmazonBedrock API.") from e raise ValueError("Could not connect to AmazonBedrock API.") from e
return output.invoke(input=inputs) message = output.invoke(input=inputs)
self.status = message
return message

View file

@ -1,52 +1,81 @@
from typing import Optional from typing import Optional
from langchain_community.llms.anthropic import Anthropic from langchain_community.chat_models.anthropic import ChatAnthropic
from pydantic.v1 import SecretStr from pydantic.v1 import SecretStr
from langflow.field_typing import Text
from langflow import CustomComponent from langflow import CustomComponent
from langflow.field_typing import BaseLanguageModel, NestedDict
class AnthropicComponent(CustomComponent): class AnthropicLLM(CustomComponent):
display_name = "Anthropic Model" display_name: str = "Anthropic model"
description = "Anthropic large language models." description: str = "Anthropic Chat&Completion large language models."
def build_config(self): def build_config(self):
return { return {
"model": {
"display_name": "Model Name",
"options": [
"claude-2.1",
"claude-2.0",
"claude-instant-1.2",
"claude-instant-1",
# Add more models as needed
],
"info": "https://python.langchain.com/docs/integrations/chat/anthropic",
"required": True,
"value": "claude-2.1",
},
"anthropic_api_key": { "anthropic_api_key": {
"display_name": "Anthropic API Key", "display_name": "Anthropic API Key",
"type": str, "required": True,
"password": True, "password": True,
"info": "Your Anthropic API key.",
}, },
"anthropic_api_url": { "max_tokens": {
"display_name": "Anthropic API URL", "display_name": "Max Tokens",
"type": str, "field_type": "int",
}, "value": 256,
"model_kwargs": {
"display_name": "Model Kwargs",
"field_type": "NestedDict",
"advanced": True,
}, },
"temperature": { "temperature": {
"display_name": "Temperature", "display_name": "Temperature",
"field_type": "float", "field_type": "float",
"value": 0.7,
}, },
"api_endpoint": {
"display_name": "API Endpoint",
"info": "Endpoint of the Anthropic API. Defaults to 'https://api.anthropic.com' if not specified.",
},
"code": {"show": False},
"inputs": {"display_name": "Input"}, "inputs": {"display_name": "Input"},
} }
def build( def build(
self, self,
anthropic_api_key: str, model: str,
anthropic_api_url: str, inputs:str,
model_kwargs: NestedDict = {}, anthropic_api_key: Optional[str] = None,
max_tokens: Optional[int] = None,
temperature: Optional[float] = None, temperature: Optional[float] = None,
inputs: str = None, api_endpoint: Optional[str] = None,
) -> BaseLanguageModel: ) -> Text:
llm = Anthropic( # Set default API endpoint if not provided
anthropic_api_key=SecretStr(anthropic_api_key), if not api_endpoint:
anthropic_api_url=anthropic_api_url, api_endpoint = "https://api.anthropic.com"
model_kwargs=model_kwargs,
temperature=temperature, try:
) output = ChatAnthropic(
return llm.invoke(input=inputs) model_name=model,
anthropic_api_key=SecretStr(anthropic_api_key) if anthropic_api_key else None,
max_tokens_to_sample=max_tokens, # type: ignore
temperature=temperature,
anthropic_api_url=api_endpoint,
)
except Exception as e:
raise ValueError("Could not connect to Anthropic API.") from e
message = output.invoke(inputs)
result = message.content if hasattr(message, "content") else message
self.status = result
return result