46
46
EMBEDDINGS_DB_PATH ,
47
47
NPCSH_EMBEDDING_MODEL ,
48
48
NPCSH_EMBEDDING_PROVIDER ,
49
+ NPCSH_DEFAULT_MODE ,
49
50
NPCSH_REASONING_MODEL ,
50
51
NPCSH_REASONING_PROVIDER ,
51
52
NPCSH_IMAGE_GEN_MODEL ,
54
55
NPCSH_VISION_MODEL ,
55
56
NPCSH_VISION_PROVIDER ,
56
57
chroma_client ,
58
+ available_reasoning_models ,
59
+ available_chat_models ,
57
60
)
58
61
59
62
from .stream import (
@@ -444,7 +447,6 @@ def get_conversation(
444
447
445
448
def execute_llm_question (
446
449
command : str ,
447
- command_history : Any ,
448
450
model : str = NPCSH_CHAT_MODEL ,
449
451
provider : str = NPCSH_CHAT_PROVIDER ,
450
452
api_url : str = NPCSH_API_URL ,
@@ -528,13 +530,11 @@ def execute_llm_question(
528
530
# print(f"LLM response: {output}")
529
531
# print(f"Messages: {messages}")
530
532
# print("type of output", type(output))
531
- command_history .add_command (command , [], output , location )
532
533
return {"messages" : messages , "output" : output }
533
534
534
535
535
536
def execute_llm_command (
536
537
command : str ,
537
- command_history : Any ,
538
538
model : Optional [str ] = None ,
539
539
provider : Optional [str ] = None ,
540
540
api_url : str = NPCSH_API_URL ,
@@ -550,7 +550,7 @@ def execute_llm_command(
550
550
This function executes an LLM command.
551
551
Args:
552
552
command (str): The command to execute.
553
- command_history (Any): The command history.
553
+
554
554
Keyword Args:
555
555
model (Optional[str]): The model to use for executing the command.
556
556
provider (Optional[str]): The provider to use for executing the command.
@@ -654,6 +654,8 @@ def execute_llm_command(
654
654
655
655
{ context }
656
656
"""
657
+ messages .append ({"role" : "user" , "content" : prompt })
658
+ # print(messages, stream)
657
659
if stream :
658
660
response = get_stream (
659
661
messages ,
@@ -663,6 +665,7 @@ def execute_llm_command(
663
665
api_key = api_key ,
664
666
npc = npc ,
665
667
)
668
+ return response
666
669
667
670
else :
668
671
response = get_llm_response (
@@ -677,7 +680,6 @@ def execute_llm_command(
677
680
output = response .get ("response" , "" )
678
681
679
682
# render_markdown(output)
680
- command_history .add_command (command , subcommands , output , location )
681
683
682
684
return {"messages" : messages , "output" : output }
683
685
except subprocess .CalledProcessError as e :
@@ -734,7 +736,6 @@ def execute_llm_command(
734
736
735
737
attempt += 1
736
738
737
- command_history .add_command (command , subcommands , "Execution failed" , location )
738
739
return {
739
740
"messages" : messages ,
740
741
"output" : "Max attempts reached. Unable to execute the command successfully." ,
@@ -743,9 +744,10 @@ def execute_llm_command(
743
744
744
745
def check_llm_command (
745
746
command : str ,
746
- command_history : Any ,
747
747
model : str = NPCSH_CHAT_MODEL ,
748
748
provider : str = NPCSH_CHAT_PROVIDER ,
749
+ reasoning_model : str = NPCSH_REASONING_MODEL ,
750
+ reasoning_provider : str = NPCSH_REASONING_PROVIDER ,
749
751
api_url : str = NPCSH_API_URL ,
750
752
api_key : str = None ,
751
753
npc : Any = None ,
@@ -760,7 +762,6 @@ def check_llm_command(
760
762
This function checks an LLM command.
761
763
Args:
762
764
command (str): The command to check.
763
- command_history (Any): The command history.
764
765
Keyword Args:
765
766
model (str): The model to use for checking the command.
766
767
provider (str): The provider to use for checking the command.
@@ -771,6 +772,21 @@ def check_llm_command(
771
772
Any: The result of checking the LLM command.
772
773
"""
773
774
775
+ ENTER_REASONING_FLOW = False
776
+ if NPCSH_DEFAULT_MODE == "reasoning" :
777
+ ENTER_REASONING_FLOW = True
778
+ if model in available_reasoning_models :
779
+ print (
780
+ """
781
+ Model provided is a reasoning model, defaulting to non reasoning model for
782
+ ReAct choices then will enter reasoning flow
783
+ """
784
+ )
785
+ reasoning_model = model
786
+ reasoning_provider = provider
787
+
788
+ model = NPCSH_CHAT_MODEL
789
+ provider = NPCSH_CHAT_PROVIDER
774
790
if messages is None :
775
791
messages = []
776
792
@@ -799,6 +815,7 @@ def check_llm_command(
799
815
800
816
Available tools:
801
817
"""
818
+
802
819
if npc .all_tools_dict is None :
803
820
prompt += "No tools available."
804
821
else :
@@ -899,14 +916,15 @@ def check_llm_command(
899
916
900
917
# Proceed according to the action specified
901
918
action = response_content_parsed .get ("action" )
902
-
919
+ explanation = response_content [ "explanation" ]
903
920
# Include the user's command in the conversation messages
921
+ print (f"action chosen: { action } " )
922
+ print (f"explanation given: { explanation } " )
904
923
905
924
if action == "execute_command" :
906
925
# Pass messages to execute_llm_command
907
926
result = execute_llm_command (
908
927
command ,
909
- command_history ,
910
928
model = model ,
911
929
provider = provider ,
912
930
api_url = api_url ,
@@ -916,6 +934,8 @@ def check_llm_command(
916
934
retrieved_docs = retrieved_docs ,
917
935
stream = stream ,
918
936
)
937
+ if stream :
938
+ return result
919
939
920
940
output = result .get ("output" , "" )
921
941
messages = result .get ("messages" , messages )
@@ -924,10 +944,10 @@ def check_llm_command(
924
944
elif action == "invoke_tool" :
925
945
tool_name = response_content_parsed .get ("tool_name" )
926
946
# print(npc)
947
+
927
948
result = handle_tool_call (
928
949
command ,
929
950
tool_name ,
930
- command_history ,
931
951
model = model ,
932
952
provider = provider ,
933
953
api_url = api_url ,
@@ -944,19 +964,26 @@ def check_llm_command(
944
964
return {"messages" : messages , "output" : output }
945
965
946
966
elif action == "answer_question" :
947
- result = execute_llm_question (
948
- command ,
949
- command_history ,
950
- model = model ,
951
- provider = provider ,
952
- api_url = api_url ,
953
- api_key = api_key ,
954
- messages = messages ,
955
- npc = npc ,
956
- retrieved_docs = retrieved_docs ,
957
- stream = stream ,
958
- images = images ,
959
- )
967
+
968
+ if ENTER_REASONING_FLOW :
969
+ print ("entering reasoning flow" )
970
+ result = enter_reasoning_human_in_the_loop (
971
+ messages , reasoning_model , reasoning_provider
972
+ )
973
+ else :
974
+ result = execute_llm_question (
975
+ command ,
976
+ model = model ,
977
+ provider = provider ,
978
+ api_url = api_url ,
979
+ api_key = api_key ,
980
+ messages = messages ,
981
+ npc = npc ,
982
+ retrieved_docs = retrieved_docs ,
983
+ stream = stream ,
984
+ images = images ,
985
+ )
986
+
960
987
if stream :
961
988
return result
962
989
messages = result .get ("messages" , messages )
@@ -969,7 +996,6 @@ def check_llm_command(
969
996
return npc .handle_agent_pass (
970
997
npc_to_pass ,
971
998
command ,
972
- command_history ,
973
999
messages = messages ,
974
1000
retrieved_docs = retrieved_docs ,
975
1001
n_docs = n_docs ,
@@ -1005,7 +1031,6 @@ def check_llm_command(
1005
1031
1006
1032
return check_llm_command (
1007
1033
command + " \n \n \n extra context: " + request_input ,
1008
- command_history ,
1009
1034
model = model ,
1010
1035
provider = provider ,
1011
1036
api_url = api_url ,
@@ -1014,6 +1039,7 @@ def check_llm_command(
1014
1039
messages = messages ,
1015
1040
retrieved_docs = retrieved_docs ,
1016
1041
n_docs = n_docs ,
1042
+ stream = stream ,
1017
1043
)
1018
1044
1019
1045
elif action == "execute_sequence" :
@@ -1024,7 +1050,6 @@ def check_llm_command(
1024
1050
result = handle_tool_call (
1025
1051
command ,
1026
1052
tool_name ,
1027
- command_history ,
1028
1053
model = model ,
1029
1054
provider = provider ,
1030
1055
api_url = api_url ,
@@ -1049,7 +1074,6 @@ def check_llm_command(
1049
1074
def handle_tool_call (
1050
1075
command : str ,
1051
1076
tool_name : str ,
1052
- command_history : Any ,
1053
1077
model : str = NPCSH_CHAT_MODEL ,
1054
1078
provider : str = NPCSH_CHAT_PROVIDER ,
1055
1079
api_url : str = NPCSH_API_URL ,
@@ -1068,7 +1092,6 @@ def handle_tool_call(
1068
1092
Args:
1069
1093
command (str): The command.
1070
1094
tool_name (str): The tool name.
1071
- command_history (Any): The command history.
1072
1095
Keyword Args:
1073
1096
model (str): The model to use for handling the tool call.
1074
1097
provider (str): The provider to use for handling the tool call.
@@ -1081,7 +1104,6 @@ def handle_tool_call(
1081
1104
the tool call.
1082
1105
1083
1106
"""
1084
- print (f"handle_tool_call invoked with tool_name: { tool_name } " )
1085
1107
# print(npc)
1086
1108
if not npc or not npc .all_tools_dict :
1087
1109
print ("not available" )
@@ -1168,7 +1190,6 @@ def handle_tool_call(
1168
1190
return handle_tool_call (
1169
1191
command ,
1170
1192
tool_name ,
1171
- command_history ,
1172
1193
model = model ,
1173
1194
provider = provider ,
1174
1195
messages = messages ,
@@ -1213,7 +1234,6 @@ def handle_tool_call(
1213
1234
1214
1235
def execute_data_operations (
1215
1236
query : str ,
1216
- command_history : Any ,
1217
1237
dataframes : Dict [str , pd .DataFrame ],
1218
1238
npc : Any = None ,
1219
1239
db_path : str = "~/npcsh_history.db" ,
@@ -1223,7 +1243,7 @@ def execute_data_operations(
1223
1243
This function executes data operations.
1224
1244
Args:
1225
1245
query (str): The query to execute.
1226
- command_history (Any): The command history.
1246
+
1227
1247
dataframes (Dict[str, pd.DataFrame]): The dictionary of dataframes.
1228
1248
Keyword Args:
1229
1249
npc (Any): The NPC object.
0 commit comments