qianfan.common.prompt package
Submodules
qianfan.common.prompt.prompt module
- class qianfan.common.prompt.prompt.Prompt(template: Optional[str] = None, name: Optional[str] = None, id: Optional[str] = None, identifier: Literal['{}', '{{}}', '[]', '[[]]', '()', '(())'] = '{}', variables: Optional[List[str]] = None, labels: List[PromptLabel] = [], type: PromptType = PromptType.User, scene_type: PromptSceneType = PromptSceneType.Text2Text, framework_type: PromptFrameworkType = PromptFrameworkType.NotUse, negative_template: Optional[str] = None, negative_variables: Optional[List[str]] = None, creator_name: Optional[str] = None)[source]
Bases:
HubSerializable- class PromptEvaluateResult(prompt: Prompt, scene: List[Dict[str, Any]], summary: str)[source]
Bases:
objectEvaluation result of a prompt
- scene: List[Dict[str, Any]]
- summary: str
- classmethod base_prompt(prompt: str, background: str = '', additional_data: str = '', output_schema: str = '') str[source]
Generates a base type prompt for language models.
- Parameters:
- prompt (str):
The main text prompt that defines the task or query for the language model.
- background (str, optional):
Additional context or background information to provide more context to the model.
- additional_data (str, optional):
Extra data that can be included to enhance the specificity or details of the prompt.
- output_schema (str, optional):
The desired schema for formatting the output of the language model.
Please refer to the following link for more details about CRISPE prompt.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Zlo55g7t3
- creator_name: Optional[str] = None
- classmethod crispe_prompt(statement: str, capacity: str = '', insight: str = '', personality: str = '', experiment: str = '') str[source]
Generates a CRISPE-type prompt for fine-tuning models.
- Parameters:
- statement (str):
The main task that the model should focus on.
- capacity (str, optional):
Capacity information specifying what role you want the model to play.
- insight (str, optional):
Insights or guidance to provide additional context for the fine-tuning task.
- personality (str, optional):
The output style or personality of the model.
- experiment (str, optional):
The limit of the output range of the model.
Please refer to the following link for more details about CRISPE prompt.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Hlo56qd21
- classmethod evaluate(prompt_list: List[Prompt], scenes: List[Dict[str, Any]], model: Completion, standard: PromptScoreStandard = PromptScoreStandard.Semantic) List[PromptEvaluateResult][source]
Evaluate a list of prompts against specified scenes using the given model.
- Parameters:
- prompt_list (List[“Prompt”]):
A list of prompt templates to be evaluated.
- scenes (List[Dict[str, Any]]):
List of scenes represented as dictionaries containing relevant information. The dict should contain the following keys:
args: A dict containing the variables to be replaced in the prompt.
expected: The expected output of the prompt.
- client (Completion):
An instance of the Completion client.
- standard (PromptScoreStandard, optional):
The scoring standard to be used for evaluating prompts.
- Returns:
- List[PromptEvaluateResult]:
A list of evaluation results, each containing the original prompt, the result of each scene, and a summary string.
Example: result_list = PromptEvaluateResult.evaluate(
prompt_list=[prompt1, prompt2, prompt3], scenes=[{
“args”: {“name”: “Alice”}, “expected”: “Hello, Alice!”
}], client=Completion(model=”ERNIE-Bot-4”), standard=PromptScoreStandard.Semantic
)
- classmethod fewshot_prompt(prompt: str = '', examples: List[Tuple[str, str]] = []) str[source]
Generates a few-shot prompt for model input.
- Parameters:
- prompt (str):
The main prompt that sets the context for what task should the model focus on.
- examples (List[Tuple[str, str]]):
A list of example tuples, where each tuple contains a model input string and its corresponding expected output. These examples help the model understand the desired behavior.
Please refer to the following link for more details about fewshot prompt.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Nlo57dbf4
- framework_type: PromptFrameworkType
- classmethod from_file(path: str, identifier: Literal['{}', '{{}}', '[]', '[[]]', '()', '(())'] = '{}') Prompt[source]
Create a Prompt object from file. The file should only contain the prompt template.
- Parameters:
- path (str):
The path of the prompt file.
- identifier (Literal[“{}”, “{{}}”, “[]”, “[[]]”, “()”, “(())”]):
The identifier of the prompt.
- id: Optional[str] = None
- identifier: Literal['{}', '{{}}', '[]', '[[]]', '()', '(())']
- labels: List[PromptLabel]
- name: Optional[str] = None
- negative_template: Optional[str] = None
- negative_variables: Optional[List[str]] = None
- optimize(optimize_quality: bool = True, simplify_prompt: bool = False, iteration_round: Literal[1, 2] = 1, enable_cot: bool = False, app_id: Optional[int] = None, service_name: Optional[str] = None, **kwargs: Any) Prompt[source]
Optimize a prompt for better performance and effectiveness.
- Parameters:
- optimize_quality (bool):
Flag indicating whether to optimize for prompt quality.
- simplify_prompt (bool):
Flag indicating whether to simplify the prompt structure.
- iteration_round (Literal[1, 2]):
The number of optimization iterations to perform (1 or 2).
- enable_cot (bool):
Flag indicating whether to enable chain of thought.
- app_id (Optional[int]):
Optional application ID for context-specific optimizations.
- service_name (Optional[str]):
Optional service used for optimizing.
- **kwargs (Any):
Additional keyword arguments for future extensibility.
- Returns:
- Prompt:
The optimized Prompt object.
Please refer to the following link for more details about prompt optimization.
API Doc: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/olr8svd33
- render(**kwargs: str) Tuple[str, Optional[str]][source]
Render the prompt with given variables.
- Parameters:
- kwargs (Any):
The value of the variables to be used for variable replacement in the template.
- save_to_file(path: str) None[source]
Save the prompt template to file.
- Parameters:
- path (str):
The path of the prompt file.
- scene_type: PromptSceneType
- set_negative_template(template: str) None[source]
Set the prompt’s negative template. The variables in the template will be extracted.
- Parameters:
- template (str):
The new negative template.
- set_template(template: str) None[source]
Set the prompt’s template. The variables in the template will be extracted.
- Parameters:
- template (str):
The new template.
- template: str
- type: PromptType
- variables: List[str]