-
Notifications
You must be signed in to change notification settings - Fork 222
feat: Add document creation for ChatBedrockConverse #601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 14 commits
4f4c25c
13fe759
71a21ff
b4d8238
58c7ecb
4812a05
003d288
114bc30
d648de1
1842cfc
42ce460
d757851
40241bd
5ab49f9
574c149
05730b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -467,9 +467,9 @@ class Joke(BaseModel): | |
| additionalModelResponseFieldPaths. | ||
| """ | ||
|
|
||
| supports_tool_choice_values: Optional[ | ||
| Sequence[Literal["auto", "any", "tool"]] | ||
| ] = None | ||
| supports_tool_choice_values: Optional[Sequence[Literal["auto", "any", "tool"]]] = ( | ||
| None | ||
| ) | ||
| """Which types of tool_choice values the model supports. | ||
|
|
||
| Inferred if not specified. Inferred as ('auto', 'any', 'tool') if a 'claude-3' | ||
|
|
@@ -514,6 +514,70 @@ def create_cache_point(cls, cache_type: str = "default") -> Dict[str, Any]: | |
| """ | ||
| return {"cachePoint": {"type": cache_type}} | ||
|
|
||
| @classmethod | ||
| def create_document( | ||
| cls, | ||
| name: str, | ||
| source: dict[str, Any], | ||
| format: Literal["pdf", "csv", "doc", "docx", "xls", "xlsx", "html", "txt", "md"], | ||
| context: Optional[str] = None, | ||
| enable_citations: Optional[bool] = False, | ||
|
|
||
| ) -> Dict[str, Any]: | ||
| """Create a document configuration for Bedrock. | ||
| Args: | ||
| name: The name of the document. | ||
| source: The source of the document. | ||
| format: The format of the document, or its extension. | ||
| context: Info for the model to understand the document for citations. | ||
| enable_citations: Whether to enable the Citations API for the document. | ||
| Returns: | ||
| Dictionary containing a properly formatted to add to message content.""" | ||
| if re.search(r"[^A-Za-z0-9 \[\]()\-]|\s{2,}", name): | ||
| raise ValueError( | ||
| "Name must be only alphanumeric characters," | ||
| " whitespace characters (no more than one in a row)," | ||
| " hyphens, parentheses, or square brackets." | ||
| ) | ||
|
|
||
| valid_source_types = ["bytes", "content", "s3Location", "text"] | ||
| if ( | ||
| len(source.keys()) > 1 | ||
| or list(source.keys())[0] not in valid_source_types | ||
| ): | ||
| raise ValueError( | ||
| f"The key for source can only be one of the following: {valid_source_types}" | ||
| ) | ||
|
|
||
| if source.get("bytes") and not isinstance(source.get("bytes"), bytes): | ||
| raise ValueError(f"Document source with type bytes must be bytes type.") | ||
|
|
||
| if source.get("text") and not isinstance(source.get("text"), str): | ||
| raise ValueError("Document source with type text must be str type.") | ||
|
|
||
| if source.get("s3Location") and not isinstance( | ||
| source.get("s3Location").get("uri"), str | ||
| ): | ||
| raise ValueError( | ||
| "Document source with type s3Location" | ||
| " must have a dictionary with a valid s3 uri as a dict." | ||
| ) | ||
|
|
||
| if source.get("content") and not isinstance(source.get("content", list)): | ||
| raise ValueError( | ||
| "Document source with type content must have a list of document content blocks." | ||
| ) | ||
|
|
||
| document = {"name": name, "source": source, "format": format} | ||
|
|
||
| if context: | ||
| document["context"] = context | ||
|
|
||
| if enable_citations: | ||
| document["citations"] = {"enabled": True} | ||
|
|
||
| return {"document": document} | ||
|
|
||
| @model_validator(mode="before") | ||
| @classmethod | ||
| def build_extra(cls, values: dict[str, Any]) -> Any: | ||
|
|
@@ -657,19 +721,6 @@ def set_disable_streaming(cls, values: Dict) -> Any: | |
| def validate_environment(self) -> Self: | ||
| """Validate that AWS credentials to and python package exists in environment.""" | ||
|
|
||
| # Skip creating new client if passed in constructor | ||
| if self.client is None: | ||
| self.client = create_aws_client( | ||
|
Comment on lines
660
to
662
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this was removed unintentionally in the 40241bd merge, let's put it back to fix the tests
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added it back in but I did change my setup so hoping nothing else broke. |
||
| region_name=self.region_name, | ||
| credentials_profile_name=self.credentials_profile_name, | ||
| aws_access_key_id=self.aws_access_key_id, | ||
| aws_secret_access_key=self.aws_secret_access_key, | ||
| aws_session_token=self.aws_session_token, | ||
| endpoint_url=self.endpoint_url, | ||
| config=self.config, | ||
| service_name="bedrock-runtime", | ||
| ) | ||
|
|
||
| # Create bedrock client for control plane API call | ||
| if self.bedrock_client is None: | ||
| bedrock_client_cfg = {} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing
contentsource currently fails becauseisinstanceis missing the second argument for type hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whoops good catch thanks!