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,88 +1,78 @@
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", "amazon.titan-text-express-v1",
"stream", "amazon.titan-text-lite-v1",
"input_value", "amazon.titan-embed-text-v1",
"system_message", "amazon.titan-embed-image-v1",
"amazon.titan-image-generator-v1",
"anthropic.claude-v2",
"anthropic.claude-v2:1",
"anthropic.claude-3-sonnet-20240229-v1:0",
"anthropic.claude-3-haiku-20240307-v1:0",
"anthropic.claude-instant-v1",
"ai21.j2-mid-v1",
"ai21.j2-ultra-v1",
"cohere.command-text-v14",
"cohere.command-light-text-v14",
"cohere.embed-english-v3",
"cohere.embed-multilingual-v3",
"meta.llama2-13b-chat-v1",
"meta.llama2-70b-chat-v1",
"mistral.mistral-7b-instruct-v0:2",
"mistral.mixtral-8x7b-instruct-v0:1",
],
value="anthropic.claude-instant-v1",
),
StrInput(name="credentials_profile_name", display_name="Credentials Profile Name"),
StrInput(name="region_name", display_name="Region Name"),
DictInput(name="model_kwargs", display_name="Model Kwargs", advanced=True),
StrInput(name="endpoint_url", display_name="Endpoint URL"),
BoolInput(name="cache", display_name="Cache"),
StrInput(
name="system_message",
display_name="System Message",
info="System message to pass to the model.",
advanced=True,
),
BoolInput(name="stream", display_name="Stream", info=STREAM_INFO_TEXT, advanced=True),
]
outputs = [
Output(display_name="Text", name="text_output", method="text_response"),
Output(display_name="Language Model", name="model_output", method="build_model"),
] ]
def build_config(self): def text_response(self) -> Text:
return { input_value = self.input_value
"model_id": { stream = self.stream
"display_name": "Model Id", system_message = self.system_message
"options": [ output = self.build_model()
"amazon.titan-text-express-v1", result = self.get_chat_result(output, stream, input_value, system_message)
"amazon.titan-text-lite-v1", self.status = result
"amazon.titan-embed-text-v1", return result
"amazon.titan-embed-image-v1",
"amazon.titan-image-generator-v1",
"anthropic.claude-v2",
"anthropic.claude-v2:1",
"anthropic.claude-3-sonnet-20240229-v1:0",
"anthropic.claude-3-haiku-20240307-v1:0",
"anthropic.claude-instant-v1",
"ai21.j2-mid-v1",
"ai21.j2-ultra-v1",
"cohere.command-text-v14",
"cohere.command-light-text-v14",
"cohere.embed-english-v3",
"cohere.embed-multilingual-v3",
"meta.llama2-13b-chat-v1",
"meta.llama2-70b-chat-v1",
"mistral.mistral-7b-instruct-v0:2",
"mistral.mixtral-8x7b-instruct-v0:1",
],
},
"credentials_profile_name": {"display_name": "Credentials Profile Name"},
"endpoint_url": {"display_name": "Endpoint URL"},
"region_name": {"display_name": "Region Name"},
"model_kwargs": {
"display_name": "Model Kwargs",
"advanced": True,
},
"cache": {"display_name": "Cache"},
"input_value": {"display_name": "Input", "input_types": ["Text", "Data", "Prompt"]},
"system_message": {
"display_name": "System Message",
"info": "System message to pass to the model.",
"advanced": True,
},
"stream": {
"display_name": "Stream",
"info": STREAM_INFO_TEXT,
"advanced": True,
},
}
def build( def build_model(self) -> BaseLanguageModel:
self, model_id = self.model_id
input_value: Text, credentials_profile_name = self.credentials_profile_name
system_message: Optional[str] = None, region_name = self.region_name
model_id: str = "anthropic.claude-instant-v1", model_kwargs = self.model_kwargs
credentials_profile_name: Optional[str] = None, endpoint_url = self.endpoint_url
region_name: Optional[str] = None, cache = self.cache
model_kwargs: Optional[dict] = None, stream = self.stream
endpoint_url: Optional[str] = None,
cache: Optional[bool] = None,
stream: bool = False,
) -> Text:
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,95 +5,98 @@ 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.",
),
DropdownInput(
name="model",
display_name="Model Name",
options=[
"claude-3-opus-20240229",
"claude-3-sonnet-20240229",
"claude-3-haiku-20240307",
"claude-2.1",
"claude-2.0",
"claude-instant-1.2",
"claude-instant-1",
],
info="https://python.langchain.com/docs/integrations/chat/anthropic",
value="claude-3-opus-20240229",
),
SecretStrInput(
name="anthropic_api_key",
display_name="Anthropic API Key",
info="Your Anthropic API key.",
),
FloatInput(name="temperature", display_name="Temperature", value=0.1),
StrInput(
name="anthropic_api_url",
display_name="Anthropic API URL",
advanced=True,
info="Endpoint of the Anthropic API. Defaults to 'https://api.anthropic.com' if not specified.",
),
BoolInput(name="stream", display_name="Stream", info=STREAM_INFO_TEXT, advanced=True),
StrInput(
name="system_message",
display_name="System Message",
info="System message to pass to the model.",
advanced=True,
),
StrInput(
name="prefill",
display_name="Prefill",
info="Prefill text to guide the model's response.",
advanced=True,
),
]
outputs = [
Output(display_name="Text", name="text_output", method="text_response"),
Output(display_name="Language Model", name="model_output", method="build_model"),
] ]
def build_config(self): def text_response(self) -> Text:
return { input_value = self.input_value
"model": { stream = self.stream
"display_name": "Model Name", system_message = self.system_message
"options": [ prefill = self.prefill
"claude-3-opus-20240229", output = self.build_model()
"claude-3-sonnet-20240229", messages = [
"claude-3-haiku-20240307", ("system", system_message),
"claude-2.1", ("human", input_value),
"claude-2.0", ("assistant", prefill),
"claude-instant-1.2", ]
"claude-instant-1", result = output.invoke(messages)
], self.status = prefill + result.content
"info": "https://python.langchain.com/docs/integrations/chat/anthropic", return prefill + result.content
"required": True,
"value": "claude-3-opus-20240229",
},
"anthropic_api_key": {
"display_name": "Anthropic API Key",
"required": True,
"password": True,
"info": "Your Anthropic API key.",
},
"max_tokens": {
"display_name": "Max Tokens",
"advanced": True,
"info": "The maximum number of tokens to generate. Set to 0 for unlimited tokens.",
},
"temperature": {
"display_name": "Temperature",
"field_type": "float",
"value": 0.1,
},
"anthropic_api_url": {
"display_name": "Anthropic API URL",
"advanced": True,
"info": "Endpoint of the Anthropic API. Defaults to 'https://api.anthropic.com' if not specified.",
},
"code": {"show": False},
"input_value": {"display_name": "Input", "input_types": ["Text", "Data", "Prompt"]},
"stream": {
"display_name": "Stream",
"advanced": True,
"info": STREAM_INFO_TEXT,
},
"system_message": {
"display_name": "System Message",
"advanced": True,
"info": "System message to pass to the model.",
},
}
def build( def build_model(self) -> BaseLanguageModel:
self, model = self.model
model: str, anthropic_api_key = self.anthropic_api_key
input_value: Text, max_tokens = self.max_tokens
system_message: Optional[str] = None, temperature = self.temperature
anthropic_api_key: Optional[str] = None, anthropic_api_url = self.anthropic_api_url or "https://api.anthropic.com"
max_tokens: Optional[int] = 1000,
temperature: Optional[float] = None,
anthropic_api_url: Optional[str] = None,
stream: bool = False,
) -> Text:
# Set default API endpoint if not provided
if not anthropic_api_url:
anthropic_api_url = "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