|
| 1 | +package us.ihmc.behaviors.reasoning; |
| 2 | + |
| 3 | +import de.kherud.llama.InferenceParameters; |
| 4 | +import de.kherud.llama.LlamaModel; |
| 5 | +import de.kherud.llama.ModelParameters; |
| 6 | +import de.kherud.llama.args.MiroStat; |
| 7 | +import us.ihmc.log.LogTools; |
| 8 | +import us.ihmc.tools.IHMCCommonPaths; |
| 9 | + |
| 10 | +public class BehaviorTreeNextActionReasoning |
| 11 | +{ |
| 12 | + private static final String SYSTEM = """ |
| 13 | + <|start_header_id|>system<|end_header_id|> |
| 14 | + You are a reasoning system that decides the next action to execute in a tree-based robotic system. |
| 15 | + The current tree and state is given by: |
| 16 | + { |
| 17 | + "nodes": [ |
| 18 | + {"id": int, "type": string, "children": [ ]} |
| 19 | + ], |
| 20 | + "state": { |
| 21 | + "currently_executing": int |
| 22 | + "is_done": bool |
| 23 | + } |
| 24 | + } |
| 25 | + There are two node types: Action and Sequence. |
| 26 | + An Action node is the only type of node that can be executed. |
| 27 | + A Sequence node can have children. When one child of an action sequence node is done, the next one in the list of children should be executed. |
| 28 | + Please consider which node is best to execute next and output only the node ID number of that action. |
| 29 | + <|eot_id|> |
| 30 | + <|start_header_id|>user<|end_header_id|> |
| 31 | + { |
| 32 | + "nodes": [ |
| 33 | + {"id": 001, "type": "Sequence, "children": [ |
| 34 | + {"id": 002, "type": "Action"}, |
| 35 | + {"id": 005, "type": "Action"}, |
| 36 | + {"id": 020, "type": "Action"}, |
| 37 | + {"id": 004, "type": "Action"}, |
| 38 | + {"id": 056, "type": "Action"} |
| 39 | + ]} |
| 40 | + ], |
| 41 | + "state": { |
| 42 | + "currently_executing": 002, |
| 43 | + "is_done": true |
| 44 | + } |
| 45 | + } |
| 46 | + <|eot_id|> |
| 47 | + <|start_header_id|>assistant<|end_header_id|> |
| 48 | + 005 |
| 49 | + <|eot_id|> |
| 50 | + <|start_header_id|>user<|end_header_id|> |
| 51 | + { |
| 52 | + "nodes": [ |
| 53 | + {"id": 001, "type": "Sequence, "children": [ |
| 54 | + {"id": 002, "type": "Action"}, |
| 55 | + {"id": 005, "type": "Action"}, |
| 56 | + {"id": 020, "type": "Action"}, |
| 57 | + {"id": 004, "type": "Action"}, |
| 58 | + {"id": 056, "type": "Action"} |
| 59 | + ]} |
| 60 | + ], |
| 61 | + "state": { |
| 62 | + "currently_executing": 005, |
| 63 | + "is_done": true |
| 64 | + } |
| 65 | + } |
| 66 | + <|eot_id|> |
| 67 | + <|start_header_id|>assistant<|end_header_id|> |
| 68 | + 020 |
| 69 | + <|eot_id|> |
| 70 | + <|start_header_id|>user<|end_header_id|> |
| 71 | + { |
| 72 | + "nodes": [ |
| 73 | + {"id": 001, "type": "Sequence, "children": [ |
| 74 | + {"id": 002, "type": "Action"}, |
| 75 | + {"id": 005, "type": "Action"}, |
| 76 | + {"id": 020, "type": "Action"}, |
| 77 | + {"id": 004, "type": "Action"}, |
| 78 | + {"id": 056, "type": "Action"} |
| 79 | + ]} |
| 80 | + ], |
| 81 | + "state": { |
| 82 | + "currently_executing": 020, |
| 83 | + "is_done": true |
| 84 | + } |
| 85 | + } |
| 86 | + <|eot_id|> |
| 87 | + <|start_header_id|>assistant<|end_header_id|> |
| 88 | + """; |
| 89 | + |
| 90 | + |
| 91 | + private final LlamaModel model; |
| 92 | + |
| 93 | + public BehaviorTreeNextActionReasoning() |
| 94 | + { |
| 95 | + String modelFilePath = IHMCCommonPaths.DOT_IHMC_DIRECTORY.resolve("llama-models/Llama-3.2-1B-Instruct-Q8_0.gguf").toString(); |
| 96 | + ModelParameters modelParams = new ModelParameters(); |
| 97 | + modelParams.setModelFilePath(modelFilePath); |
| 98 | + modelParams.setNGpuLayers(33); |
| 99 | + modelParams.setNThreads(8); |
| 100 | + modelParams.setNCtx(4098); |
| 101 | + |
| 102 | + LlamaModel.setLogger(null, (level, message) -> {}); |
| 103 | + |
| 104 | + model = new LlamaModel(modelParams); |
| 105 | + } |
| 106 | + |
| 107 | + public int queryNextLeafToExecuteIndex() |
| 108 | + { |
| 109 | + String prompt = SYSTEM; |
| 110 | +// prompt += """ |
| 111 | +// Hello! |
| 112 | +// """; |
| 113 | + |
| 114 | + InferenceParameters inferParams = new InferenceParameters(prompt); |
| 115 | + inferParams.setPenalizeNl(true); |
| 116 | + inferParams.setTemperature(0.3f); |
| 117 | + inferParams.setMiroStat(MiroStat.V2); |
| 118 | + inferParams.setStopStrings("<|eot_id|>"); |
| 119 | + inferParams.setTopK(40); |
| 120 | + inferParams.setTopP(0.25f); |
| 121 | + inferParams.setRepeatPenalty(1.15f); |
| 122 | + |
| 123 | + String reponse = model.complete(inferParams); |
| 124 | + |
| 125 | + LogTools.info(prompt + reponse); |
| 126 | + |
| 127 | + return 0; |
| 128 | + } |
| 129 | + |
| 130 | + public void destroy() |
| 131 | + { |
| 132 | + model.close(); |
| 133 | + } |
| 134 | + |
| 135 | + public static void main(String[] args) |
| 136 | + { |
| 137 | + BehaviorTreeNextActionReasoning reasoning = new BehaviorTreeNextActionReasoning(); |
| 138 | + reasoning.queryNextLeafToExecuteIndex(); |
| 139 | + reasoning.destroy(); |
| 140 | + |
| 141 | + System.exit(0); // FIXME: Not sure why it's not exiting automatically. |
| 142 | + } |
| 143 | +} |
0 commit comments