qianfan.resources package
- class qianfan.resources.ChatCompletion(model: Optional[str] = None, endpoint: Optional[str] = None, **kwargs: Any)[source]
Bases:
BaseResourceQianFan ChatCompletion is an agent for calling QianFan ChatCompletion API.
- async abatch_do(messages_list: List[Union[List[Dict], QfMessages]], worker_num: Optional[int] = None, **kwargs: Any) List[Union[QfResponse, AsyncIterator[QfResponse]]][source]
Async batch perform chat-based language generation using user-supplied messages.
- Parameters:
- messages_list: List[Union[List[Dict], QfMessages]]:
List of the messages list in the conversation. Please refer to ChatCompletion.do for more information of each messages.
- worker_num (Optional[int]):
The number of prompts to process at the same time, default to None, which means this number will be decided dynamically.
- kwargs (Any):
Please refer to ChatCompletion.do for other parameters such as model, endpoint, retry_count, etc.
``` response_list = await ChatCompletion().abatch_do([…], worker_num = 10) for response in response_list:
# response is QfResponse if succeed, or response will be exception print(response)
- async ado(messages: Union[List[Dict], QfMessages], model: Optional[str] = None, endpoint: Optional[str] = None, stream: bool = False, retry_count: int = 1, request_timeout: float = 60, request_id: Optional[str] = None, backoff_factor: float = 1, auto_concat_truncate: bool = False, truncated_continue_prompt: str = '继续', **kwargs: Any) Union[QfResponse, AsyncIterator[QfResponse]][source]
Async perform chat-based language generation using user-supplied messages.
- Parameters:
- messages (Union[List[Dict], QfMessages]):
A list of messages in the conversation including the one from system. Each message should be a dictionary containing ‘role’ and ‘content’ keys, representing the role (either ‘user’, or ‘assistant’) and content of the message, respectively. Alternatively, you can provide a QfMessages object for convenience.
- model (Optional[str]):
The name or identifier of the language model to use. If not specified, the default model is used(ERNIE-Bot-turbo).
- endpoint (Optional[str]):
The endpoint for making API requests. If not provided, the default endpoint is used.
- stream (bool):
If set to True, the responses are streamed back as an iterator. If False, a single response is returned.
- retry_count (int):
The number of times to retry the request in case of failure.
- request_timeout (float):
The maximum time (in seconds) to wait for a response from the model.
- backoff_factor (float):
A factor to increase the waiting time between retry attempts.
- auto_concat_truncate (bool):
[Experimental] If set to True, continuously requesting will be run until is_truncated is False. As a result, the entire reply will be returned. Cause this feature highly relies on the understanding ability of LLM, Use it carefully.
- truncated_continue_prompt (str):
[Experimental] The prompt to use when requesting more content for auto truncated reply.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Additional parameters like temperature will vary depending on the model, please refer to the API documentation. The additional parameters can be passed as follows:
` ChatCompletion().ado(messages = ..., temperature = 0.2, top_p = 0.5) `
- batch_do(messages_list: Union[List[List[Dict]], List[QfMessages]], worker_num: Optional[int] = None, **kwargs: Any) BatchRequestFuture[source]
Batch perform chat-based language generation using user-supplied messages.
- Parameters:
- messages_list: List[Union[List[Dict], QfMessages]]:
List of the messages list in the conversation. Please refer to ChatCompletion.do for more information of each messages.
- worker_num (Optional[int]):
The number of prompts to process at the same time, default to None, which means this number will be decided dynamically.
- kwargs (Any):
Please refer to ChatCompletion.do for other parameters such as model, endpoint, retry_count, etc.
``` response_list = ChatCompletion().batch_do([…], worker_num = 10) for response in response_list:
# return QfResponse if succeed, or exception will be raised print(response.result())
# or while response_list.finished_count() != response_list.task_count():
time.sleep(1)
- do(messages: Union[List[Dict], QfMessages], model: Optional[str] = None, endpoint: Optional[str] = None, stream: bool = False, retry_count: int = 1, request_timeout: float = 60, request_id: Optional[str] = None, backoff_factor: float = 1, auto_concat_truncate: bool = False, truncated_continue_prompt: str = '继续', **kwargs: Any) Union[QfResponse, Iterator[QfResponse]][source]
Perform chat-based language generation using user-supplied messages.
- Parameters:
- messages (Union[List[Dict], QfMessages]):
A list of messages in the conversation including the one from system. Each message should be a dictionary containing ‘role’ and ‘content’ keys, representing the role (either ‘user’, or ‘assistant’) and content of the message, respectively. Alternatively, you can provide a QfMessages object for convenience.
- model (Optional[str]):
The name or identifier of the language model to use. If not specified, the default model is used(ERNIE-Bot-turbo).
- endpoint (Optional[str]):
The endpoint for making API requests. If not provided, the default endpoint is used.
- stream (bool):
If set to True, the responses are streamed back as an iterator. If False, a single response is returned.
- retry_count (int):
The number of times to retry the request in case of failure.
- request_timeout (float):
The maximum time (in seconds) to wait for a response from the model.
- backoff_factor (float):
A factor to increase the waiting time between retry attempts.
- auto_concat_truncate (bool):
[Experimental] If set to True, continuously requesting will be run until is_truncated is False. As a result, the entire reply will be returned. Cause this feature highly relies on the understanding ability of LLM, Use it carefully.
- truncated_continue_prompt (str):
[Experimental] The prompt to use when requesting more content for auto truncated reply.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Additional parameters like temperature will vary depending on the model, please refer to the API documentation. The additional parameters can be passed as follows:
` ChatCompletion().do(messages = ..., temperature = 0.2, top_p = 0.5) `
- class qianfan.resources.Completion(model: Optional[str] = None, endpoint: Optional[str] = None, **kwargs: Any)[source]
Bases:
BaseResourceQianFan Completion is an agent for calling QianFan completion API.
- async abatch_do(prompt_list: List[str], worker_num: Optional[int] = None, **kwargs: Any) List[Union[QfResponse, AsyncIterator[QfResponse]]][source]
Async batch generate a completion based on the user-provided prompt.
- Parameters:
- prompt_list (List[str]):
The input prompt list to generate the continuation from.
- worker_num (Optional[int]):
The number of prompts to process at the same time, default to None, which means this number will be decided dynamically.
- kwargs (Any):
Please refer to Completion.ado for other parameters such as model, endpoint, retry_count, etc.
``` response_list = await Completion().abatch_do([…], worker_num = 10) for response in response_list:
# response is QfResponse if succeed, or response will be exception print(response)
- async ado(prompt: str, model: Optional[str] = None, endpoint: Optional[str] = None, stream: bool = False, retry_count: int = 1, request_timeout: float = 60, request_id: Optional[str] = None, backoff_factor: float = 1, **kwargs: Any) Union[QfResponse, AsyncIterator[QfResponse]][source]
Async generate a completion based on the user-provided prompt.
- Parameters:
- prompt (str):
The input prompt to generate the continuation from.
- model (Optional[str]):
The name or identifier of the language model to use. If not specified, the default model is used(ERNIE-Bot-turbo).
- endpoint (Optional[str]):
The endpoint for making API requests. If not provided, the default endpoint is used.
- stream (bool):
If set to True, the responses are streamed back as an iterator. If False, a single response is returned.
- retry_count (int):
The number of times to retry the request in case of failure.
- request_timeout (float):
The maximum time (in seconds) to wait for a response from the model.
- backoff_factor (float):
A factor to increase the waiting time between retry attempts.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Additional parameters like temperature will vary depending on the model, please refer to the API documentation. The additional parameters can be passed as follows:
` Completion().do(prompt = ..., temperature = 0.2, top_p = 0.5) `
- batch_do(prompt_list: List[str], worker_num: Optional[int] = None, **kwargs: Any) BatchRequestFuture[source]
Batch generate a completion based on the user-provided prompt.
- Parameters:
- prompt_list (List[str]):
The input prompt list to generate the continuation from.
- worker_num (Optional[int]):
The number of prompts to process at the same time, default to None, which means this number will be decided dynamically.
- kwargs (Any):
Please refer to Completion.do for other parameters such as model, endpoint, retry_count, etc.
``` response_list = Completion().batch_do([”…”, “…”], worker_num = 10) for response in response_list:
# return QfResponse if succeed, or exception will be raised print(response.result())
# or while response_list.finished_count() != response_list.task_count():
time.sleep(1)
- do(prompt: str, model: Optional[str] = None, endpoint: Optional[str] = None, stream: bool = False, retry_count: int = 1, request_timeout: float = 60, request_id: Optional[str] = None, backoff_factor: float = 1, **kwargs: Any) Union[QfResponse, Iterator[QfResponse]][source]
Generate a completion based on the user-provided prompt.
- Parameters:
- prompt (str):
The input prompt to generate the continuation from.
- model (Optional[str]):
The name or identifier of the language model to use. If not specified, the default model is used(ERNIE-Bot-turbo).
- endpoint (Optional[str]):
The endpoint for making API requests. If not provided, the default endpoint is used.
- stream (bool):
If set to True, the responses are streamed back as an iterator. If False, a single response is returned.
- retry_count (int):
The number of times to retry the request in case of failure.
- request_timeout (float):
The maximum time (in seconds) to wait for a response from the model.
- backoff_factor (float):
A factor to increase the waiting time between retry attempts.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Additional parameters like temperature will vary depending on the model, please refer to the API documentation. The additional parameters can be passed as follows:
` Completion().do(prompt = ..., temperature = 0.2, top_p = 0.5) `
- class qianfan.resources.Data[source]
Bases:
objectClass for Data API
- classmethod annotate_an_entity(entity_id: str, dataset_id: int, content: Optional[List[Dict[str, Any]]] = None, labels: Optional[List[Dict[str, Any]]] = None, **kwargs: Any) QfRequest[source]
annotate an entity within a dataset
- Parameters:
- entity_id (str):
entity id to be annotating
- dataset_id (int):
dataset id to do annotate
- content (Optional[Dict[str, Any]]):
the prompt and LLM responses on a conversation
- labels (Optional[Dict[str, Any]]):
description of an image
- **kwargs (Any):
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/mlp6izcqr
- classmethod create_bare_dataset(name: str, data_set_type: DataSetType, project_type: DataProjectType, template_type: DataTemplateType, storage_type: DataStorageType = DataStorageType.PublicBos, storage_id: Optional[str] = None, storage_path: Optional[str] = None, **kwargs: Any) QfRequest[source]
create a bare dataset。
- Parameters:
- name (str):
the name of the dataset.
- data_set_type (DataSetType):
the type of the dataset.
- project_type (DataProjectType):
the project type.
- template_type (DataTemplateType):
the template type.
- storage_type (DataStorageType):
the type of data storage.
- storage_id (Optional[str]):
the storage ID when the storage type is PrivateBos.
- storage_path (Optional[str]):
the storage path when the storage type is PrivateBos.
- **kwargs:
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/qloic44vr
- classmethod create_data_import_task(dataset_id: int, is_annotated: bool, import_source: DataSourceType, file_url: str, **kwargs: Any) QfRequest[source]
create data import task
- Parameters:
- dataset_id (int):
dataset id
- is_annotated (bool):
has dataset been annotated
- import_source (DataSourceType):
the source for importing dataset
- file_url (str):
file url
- **kwargs:
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Yloic82qy
- classmethod create_dataset_augmenting_task(name: str, source_dataset_id: int, destination_dataset_id: int, service_name: str, service_url: str, app_id: int, num_seed_fewshot: int, num_instances_to_generate: int, similarity_threshold: float, **kwargs: Any) QfRequest[source]
create a data augmenting task
- Parameters:
- name (str):
name of augment task
- source_dataset_id (int):
dataset id need to be augmented.
- destination_dataset_id (int):
where dataset should be stored after augmentation
- service_name (str):
which LLM should be used for augmenting task
- service_url (str):
service url related to service_name
- app_id (int):
app id
- num_seed_fewshot (int):
the number of sample used for augmenting each data
- num_instances_to_generate (int):
the number of instance to generate
- similarity_threshold (float):
similarity threshold
- **kwargs (Any):
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Dlp6iv0zw
- classmethod create_dataset_etl_task(source_dataset_id: int, destination_dataset_id: int, operations: Dict[str, List[Dict[str, Any]]], **kwargs: Any) QfRequest[source]
create a post-pretrain dataset etl task
- Parameters:
- source_dataset_id (int):
dataset id need to be processed.
- destination_dataset_id (int):
where dataset should be stored after etl
- operations (Dict[str, List[Dict[str, Any]]]),
etl operator settings.
- **kwargs (Any):
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/8lp6irqen
- classmethod create_dataset_export_task(dataset_id: int, export_destination_type: DataExportDestinationType, storage_id: Optional[str] = None, is_export_with_annotation: bool = True, **kwargs: Any) QfRequest[source]
create dataset export task
- Args:
- dataset_id (int):
dataset id
- export_destination_type (DataExportDestinationType):
export destination type
- storage_id (Optional[str]):
storage id of user’s BOS, needed when export_destination_type is PrivateBos, Default to None.
- is_export_with_annotation (Optional[bool]):
is export dataset with annotation, Defaults to True.
- **kwargs:
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/bloicnydp
- classmethod delete_an_entity(entity_ids: List[str], dataset_id: int, **kwargs: Any) QfRequest[source]
delete an entity from dataset
- Parameters:
- entity_ids (List[str]):
entity id list
- dataset_id (int):
dataset id to do delete
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/ilp6j1rse
- classmethod delete_dataset(dataset_id: int, **kwargs: Any) QfRequest[source]
delete dataset
- Parameters:
- dataset_id (int):
dataset id.
- **kwargs:
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Oloicp6fk
- classmethod delete_dataset_augmenting_task(task_ids: List[int], **kwargs: Any) QfRequest[source]
delete dataset augmenting task
- Parameters:
- task_ids (List[int]):
dataset augmenting task id list.
- **kwargs (Any):
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/glp6iy6h3
- classmethod delete_dataset_etl_task(etl_ids: List[int], **kwargs: Any) QfRequest[source]
delete post-pretrain dataset etl task
- Parameters:
- etl_ids (List[int]):
dataset etl task id list.
- **kwargs (Any):
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Glp6iu8ny
- classmethod get_dataset_aug_task_list(keyword: Optional[str] = None, sorted_by_start_time_asc: Optional[bool] = None, page_size: int = 10, offset: int = 0, **kwargs: Any) QfRequest[source]
get a post-pretrain dataset etl task info
- Parameters:
- keyword: (Optional[str]):
optional keyword to search augmentation task, default to None.
- sorted_by_start_time_asc (Optional[bool]):
is result list sorted by starting time in ascending order if True, sorted by starting time in descending order if False, sorted by id in ascending order if None. default to None
- page_size (int):
the length of etl list showing, default to 10.
- offset (int):
where to start list etl task, default to 0.
- **kwargs (Any):
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Flp7n9xmp
- classmethod get_dataset_augmenting_task_info(task_id: int, **kwargs: Any) QfRequest[source]
get a data augmenting task info
- Parameters:
- task_id (int):
dataset augmenting task id.
- **kwargs (Any):
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Clp6iwiy9
- classmethod get_dataset_etl_task_info(etl_id: int, **kwargs: Any) QfRequest[source]
get a post-pretrain dataset etl task info
- Parameters:
- etl_id (int):
dataset etl task id.
- **kwargs (Any):
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/mlp6it4vd
- classmethod get_dataset_etl_task_list(page_size: int = 10, offset: int = 0, **kwargs: Any) QfRequest[source]
get a post-pretrain dataset etl task info
- Parameters:
- page_size (int):
the length of etl list showing, default to 10.
- offset (int):
where to start list etl task, default to 0.
- **kwargs (Any):
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/elp7myxvp
- classmethod get_dataset_export_records(dataset_id: int, **kwargs: Any) QfRequest[source]
get dataset export records
- Parameters:
- dataset_id (int):
dataset id.
- **kwargs:
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Zlonqgtw0
- classmethod get_dataset_import_error_detail(dataset_id: int, error_code: int, **kwargs: Any) QfRequest[source]
get dataset status in dataset id list
- Parameters:
- dataset_id (int):
dataset id.
- error_code (int):
error code used to query
- **kwargs:
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/hlonqulbq
- classmethod get_dataset_info(dataset_id: int, **kwargs: Any) QfRequest[source]
get dataset info
- Parameters:
- dataset_id (int):
dataset id.
- **kwargs:
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Xloick80a
- classmethod get_dataset_status_in_batch(dataset_id_list: List[int], **kwargs: Any) QfRequest[source]
get dataset status in dataset id list
- Parameters:
- dataset_id_list (List[int]):
dataset id list.
- **kwargs:
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Sloicm9qz
- classmethod list_all_entity_in_dataset(dataset_id: int, offset: int = 0, page_size: int = 20, import_time_closure: Optional[List[int]] = None, annotating_time_closure: Optional[List[int]] = None, listing_type: EntityListingType = EntityListingType.All, label_id_str: Optional[str] = None, **kwargs: Any) QfRequest[source]
delete an entity from dataset
- Parameters:
- dataset_id (int):
dataset id
- offset (int):
offset of dataset where the list start, default to 0
- page_size (int):
window size of the list, default to 20, the maximum value is 30 and the minimum is 1
- import_time_closure (Optional[List[int]]):
a list containing start timestamp and end timestamp of importing time, default to None
- annotating_time_closure (Optional[List[int]]):
a list containing start timestamp and end timestamp of annotating time, default to None
- listing_type (EntityListingType):
type of listing, default to EntityListingType.All
- label_id_str (Optional[str]):
label id of text2image, default to None
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Ulp6j2yep
- classmethod release_dataset(dataset_id: int, **kwargs: Any) QfRequest[source]
release dataset
- Parameters:
- dataset_id (int):
dataset id.
- **kwargs:
any other parameters.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Uloic6krs
- class qianfan.resources.Embedding(model: Optional[str] = None, endpoint: Optional[str] = None, **kwargs: Any)[source]
Bases:
BaseResourceQianFan Embedding is an agent for calling QianFan embedding API.
- async abatch_do(texts_list: List[List[str]], worker_num: Optional[int] = None, **kwargs: Any) List[Union[QfResponse, AsyncIterator[QfResponse]]][source]
Async batch generate embeddings for a list of input texts using a specified model.
- Parameters:
- texts_list (List[List[str]]):
List of the input text list to generate the embeddings.
- worker_num (Optional[int]):
The number of prompts to process at the same time, default to None, which means this number will be decided dynamically.
- kwargs (Any):
Please refer to Embedding.ado for other parameters such as model, endpoint, retry_count, etc.
``` response_list = await Embedding().abatch_do([…], worker_num = 10) for response in response_list:
# response is QfResponse if succeed, or response will be exception print(response)
- async ado(texts: List[str], model: Optional[str] = None, endpoint: Optional[str] = None, stream: bool = False, retry_count: int = 1, request_timeout: float = 60, request_id: Optional[str] = None, backoff_factor: float = 1, **kwargs: Any) Union[QfResponse, AsyncIterator[QfResponse]][source]
Async generate embeddings for a list of input texts using a specified model.
- Parameters:
- texts (List[str]):
A list of input texts for which embeddings need to be generated.
- model (Optional[str]):
The name or identifier of the language model to use. If not specified, the default model is used(ERNIE-Bot-turbo).
- endpoint (Optional[str]):
The endpoint for making API requests. If not provided, the default endpoint is used.
- stream (bool):
If set to True, the responses are streamed back as an iterator. If False, a single response is returned.
- retry_count (int):
The number of times to retry the request in case of failure.
- request_timeout (float):
The maximum time (in seconds) to wait for a response from the model.
- backoff_factor (float):
A factor to increase the waiting time between retry attempts.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Additional parameters like temperature will vary depending on the model, please refer to the API documentation. The additional parameters can be passed as follows:
` Embedding().do(texts = ..., temperature = 0.2, top_p = 0.5) `
- batch_do(texts_list: List[List[str]], worker_num: Optional[int] = None, **kwargs: Any) BatchRequestFuture[source]
Batch generate embeddings for a list of input texts using a specified model.
- Parameters:
- texts_list (List[List[str]]):
List of the input text list to generate the embeddings.
- worker_num (Optional[int]):
The number of prompts to process at the same time, default to None, which means this number will be decided dynamically.
- kwargs (Any):
Please refer to Completion.do for other parameters such as model, endpoint, retry_count, etc.
``` response_list = Completion().batch_do([”…”, “…”], worker_num = 10) for response in response_list:
# return QfResponse if succeed, or exception will be raised print(response.result())
# or while response_list.finished_count() != response_list.task_count():
time.sleep(1)
- do(texts: List[str], model: Optional[str] = None, endpoint: Optional[str] = None, stream: bool = False, retry_count: int = 1, request_timeout: float = 60, request_id: Optional[str] = None, backoff_factor: float = 1, **kwargs: Any) Union[QfResponse, Iterator[QfResponse]][source]
Generate embeddings for a list of input texts using a specified model.
- Parameters:
- texts (List[str]):
A list of input texts for which embeddings need to be generated.
- model (Optional[str]):
The name or identifier of the language model to use. If not specified, the default model is used(ERNIE-Bot-turbo).
- endpoint (Optional[str]):
The endpoint for making API requests. If not provided, the default endpoint is used.
- stream (bool):
If set to True, the responses are streamed back as an iterator. If False, a single response is returned.
- retry_count (int):
The number of times to retry the request in case of failure.
- request_timeout (float):
The maximum time (in seconds) to wait for a response from the model.
- backoff_factor (float):
A factor to increase the waiting time between retry attempts.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Additional parameters like temperature will vary depending on the model, please refer to the API documentation. The additional parameters can be passed as follows:
` Embedding().do(texts = ..., temperature = 0.2, top_p = 0.5) `
- class qianfan.resources.FineTune[source]
Bases:
objectClass for FineTune API
- classmethod create_job(job: Dict[str, Any], **kwargs: Any) QfRequest[source]
Create a job for fine-tuning a model.
This function creates a job for fine-tuning a model.
- Parameters:
- job (Dict[str, Any]):
A dictionary containing job details and configurations. The fields are same with the API doc.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/mlmrgo4yx
- classmethod create_task(name: str, description: Optional[str] = None, **kwargs: Any) QfRequest[source]
Create a model fine-tuning task.
This function is used to create a model fine-tuning task. The task can be customized with a name and description.
- Parameters:
- name (str):
The name of the fine-tuning task.
- description (Optional[str]):
An optional description for the fine-tuning task.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/almrgn397
- classmethod get_job(task_id: int, job_id: int, **kwargs: Any) QfRequest[source]
Retrieves a job for model fine-tuning.
This method is responsible for retrieving a job for the specified fine-tuning task and job IDs.
- Parameters:
- task_id (int):
The ID of the task associated with the fine-tuning job.
- job_id (int):
The ID of the fine-tuning job to retrieve.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/wlmrgowee
- classmethod stop_job(task_id: int, job_id: int, **kwargs: Any) QfRequest[source]
Stop a fine-tuning job.
This function allows the stopping of a fine-tuning job associated with a specific task.
- Parameters:
- task_id (int):
The identifier of the task associated with the fine-tuning job.
- job_id (int):
The identifier of the fine-tuning job to be stopped.
- kwargs:
Additional keyword arguments that can be passed to customize the request.
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/2lnlebz15
- class qianfan.resources.Image2Text(model: Optional[str] = None, endpoint: Optional[str] = None, **kwargs: Any)[source]
Bases:
BaseResourceQianFan Image2Text API Resource
- async abatch_do(input_list: List[Tuple[str, str]], worker_num: Optional[int] = None, **kwargs: Any) List[Union[QfResponse, AsyncIterator[QfResponse]]][source]
Async batch generate execute a image2text action on the provided inputs and generate responses.
- Parameters:
- input_list (Tuple(str, str)):
The list user input prompt and base64 encoded image data for which a response is generated.
- worker_num (Optional[int]):
The number of prompts to process at the same time, default to None, which means this number will be decided dynamically.
- kwargs (Any):
Please refer to Plugin.ado for other parameters such as model, endpoint, retry_count, etc.
``` response_list = await Image2Text(endpoint=””).abatch_do([(”…”, “…”),
(”…”, “…”)], worker_num = 10)
- for response in response_list:
# response is QfResponse if succeed, or response will be exception print(response)
- async ado(prompt: str, image: str, model: Optional[str] = None, endpoint: Optional[str] = None, stream: bool = False, retry_count: int = 1, request_timeout: float = 60, request_id: Optional[str] = None, backoff_factor: float = 0, **kwargs: Any) Union[QfResponse, AsyncIterator[QfResponse]][source]
Async execute a image2text action on the provided input prompt and generate responses.
- Parameters:
- prompt (str):
The user input or prompt for which a response is generated.
- image (str):
The user input base64 encoded image data for which a response is generated.
- model (Optional[str]):
The name or identifier of the language model to use.
- endpoint (Optional[str]):
The endpoint for making API requests. If not provided, the default endpoint is used.
- stream (bool):
Whether to stream responses or not.
- retry_count (int):
The number of times to retry the request in case of failure.
- request_timeout (float):
The maximum time (in seconds) to wait for a response from the model.
- backoff_factor (float):
A factor to increase the waiting time between retry attempts.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Additional parameters like temperature will vary depending on the model, please refer to the API documentation. The additional parameters can be passed as follows:
` Image2Text(endpoint="").ado(prompt="", image="", xx=vv) `
- batch_do(input_list: List[Tuple[str, str]], worker_num: Optional[int] = None, **kwargs: Any) BatchRequestFuture[source]
Batch generate execute a image2text action on the provided inputs and generate responses.
- Parameters:
- input_list (Tuple(str, str)):
The list user input prompt and base64 encoded image data for which a response is generated.
- worker_num (Optional[int]):
The number of prompts to process at the same time, default to None, which means this number will be decided dynamically.
- kwargs (Any):
Please refer to Plugin.do for other parameters such as model, endpoint, retry_count, etc.
``` response_list = Image2Text(endpoint=””).batch_do([(”…”, “…”),
(”…”, “…”)], worker_num = 10)
- for response in response_list:
# return QfResponse if succeed, or exception will be raised print(response.result())
# or while response_list.finished_count() != response_list.task_count():
time.sleep(1)
- do(prompt: str, image: str, model: Optional[str] = None, endpoint: Optional[str] = None, stream: bool = False, retry_count: int = 1, request_timeout: float = 60, request_id: Optional[str] = None, backoff_factor: float = 0, **kwargs: Any) Union[QfResponse, Iterator[QfResponse]][source]
Execute a image2text action on the provided input prompt and generate responses.
- Parameters:
- prompt (str):
The user input or prompt for which a response is generated.
- image (str):
The user input base64 encoded image data for which a response is generated.
- model (Optional[str]):
The name or identifier of the language model to use.
- endpoint (Optional[str]):
The endpoint for making API requests. If not provided, the default endpoint is used.
- stream (bool):
Whether to stream responses or not.
- retry_count (int):
The number of times to retry the request in case of failure.
- request_timeout (float):
The maximum time (in seconds) to wait for a response from the model.
- backoff_factor (float):
A factor to increase the waiting time between retry attempts.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Additional parameters like temperature will vary depending on the model, please refer to the API documentation. The additional parameters can be passed as follows:
` Image2Text(endpoint="").do(prompt="", image="", xxx=vvv) `
- class qianfan.resources.Model[source]
Bases:
objectClass for Model Management API
- classmethod create_evaluation_task(name: str, version_info: List[Dict[str, Any]], dataset_id: int, eval_config: Dict[str, Any], description: Optional[str] = None, pending_eval_id: Optional[int] = None, result_dataset_storage_type: DataStorageType = DataStorageType.PublicBos, result_dataset_storage_id: Optional[str] = None, result_dataset_raw_path: Optional[str] = None, **kwargs: Any) QfRequest[source]
Create an evaluation task on model(s) with dataset
- Parameters:
- name (str):
the evaluation name you want to use
- version_info (List[Dict[str, Any]]):
a list of model info which will be evaluated
- dataset_id (int):
dataset’s id for evaluation
- eval_config (Dict[str, Any]):
the detail info about how to conduct this evaluation
- description (Optional[str]):
description about evaluation, default to None.
- pending_eval_id (Optional[int]):
the id of evaluation which doesn’t start yet, you can set this parameter to modify the spec of specific evaluation and start it. Default to None
- result_dataset_storage_type (DataStorageType):
where to place evaluation result dataset, default to DataStorageType.PublicBos
- result_dataset_storage_id (Optional[str]):
user bos id, only used when result_dataset_storage_type is DataStorageType.PrivateBos, default to None.
- result_dataset_raw_path (Optional[str]):
bos path, only used when result_dataset_storage_type is DataStorageType.PrivateBos, default to None.
- **kwargs (Any):
arbitrary arguments
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Hlpbyhl9o
- classmethod detail(model_version_id: int, **kwargs: Any) QfRequest[source]
Retrieve detailed information for a specific model version.
This method is used to fetch detailed information about a particular model version identified by the model_version_id parameter. The information includes various attributes and properties associated with the specified model version.
- Parameters:
- model_version_id (int):
The unique identifier for the model version whose details are to be retrieved.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/ylnljj3ku
- classmethod get_evaluation_info(eval_id: int, **kwargs: Any) QfRequest[source]
Get an evaluation task info
- Parameters:
- eval_id (int):
the id of evaluation you want to check
- **kwargs (Any):
arbitrary arguments
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/wlpbyj1dn
- classmethod get_evaluation_result(eval_id: int, **kwargs: Any) QfRequest[source]
Get the result of an evaluation
- Parameters:
- eval_id (int):
the id of evaluation you want to check
- **kwargs (Any):
arbitrary arguments
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/7lpbyk8fj
- classmethod list(model_id: int, **kwargs: Any) QfRequest[source]
List all versions and source information of a model.
This class method is used to retrieve information about all versions of a specific model, along with their source details.
- Parameters:
- model_id (int):
The unique identifier of the model for which you want to list versions.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clnlizwcs
- classmethod publish(is_new: bool, version_meta: Dict[str, Any], model_name: Optional[str] = None, model_id: Optional[int] = None, tags: Optional[List[str]] = None, **kwargs: Any) QfRequest[source]
Publishes a trained model to the model repository.
This function allows for the publishing of a trained model to a model repository.
- Parameters:
- is_new (bool):
A boolean indicating whether this is a new model to be published.
- version_meta (Dict[str, Any]):
Metadata for the model being published.
- model_name (Optional[str]):
The name of the model to be published (required when is_new is True).
- model_id (Optional[int]):
The ID of the model to be published (required when is_new is False).
- tags (Optional[List[str]]):
A list of tags associated with the model (optional).
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Jlnlm0rdx
- classmethod stop_evaluation_task(eval_id: int, **kwargs: Any) QfRequest[source]
Stop an evaluation task
- Parameters:
- eval_id (int):
the id of evaluation you want to stop
- **kwargs (Any):
arbitrary arguments
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/klpbyl1ea
- class qianfan.resources.Plugin(endpoint: Optional[str] = None, **kwargs: Any)[source]
Bases:
BaseResourceQianFan Plugin API Resource
- async abatch_do(query_list: List[Union[str, QfResponse, List[Dict]]], worker_num: Optional[int] = None, **kwargs: Any) List[Union[QfResponse, AsyncIterator[QfResponse]]][source]
Async batch execute a plugin action on the provided input prompt and generate responses.
- Parameters:
- query_list List[Union[str, QfResponse, List[Dict]]]:
The list user input or prompt for which a response is generated.
- worker_num (Optional[int]):
The number of prompts to process at the same time, default to None, which means this number will be decided dynamically.
- kwargs (Any):
Please refer to Plugin.ado for other parameters such as model, endpoint, retry_count, etc.
``` response_list = await Plugin().abatch_do([…], worker_num = 10) for response in response_list:
# response is QfResponse if succeed, or response will be exception print(response)
- async ado(query: Union[str, QfResponse, List[Dict]], plugins: Optional[List[str]] = None, model: Optional[str] = None, endpoint: Optional[str] = None, stream: bool = False, retry_count: int = 1, request_timeout: float = 60, request_id: Optional[str] = None, backoff_factor: float = 1, **kwargs: Any) Union[QfResponse, AsyncIterator[QfResponse]][source]
Async execute a plugin action on the provided input prompt and generate responses.
- Parameters:
- query Union[str, QfResponse, List[Dict]]:
The user input for which a response is generated.
- plugins (Optional[List[str]]):
A list of plugins to be used.
- model (Optional[str]):
The name or identifier of the language model to use. If not specified, the default model is used(ERNIE-Bot-turbo).
- endpoint (Optional[str]):
The endpoint for making API requests. If not provided, the default endpoint is used.
- stream (bool):
If set to True, the responses are streamed back as an iterator. If False, a single response is returned.
- retry_count (int):
The number of times to retry the request in case of failure.
- request_timeout (float):
The maximum time (in seconds) to wait for a response from the model.
- backoff_factor (float):
A factor to increase the waiting time between retry attempts.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Additional parameters like temperature will vary depending on the model, please refer to the API documentation. The additional parameters can be passed as follows:
` Plugin().do(prompt = ..., temperature = 0.2, top_p = 0.5) `
- batch_do(query_list: List[Union[str, QfResponse, List[Dict]]], worker_num: Optional[int] = None, **kwargs: Any) BatchRequestFuture[source]
Batch generate execute a plugin action on the provided input prompt and generate responses.
- Parameters:
- query_list List[Union[str, QfResponse, List[Dict]]]:
The list user input or prompt for which a response is generated.
- worker_num (Optional[int]):
The number of prompts to process at the same time, default to None, which means this number will be decided dynamically.
- kwargs (Any):
Please refer to Plugin.do for other parameters such as model, endpoint, retry_count, etc.
``` response_list = Plugin().batch_do([”…”, “…”], worker_num = 10) for response in response_list:
# return QfResponse if succeed, or exception will be raised print(response.result())
# or while response_list.finished_count() != response_list.task_count():
time.sleep(1)
- do(query: Union[str, QfResponse, List[Dict]], plugins: Optional[List[str]] = None, model: Optional[str] = None, endpoint: Optional[str] = None, stream: bool = False, retry_count: int = 1, request_timeout: float = 60, request_id: Optional[str] = None, backoff_factor: float = 1, **kwargs: Any) Union[QfResponse, Iterator[QfResponse]][source]
Execute a plugin action on the provided input prompt and generate responses.
- Parameters:
- query Union[str, QfResponse, List[Dict]]:
The user input for which a response is generated.
- plugins (Optional[List[str]]):
A list of plugins to be used.
- model (Optional[str]):
The name or identifier of the language model to use. If not specified, the default model is used(ERNIE-Bot-turbo).
- endpoint (Optional[str]):
The endpoint for making API requests. If not provided, the default endpoint is used.
- stream (bool):
If set to True, the responses are streamed back as an iterator. If False, a single response is returned.
- retry_count (int):
The number of times to retry the request in case of failure.
- request_timeout (float):
The maximum time (in seconds) to wait for a response from the model.
- backoff_factor (float):
A factor to increase the waiting time between retry attempts.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Additional parameters like temperature will vary depending on the model, please refer to the API documentation. The additional parameters can be passed as follows:
` Plugin().do(prompt = ..., temperature = 0.2, top_p = 0.5) `
- class qianfan.resources.Prompt[source]
Bases:
objectClass for Prompt API
- classmethod create(name: str, template: str, identifier: Literal['{}', '{{}}', '[]', '[[]]', '()', '(())'] = '{}', scene: PromptSceneType = PromptSceneType.Text2Text, framework: PromptFrameworkType = PromptFrameworkType.NotUse, variables: Optional[List[str]] = None, label_ids: Optional[List[int]] = None, negative_template: Optional[str] = None, negative_variables: Optional[List[str]] = None, **kwargs: Any) QfRequest[source]
Creates a prompt template.
- Parameters:
- name (str):
A descriptive name for the prompt template.
- template (str):
The main text of the prompt template.
- identifier (Literal[“{}”, “{{}}”, “[]”, “[[]]”, “()”, “(())”]):
The identifier pattern to be used for variable replacement in the template.
- scene (PromptSceneType):
The type of prompt scene, e.g., Text2Text/Text2Image.
- framework (PromptFrameworkType):
The framework to be used for prompt generation.
- variables (Optional[List[str]]):
List of variables used in the template. If not provided, sdk will automatically find variables in the template. The variables only support English, numbers, and underscores (_), and cannot start with a number. They must be between 2 and 30 characters in length.
- label_ids (Optional[List[int]]):
List of label IDs associated with the prompt.
- negative_template (Optional[str]):
An optional negative example template. Only available when scene is Text2Image.
- negative_variables (Optional[List[str]]):
List of variables for the negative example. Only available when scene is Text2Image.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
- Returns:
QfRequest: An object representing the prompt request.
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
Usage: ```python prompt_request = Prompt.create(
name=”MyPrompt”, template=”Generate a sentence with {object}.”, identifier=”{}”, scene=PromptSceneType.Text2Text, framework=PromptFrameworkType.NotUse, variables=[“object”], label_ids=[1, 2], negative_template=”Avoid using {profanity} in the sentence.”, negative_variables=[“profanity”], custom_arg=”value” # Additional keyword arguments can be included.
)
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Hlp7waib4
- classmethod delete(id: int, **kwargs: Any) QfRequest[source]
Deletes a prompt template.
This method is responsible for deleting a prompt template based on the specified template ID.
- Parameters:
- id (int):
The ID of the prompt template to delete.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
- Returns:
- QfRequest:
An instance of the QfRequest class representing the API request.
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Hlp7tri81
- classmethod info(id: int, **kwargs: Any) QfRequest[source]
Renders a prompt template and retrieves template details.
This method is responsible for rendering a prompt template and obtaining details about the template.
- Parameters:
- id (int):
The ID of the prompt template to render.
- kwargs (Any):
The value of the variables to be used for variable replacement in the template.
- Returns:
- QfRequest:
An object representing the request for rendering the prompt template.
- Note:
The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Olp7ysef9
- classmethod list(offset: int = 0, page_size: int = 10, name: Optional[str] = None, label_ids: List[int] = [], type: Optional[PromptType] = None, **kwargs: Any) QfRequest[source]
Retrieves a list of prompt templates.
This method is responsible for retrieving a list of prompt templates based on the specified parameters.
- Parameters:
- offset (int):
The index from which to start retrieving prompt templates. Default is 0.
- page_size (int):
The number of prompt templates to retrieve per page. Default is 10.
- name (Optional[str]):
A filter for prompt templates by name.
- label_ids (List[int]):
A list of label IDs to filter prompt templates.
- type (Optional[PromptType]):
A filter for prompt templates by type.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
- Returns:
- QfRequest:
An object representing the request to retrieve prompt templates.
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/ulp7tycbq
- classmethod list_labels(offset: int = 0, page_size: int = 10, **kwargs: Any) QfRequest[source]
Retrieves a list of labels for prompt templates.
This method is responsible for retrieving a list of labels. Labels provide information about the categories or attributes associated with each template.
- Parameters:
- offset (int):
The offset for paginating through the list of labels. Default is 0.
- page_size (int):
The number of labels to include in each page. Default is 10.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
- Returns:
- QfRequest:
An instance of the QfRequest class representing the API request.
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/zlp7u6inp
- classmethod update(id: int, name: Optional[str] = None, label_ids: Optional[List[int]] = None, template: Optional[str] = None, identifier: Optional[Literal['{}', '{{}}', '[]', '[[]]', '()', '(())']] = None, negative_template: Optional[str] = None, **kwargs: Any) QfRequest[source]
Update information for a prompt template.
This method is responsible for updating various attributes of a prompt template identified by the provided ID.
- Parameters:
- id (int):
The ID of the prompt template to update.
- name (Optional[str]):
The new name for the prompt template.
- label_ids (Optional[List[int]]):
The updated list of label IDs associated with the prompt template.
- template (Optional[str]):
The modified template for the prompt.
- identifier (Optional[Literal[“{}”, “{{}}”, “[]”, “[[]]”, “()”, “(())”]]):
The updated identifier format for the prompt.
- negative_template (Optional[str]):
The revised negative template for the prompt.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
- Returns:
- QfRequest:
An instance of QfRequest representing the update request.
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/plp7tp3kx
- class qianfan.resources.QfMessages[source]
Bases:
objectAn auxiliary class for representing a list of messages in a chat model.
Example usage:
messages = QfMessages() # append a message by str messages.append("Hello!") # send the messages directly resp = qianfan.ChatCompletion().do(messages = messages) # append the response to the messages and continue the conversation messages.append(resp) messages.append("next message", role = QfRole.User) # role is optional
- append(message: Union[str, QfResponse], role: Optional[Union[str, QfRole]] = None) None[source]
Appends a message to the QfMessages object.
- Parameters:
- message (Union[str, QfResponse]):
The message to be appended. It can be a string or a QfResponse object. When the object is a QfResponse object, the role of the message sender will be QfRole.Assistant by default, unless you specify the role using the ‘role’
- role (Optional[Union[str, QfRole]]):
An optional parameter to specify the role of the message sender. If not provided, the function will determine the role based on the existed message.
Example usage can be found in the introduction of this class.
- class qianfan.resources.QfResponse(code: int, headers: ~typing.Dict[str, str] = <factory>, body: ~typing.Dict[str, ~typing.Any] = <factory>, statistic: ~typing.Dict[str, ~typing.Any] = <factory>)[source]
Bases:
MappingResponse from Qianfan API
- body: Dict[str, Any]
The JSON-formatted body of the response.
- code: int
The HTTP status code of the response.
- headers: Dict[str, str]
A dictionary of HTTP headers included in the response.
- statistic: Dict[str, Any]
key: request_latency: request elapsed time in seconds, or received elapsed time
of each response if stream=True
- first_token_latency: first token elapsed time int seconds
only existed in streaming calling
- total_latency: resource elapsed time int seconds, include request, serialization
and the waiting time if rate_limit is set.
- class qianfan.resources.QfRole(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
EnumRole type supported in Qianfan
- Assistant = 'assistant'
- Function = 'function'
- User = 'user'
- class qianfan.resources.Service[source]
Bases:
objectClass for Service API
- classmethod create(model_id: int, model_version_id: int, name: str, uri: str, replicas: int, pool_type: DeployPoolType = DeployPoolType.PrivateResource, description: Optional[str] = None, **kwargs: Any) QfRequest[source]
Create a service for the given model.
This function creates a service associated with the specified model and iteration.
- Parameters:
- model_id (int):
The ID of the model for which the service is to be created.
- model_version_id (int):
The ID of the version of the model.
- name (str):
The name for the created service.
- uri (str):
The URI (Uniform Resource Identifier) for accessing the service.
- replicas (int):
The number of replicas for the service.
- pool_type (int):
The type of pooling for the service (1 for public and 2 for private).
- description (Optional[str], optional):
An optional description for the service.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Plnlmxdgy
- classmethod get(id: int, **kwargs: Any) QfRequest[source]
Retrieve information for a specific service.
This method allows retrieval of information pertaining to a specific service based on its unique identifier.
- Parameters:
- id (int):
The unique identifier for the service.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/llnlmyp8o
- classmethod list(api_type_filter: Optional[List[Union[str, ServiceType]]] = None, **kwargs: Any) QfRequest[source]
list all services.
This method allows calling list API to get all services, including common: preset model services. custom user-deployed model services.
- Parameters:
- api_type_filter (Optional[List[str]]):
- Optional, filter the services by ServiceType.
- Concretely, the value of this parameter can be one or more of:
‘chat’, ‘completions’, ‘embeddings’, ‘text2image’, ‘image2text’
If the value is None, all services will be returned.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Note: The @console_api_request decorator is applied to this method, enabling it to send the generated QfRequest and return a QfResponse to the user.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/4lqoklvr1
- class qianfan.resources.Text2Image(model: Optional[str] = None, endpoint: Optional[str] = None, **kwargs: Any)[source]
Bases:
BaseResourceQianFan Text2Image API Resource
- async abatch_do(prompt_list: List[str], worker_num: Optional[int] = None, **kwargs: Any) List[Union[QfResponse, AsyncIterator[QfResponse]]][source]
Async batch execute a text2image action on the provided input prompt and generate responses.
- Parameters:
- prompt_list (List[str]):
The list user input or prompt for which a response is generated.
- worker_num (Optional[int]):
The number of prompts to process at the same time, default to None, which means this number will be decided dynamically.
- kwargs (Any):
Please refer to Text2Image.ado for other parameters such as model, endpoint, retry_count, etc.
``` response_list = await Text2Image().abatch_do([…], worker_num = 10) for response in response_list:
# response is QfResponse if succeed, or response will be exception print(response)
- async ado(prompt: str, model: Optional[str] = None, endpoint: Optional[str] = None, with_decode: Optional[str] = None, retry_count: int = 1, request_timeout: float = 60, request_id: Optional[str] = None, backoff_factor: float = 0, **kwargs: Any) Union[QfResponse, AsyncIterator[QfResponse]][source]
Async execute a text2image action on the provided input prompt and generate responses.
- Parameters:
- prompt (str):
The user input or prompt for which a response is generated.
- model (Optional[str]):
The name or identifier of the language model to use. If not specified, the default model is used(Stable-Diffusion-XL).
- endpoint (Optional[str]):
The endpoint for making API requests. If not provided, the default endpoint is used.
- with_decode(Optional[str]):
The way to decode data. If not provided, the decode is not used. use “base64” to auto decode from data.
- retry_count (int):
The number of times to retry the request in case of failure.
- request_timeout (float):
The maximum time (in seconds) to wait for a response from the model.
- backoff_factor (float):
A factor to increase the waiting time between retry attempts.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Additional parameters like temperature will vary depending on the model, please refer to the API documentation. The additional parameters can be passed as follows:
` Text2Image().do(prompt = ..., steps=20) `
- batch_do(prompt_list: List[str], worker_num: Optional[int] = None, **kwargs: Any) BatchRequestFuture[source]
Batch generate execute a text2image action on the provided input prompt and generate responses.
- Parameters:
- prompt_list (List[str]):
The list user input or prompt for which a response is generated.
- worker_num (Optional[int]):
The number of prompts to process at the same time, default to None, which means this number will be decided dynamically.
- kwargs (Any):
Please refer to Text2Image.do for other parameters such as model, endpoint, retry_count, etc.
``` response_list = Text2Image().batch_do([”…”, “…”], worker_num = 10) for response in response_list:
# return QfResponse if succeed, or exception will be raised print(response.result())
# or while response_list.finished_count() != response_list.task_count():
time.sleep(1)
- do(prompt: str, model: Optional[str] = None, endpoint: Optional[str] = None, with_decode: Optional[str] = None, retry_count: int = 1, request_timeout: float = 60, request_id: Optional[str] = None, backoff_factor: float = 0, **kwargs: Any) Union[QfResponse, Iterator[QfResponse]][source]
Execute a text2image action on the provided input prompt and generate responses.
- Parameters:
- prompt (str):
The user input or prompt for which a response is generated.
- model (Optional[str]):
The name or identifier of the language model to use. If not specified, the default model is used(Stable-Diffusion-XL).
- endpoint (Optional[str]):
The endpoint for making API requests. If not provided, the default endpoint is used.
- with_decode(Optional[str]):
The way to decode data. If not provided, the decode is not used. use “base64” to auto decode from data.
- retry_count (int):
The number of times to retry the request in case of failure.
- request_timeout (float):
The maximum time (in seconds) to wait for a response from the model.
- backoff_factor (float):
A factor to increase the waiting time between retry attempts.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Additional parameters like temperature will vary depending on the model, please refer to the API documentation. The additional parameters can be passed as follows:
` Text2Image().do(prompt = ..., steps=20) `
- class qianfan.resources.Tokenizer[source]
Bases:
objectClass for Tokenizer API
- classmethod count_tokens(text: str, mode: Literal['local', 'remote'] = 'local', model: str = 'ERNIE-Bot', **kwargs: Any) int[source]
Count the number of tokens in a given text.
- Parameters:
- text (str):
The input text for which tokens need to be counted.
- mode (str, optional):
- local (default):
local SIMULATION (Chinese characters count + English word count * 1.3)
- remote:
use qianfan api to calculate the token count. API will return accurate token count, but only ERNIE-Bot series models are supported.
- model (str, optional):
The name of the model to be used for token counting, which may influence the counting strategy. Default is ‘ERNIE-Bot’.
- kwargs (Any):
Additional keyword arguments that can be passed to customize the request.
Subpackages
- qianfan.resources.auth package
- qianfan.resources.console package
- Submodules
- qianfan.resources.console.consts module
- qianfan.resources.console.data module
DataData.annotate_an_entity()Data.create_bare_dataset()Data.create_data_import_task()Data.create_dataset_augmenting_task()Data.create_dataset_etl_task()Data.create_dataset_export_task()Data.delete_an_entity()Data.delete_dataset()Data.delete_dataset_augmenting_task()Data.delete_dataset_etl_task()Data.get_dataset_aug_task_list()Data.get_dataset_augmenting_task_info()Data.get_dataset_etl_task_info()Data.get_dataset_etl_task_list()Data.get_dataset_export_records()Data.get_dataset_import_error_detail()Data.get_dataset_info()Data.get_dataset_status_in_batch()Data.list_all_entity_in_dataset()Data.release_dataset()
- qianfan.resources.console.finetune module
- qianfan.resources.console.model module
- qianfan.resources.console.prompt module
- qianfan.resources.console.service module
- qianfan.resources.console.utils module
- qianfan.resources.images package
- qianfan.resources.llm package
- qianfan.resources.requestor package
- qianfan.resources.tools package
Submodules
qianfan.resources.http_client module
qianfan.resources.rate_limiter module
Implementation of Rate Limiter
- class qianfan.resources.rate_limiter.RateLimiter(query_per_second: float = 0, **kwargs: Any)[source]
Bases:
objectImplementation of Rate Limiter. They’re different rules between synchronous and asynchronous method using, we recommend only use one of two method within single rate limiter at same time
qianfan.resources.typing module
- class qianfan.resources.typing.QfLLMInfo(endpoint: str, required_keys: ~typing.Set[str] = <factory>, optional_keys: ~typing.Set[str] = <factory>)[source]
Bases:
objectLLM info in SDK
- endpoint: str
- optional_keys: Set[str]
- required_keys: Set[str]
- class qianfan.resources.typing.QfMessages[source]
Bases:
objectAn auxiliary class for representing a list of messages in a chat model.
Example usage:
messages = QfMessages() # append a message by str messages.append("Hello!") # send the messages directly resp = qianfan.ChatCompletion().do(messages = messages) # append the response to the messages and continue the conversation messages.append(resp) messages.append("next message", role = QfRole.User) # role is optional
- append(message: Union[str, QfResponse], role: Optional[Union[str, QfRole]] = None) None[source]
Appends a message to the QfMessages object.
- Parameters:
- message (Union[str, QfResponse]):
The message to be appended. It can be a string or a QfResponse object. When the object is a QfResponse object, the role of the message sender will be QfRole.Assistant by default, unless you specify the role using the ‘role’
- role (Optional[Union[str, QfRole]]):
An optional parameter to specify the role of the message sender. If not provided, the function will determine the role based on the existed message.
Example usage can be found in the introduction of this class.
- class qianfan.resources.typing.QfRequest(method: str, url: str, query: ~typing.Dict[str, str] = <factory>, headers: ~typing.Dict[str, str] = <factory>, json_body: ~typing.Dict[str, ~typing.Any] = <factory>, retry_config: ~qianfan.resources.typing.RetryConfig = <factory>)[source]
Bases:
objectRequest object used in SDK
- headers: Dict[str, str]
- json_body: Dict[str, Any]
- method: str
- query: Dict[str, str]
- requests_args() Dict[str, Any][source]
convert self to args of requests.request() or aiohttp.requests()
- retry_config: RetryConfig
- url: str
- class qianfan.resources.typing.QfResponse(code: int, headers: ~typing.Dict[str, str] = <factory>, body: ~typing.Dict[str, ~typing.Any] = <factory>, statistic: ~typing.Dict[str, ~typing.Any] = <factory>)[source]
Bases:
MappingResponse from Qianfan API
- body: Dict[str, Any]
The JSON-formatted body of the response.
- code: int
The HTTP status code of the response.
- headers: Dict[str, str]
A dictionary of HTTP headers included in the response.
- statistic: Dict[str, Any]
key: request_latency: request elapsed time in seconds, or received elapsed time
of each response if stream=True
- first_token_latency: first token elapsed time int seconds
only existed in streaming calling
- total_latency: resource elapsed time int seconds, include request, serialization
and the waiting time if rate_limit is set.
- class qianfan.resources.typing.QfRole(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
EnumRole type supported in Qianfan
- Assistant = 'assistant'
- Function = 'function'
- User = 'user'
- class qianfan.resources.typing.RetryConfig(retry_count: int = 1, timeout: float = 10, backoff_factor: float = 1, jitter: float = 1, retry_err_codes: ~typing.Set[int] = <factory>)[source]
Bases:
objectThe retry config used in SDK
- backoff_factor: float = 1
- jitter: float = 1
- retry_count: int = 1
- retry_err_codes: Set[int]
- timeout: float = 10