mojentic 0.7.1__py3-none-any.whl → 0.7.3__py3-none-any.whl

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.
@@ -2,7 +2,7 @@ import os
2
2
  from mojentic.agents.base_llm_agent import BaseLLMAgent
3
3
  from mojentic.llm.llm_broker import LLMBroker
4
4
  from mojentic.llm.tools.date_resolver import ResolveDateTool
5
- from mojentic.llm.tools.file_manager import FileManager, ListFilesTool, ReadFileTool, WriteFileTool
5
+ from mojentic.llm.tools.file_manager import FileManager, ListFilesTool, ReadFileTool, WriteFileTool, FilesystemGateway
6
6
  from mojentic.llm.tools.tool_wrapper import ToolWrapper
7
7
 
8
8
  #
@@ -15,19 +15,22 @@ temporal_specialist = BaseLLMAgent(
15
15
  behaviour="You are a historian and sociologist who focuses on sorting out temporal events, determining what happened or will happen when."
16
16
  )
17
17
 
18
+ if not os.path.exists("local"):
19
+ os.mkdir("local")
20
+
21
+ # Create a filesystem gateway for the local directory
22
+ fs = FilesystemGateway(base_path="local")
23
+
18
24
  knowledge_specialist = BaseLLMAgent(
19
25
  llm=LLMBroker(model="llama3.3-70b-32k"),
20
26
  tools=[
21
- ListFilesTool(path="local"),
22
- ReadFileTool(path="local"),
23
- WriteFileTool(path="local"),
27
+ ListFilesTool(fs),
28
+ ReadFileTool(fs),
29
+ WriteFileTool(fs),
24
30
  ],
25
31
  behaviour="You are a knowledge management agent who focuses on sorting out facts and information, able to organize elemental ideas and make connections between them. You can list files to find out where you stored information, read files to review that information, and write files to store that information for later retrieval."
26
32
  )
27
33
 
28
- if not os.path.exists("local"):
29
- os.mkdir("local")
30
-
31
34
 
32
35
 
33
36
  coordinator = BaseLLMAgent(
@@ -42,14 +45,14 @@ coordinator = BaseLLMAgent(
42
45
  result = coordinator.generate_response("""
43
46
 
44
47
  I have several things I need to do this week:
45
-
48
+
46
49
  - On Monday, I need to ensure that I have called Scotiabank and ordered replacement cards for my current, credit, and line of credit accounts.
47
50
  - On Wednesday, I need to drive into Toronto for work. While in Toronto I need to pick up razors. I need to make sure I see Gregg, Britney and Vikram.
48
51
  - On Thursday, I need to ensure I'm up by 7am so that I can be showered and ready for work by 9.
49
52
  - On Friday, I need to ensure that I have my laundry done and my bags packed for my trip to Ottawa.
50
-
53
+
51
54
  Create me a markdown file for each day of the week, named "YYYY-MM-DD-ToDo.md" where the date is the date of that day.
52
55
  Make a list of to-do items in the markdown file, and add a section for the day's daily notes that I can fill out each day.
53
56
  """)
54
57
 
55
- print(result)
58
+ print(result)
@@ -1,88 +1,181 @@
1
+ """
2
+ This example demonstrates the use of all the file management tools available in mojentic.
3
+
4
+ It creates an IterativeProblemSolver with access to a comprehensive set of file management tools:
5
+ - ListFilesTool: List files in the top-level directory
6
+ - ListAllFilesTool: List all files recursively
7
+ - ReadFileTool: Read the content of a file
8
+ - WriteFileTool: Write content to a file
9
+ - CreateDirectoryTool: Create a new directory
10
+ - FindFilesByGlobTool: Find files matching a glob pattern
11
+ - FindFilesContainingTool: Find files containing text matching a regex pattern
12
+ - FindLinesMatchingTool: Find lines in a file matching a regex pattern
13
+ - EditFileWithDiffTool: Edit a file by applying a diff
14
+
15
+ The solver is then given a task that requires using all of these tools.
16
+ """
17
+
1
18
  import os
2
19
  from pathlib import Path
3
20
 
4
- from pydantic import BaseModel
5
-
6
- from mojentic.agents.base_llm_agent import BaseLLMAgent
7
- from mojentic.agents.output_agent import OutputAgent
8
- from mojentic.dispatcher import Dispatcher
9
- from mojentic.event import Event
21
+ from mojentic.agents.iterative_problem_solver import IterativeProblemSolver
10
22
  from mojentic.llm.gateways import OpenAIGateway
11
23
  from mojentic.llm.llm_broker import LLMBroker
12
- from mojentic.llm.tools.file_manager import ReadFileTool, WriteFileTool
13
- from mojentic.router import Router
14
-
15
-
16
- class RequestEvent(Event):
17
- text: str
18
-
19
-
20
- class ResponseEvent(Event):
21
- text: str
22
-
23
-
24
- class ResponseModel(BaseModel):
25
- text: str
26
-
27
-
28
- base_dir = Path(__file__).parent.parent.parent.parent / "code-playground"
29
-
30
-
31
- class RequestAgent(BaseLLMAgent):
32
- def __init__(self, llm: LLMBroker):
33
- super().__init__(llm,
34
- "You are a helpful assistant.")
35
- self.add_tool(ReadFileTool(str(base_dir)))
36
- self.add_tool(WriteFileTool(str(base_dir)))
37
-
38
- def receive_event(self, event):
39
- response = self.generate_response(event.text)
40
- return [ResponseEvent(source=type(self), correlation_id=event.correlation_id, text=response)]
24
+ from mojentic.llm.tools.ephemeral_task_manager import EphemeralTaskList, AppendTaskTool, \
25
+ ClearTasksTool, CompleteTaskTool, InsertTaskAfterTool, ListTasksTool, PrependTaskTool, \
26
+ StartTaskTool
27
+ from mojentic.llm.tools.file_manager import (
28
+ ReadFileTool, WriteFileTool, ListFilesTool, ListAllFilesTool,
29
+ FindFilesByGlobTool, FindFilesContainingTool, FindLinesMatchingTool,
30
+ EditFileWithDiffTool, CreateDirectoryTool, FilesystemGateway
31
+ )
41
32
 
33
+ base_dir = Path(__file__).parent.parent.parent.parent / "code-playground2"
42
34
 
43
- # with open(base_dir / "spec.md", 'w') as file:
44
- # file.write("""
45
- # Primes
46
- #
47
- # You are to write some python code that will generate the first 100 prime numbers.
48
- #
49
- # Store the code in a file called primes.py
50
- # """.strip())
35
+ # Initialize the LLM broker
51
36
 
52
- #
53
- # OK this example is fun, it shows trying to make 2 consecutive
54
- # tool calls. The first tool call reads a file, the second writes a file.
55
- #
56
- # Ollama 3.1 70b seems to handle this consistently, 3.3 70b seems flakey, flakier when num_ctx is set to 32768
57
- # Ollama 3.1 8b seems to handle it about 1/3 the time
58
- # OpenAI gpt-4o-mini handles it perfectly every single time
59
- #
60
-
61
-
62
- # llm = LLMBroker("qwen2.5-coder:32b")
63
- # llm = LLMBroker("llama3.3")
64
37
  api_key = os.getenv("OPENAI_API_KEY")
65
38
  gateway = OpenAIGateway(api_key)
66
- llm = LLMBroker(model="gpt-4o", gateway=gateway)
67
- request_agent = RequestAgent(llm)
68
- output_agent = OutputAgent()
69
-
70
- router = Router({
71
- RequestEvent: [request_agent, output_agent],
72
- ResponseEvent: [output_agent]
73
- })
74
-
75
- dispatcher = Dispatcher(router)
76
- dispatcher.dispatch(RequestEvent(source=str, text="""
77
- # Mandelbrodt Set
39
+ llm = LLMBroker(model="o4-mini", gateway=gateway)
78
40
 
79
- You are to implement a program that will generate an image of the Mandelbrot set.
80
-
81
- The program should be written in Swift for macOS utilizing SwiftUI.
82
-
83
- The program should generate an image of the Mandelbrot set and display it in a window.
84
-
85
- Generate any files you require to store the source code.
86
-
87
- Generate a README.md file that includes instructions on how to run the program.
88
- """))
41
+ # llm = LLMBroker("qwen2.5-coder:32b")
42
+ # llm = LLMBroker("llama3.3")
43
+ # llm = LLMBroker(model="qwen3-128k:32b")
44
+
45
+ # Create a filesystem gateway for the sandbox
46
+ fs = FilesystemGateway(base_path=str(base_dir))
47
+
48
+ task_manager = EphemeralTaskList()
49
+
50
+ # Create a list of all file management tools
51
+ tools = [
52
+ ReadFileTool(fs),
53
+ WriteFileTool(fs),
54
+ ListFilesTool(fs),
55
+ ListAllFilesTool(fs),
56
+ CreateDirectoryTool(fs),
57
+ FindFilesByGlobTool(fs),
58
+ FindFilesContainingTool(fs),
59
+ FindLinesMatchingTool(fs),
60
+ EditFileWithDiffTool(fs),
61
+ AppendTaskTool(task_manager),
62
+ ClearTasksTool(task_manager),
63
+ CompleteTaskTool(task_manager),
64
+ InsertTaskAfterTool(task_manager),
65
+ ListTasksTool(task_manager),
66
+ PrependTaskTool(task_manager),
67
+ StartTaskTool(task_manager),
68
+ ]
69
+
70
+ # Create the iterative problem solver with the tools
71
+ solver = IterativeProblemSolver(
72
+ llm=llm,
73
+ available_tools=tools,
74
+ max_iterations=5,
75
+ system_prompt="""
76
+ # 0 - Project Identity & Context
77
+
78
+ You are an expert and principled software engineer, well versed in writing Python games. You work
79
+ carefully and purposefully and always check your work with an eye to testability and correctness.
80
+ You know that every line of code you write is a liability, and you take care that every line
81
+ matters.
82
+
83
+ # 1 - Universal Engineering Principles
84
+
85
+ * **Code is communication** — optimise for the next human reader.
86
+ * **Simple Design Heuristics** — guiding principles, not iron laws; consult the user when you
87
+ need to break them.
88
+ 1. **All tests pass** — correctness is non‑negotiable.
89
+ 2. **Reveals intent** — code should read like an explanation.
90
+ 3. **No *****knowledge***** duplication** — avoid multiple spots that must change together;
91
+ identical code is only a smell when it hides duplicate *decisions*.
92
+ 4. **Minimal entities** — remove unnecessary indirection, classes, or parameters.
93
+ * **Small, safe increments** — single‑reason commits; avoid speculative work (**YAGNI**).
94
+ * **Tests are the executable spec** — red first, green always; test behaviour not implementation.
95
+ * **Compose over inherit**; favour pure functions where practical, avoid side-effects.
96
+ * **Functional core, imperative shell** — isolate pure business logic from I/O and side effects;
97
+ push mutations to the system boundaries, build mockable gateways at those boundaries.
98
+ * **Psychological safety** — review code, not colleagues; critique ideas, not authors.
99
+ * **Version‑control etiquette** — descriptive commit messages, branch from `main`, PRs require
100
+ green CI.
101
+
102
+ # 2 - Python‑Specific Conventions
103
+
104
+ ## 2.1 Runtime & Environment
105
+
106
+ * **Python ≥ 3.12** (support the two most recent LTS releases).
107
+ * Create an isolated environment:
108
+
109
+ ```bash
110
+ python -m venv .venv
111
+ source .venv/bin/activate
112
+ pip install -e ".[dev]"
113
+ ```
114
+ * Enforce `pre‑commit` hooks (flake8, mypy, black, pytest).
115
+
116
+ ## 2.2 Core Libraries
117
+
118
+ Mandatory: pydantic, structlog, pytest, pytest-spec, pytest-cov, pytest-mock, flake8, black,
119
+ pre‑commit, mkdocs‑material. Add new libs only when they eliminate **significant** boilerplate or
120
+ risk.
121
+
122
+ ## 2.3 Project Structure & Imports
123
+
124
+ * **src‑layout**: code in `src/<package_name>/`; tests live beside code as `*_spec.py`.
125
+ * Import order: 1) stdlib, 2) third‑party, 3) first‑party — each group alphabetised with a blank
126
+ line.
127
+
128
+ ## 2.4 Naming & Style
129
+
130
+ * `snake_case` for functions & vars, `PascalCase` for classes, `UPPER_SNAKE` for constants.
131
+ * Prefix intentionally unused vars/args with `_`.
132
+ * **flake8** (with plugins) handles linting, and **black** auto‑formats code. Max line length
133
+ **100**.
134
+ * Cyclomatic complexity cap: **10** (flake8 `C901`).
135
+ * Use **f‑strings**; avoid magic numbers.
136
+
137
+ ## 2.5 Type Hints & Docstrings
138
+
139
+ * **100% type coverage**; code must pass `mypy --strict`.
140
+ * Use `pydantic.BaseModel` for data models; don't use bare `@dataclass` if validation is needed.
141
+ * Docstrings in Google format; omit the obvious.
142
+
143
+ ## 2.6 Logging & Observability
144
+
145
+ * Configure **structlog** for JSON output by default.
146
+ * Never use `print` for diagnostics; reserve for user‑facing CLI UX.
147
+ * Log levels: `DEBUG` (dev detail) → `INFO` (lifecycle) → `WARNING` (recoverable) → `ERROR` (user
148
+ visible).
149
+
150
+ ## 2.7 Testing Strategy
151
+
152
+ * **pytest** with **pytest-spec** for specification-style output.
153
+ * Test files end with `_spec.py` and live in the same folder as the code under test.
154
+ * Use **Arrange / Act / Assert** blocks separated by a blank line (no comments) **or** BDD
155
+ `describe/should` classes.
156
+ * Function names: use `should_*` and BDD-style specifications.
157
+ * Class names: use `Describe*` and BDD-style test suites.
158
+ * **Mocking**: Use `pytest-mock`'s `mocker` fixture; don't use `unittest.mock.MagicMock` directly.
159
+ * One behavioural expectation per test. Fixtures are helpers, not magic.
160
+ * Tests should fail for one reason, avoid multiple assert statements, split the test cases
161
+
162
+ # 3 - Planning and Goal Tracking
163
+
164
+ - Use the provided task manager tools to create your plans and work through them step by step.
165
+ - Before declaring yourself finished list all tasks, ensure they are all complete, and that you
166
+ have not missed any steps
167
+ - If you've missed or forgotten some steps, add them to the task list and continue
168
+ - When all tasks are complete, and you can think of no more to add, declare yourself finished.
169
+ """
170
+ )
171
+
172
+ # Define the task
173
+ task = """
174
+ Create a new Python project that is a graphical clone of Windows MineSweeper.
175
+
176
+ Ensure it is well tested.
177
+ """
178
+
179
+ # Solve the task and print the result
180
+ result = solver.solve(task)
181
+ print(result)
_examples/file_tool.py CHANGED
@@ -5,7 +5,7 @@ from mojentic.agents.output_agent import OutputAgent
5
5
  from mojentic.dispatcher import Dispatcher
6
6
  from mojentic.event import Event
7
7
  from mojentic.llm.llm_broker import LLMBroker
8
- from mojentic.llm.tools.file_manager import ReadFileTool, WriteFileTool
8
+ from mojentic.llm.tools.file_manager import ReadFileTool, WriteFileTool, FilesystemGateway
9
9
  from mojentic.router import Router
10
10
 
11
11
 
@@ -25,8 +25,10 @@ class RequestAgent(BaseLLMAgent):
25
25
  def __init__(self, llm: LLMBroker):
26
26
  super().__init__(llm,
27
27
  "You are a helpful assistant.")
28
- self.add_tool(ReadFileTool("/tmp"))
29
- self.add_tool(WriteFileTool("/tmp"))
28
+ # Create a filesystem gateway for the /tmp directory
29
+ fs = FilesystemGateway(base_path="/tmp")
30
+ self.add_tool(ReadFileTool(fs))
31
+ self.add_tool(WriteFileTool(fs))
30
32
 
31
33
  def receive_event(self, event):
32
34
  response = self.generate_response(event.text)
mojentic/__init__.py CHANGED
@@ -7,10 +7,12 @@ import logging
7
7
 
8
8
  import structlog
9
9
 
10
+ # Core components
10
11
  from .dispatcher import Dispatcher
11
12
  from .event import Event
12
13
  from .router import Router
13
14
 
15
+ # Initialize logging
14
16
  logging.basicConfig(level=logging.INFO)
15
17
  structlog.configure(logger_factory=structlog.stdlib.LoggerFactory(), processors=[
16
18
  structlog.stdlib.filter_by_level,
@@ -22,13 +24,6 @@ structlog.configure(logger_factory=structlog.stdlib.LoggerFactory(), processors=
22
24
  logger = structlog.get_logger()
23
25
  logger.info("Starting logger")
24
26
 
25
- # logger = logging.getLogger("mojentic")
26
- # path = Path().cwd()
27
- # log_filename = path / 'output.log'
28
- # print(f"Logging to {log_filename}")
29
- # logging.basicConfig(filename=log_filename, level=logging.DEBUG)
30
- # logger.info("Starting logger")
31
-
32
27
 
33
28
  __version__: str
34
29
  try:
@@ -1,7 +1,16 @@
1
+ """
2
+ Mojentic agents module for creating and working with various agent types.
3
+ """
4
+
5
+ # Base agent types
1
6
  from .base_agent import BaseAgent
2
- from .base_llm_agent import BaseLLMAgent
3
- from .base_llm_agent import BaseLLMAgentWithMemory
7
+ from .base_llm_agent import BaseLLMAgent, BaseLLMAgentWithMemory
8
+
9
+ # Special purpose agents
4
10
  from .correlation_aggregator_agent import BaseAggregatingAgent
5
11
  from .output_agent import OutputAgent
6
12
  from .iterative_problem_solver import IterativeProblemSolver
7
13
  from .simple_recursive_agent import SimpleRecursiveAgent
14
+
15
+ # Agent brokering
16
+ from .agent_broker import AgentBroker
@@ -1 +1,5 @@
1
+ """
2
+ Mojentic context module for managing shared working memory and context.
3
+ """
4
+
1
5
  from .shared_working_memory import SharedWorkingMemory
mojentic/llm/__init__.py CHANGED
@@ -1,4 +1,16 @@
1
+ """
2
+ Mojentic LLM module for interacting with Large Language Models.
3
+ """
4
+
5
+ # Main LLM components
1
6
  from .llm_broker import LLMBroker
2
- from .registry.llm_registry import LLMRegistry
3
7
  from .chat_session import ChatSession
4
- from .message_composers import MessageBuilder, FileTypeSensor
8
+ from .message_composers import MessageBuilder, FileTypeSensor
9
+ from .registry.llm_registry import LLMRegistry
10
+
11
+ # Re-export gateway components at the LLM level
12
+ from .gateways.models import (
13
+ LLMMessage,
14
+ LLMGatewayResponse,
15
+ MessageRole
16
+ )
@@ -1,3 +1,25 @@
1
+ """
2
+ Mojentic LLM gateways module for connecting to various LLM providers.
3
+ """
4
+
5
+ # Gateway implementations
1
6
  from .llm_gateway import LLMGateway
2
7
  from .ollama import OllamaGateway
3
8
  from .openai import OpenAIGateway
9
+ from .anthropic import AnthropicGateway
10
+ from .file_gateway import FileGateway
11
+ from .embeddings_gateway import EmbeddingsGateway
12
+ from .tokenizer_gateway import TokenizerGateway
13
+
14
+ # Message adapters
15
+ from .anthropic_messages_adapter import adapt_messages_to_anthropic
16
+ from .ollama_messages_adapter import adapt_messages_to_ollama
17
+ from .openai_messages_adapter import adapt_messages_to_openai
18
+
19
+ # Common models
20
+ from .models import (
21
+ LLMMessage,
22
+ MessageRole,
23
+ LLMGatewayResponse,
24
+ LLMToolCall
25
+ )
@@ -29,7 +29,7 @@ class AnthropicGateway(LLMGateway):
29
29
  response = self.client.messages.create(
30
30
  **anthropic_args,
31
31
  temperature=args.get('temperature', 1.0),
32
- max_tokens=args.get('num_predict', 2000),
32
+ max_tokens=args.get('max_tokens', args.get('num_predict', 2000)),
33
33
  # thinking={
34
34
  # "type": "enabled",
35
35
  # "budget_tokens": 32768,
@@ -19,7 +19,7 @@ class LLMGateway:
19
19
  object_model: Optional[Type[BaseModel]] = None,
20
20
  tools: Optional[List[LLMTool]] = None,
21
21
  temperature: float = 1.0,
22
- num_ctx: int = 32768,
22
+ num_ctx: int = 32768, max_tokens: int = 16384,
23
23
  num_predict: int = -1) -> LLMGatewayResponse:
24
24
  """
25
25
  Complete the LLM request.
@@ -39,6 +39,8 @@ class LLMGateway:
39
39
  The temperature to use for the response. Defaults to 1.0.
40
40
  num_ctx : int
41
41
  The number of context tokens to use. Defaults to 32768.
42
+ max_tokens : int
43
+ The maximum number of tokens to generate. Defaults to 16384.
42
44
  num_predict : int
43
45
  The number of tokens to predict. Defaults to no limit.
44
46
 
@@ -35,6 +35,8 @@ class OllamaGateway(LLMGateway):
35
35
  )
36
36
  if args.get('num_predict', 0) > 0:
37
37
  options.num_predict = args['num_predict']
38
+ if 'max_tokens' in args:
39
+ options.num_predict = args['max_tokens']
38
40
  return options
39
41
 
40
42
  def complete(self, **args) -> LLMGatewayResponse:
@@ -56,6 +58,8 @@ class OllamaGateway(LLMGateway):
56
58
  The temperature to use for the response. Defaults to 1.0.
57
59
  num_ctx : int, optional
58
60
  The number of context tokens to use. Defaults to 32768.
61
+ max_tokens : int, optional
62
+ The maximum number of tokens to generate. Defaults to 16384.
59
63
  num_predict : int, optional
60
64
  The number of tokens to predict. Defaults to no limit.
61
65
 
@@ -120,6 +124,8 @@ class OllamaGateway(LLMGateway):
120
124
  The temperature to use for the response. Defaults to 1.0.
121
125
  num_ctx : int, optional
122
126
  The number of context tokens to use. Defaults to 32768.
127
+ max_tokens : int, optional
128
+ The maximum number of tokens to generate. Defaults to 16384.
123
129
  num_predict : int, optional
124
130
  The number of tokens to predict. Defaults to no limit.
125
131
 
@@ -46,6 +46,8 @@ class OpenAIGateway(LLMGateway):
46
46
  The temperature to use for the response. Defaults to 1.0.
47
47
  num_ctx : int, optional
48
48
  The number of context tokens to use. Defaults to 32768.
49
+ max_tokens : int, optional
50
+ The maximum number of tokens to generate. Defaults to 16384.
49
51
  num_predict : int, optional
50
52
  The number of tokens to predict. Defaults to no limit.
51
53
 
@@ -68,6 +70,9 @@ class OpenAIGateway(LLMGateway):
68
70
  if 'tools' in args and args['tools'] is not None:
69
71
  openai_args['tools'] = [t.descriptor for t in args['tools']]
70
72
 
73
+ if 'max_tokens' in args:
74
+ openai_args['max_tokens'] = args['max_tokens']
75
+
71
76
  response = completion(**openai_args)
72
77
 
73
78
  object = None