Python Script/Function
Where it can appear
| Location | Python ChatTree | Xmind ChatTree |
|---|---|---|
| "#activity#execute_script" node | function name or lambda expression of the "function" attribute | single-line/multi-line script starting from the second line |
| "#multi_turn_interact#" node | function name or lambda expression of the "execute_function" attribute | single-line/multi-line script of the "execute_script" attribute |
| "on_infoitem_change_trigger" attribute of "#start#" node | function name or lambda expression of "function" field | single-line script after each line "-->" |
| "#condition#script" node | name of function that returns bool value or lambda expression that returns bool value of the "function" attribute | Single-line/multi-line bool expression script starting from the second line |
| The "dynamic_reference_information" attribute of the "#start#" node | name of function that returns str value or lambda expression that returns str value of the "function" field | single-line str expression script after each line of "-->" |
| "#start#" node's "intent_trigger" attribute | "function" field's function name or lambda expression | single-line script after each line "-->" |
| "#single_turn_interact#" node's "execute_script_before_asking" attribute | function name or lambda expression | (None) |
| "before_execution" and "after_execution" attributes of all nodes | function name or lambda expression (see "IDE debugging" for details) | (None) |
Xmind ChatTree: script
- The script is a piece of restricted Python code that cannot contain keywords such as import, open, globals, locals, exit, quit, input, etc.
- The current dialogue context environment variable
ctxcan be used in the script. For details, see the following content.
Python ChatTree: function
- The function must have and only one parameter
ctx, the current dialogue context environment variable. For details, see the following content
Use InfoItems in Scripts/Functions
- [Get the status of the InfoItem] Use
ctx["{...}"].state()to get the status of the InfoItem (here{...}is the name of the InfoItem, the same below):- Return value is -1: Indicates that the InfoItem has no value yet. For user input InfoItems, it may be a node that has not yet been executed to assign a value to the InfoItem ("#single_turn_interact#", "#multi_turn_interact#", "#activity#execute_script", etc.), or it may be the effect of nodes such as "#activity#clear_info", "#activity#ask_again" etc., it is also possible to use the following method of "Delete the value of the InfoItem"
- Return value is 0: Indicates that the InfoItem value is
None. For the user input InfoItem, it is the execution of the "#single_turn_interact#" node but the user refuses to provide it (such as the user replying "unclear", "not convenient to say", etc.), or it may be the effect of the "#activity#skip_asking" node, it is also possible to directly assign None to the InfoItem in a script or function - Return value is 1: Indicates that the InfoItem has a value and is not
None. For the user input InfoItem, the execution reaches the "#single_turn_interact#" or "#multi_turn_interact#" node and the user provides a valid answer, it is also possible to directly assign a value to the InfoItem in a script or function
- [Get the value of the InfoItem] Use
ctx["{...}"].as_str()orctx["{...}"].as_bool()orctx["{...}"].as_num()orctx["{...}"].as_json()to get the value of the InfoItem (only usectx["{...}"]an error will be reported when referenced as a right value), andNonewill be returned when the status of the InfoItem is 0 or -1. The value of the InfoItem will be of typestrwhen actually stored, the reason for this is because the information extracted from the user's utterance (user input InfoItem) is first in text form regardless of the data type (at the same time, the code definition InfoItem and the system InfoItem also use the same storage form, see "InfoItem"), the system will be based onas_str()/as_bool()/as_num()/as_json()intelligently converts the value into the corresponding data type and returns it (whereas_num()andas_bool()are judged based on rules, if you are not sure or a problem has occurred, you can judge by yourself after extracting the value based onas_str()) - [Assign values to InfoItems] Use
ctx["{...}"] = ...to assign values to InfoItems (but note that=here cannot be+=or-=). If the assigned value isNone, it will be stored asNone, otherwise it will be intelligently converted tostrfor storage (the reason is the same as above) - [Delete the value of the InfoItem] Use
del ctx["{...}"]to clear the value of the InfoItem, that is, the status of the InfoItem becomes -1 - [Value of json InfoItem] For InfoItems of dict/list data type, json format strings are actually used to store them (see the "json" InfoItem modifier in "Node - #single_turn_interact#" and "Node - #multi_turn_interact#"), but there are special treatments as follows:
- When using
ctx["{...}"] = ...to assign a value, if it is a dict value, the system will automatically wrap the value into a list value with only one member outside and then assign the value (because this type of InfoItem is mainly used to store table information) - When using
ctx["{...}"].as_json()to refer to its value, the outermost layer is definitely a list (even if there is only one list member) - When extracting this InfoItem from user input, if the user does not provide the content of some fields, it may be one of the following situations:
- The key of this field does not exist
- The key of this field exists, but the value is an empty string or a string that means empty (such as "none", "nothing", etc.)
- When using
- [System InfoItems] The above InfoItems (
{...}) can also be system InfoItems (i.e.{_..._}), but do not assign or clear system InfoItems at will, otherwise it may cause system errors
Other available ctx methods and properties
- You can use
ctx.get_chat_records()in the script to obtain the current dialogue records as a list variable (the first one is the system output, and the last one is the current user input). The format of the return value is as follows:[\{"role":"assistant", "content":"..."\}, \{"role":"user", "content":"..."\}, \{"role":"assistant", "content":"..."\}, ..., \{"role":"user", "content":"..."\}] - You can use
ctx.get_infoitems()andctx.get_infoitems_before_ask_again()in the script to respectively obtain the dict variable of the currently assigned InfoItem and the dict variable of the InfoItem before "#actvity#ask_again", where the key of dict is the name of the InfoItem. - You can use
get_offset_date(year_offset, month_offset, day_offset)in the script to get the current date plus the offset. The return value format is "YYYY-MM-DD", where year_offset, month_offset, and day_offset can be positive integers, zero, and negative integers, which are the offsets of the year, month, and day respectively. - The script can use
ctx.script_debug_info(s)to output debugging information. The relevant debugging information can be viewed in the system execution trace information on the dialog test page (see "Chat Test"), or obtained through the "Get execution trace" API interface (see "API")
Others
- In the Xmind ChatTree, when the script calls the function in the Python code file pointed to by the "code file" attribute of the "#start#" node, the
ctxvariable must be passed in as a parameter so that the code file can be processed as above (ctx must also be used as the only parameter when defining related functions in the Python ChatTree) - In the Xmind ChatTree, if you want to exit the execution of the Python script midway, just execute
ctx.exit_script()(this function cannot be used in the Python ChatTree, just return directly in the Python ChatTree) - In the Xmind ChatTree, all Python scripts (excluding code files) have been
import os, sys, json, datetime, time, math, random, copy, re, requests, urlliband other commonly used Python packages. Just use the package name and the function name in the package to call (you can import it yourself in the Python ChatTree)