DCBrain API¶
DCBrain 类¶
DCBrain 是动态规划型大脑,结合 DSL 技术与有限状态机实现稳定的长程推理能力。
Bases: BaseBrain
动态规划型的大脑,非协程安全!
此封装会结合DSL技术与动态规划技术,来实现一个动态规划型的大脑,从而实现稳定的长程推理能力。
DCBrain会结合有限状态机相关能力实现对思维链的管理与调度。
目前DCBrain有效状态如下:
- BRAIN_STATES.init 主要负责配合Memory进行记忆召回,进而填充上下文
- BRAIN_STATES.planning 主要负责调用default_llm和planning_jinja2_template进行动态规划,生成一个Plan,用于解决问题
- BRAIN_STATES.running 调用TFChainInterpreter进行Plan执行,返回结果
- BRAIN_STATES.succeeded 根据running结果判断是否成功解决,如果成功解决将会向memory提交对应的msg信息
- BRAIN_STATES.failed 一般用于遇到已知异常的时候,判断尝试标记为失败
- BRAIN_STATES.aborted 一般用于遇到未知异常的时候,判断尝试标记为中止
注意:DCBrain的状态机是有限状态机,当前仅支持单一线程运行,不能并发运行多个任务,否则会导致状态混乱。虽然目前支持相关async方法,但也主要 是为了实现对主进程的协程支持,可以释放主线程的IO阻塞,并不意味着可以多线程并发运行。若需要多线程并发运行,请使用多个DCBrain实例。
default_llm
cached
property
¶
default_llm: ChatLLM
DCBrain 的基础 LLM 实例(实例级缓存)。
首次访问时按 default_llm_uri 构造,后续访问复用同一对象——
Neural register/unregister 依赖稳定 id。
plan_llm
cached
property
¶
plan_llm: ChatLLM
Plan LLM 实例(实例级缓存)。优先 default_plan_llm_uri,回退 default_llm_uri。
与 default_llm / validate_llm 是三个独立缓存实例,互不共享
(TFROB-222 mutation 隔离:每次进入 planning 状态前 clear_all_prompts 重配,
单线程 FSM 下安全)。
tags_scope
property
¶
tags_scope: list[str]
获取当前Brain的tags_scope,会从所有子Chain中提取tags,合并后去重
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: 当前Brain的tags_scope |
model_post_init ¶
model_post_init(__context: Any) -> None
模型初始化后的处理方法,用于在模型初始化完成后,进行一些额外的操作
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
__context
|
Any
|
传入的上下文信息 |
required |
Source code in tfrobot/brain/dc_brain.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | |
validate_token_usage ¶
validate_token_usage(brain_result: BrainResult) -> None
判断BrainResult中的Token使用未超过当前Brain的限制
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
brain_result
|
BrainResult
|
BrainResult |
required |
Source code in tfrobot/brain/dc_brain.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |
construct_plan_chain ¶
construct_plan_chain(
current_suggestion: Optional[str] = None,
) -> Chain
使用当前的default_llm配合 PlanPrompt和BrainContext 动态构建一个可以用于规划Plan的Chain
需要注意构建plan_chain的时候没有动态注册neural。因为此时应该避免工具的注入导致其plan的上下文混乱,虽说neural不直接提供工具, 但是如果一个Chain注册到Neural,在LLM执行时,会在发现没有工具可用时动态召回工具。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_suggestion
|
Optional[str]
|
上一轮 validate 的驳回建议,注入模板供 LLM 参考 |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Chain |
Chain
|
返回的Chain |
Source code in tfrobot/brain/dc_brain.py
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 | |
prepare_init ¶
prepare_init(event_data: TFEventData) -> None
准备init状态的处理。主要是设置当前的PlanChain
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
635 636 637 638 639 640 641 642 | |
async_prepare_init
async
¶
async_prepare_init(event_data: TFEventData) -> None
准备init状态的处理。主要是设置当前的PlanChain
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
644 645 646 647 648 649 650 651 | |
condition_init ¶
condition_init(event_data: TFEventData) -> bool
判断是否满足init状态的条件
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
是否满足init状态的条件 |
Source code in tfrobot/brain/dc_brain.py
653 654 655 656 657 658 659 660 661 662 663 | |
async_condition_init
async
¶
async_condition_init(event_data: TFEventData) -> bool
判断是否满足init状态的条件
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
是否满足init状态的条件 |
Source code in tfrobot/brain/dc_brain.py
665 666 667 668 669 670 671 672 673 674 675 | |
before_init ¶
before_init(event_data: TFEventData) -> None
init状态之前的处理。主要
- 调用Memory进行内容召回并且丰富上下文
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
677 678 679 680 681 682 683 684 685 686 | |
async_before_init
async
¶
async_before_init(event_data: TFEventData) -> None
init状态之前的处理。主要
- 调用Memory进行内容召回并且丰富上下文
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
688 689 690 691 692 693 694 695 696 697 | |
on_enter_init ¶
on_enter_init(event_data: TFEventData) -> None
- 将当前Brain中的Chains相关设置添加到current_input.additional_info中去
- 将当前Brian中可用Tags注入到current_input.additional_info中去
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
状态机过程数据 |
required |
Source code in tfrobot/brain/dc_brain.py
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 | |
on_aenter_init
async
¶
on_aenter_init(event_data: TFEventData) -> None
进入init状态时的操作(异步版本)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
状态机过程数据 |
required |
Source code in tfrobot/brain/dc_brain.py
742 743 744 745 746 747 748 749 | |
after_init ¶
after_init(event_data: TFEventData) -> None
init状态之后的处理。主要是设置当前的PlanChain
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
751 752 753 754 755 756 757 758 | |
async_after_init
async
¶
async_after_init(event_data: TFEventData) -> None
init状态之后的处理。主要是设置当前的PlanChain
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
760 761 762 763 764 765 766 767 | |
prepare_plan ¶
prepare_plan(event_data: TFEventData) -> None
准备plan状态的处理。
在准备过程中动态判断是否有:event_data.brain_intermediate.context.current_suggestion 如果有完善建议,则需要重新将current_input与current_suggestion连接
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 | |
async_prepare_plan
async
¶
async_prepare_plan(event_data: TFEventData) -> None
准备plan状态的处理。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 | |
condition_plan ¶
condition_plan(event_data: TFEventData) -> bool
判断是否满足plan状态的条件
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
是否满足plan状态的条件 |
Source code in tfrobot/brain/dc_brain.py
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 | |
async_condition_plan
async
¶
async_condition_plan(event_data: TFEventData) -> bool
判断是否满足plan状态的条件
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
是否满足plan状态的条件 |
Source code in tfrobot/brain/dc_brain.py
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 | |
before_plan ¶
before_plan(event_data: TFEventData) -> None
如果当前有Plan与Exec的历史,将其追加到intermediate_plans中
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 | |
async_before_plan
async
¶
async_before_plan(event_data: TFEventData) -> None
如果当前有Plan与Exec的历史,将其追加到intermediate_plans中
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 | |
on_enter_planning ¶
on_enter_planning(event_data: TFEventData) -> None
进入planning状态时的处理:验证用量 + 构建上下文 + 运行 LLM 生成计划
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 | |
on_aenter_planning
async
¶
on_aenter_planning(event_data: TFEventData) -> None
进入planning状态时的处理(异步版本):验证用量 + 构建上下文 + 运行 LLM 生成计划
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 | |
after_plan ¶
after_plan(event_data: TFEventData) -> None
plan 转换后处理(no-op,核心逻辑已迁移至 on_enter_planning)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1005 1006 1007 1008 1009 1010 1011 1012 | |
async_after_plan
async
¶
async_after_plan(event_data: TFEventData) -> None
plan 转换后处理(no-op,核心逻辑已迁移至 on_aenter_planning)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1014 1015 1016 1017 1018 1019 1020 1021 | |
prepare_exec_plan ¶
prepare_exec_plan(event_data: TFEventData) -> None
准备exec_plan状态的处理。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1023 1024 1025 1026 1027 1028 1029 1030 | |
async_prepare_exec_plan
async
¶
async_prepare_exec_plan(event_data: TFEventData) -> None
准备exec_plan状态的处理。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1032 1033 1034 1035 1036 1037 1038 1039 | |
condition_exec_plan ¶
condition_exec_plan(event_data: TFEventData) -> bool
判断是否满足exec_plan状态的条件
当前有未执行的计划
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
是否满足exec_plan状态的条件 |
Source code in tfrobot/brain/dc_brain.py
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 | |
async_condition_exec_plan
async
¶
async_condition_exec_plan(event_data: TFEventData) -> bool
判断是否满足exec_plan状态的条件
当前有未执行的计划
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
是否满足exec_plan状态的条件 |
Source code in tfrobot/brain/dc_brain.py
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 | |
before_exec_plan ¶
before_exec_plan(event_data: TFEventData) -> None
执行计划之前的处理
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1075 1076 1077 1078 1079 1080 1081 1082 | |
async_before_exec_plan
async
¶
async_before_exec_plan(event_data: TFEventData) -> None
执行计划之前的处理
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1084 1085 1086 1087 1088 1089 1090 1091 | |
on_enter_running ¶
on_enter_running(event_data: TFEventData) -> None
进入running状态时的操作:解析并执行 Plan
使用 interpreter.evaluate() 获取 ConsoleResult,其中: - TFLLMInterrupt / TFUserInterruptError / TFUserNewInputError 会穿透 raise(不可恢复中断) - TFInterpreterError / TFChainError 等执行异常由解释器内部消化,通过 meta.success=False 标记
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 | |
on_aenter_running
async
¶
on_aenter_running(event_data: TFEventData) -> None
进入running状态时的操作(异步版本):解析并执行 Plan
使用 interpreter.aevaluate() 获取 ConsoleResult,其中: - TFLLMInterrupt / TFUserInterruptError / TFUserNewInputError 会穿透 raise(不可恢复中断) - TFInterpreterError / TFChainError 等执行异常由解释器内部消化,通过 meta.success=False 标记
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 | |
after_exec_plan ¶
after_exec_plan(event_data: TFEventData) -> None
exec_plan 转换后处理(no-op,核心逻辑已迁移至 on_enter_running)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
|
required |
Source code in tfrobot/brain/dc_brain.py
1217 1218 1219 1220 1221 1222 1223 1224 | |
async_after_exec_plan
async
¶
async_after_exec_plan(event_data: TFEventData) -> None
exec_plan 转换后处理(no-op,核心逻辑已迁移至 on_aenter_running)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
|
required |
Source code in tfrobot/brain/dc_brain.py
1226 1227 1228 1229 1230 1231 1232 1233 | |
construct_validate_chain ¶
construct_validate_chain(
plan: str,
result: str,
rejection_history: Optional[
Sequence[tuple[str, str, Optional[str]]]
] = None,
) -> Chain
构建用户校验当前任务是否完成的思维链
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plan
|
str
|
当前的计划 |
required |
result
|
str
|
当前的执行结果 |
required |
rejection_history
|
Optional[Sequence[tuple[str, str, Optional[str]]]]
|
历史驳回记录列表, 每项为 (plan, result, suggestion),用于让 validate LLM 了解之前的尝试与驳回原因 |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Chain |
Chain
|
用户校验当前任务是否完成的思维链 |
Source code in tfrobot/brain/dc_brain.py
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 | |
prepare_succeed ¶
prepare_succeed(event_data: TFEventData) -> None
准备succeed状态的处理。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1341 1342 1343 1344 1345 1346 1347 1348 | |
async_prepare_succeed
async
¶
async_prepare_succeed(event_data: TFEventData) -> None
准备succeed状态的处理。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1350 1351 1352 1353 1354 1355 1356 1357 | |
condition_succeed ¶
condition_succeed(event_data: TFEventData) -> bool
判断是否可以进入成功状态
通过 current_result.meta.success 判断解释器执行是否成功, 成功后再调用 validate_chain 验证结果是否充分回应了用户问题。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
状态机过程数据 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
是否可以进入成功状态 |
Source code in tfrobot/brain/dc_brain.py
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 | |
async_condition_succeed
async
¶
async_condition_succeed(event_data: TFEventData) -> bool
判断是否可以进入成功状态
通过 current_result.meta.success 判断解释器执行是否成功, 成功后再调用 validate_chain 验证结果是否充分回应了用户问题。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
状态机过程数据 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
是否可以进入成功状态 |
Source code in tfrobot/brain/dc_brain.py
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 | |
before_succeed ¶
before_succeed(event_data: TFEventData) -> None
成功状态之前的处理
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1463 1464 1465 1466 1467 1468 1469 1470 | |
async_before_succeed
async
¶
async_before_succeed(event_data: TFEventData) -> None
成功状态之前的处理
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1472 1473 1474 1475 1476 1477 1478 1479 | |
on_enter_succeeded ¶
on_enter_succeeded(event_data: TFEventData) -> None
进入succeeded状态时的操作
- 如果当前会话有缓存未完成的Plan与Result,进行清理
注意:消息持久化(commit)由 run() / async_run() 统一负责,此处不重复提交。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
状态机过程数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 | |
on_aenter_succeeded
async
¶
on_aenter_succeeded(event_data: TFEventData) -> None
进入succeeded状态时的操作(异步版本)
注意:消息持久化(commit)由 run() / async_run() 统一负责,此处不重复提交。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
状态机过程数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 | |
after_succeed ¶
after_succeed(event_data: TFEventData) -> None
成功状态之后的处理
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1522 1523 1524 1525 1526 1527 1528 1529 | |
async_after_succeed
async
¶
async_after_succeed(event_data: TFEventData) -> None
成功状态之后的处理
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1531 1532 1533 1534 1535 1536 1537 1538 | |
prepare_fail ¶
prepare_fail(event_data: TFEventData) -> None
准备failed状态的处理。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1540 1541 1542 1543 1544 1545 1546 1547 | |
async_prepare_fail
async
¶
async_prepare_fail(event_data: TFEventData) -> None
准备failed状态的处理。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1549 1550 1551 1552 1553 1554 1555 1556 | |
condition_fail ¶
condition_fail(event_data: TFEventData) -> bool
判断是否满足失败状态的条件。Fail状态会生成一条消息记录到当前的conversation中。而Abort不会。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
是否满足失败状态的条件 |
Source code in tfrobot/brain/dc_brain.py
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 | |
async_condition_fail
async
¶
async_condition_fail(event_data: TFEventData) -> bool
判断是否满足失败状态的条件。Fail状态会生成一条消息记录到当前的conversation中。而Abort不会。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
是否满足失败状态的条件 |
Source code in tfrobot/brain/dc_brain.py
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 | |
before_fail ¶
before_fail(event_data: TFEventData) -> None
失败状态之前的处理
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1584 1585 1586 1587 1588 1589 1590 1591 | |
async_before_fail
async
¶
async_before_fail(event_data: TFEventData) -> None
失败状态之前的处理
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1593 1594 1595 1596 1597 1598 1599 1600 | |
on_enter_failed ¶
on_enter_failed(event_data: TFEventData) -> None
进入失败状态时的操作
- 生成一条错误消息用于提示用户
注意:消息持久化(commit)由 run() / async_run() 统一负责,此处不重复提交。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
状态机过程数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 | |
on_aenter_failed
async
¶
on_aenter_failed(event_data: TFEventData) -> None
进入失败状态时的操作(异步版本)
- 生成一条错误消息用于提示用户
注意:消息持久化(commit)由 run() / async_run() 统一负责,此处不重复提交。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
状态机过程数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 | |
after_fail ¶
after_fail(event_data: TFEventData) -> None
失败状态之后的处理
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1666 1667 1668 1669 1670 1671 1672 1673 | |
async_after_fail
async
¶
async_after_fail(event_data: TFEventData) -> None
失败状态之后的处理
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1675 1676 1677 1678 1679 1680 1681 1682 | |
prepare_abort ¶
prepare_abort(event_data: TFEventData) -> None
准备abort状态的处理。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1684 1685 1686 1687 1688 1689 1690 1691 | |
async_prepare_abort
async
¶
async_prepare_abort(event_data: TFEventData) -> None
准备abort状态的处理。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1693 1694 1695 1696 1697 1698 1699 1700 | |
condition_abort ¶
condition_abort(event_data: TFEventData) -> bool
判断是否满足abort状态的条件
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
是否满足abort状态的条件 |
Source code in tfrobot/brain/dc_brain.py
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 | |
async_condition_abort
async
¶
async_condition_abort(event_data: TFEventData) -> bool
判断是否满足abort状态的条件
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
是否满足abort状态的条件 |
Source code in tfrobot/brain/dc_brain.py
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 | |
before_abort ¶
before_abort(event_data: TFEventData) -> None
abort状态之前的处理
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1728 1729 1730 1731 1732 1733 1734 1735 | |
async_before_abort
async
¶
async_before_abort(event_data: TFEventData) -> None
abort状态之前的处理
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1737 1738 1739 1740 1741 1742 1743 1744 | |
on_enter_aborted ¶
on_enter_aborted(event_data: TFEventData) -> None
进入abort状态时的操作
注意:消息持久化(commit)由 run() / async_run() 统一负责,此处不重复提交。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
状态机过程数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 | |
on_aenter_aborted
async
¶
on_aenter_aborted(event_data: TFEventData) -> None
进入abort状态时的操作(异步版本)
注意:消息持久化(commit)由 run() / async_run() 统一负责,此处不重复提交。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
状态机过程数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 | |
after_abort ¶
after_abort(event_data: TFEventData) -> None
abort状态之后的处理
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1786 1787 1788 1789 1790 1791 1792 1793 | |
async_after_abort
async
¶
async_after_abort(event_data: TFEventData) -> None
abort状态之后的处理
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
TFEventData
|
事件数据 |
required |
Source code in tfrobot/brain/dc_brain.py
1795 1796 1797 1798 1799 1800 1801 1802 | |
commit_message ¶
commit_message(msg: UserAndAssMsg) -> None
提交消息
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
UserAndAssMsg
|
消息 |
required |
Source code in tfrobot/brain/dc_brain.py
1817 1818 1819 1820 1821 1822 1823 1824 | |
async_commit_message
async
¶
async_commit_message(msg: UserAndAssMsg) -> None
提交消息
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
UserAndAssMsg
|
消息 |
required |
Source code in tfrobot/brain/dc_brain.py
1826 1827 1828 1829 1830 1831 1832 1833 | |
connect_to_neural ¶
connect_to_neural(neural: Neural) -> None
实现NeuralProtocol协议,向Neural注册自己
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neural
|
Neural
|
Neural实例 |
required |
Source code in tfrobot/brain/dc_brain.py
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 | |
disconnect_from_neural ¶
disconnect_from_neural(neural: Neural) -> None
实现NeuralProtocol协议,从Neural注销自己
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neural
|
Neural
|
Neural实例 |
required |
Source code in tfrobot/brain/dc_brain.py
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 | |
辅助函数¶
构造新的Chain的全局私有函数。
此函数会清空default_llm的所有prompt并重新设置,因此在default_llm中配置prompt是没有意义的。
注意:Chain仅支持纯文本或字典类型的JsonSchema返回。如需返回list/int/float等类型, 请使用字典包装,如:{"result": [1, 2, 3]} 而不是直接 [1, 2, 3]。
如果提供了response_example,本函数在构建Chain的时候会自动向SystemPrompt中添加JSON示例相关Prompt。因此调用方准备Prompt可以不用刻意准备JSON生成引导了。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default_llm
|
ChatLLM
|
Chain使用的基础LLM |
required |
sys_additional_prompt
|
AdditionalInfoPrompt | list[AdditionalInfoPrompt] | None
|
动态构建的SysPrompt,用于动态构建Chain, 强调当前Chain的目标与上下文环境 |
None
|
before_input_prompt
|
AdditionalInfoPrompt | list[AdditionalInfoPrompt] | None
|
动态构建的SysPrompt,用于动态构建Chain, 强调当前Chain的目标与上下文环境 |
None
|
neural
|
Optional[Neural]
|
Neural实例,如果构建处有身处于Neural环境中,尽量传递此参数 |
None
|
max_iterations
|
int
|
Chain的最大迭代次数。默认为10 |
10
|
max_tokens
|
int
|
Chain的最大token数。默认为128,000 |
128000
|
response_format
|
Optional[Union[dict, LLMResponseFormat]]
|
Chain的响应格式。一个符合JsonSchema的字典 |
None
|
response_example
|
Optional[Union[dict, LLMResponseFormat]]
|
Chain的响应示例。一个符合JsonSchema的字典 |
None
|
inject_trace
|
bool | None
|
设置LLM的inject_trace。None表示不修改(保持LLM默认值) |
None
|
reformat_input_prompt
|
list[BasePrompt] | None
|
设置LLM的reformat_input_prompt |
None
|
enable_trace_intermediate
|
bool | None
|
设置Chain的enable_trace_intermediate。None表示不修改(保持默认值) |
None
|
**kwargs
|
Any
|
传递给Chain构造函数的额外参数 |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Chain |
Chain
|
构造的Chain实例。Chain中的llm仅有可能包括system_msg_prompt,其它的prompt均已清空,可以自行继续设定。 |
Source code in tfrobot/brain/dc_brain.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |