llassembly 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- llassembly-0.0.1/.github/workflows/publish.yaml +28 -0
- llassembly-0.0.1/.gitignore +207 -0
- llassembly-0.0.1/LICENSE +21 -0
- llassembly-0.0.1/PKG-INFO +423 -0
- llassembly-0.0.1/README.md +407 -0
- llassembly-0.0.1/llassembly/__init__.py +8 -0
- llassembly-0.0.1/llassembly/asm_emulator.py +588 -0
- llassembly-0.0.1/llassembly/langchain.py +173 -0
- llassembly-0.0.1/llassembly/langgraph.py +132 -0
- llassembly-0.0.1/llassembly/langgraph_plan.py +164 -0
- llassembly-0.0.1/llassembly/prompts.py +24 -0
- llassembly-0.0.1/llassembly/prompts_md/base.md +62 -0
- llassembly-0.0.1/pyproject.toml +40 -0
- llassembly-0.0.1/tests/__init__.py +0 -0
- llassembly-0.0.1/tests/conftest.py +68 -0
- llassembly-0.0.1/tests/test_asm_emulator.py +283 -0
- llassembly-0.0.1/tests/test_langchain.py +48 -0
- llassembly-0.0.1/tests/test_langgraph.py +56 -0
- llassembly-0.0.1/tests/use_cases/__init__.py +0 -0
- llassembly-0.0.1/tests/use_cases/npc_in_game_unit/cassettes/test_unit_controls/test_unit_condition_stmt.yaml +206 -0
- llassembly-0.0.1/tests/use_cases/npc_in_game_unit/cassettes/test_unit_controls/test_unit_conditional_loop.yaml +133 -0
- llassembly-0.0.1/tests/use_cases/npc_in_game_unit/cassettes/test_unit_controls/test_unit_moves.yaml +177 -0
- llassembly-0.0.1/tests/use_cases/npc_in_game_unit/fixtures/__init__.py +0 -0
- llassembly-0.0.1/tests/use_cases/npc_in_game_unit/fixtures/unit_controls.py +88 -0
- llassembly-0.0.1/tests/use_cases/npc_in_game_unit/test_unit_controls.py +185 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
name: Publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- "v*"
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
publish:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
timeout-minutes: 5
|
|
12
|
+
environment: pypi
|
|
13
|
+
permissions:
|
|
14
|
+
id-token: write
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v6
|
|
17
|
+
|
|
18
|
+
- name: Install uv
|
|
19
|
+
uses: astral-sh/setup-uv@v7
|
|
20
|
+
|
|
21
|
+
- name: Set up Python
|
|
22
|
+
run: uv python install 3.14
|
|
23
|
+
|
|
24
|
+
- name: Build
|
|
25
|
+
run: uv build
|
|
26
|
+
|
|
27
|
+
- name: Publish
|
|
28
|
+
run: uv publish
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
# Marimo
|
|
205
|
+
marimo/_static/
|
|
206
|
+
marimo/_lsp/
|
|
207
|
+
__marimo__/
|
llassembly-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aleh Shydlouski
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: llassembly
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Planner for llm tool calls
|
|
5
|
+
Project-URL: Homepage, https://github.com/electronick1/LLAssembly
|
|
6
|
+
Project-URL: Repository, https://github.com/electronick1/LLAssembly
|
|
7
|
+
Author-email: Aleh Shydlouski <oleg.ivye@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: LLM,assembly,tool calls
|
|
11
|
+
Requires-Python: >=3.12
|
|
12
|
+
Requires-Dist: langchain>=1.2.7
|
|
13
|
+
Requires-Dist: langgraph>=1.0.8
|
|
14
|
+
Requires-Dist: pydantic>=2.12.4
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
## About
|
|
19
|
+
|
|
20
|
+
LLAssembly is a tool-orchestration library for LLM agents.
|
|
21
|
+
|
|
22
|
+
Rather than having the LLM invoke tools repeatedly in a fixed sequence, LLAssembly asks the model to write complete execution plan up-front that includes conditionals, loops, and state tracking in assembly-like program that then initiates tools during emulation process, enabling complex control flow within a single agent invocation.
|
|
23
|
+
|
|
24
|
+
Below is an updated version of the diagram from the [official LangChain documentation](https://docs.langchain.com/oss/python/langchain/agents), extended with the LLAssembly execution plan:
|
|
25
|
+
<details>
|
|
26
|
+
<summary>Diagram (click to expand)</summary>
|
|
27
|
+
|
|
28
|
+
```mermaid theme={null}
|
|
29
|
+
%%{
|
|
30
|
+
init: {
|
|
31
|
+
"fontFamily": "monospace",
|
|
32
|
+
"flowchart": {
|
|
33
|
+
"curve": "curve"
|
|
34
|
+
},
|
|
35
|
+
"themeVariables": {"edgeLabelBackground": "transparent"}
|
|
36
|
+
}
|
|
37
|
+
}%%
|
|
38
|
+
graph TD
|
|
39
|
+
%% Outside the agent
|
|
40
|
+
QUERY([input])
|
|
41
|
+
LLM{model}
|
|
42
|
+
TOOL(tools)
|
|
43
|
+
ANSWER([output])
|
|
44
|
+
|
|
45
|
+
%% Main flows (no inline labels)
|
|
46
|
+
QUERY --> LLM
|
|
47
|
+
LLM --"action"--> TOOL
|
|
48
|
+
TOOL --"observation"--> LLM
|
|
49
|
+
TOOL --> A
|
|
50
|
+
F --> TOOL
|
|
51
|
+
LLM --"finish"--> ANSWER
|
|
52
|
+
|
|
53
|
+
subgraph LLAsembly tools plan
|
|
54
|
+
A([Start]) --> AA[Executes Tool #1]
|
|
55
|
+
AA --> B[What Tool #1 replied?]
|
|
56
|
+
B -- Answer #1 --> C[Executes Tool #2]
|
|
57
|
+
B -- Answer #2 --> D[Executes Tool #3]
|
|
58
|
+
C --> E[Repeat Tool #2?]
|
|
59
|
+
E -- Yes --> C
|
|
60
|
+
E -- No --> F([End])
|
|
61
|
+
D --> F
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
classDef blueHighlight fill:#0a1c25,stroke:#0a455f,color:#bae6fd;
|
|
65
|
+
classDef greenHighlight fill:#0b1e1a,stroke:#0c4c39,color:#9ce4c4;
|
|
66
|
+
class QUERY blueHighlight;
|
|
67
|
+
class ANSWER blueHighlight;
|
|
68
|
+
```
|
|
69
|
+
</details>
|
|
70
|
+
|
|
71
|
+
⚠️ Work in progress! LLAssembly is under active development, some parts not tested well and could be unstable. Feedback and PRs are welcome. If you hit issues, please open a ticket.
|
|
72
|
+
|
|
73
|
+
## Use Cases
|
|
74
|
+
|
|
75
|
+
LLAssembly was originally designed for in-game NPC unit control throught natural language commands. A command like:
|
|
76
|
+
`Go to 5,5 if you see enemy on the road attack him and run to 7,7` is a sequence of actions with conditions ("if you see enemy ...") and repeated checks (“look for an enemy at each step”). A traditional “get next tool to call” approach often needs an LLM round trips at each step to decide what to do next, which can quickly balloon into hundreds of requests per unit and introduce latency. With LLAssembly, you make only one request that generates a complete execution plan to react on environment change, implement conditions, loops and track state between tool calls.
|
|
77
|
+
|
|
78
|
+
This approach is particularly useful in scenarios where you need to reduce the number of requests to LLMs, and when context/environment between tool calls changes rapidly. For instance:
|
|
79
|
+
|
|
80
|
+
- **Robotics**: When decisions depend on sensor input and must happen quickly, minimizing LLM round trips is crucial.
|
|
81
|
+
- **Code Assistants**: When execution requires complex control-flow and number of LLM requests is what you are paying for
|
|
82
|
+
- **Game AI**: When you want to control NPC depending on the rapidly changing environment and there is no time to wait for a next action from LLM
|
|
83
|
+
- **Automated Workflows**: When you need to orchestrate multiple tools with a branching logic
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
## Why Assembly?
|
|
87
|
+
|
|
88
|
+
When you want tool orchestration with branching logic or loops, there are a few common approaches, each with tradeoffs:
|
|
89
|
+
- The traditional approach when you ask LLM for a next tool to run on every step results in many LLM requests and additional delays to get reply from LLM.
|
|
90
|
+
- Creating your own DSL (doman specific language) that will describe the logic for the tool calls - often leads to LLM hallucination, as it tends to make things up due to the luck of context (training set) about this custom DSL.
|
|
91
|
+
- Asking the model for a high-level language code (e.g. Python, JS, Lua, ...) for execution plan to invoke tool calls - this could be a more stable approach because LLMs are better at generating python code than Assembly. But it's quite unsafe and complicated to emulate high-level programming languages based on the user input. Assembly code (the light version of it) can be emulated in 300 lines of python code in a very strict and easy to control environment.
|
|
92
|
+
|
|
93
|
+
The Assembly (also SQL) instructions set is a middle ground between custom DSL and high-level programming code - it can be emulated in a strict environment (in fact it's converted to a LangGraph sub-graph) and most LLMs have more than enough context about Assembly to handle tool calls, for example `gpt-oss:20b` that fits in 16G GPU getting things done in handling NPC unit commands.
|
|
94
|
+
|
|
95
|
+
## How It Works
|
|
96
|
+
|
|
97
|
+
Currently LLAssembly supports LangChain and LangGraph. When you invoke the agent:
|
|
98
|
+
1. Your request is wrapped in a system prompt that instructs the LLM to generate assembly-like instructions rather than directly calling tools.
|
|
99
|
+
2. The LLM returns a sequence of Assembly instructions describing the intended behavior and control flow.
|
|
100
|
+
3. The assembly code is parsed and executed through a lightweight emulator, converting each Assembly instruction to the LangGraph nodes
|
|
101
|
+
4. During execution, the emulator performs the actual tool calls, stores intermediate results, and evaluates branches/loops based on tool outputs and tracked state.
|
|
102
|
+
5. The results are returned to the user, including all the intermediate tool responses
|
|
103
|
+
|
|
104
|
+
## Installation
|
|
105
|
+
|
|
106
|
+
WIP section.
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
## Examples
|
|
110
|
+
|
|
111
|
+
For LangChain simple add `ToolsPlannerMiddleware()` to the middlewares, it will modify the system prompt to produce assembly instructions and start emulation proces that will invoke tools provided to the agent.
|
|
112
|
+
For LangGraph add `ToolsPlannerNode(ollama_model, tools=[...])` to your graph for sync requests and `AToolsPlannerNode(...)` for async, this node will build sub-graph with assembly instructions invoking tools during sub-graph execution.
|
|
113
|
+
|
|
114
|
+
See more examples in `tests/use_cases`. Here's how you might use it to control a in-game NPC unit:
|
|
115
|
+
|
|
116
|
+
Langchain example:
|
|
117
|
+
```python
|
|
118
|
+
from langchain.agents import create_agent
|
|
119
|
+
from llassembly import ToolsPlannerMiddleware
|
|
120
|
+
|
|
121
|
+
agent = create_agent(
|
|
122
|
+
model=ollama_model,
|
|
123
|
+
tools=[
|
|
124
|
+
make_one_step,
|
|
125
|
+
get_current_position,
|
|
126
|
+
pick_sword,
|
|
127
|
+
attack_enemy,
|
|
128
|
+
has_sword,
|
|
129
|
+
get_enemies_around
|
|
130
|
+
],
|
|
131
|
+
middleware=[ToolsPlannerMiddleware()],
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
result = agent.invoke({
|
|
135
|
+
"messages": [
|
|
136
|
+
SystemMessage("Control in-game NPC unit based on the provided commands."),
|
|
137
|
+
HumanMessage("Go to 5,5 if you see enemy on the road attack him and run to 7,7"),
|
|
138
|
+
]
|
|
139
|
+
})
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
LangGraph example:
|
|
143
|
+
```python
|
|
144
|
+
from llassembly import ToolsPlannerNode
|
|
145
|
+
|
|
146
|
+
agent_graph = StateGraph(MessagesState)
|
|
147
|
+
agent_graph.add_node("llm_tools_planner", ToolsPlannerNode(ollama_model, tools=[...]))
|
|
148
|
+
agent_graph.add_edge(START, "llm_tools_planner")
|
|
149
|
+
agent_graph.add_edge("llm_tools_planner", END)
|
|
150
|
+
agent = agent_graph.compile()
|
|
151
|
+
result = agent.invoke({
|
|
152
|
+
"messages": [
|
|
153
|
+
SystemMessage("Control in-game NPC unit based on the provided commands."),
|
|
154
|
+
HumanMessage("Go to 5,5 if you see enemy on the road attack him and run to 7,7"),
|
|
155
|
+
]
|
|
156
|
+
})
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
This will result in assembly code like this (generated by gpt-oss:20b):
|
|
160
|
+
```assembly
|
|
161
|
+
; Target coordinates for first phase
|
|
162
|
+
MOV R1, 5 ; R1 = target_x
|
|
163
|
+
MOV R2, 5 ; R2 = target_y
|
|
164
|
+
loop_to_5_5:
|
|
165
|
+
CALL get_current_position ; Get current position (x,y)
|
|
166
|
+
POP R3 ; R3 = current_x
|
|
167
|
+
POP R4 ; R4 = current_y
|
|
168
|
+
|
|
169
|
+
CMP R3, R1 ; Is current_x == target_x ?
|
|
170
|
+
JNE not_at_5_5 ; If not, still moving
|
|
171
|
+
CMP R4, R2 ; Is current_y == target_y ?
|
|
172
|
+
JNE not_at_5_5 ; If not, still moving
|
|
173
|
+
JMP after_5_5 ; Destination reached
|
|
174
|
+
|
|
175
|
+
not_at_5_5:
|
|
176
|
+
CALL get_enemies_around ; Check for enemy around
|
|
177
|
+
POP R5 ; R5 = enemy_id (0 if none)
|
|
178
|
+
CMP R5, 0 ; Any enemy found?
|
|
179
|
+
JNE enemy_found
|
|
180
|
+
JMP step_5_5 ; No enemy, just step
|
|
181
|
+
|
|
182
|
+
enemy_found:
|
|
183
|
+
CALL has_sword ; Does unit already have a sword?
|
|
184
|
+
POP R6 ; R6 = has_sword flag (1 or 0)
|
|
185
|
+
CMP R6, 0 ; No sword?
|
|
186
|
+
JNE have_sword
|
|
187
|
+
CALL pick_sword ; Pick a sword
|
|
188
|
+
have_sword:
|
|
189
|
+
PUSH R5 ; Push enemy_id for attack
|
|
190
|
+
CALL attack_enemy ; Attack the enemy
|
|
191
|
+
JMP step_5_5 ; Continue moving after attack
|
|
192
|
+
|
|
193
|
+
step_5_5:
|
|
194
|
+
PUSH R1 ; Push target_x
|
|
195
|
+
PUSH R2 ; Push target_y
|
|
196
|
+
CALL make_one_step ; Take one step towards (5,5)
|
|
197
|
+
JMP loop_to_5_5 ; Repeat loop
|
|
198
|
+
|
|
199
|
+
after_5_5:
|
|
200
|
+
; Set new target coordinates for second phase
|
|
201
|
+
MOV R1, 7 ; R1 = target_x (7)
|
|
202
|
+
MOV R2, 7 ; R2 = target_y (7)
|
|
203
|
+
JMP loop_to_7_7
|
|
204
|
+
|
|
205
|
+
loop_to_7_7:
|
|
206
|
+
CALL get_current_position ; Get current position (x,y)
|
|
207
|
+
POP R3 ; R3 = current_x
|
|
208
|
+
POP R4 ; R4 = current_y
|
|
209
|
+
|
|
210
|
+
CMP R3, R1 ; Is current_x == target_x ?
|
|
211
|
+
JNE not_at_7_7 ; If not, still moving
|
|
212
|
+
CMP R4, R2 ; Is current_y == target_y ?
|
|
213
|
+
JNE not_at_7_7 ; If not, still moving
|
|
214
|
+
JMP end_program ; Destination reached
|
|
215
|
+
|
|
216
|
+
not_at_7_7:
|
|
217
|
+
PUSH R1 ; Push target_x
|
|
218
|
+
PUSH R2 ; Push target_y
|
|
219
|
+
CALL make_one_step ; Take one step towards (7,7)
|
|
220
|
+
JMP loop_to_7_7 ; Repeat loop
|
|
221
|
+
|
|
222
|
+
end_program:
|
|
223
|
+
RET ; Return from program
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Which then will be converted to the LangGraph sub-graph where each ASM instruction is
|
|
227
|
+
a separate node. Such sub-graph will be executed with a provided context/state
|
|
228
|
+
and each "CALL" instruction will result in a tool call produce `ToolMessage`.
|
|
229
|
+
|
|
230
|
+
LangGraph with `get_graph().draw_mermaid()`:
|
|
231
|
+
<details>
|
|
232
|
+
<summary>Diagram (click to expand)</summary>
|
|
233
|
+
|
|
234
|
+
```mermaid
|
|
235
|
+
---
|
|
236
|
+
config:
|
|
237
|
+
flowchart:
|
|
238
|
+
curve: linear
|
|
239
|
+
---
|
|
240
|
+
graph TD;
|
|
241
|
+
__start__([<p>__start__</p>]):::first
|
|
242
|
+
tools_planner_inst_\231(MOV R1, 5)
|
|
243
|
+
tools_planner_inst_\232(MOV R2, 5)
|
|
244
|
+
tools_planner_inst_\233(loop_to_5_5)
|
|
245
|
+
tools_planner_inst_\234(CALL get_current_position)
|
|
246
|
+
get_current_position_inst_\234(get_current_position tool instruction_#4)
|
|
247
|
+
tools_planner_inst_\235(POP R3)
|
|
248
|
+
tools_planner_inst_\236(POP R4)
|
|
249
|
+
tools_planner_inst_\237(CMP R3, R1)
|
|
250
|
+
tools_planner_inst_\238(JNE not_at_5_5)
|
|
251
|
+
tools_planner_inst_\239(CMP R4, R2)
|
|
252
|
+
tools_planner_inst_\2310(JNE not_at_5_5)
|
|
253
|
+
tools_planner_inst_\2311(JMP after_5_5)
|
|
254
|
+
tools_planner_inst_\2312(not_at_5_5)
|
|
255
|
+
tools_planner_inst_\2313(CALL get_enemies_around)
|
|
256
|
+
get_enemies_around_inst_\2313(get_enemies_around tool instruction_#13)
|
|
257
|
+
tools_planner_inst_\2314(POP R5)
|
|
258
|
+
tools_planner_inst_\2315(CMP R5, 0)
|
|
259
|
+
tools_planner_inst_\2316(JNE enemy_found)
|
|
260
|
+
tools_planner_inst_\2317(JMP step_5_5)
|
|
261
|
+
tools_planner_inst_\2318(enemy_found:)
|
|
262
|
+
tools_planner_inst_\2319(CALL has_sword)
|
|
263
|
+
has_sword_inst_\2319(has_sword tool instruction_#19)
|
|
264
|
+
tools_planner_inst_\2320(POP R6)
|
|
265
|
+
tools_planner_inst_\2321(CMP R6, 0)
|
|
266
|
+
tools_planner_inst_\2322(JNE have_sword)
|
|
267
|
+
tools_planner_inst_\2323(CALL pick sword)
|
|
268
|
+
pick_sword_inst_\2323(pick_sword tool instruction_#23)
|
|
269
|
+
tools_planner_inst_\2324(have_sword:)
|
|
270
|
+
tools_planner_inst_\2325(PUSH R5)
|
|
271
|
+
tools_planner_inst_\2326(CALL attack_enemy)
|
|
272
|
+
attack_enemy_inst_\2326(attack_enemy tool instruction_#26)
|
|
273
|
+
tools_planner_inst_\2327(JMP step_5_5)
|
|
274
|
+
tools_planner_inst_\2328(step_5_5:)
|
|
275
|
+
tools_planner_inst_\2329(PUSH R1)
|
|
276
|
+
tools_planner_inst_\2330(PUSH R2)
|
|
277
|
+
tools_planner_inst_\2331(CALL make_one_step)
|
|
278
|
+
make_one_step_inst_\2331(make_one_step tool instruction_#31)
|
|
279
|
+
tools_planner_inst_\2332(JMP loop_to_5_5)
|
|
280
|
+
tools_planner_inst_\2333(after_5_5:)
|
|
281
|
+
tools_planner_inst_\2334(MOV R1, 7)
|
|
282
|
+
tools_planner_inst_\2335(MOV R2, 7)
|
|
283
|
+
tools_planner_inst_\2336(JMP loop_to_7_7)
|
|
284
|
+
tools_planner_inst_\2337(loop_to_7_7:)
|
|
285
|
+
tools_planner_inst_\2338(CALL get_current_position)
|
|
286
|
+
get_current_position_inst_\2338(get_current_position tool instruction_#38)
|
|
287
|
+
tools_planner_inst_\2339(POP R3)
|
|
288
|
+
tools_planner_inst_\2340(POP R4)
|
|
289
|
+
tools_planner_inst_\2341(CMP R3, R1)
|
|
290
|
+
tools_planner_inst_\2342(JNE not_at_7_7)
|
|
291
|
+
tools_planner_inst_\2343(CMP R4, R2)
|
|
292
|
+
tools_planner_inst_\2344(JNE not_at_7_7)
|
|
293
|
+
tools_planner_inst_\2345(JMP end_program)
|
|
294
|
+
tools_planner_inst_\2346(not_at_7_7)
|
|
295
|
+
tools_planner_inst_\2347(PUSH R1)
|
|
296
|
+
tools_planner_inst_\2348(PUSH R2)
|
|
297
|
+
tools_planner_inst_\2349(CALL make_one_step)
|
|
298
|
+
make_one_step_inst_\2349(make_one_step_inst_#49)
|
|
299
|
+
tools_planner_inst_\2350(JMP loop_to_7_7)
|
|
300
|
+
tools_planner_inst_\2351(end_program:)
|
|
301
|
+
tools_planner_inst_\2352(RET)
|
|
302
|
+
__end__([<p>__end__</p>]):::last
|
|
303
|
+
__start__ --> tools_planner_inst_\230;
|
|
304
|
+
attack_enemy_inst_\2326 --> tools_planner_inst_\2327;
|
|
305
|
+
get_current_position_inst_\2338 --> tools_planner_inst_\2339;
|
|
306
|
+
get_current_position_inst_\234 --> tools_planner_inst_\235;
|
|
307
|
+
get_enemies_around_inst_\2313 --> tools_planner_inst_\2314;
|
|
308
|
+
has_sword_inst_\2319 --> tools_planner_inst_\2320;
|
|
309
|
+
make_one_step_inst_\2331 --> tools_planner_inst_\2332;
|
|
310
|
+
make_one_step_inst_\2349 --> tools_planner_inst_\2350;
|
|
311
|
+
pick_sword_inst_\2323 --> tools_planner_inst_\2324;
|
|
312
|
+
tools_planner_inst_\230 --> tools_planner_inst_\231;
|
|
313
|
+
tools_planner_inst_\231 --> tools_planner_inst_\232;
|
|
314
|
+
tools_planner_inst_\2310 -. False .-> tools_planner_inst_\2311;
|
|
315
|
+
tools_planner_inst_\2310 -. True .-> tools_planner_inst_\2312;
|
|
316
|
+
tools_planner_inst_\2311 -. False .-> tools_planner_inst_\2312;
|
|
317
|
+
tools_planner_inst_\2311 -. True .-> tools_planner_inst_\2333;
|
|
318
|
+
tools_planner_inst_\2312 --> tools_planner_inst_\2313;
|
|
319
|
+
tools_planner_inst_\2313 --> get_enemies_around_inst_\2313;
|
|
320
|
+
tools_planner_inst_\2314 --> tools_planner_inst_\2315;
|
|
321
|
+
tools_planner_inst_\2315 --> tools_planner_inst_\2316;
|
|
322
|
+
tools_planner_inst_\2316 -. False .-> tools_planner_inst_\2317;
|
|
323
|
+
tools_planner_inst_\2316 -. True .-> tools_planner_inst_\2318;
|
|
324
|
+
tools_planner_inst_\2317 -. False .-> tools_planner_inst_\2318;
|
|
325
|
+
tools_planner_inst_\2317 -. True .-> tools_planner_inst_\2328;
|
|
326
|
+
tools_planner_inst_\2318 --> tools_planner_inst_\2319;
|
|
327
|
+
tools_planner_inst_\2319 --> has_sword_inst_\2319;
|
|
328
|
+
tools_planner_inst_\232 --> tools_planner_inst_\233;
|
|
329
|
+
tools_planner_inst_\2320 --> tools_planner_inst_\2321;
|
|
330
|
+
tools_planner_inst_\2321 --> tools_planner_inst_\2322;
|
|
331
|
+
tools_planner_inst_\2322 -. False .-> tools_planner_inst_\2323;
|
|
332
|
+
tools_planner_inst_\2322 -. True .-> tools_planner_inst_\2324;
|
|
333
|
+
tools_planner_inst_\2323 --> pick_sword_inst_\2323;
|
|
334
|
+
tools_planner_inst_\2324 --> tools_planner_inst_\2325;
|
|
335
|
+
tools_planner_inst_\2325 --> tools_planner_inst_\2326;
|
|
336
|
+
tools_planner_inst_\2326 --> attack_enemy_inst_\2326;
|
|
337
|
+
tools_planner_inst_\2327 -. False .-> tools_planner_inst_\2328;
|
|
338
|
+
tools_planner_inst_\2328 --> tools_planner_inst_\2329;
|
|
339
|
+
tools_planner_inst_\2329 --> tools_planner_inst_\2330;
|
|
340
|
+
tools_planner_inst_\233 --> tools_planner_inst_\234;
|
|
341
|
+
tools_planner_inst_\2330 --> tools_planner_inst_\2331;
|
|
342
|
+
tools_planner_inst_\2331 --> make_one_step_inst_\2331;
|
|
343
|
+
tools_planner_inst_\2332 -. True .-> tools_planner_inst_\233;
|
|
344
|
+
tools_planner_inst_\2332 -. False .-> tools_planner_inst_\2333;
|
|
345
|
+
tools_planner_inst_\2333 --> tools_planner_inst_\2334;
|
|
346
|
+
tools_planner_inst_\2334 --> tools_planner_inst_\2335;
|
|
347
|
+
tools_planner_inst_\2335 --> tools_planner_inst_\2336;
|
|
348
|
+
tools_planner_inst_\2336 -. False .-> tools_planner_inst_\2337;
|
|
349
|
+
tools_planner_inst_\2337 --> tools_planner_inst_\2338;
|
|
350
|
+
tools_planner_inst_\2338 --> get_current_position_inst_\2338;
|
|
351
|
+
tools_planner_inst_\2339 --> tools_planner_inst_\2340;
|
|
352
|
+
tools_planner_inst_\234 --> get_current_position_inst_\234;
|
|
353
|
+
tools_planner_inst_\2340 --> tools_planner_inst_\2341;
|
|
354
|
+
tools_planner_inst_\2341 --> tools_planner_inst_\2342;
|
|
355
|
+
tools_planner_inst_\2342 -. False .-> tools_planner_inst_\2343;
|
|
356
|
+
tools_planner_inst_\2342 -. True .-> tools_planner_inst_\2346;
|
|
357
|
+
tools_planner_inst_\2343 --> tools_planner_inst_\2344;
|
|
358
|
+
tools_planner_inst_\2344 -. False .-> tools_planner_inst_\2345;
|
|
359
|
+
tools_planner_inst_\2344 -. True .-> tools_planner_inst_\2346;
|
|
360
|
+
tools_planner_inst_\2345 -. False .-> tools_planner_inst_\2346;
|
|
361
|
+
tools_planner_inst_\2345 -. True .-> tools_planner_inst_\2351;
|
|
362
|
+
tools_planner_inst_\2346 --> tools_planner_inst_\2347;
|
|
363
|
+
tools_planner_inst_\2347 --> tools_planner_inst_\2348;
|
|
364
|
+
tools_planner_inst_\2348 --> tools_planner_inst_\2349;
|
|
365
|
+
tools_planner_inst_\2349 --> make_one_step_inst_\2349;
|
|
366
|
+
tools_planner_inst_\235 --> tools_planner_inst_\236;
|
|
367
|
+
tools_planner_inst_\2350 -. True .-> tools_planner_inst_\2337;
|
|
368
|
+
tools_planner_inst_\2350 -. False .-> tools_planner_inst_\2351;
|
|
369
|
+
tools_planner_inst_\2351 --> tools_planner_inst_\2352;
|
|
370
|
+
tools_planner_inst_\2352 --> tools_planner_inst_\2353;
|
|
371
|
+
tools_planner_inst_\236 --> tools_planner_inst_\237;
|
|
372
|
+
tools_planner_inst_\237 --> tools_planner_inst_\238;
|
|
373
|
+
tools_planner_inst_\238 -. True .-> tools_planner_inst_\2312;
|
|
374
|
+
tools_planner_inst_\238 -. False .-> tools_planner_inst_\239;
|
|
375
|
+
tools_planner_inst_\239 --> tools_planner_inst_\2310;
|
|
376
|
+
tools_planner_inst_\2353 --> __end__;
|
|
377
|
+
classDef default fill:#f2f0ff,line-height:1.2
|
|
378
|
+
classDef first fill-opacity:0
|
|
379
|
+
classDef last fill:#bfb6fc
|
|
380
|
+
```
|
|
381
|
+
</details>
|
|
382
|
+
|
|
383
|
+
See more examples in `tests/use_cases`, as well as example of Assembly code produced
|
|
384
|
+
for the tool calls in `cassettes` folder for each test module.
|
|
385
|
+
|
|
386
|
+
## Implementation details:
|
|
387
|
+
|
|
388
|
+
#### Tool invocation from Assembly
|
|
389
|
+
Assembly calls a tool by pushing its input arguments onto the stack, then popping the tool’s output back off the stack. If your tool returns multiple values, declare the tool-call function’s return type as a tuple[...] so the assembly can read each result value correctly.
|
|
390
|
+
```python
|
|
391
|
+
@tool
|
|
392
|
+
def get_current_position(unit_id: int) -> tuple[int, int]:
|
|
393
|
+
return 5,5
|
|
394
|
+
```
|
|
395
|
+
This translates to assembly:
|
|
396
|
+
```Assembly
|
|
397
|
+
PUSH 1
|
|
398
|
+
CALL get_current_position
|
|
399
|
+
POP R1 ; x coordinate
|
|
400
|
+
POP R2 ; y coordinate
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
#### Tool input/output data types
|
|
404
|
+
The assembly emulator can store values of any type in its registers and stack, which makes it possible to write tool functions that return arbitrary objects. For Assembly, any non-integer value is coerced to a string (or to a JSON-formatted string where appropriate). When a value is stored as a string, comparison (CMP) and arithmetic instructions are not supported for that operand.
|
|
405
|
+
```Assembly
|
|
406
|
+
CALL get_unit
|
|
407
|
+
POP R1 ; a json string representing unit object from get_unit
|
|
408
|
+
CMP R1 100 ; Not possible, R1 is json string
|
|
409
|
+
PUSH R1
|
|
410
|
+
CALL heal_unit ; heal_unit will receive json string representing unit
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
#### Messages result
|
|
414
|
+
By default LangChain Middleware or LangGraph node will include all messages produced during assembly emulation including `AIMessage` that initiates tool in LangChain/LangGraph (with input arguments) as well as `ToolMessage` that contains the result of the tool invocation. You can control that by setting `infer_tools_messages` option provided to Langchain middleware or LangGraph node. If `False` - only assembly code will be included in the result messages.
|
|
415
|
+
|
|
416
|
+
#### Max number of assembly instructions to execute
|
|
417
|
+
By default the number of assembly instructions that could executed during emulation process is limited to 1000 (to prevent infinite jumps), you can control that by setting `max_instructions_to_exec` option in Langchain middleware or LangGraph node.
|
|
418
|
+
|
|
419
|
+
#### Custom prompt
|
|
420
|
+
By default the `llassembly/prompts_md/base.md` prompt is used to generate assembly instructions. You can replace that to your custom prompt setting `prompt_path` in Langchain middleware. Make sure that assembly generated by your custom prompt is supported by LLAssembly emulator.
|
|
421
|
+
|
|
422
|
+
#### Async support
|
|
423
|
+
Both LangChain and LangGraph implementations support async invocation of the agent. In case of Langchain the middleware will handle async by itself, in case of LangGraph use `AToolsPlannerNode` instead of `ToolsPlannerNode`
|