anthropic_utils
convert_dict_to_input
Convert dictionary to an input that can be used by the Anthropic API. The output is a dictionary with keys “role” and “contents”.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_dict
|
dict
|
Content dictionary with keys “role” and “content” where the values are strings. |
required |
media_folder
|
str
|
Folder where media files are stored ({data_folder}/media). |
required |
Returns:
| Type | Description |
|---|---|
dict
|
dict with keys “role” and “contents” where the value of role is either “user” or “model” and the value of contents is a list of inputs to make up an input (which can include text or image/video inputs). |
Source code in src/prompto/apis/anthropic/anthropic_utils.py
parse_content
Parse contents data and create a list of multimedia data objects. If contents is a single dictionary, a list with a single multimedia data object is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contents
|
list[dict | str] | dict | str
|
Contents data to parse and create Part object(s). Can be a list of dictionaries and strings, or a single dictionary or string. |
required |
media_folder
|
str
|
Folder where media files are stored ({data_folder}/media). |
required |
Returns:
| Type | Description |
|---|---|
list[dict]
|
List of dictionaries each defining a text or image object |
Source code in src/prompto/apis/anthropic/anthropic_utils.py
parse_content_value
Parse content dictionary and create a dictionary input for Anthropic API. If content is a string, a dictionary to represent a text object is returned. If content is a dictionary, expected keys are: - type: str, multimedia type, one of [“text”, “image”]
If type is “text”, expect a key “text” with the text content. If type is “image”, expect a key “source” which is a dictionary with keys: - url: str, URL of the image (can be a local path or a URL starting with “https://”) - detail: str, optional detail parameter (default is “auto)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
dict | str
|
Either a dictionary or a string which defines a multimodal object. |
required |
media_folder
|
str
|
Folder where media files are stored ({data_folder}/media). |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary which defines a text or image object |
Source code in src/prompto/apis/anthropic/anthropic_utils.py
process_response
Helper function to process the response from the Anthropic API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Message
|
The response from the Anthropic API |
required |
Returns:
| Type | Description |
|---|---|
str | list[str]
|
The processed response. If there are multiple responses, a list of strings is returned, otherwise a single string is returned |