AntropicModel: add prefill field for structured outputs, AmazonBedrock: New component format

This commit is contained in:
namastex888 2024-06-14 22:18:59 +00:00
commit d9eb3decf1
2 changed files with 151 additions and 157 deletions

View file

@ -1,33 +1,20 @@
from typing import Optional
from langchain_community.chat_models.bedrock import BedrockChat from langchain_community.chat_models.bedrock import BedrockChat
from langflow.base.constants import STREAM_INFO_TEXT from langflow.base.constants import STREAM_INFO_TEXT
from langflow.base.models.model import LCModelComponent from langflow.base.models.model import LCModelComponent
from langflow.field_typing import Text from langflow.field_typing import BaseLanguageModel, Text
from langflow.inputs import BoolInput, DictInput, DropdownInput, StrInput
from langflow.template import Output
class AmazonBedrockComponent(LCModelComponent): class AmazonBedrockComponent(LCModelComponent):
display_name: str = "Amazon Bedrock" display_name: str = "Amazon Bedrock"
description: str = "Generate text using Amazon Bedrock LLMs." description: str = "Generate text using Amazon Bedrock LLMs."
icon = "Amazon" icon = "Amazon"
field_order = [ inputs = [
"model_id", StrInput(name="input_value", display_name="Input", input_types=["Text", "Data", "Prompt"]),
"credentials_profile_name", DropdownInput(
"region_name", name="model_id",
"model_kwargs", display_name="Model Id",
"endpoint_url", options=[
"cache",
"stream",
"input_value",
"system_message",
]
def build_config(self):
return {
"model_id": {
"display_name": "Model Id",
"options": [
"amazon.titan-text-express-v1", "amazon.titan-text-express-v1",
"amazon.titan-text-lite-v1", "amazon.titan-text-lite-v1",
"amazon.titan-embed-text-v1", "amazon.titan-embed-text-v1",
@ -49,40 +36,43 @@ class AmazonBedrockComponent(LCModelComponent):
"mistral.mistral-7b-instruct-v0:2", "mistral.mistral-7b-instruct-v0:2",
"mistral.mixtral-8x7b-instruct-v0:1", "mistral.mixtral-8x7b-instruct-v0:1",
], ],
}, value="anthropic.claude-instant-v1",
"credentials_profile_name": {"display_name": "Credentials Profile Name"}, ),
"endpoint_url": {"display_name": "Endpoint URL"}, StrInput(name="credentials_profile_name", display_name="Credentials Profile Name"),
"region_name": {"display_name": "Region Name"}, StrInput(name="region_name", display_name="Region Name"),
"model_kwargs": { DictInput(name="model_kwargs", display_name="Model Kwargs", advanced=True),
"display_name": "Model Kwargs", StrInput(name="endpoint_url", display_name="Endpoint URL"),
"advanced": True, BoolInput(name="cache", display_name="Cache"),
}, StrInput(
"cache": {"display_name": "Cache"}, name="system_message",
"input_value": {"display_name": "Input", "input_types": ["Text", "Data", "Prompt"]}, display_name="System Message",
"system_message": { info="System message to pass to the model.",
"display_name": "System Message", advanced=True,
"info": "System message to pass to the model.", ),
"advanced": True, BoolInput(name="stream", display_name="Stream", info=STREAM_INFO_TEXT, advanced=True),
}, ]
"stream": { outputs = [
"display_name": "Stream", Output(display_name="Text", name="text_output", method="text_response"),
"info": STREAM_INFO_TEXT, Output(display_name="Language Model", name="model_output", method="build_model"),
"advanced": True, ]
},
}
def build( def text_response(self) -> Text:
self, input_value = self.input_value
input_value: Text, stream = self.stream
system_message: Optional[str] = None, system_message = self.system_message
model_id: str = "anthropic.claude-instant-v1", output = self.build_model()
credentials_profile_name: Optional[str] = None, result = self.get_chat_result(output, stream, input_value, system_message)
region_name: Optional[str] = None, self.status = result
model_kwargs: Optional[dict] = None, return result
endpoint_url: Optional[str] = None,
cache: Optional[bool] = None, def build_model(self) -> BaseLanguageModel:
stream: bool = False, model_id = self.model_id
) -> Text: credentials_profile_name = self.credentials_profile_name
region_name = self.region_name
model_kwargs = self.model_kwargs
endpoint_url = self.endpoint_url
cache = self.cache
stream = self.stream
try: try:
output = BedrockChat( output = BedrockChat(
credentials_profile_name=credentials_profile_name, credentials_profile_name=credentials_profile_name,
@ -92,8 +82,8 @@ class AmazonBedrockComponent(LCModelComponent):
endpoint_url=endpoint_url, endpoint_url=endpoint_url,
streaming=stream, streaming=stream,
cache=cache, cache=cache,
) # 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
return self.get_chat_result(output, stream, input_value, system_message)

View file

@ -5,30 +5,31 @@ from pydantic.v1 import SecretStr
from langflow.base.constants import STREAM_INFO_TEXT from langflow.base.constants import STREAM_INFO_TEXT
from langflow.base.models.model import LCModelComponent from langflow.base.models.model import LCModelComponent
from langflow.field_typing import Text from langflow.field_typing import BaseLanguageModel, Text
from langflow.inputs import BoolInput, DropdownInput, FloatInput, IntInput, SecretStrInput, StrInput
from langflow.template import Output
class AnthropicLLM(LCModelComponent): class AnthropicModelComponent(LCModelComponent):
display_name: str = "Anthropic" display_name = "Anthropic"
description: str = "Generate text using Anthropic Chat&Completion LLMs." description = "Generate text using Anthropic Chat&Completion LLMs with prefill support."
icon = "Anthropic" icon = "Anthropic"
field_order = [ inputs = [
"model", StrInput(
"anthropic_api_key", name="input_value",
"max_tokens", display_name="Input",
"temperature", input_types=["Text", "Data", "Prompt", "Message"]),
"anthropic_api_url", IntInput(
"input_value", name="max_tokens",
"system_message", display_name="Max Tokens",
"stream", advanced=True,
] info="The maximum number of tokens to generate. Set to 0 for unlimited tokens.",
),
def build_config(self): DropdownInput(
return { name="model",
"model": { display_name="Model Name",
"display_name": "Model Name", options=[
"options": [
"claude-3-opus-20240229", "claude-3-opus-20240229",
"claude-3-sonnet-20240229", "claude-3-sonnet-20240229",
"claude-3-haiku-20240307", "claude-3-haiku-20240307",
@ -37,63 +38,65 @@ class AnthropicLLM(LCModelComponent):
"claude-instant-1.2", "claude-instant-1.2",
"claude-instant-1", "claude-instant-1",
], ],
"info": "https://python.langchain.com/docs/integrations/chat/anthropic", info="https://python.langchain.com/docs/integrations/chat/anthropic",
"required": True, value="claude-3-opus-20240229",
"value": "claude-3-opus-20240229", ),
}, SecretStrInput(
"anthropic_api_key": { name="anthropic_api_key",
"display_name": "Anthropic API Key", display_name="Anthropic API Key",
"required": True, info="Your Anthropic API key.",
"password": True, ),
"info": "Your Anthropic API key.", FloatInput(name="temperature", display_name="Temperature", value=0.1),
}, StrInput(
"max_tokens": { name="anthropic_api_url",
"display_name": "Max Tokens", display_name="Anthropic API URL",
"advanced": True, advanced=True,
"info": "The maximum number of tokens to generate. Set to 0 for unlimited tokens.", info="Endpoint of the Anthropic API. Defaults to 'https://api.anthropic.com' if not specified.",
}, ),
"temperature": { BoolInput(name="stream", display_name="Stream", info=STREAM_INFO_TEXT, advanced=True),
"display_name": "Temperature", StrInput(
"field_type": "float", name="system_message",
"value": 0.1, display_name="System Message",
}, info="System message to pass to the model.",
"anthropic_api_url": { advanced=True,
"display_name": "Anthropic API URL", ),
"advanced": True, StrInput(
"info": "Endpoint of the Anthropic API. Defaults to 'https://api.anthropic.com' if not specified.", name="prefill",
}, display_name="Prefill",
"code": {"show": False}, info="Prefill text to guide the model's response.",
"input_value": {"display_name": "Input", "input_types": ["Text", "Data", "Prompt"]}, advanced=True,
"stream": { ),
"display_name": "Stream", ]
"advanced": True, outputs = [
"info": STREAM_INFO_TEXT, Output(display_name="Text", name="text_output", method="text_response"),
}, Output(display_name="Language Model", name="model_output", method="build_model"),
"system_message": { ]
"display_name": "System Message",
"advanced": True,
"info": "System message to pass to the model.",
},
}
def build( def text_response(self) -> Text:
self, input_value = self.input_value
model: str, stream = self.stream
input_value: Text, system_message = self.system_message
system_message: Optional[str] = None, prefill = self.prefill
anthropic_api_key: Optional[str] = None, output = self.build_model()
max_tokens: Optional[int] = 1000, messages = [
temperature: Optional[float] = None, ("system", system_message),
anthropic_api_url: Optional[str] = None, ("human", input_value),
stream: bool = False, ("assistant", prefill),
) -> Text: ]
# Set default API endpoint if not provided result = output.invoke(messages)
if not anthropic_api_url: self.status = prefill + result.content
anthropic_api_url = "https://api.anthropic.com" return prefill + result.content
def build_model(self) -> BaseLanguageModel:
model = self.model
anthropic_api_key = self.anthropic_api_key
max_tokens = self.max_tokens
temperature = self.temperature
anthropic_api_url = self.anthropic_api_url or "https://api.anthropic.com"
try: try:
output = ChatAnthropic( output = ChatAnthropic(
model_name=model, model=model,
anthropic_api_key=(SecretStr(anthropic_api_key) if anthropic_api_key else None), anthropic_api_key=(SecretStr(anthropic_api_key) if anthropic_api_key else None),
max_tokens_to_sample=max_tokens, # type: ignore max_tokens_to_sample=max_tokens, # type: ignore
temperature=temperature, temperature=temperature,
@ -102,4 +105,5 @@ class AnthropicLLM(LCModelComponent):
except Exception as e: except Exception as e:
raise ValueError("Could not connect to Anthropic API.") from e raise ValueError("Could not connect to Anthropic API.") from e
return self.get_chat_result(output, stream, input_value, system_message) return output