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.
- _examples/broker_as_tool.py +13 -10
- _examples/coding_file_tool.py +170 -77
- _examples/file_tool.py +5 -3
- mojentic/__init__.py +2 -7
- mojentic/agents/__init__.py +11 -2
- mojentic/context/__init__.py +4 -0
- mojentic/llm/__init__.py +14 -2
- mojentic/llm/gateways/__init__.py +22 -0
- mojentic/llm/gateways/anthropic.py +1 -1
- mojentic/llm/gateways/llm_gateway.py +3 -1
- mojentic/llm/gateways/ollama.py +6 -0
- mojentic/llm/gateways/openai.py +5 -0
- mojentic/llm/llm_broker.py +42 -24
- mojentic/llm/message_composers.py +1 -1
- mojentic/llm/registry/__init__.py +6 -0
- mojentic/llm/registry/populate_registry_from_ollama.py +13 -12
- mojentic/llm/tools/__init__.py +18 -0
- mojentic/llm/tools/date_resolver.py +5 -2
- mojentic/llm/tools/ephemeral_task_manager/__init__.py +8 -8
- mojentic/llm/tools/file_manager.py +603 -42
- mojentic/llm/tools/file_manager_spec.py +723 -0
- mojentic/llm/tools/tool_wrapper.py +7 -3
- mojentic/tracer/__init__.py +8 -3
- {mojentic-0.7.1.dist-info → mojentic-0.7.3.dist-info}/METADATA +3 -2
- {mojentic-0.7.1.dist-info → mojentic-0.7.3.dist-info}/RECORD +28 -27
- {mojentic-0.7.1.dist-info → mojentic-0.7.3.dist-info}/WHEEL +0 -0
- {mojentic-0.7.1.dist-info → mojentic-0.7.3.dist-info}/licenses/LICENSE.md +0 -0
- {mojentic-0.7.1.dist-info → mojentic-0.7.3.dist-info}/top_level.txt +0 -0
mojentic/llm/llm_broker.py
CHANGED
|
@@ -5,18 +5,19 @@ from typing import List, Optional, Type
|
|
|
5
5
|
import structlog
|
|
6
6
|
from pydantic import BaseModel
|
|
7
7
|
|
|
8
|
-
from mojentic.tracer.tracer_system import TracerSystem
|
|
9
8
|
from mojentic.llm.gateways.llm_gateway import LLMGateway
|
|
10
9
|
from mojentic.llm.gateways.models import MessageRole, LLMMessage, LLMGatewayResponse
|
|
11
10
|
from mojentic.llm.gateways.ollama import OllamaGateway
|
|
12
11
|
from mojentic.llm.gateways.tokenizer_gateway import TokenizerGateway
|
|
12
|
+
from mojentic.tracer.tracer_system import TracerSystem
|
|
13
13
|
|
|
14
14
|
logger = structlog.get_logger()
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
class LLMBroker():
|
|
18
18
|
"""
|
|
19
|
-
This class is responsible for managing interaction with a Large Language Model. It abstracts
|
|
19
|
+
This class is responsible for managing interaction with a Large Language Model. It abstracts
|
|
20
|
+
the user
|
|
20
21
|
from the specific mechanics of the LLM and provides a common interface for generating responses.
|
|
21
22
|
"""
|
|
22
23
|
|
|
@@ -25,7 +26,8 @@ class LLMBroker():
|
|
|
25
26
|
model: str
|
|
26
27
|
tracer: Optional[TracerSystem]
|
|
27
28
|
|
|
28
|
-
def __init__(self, model: str, gateway: Optional[LLMGateway] = None,
|
|
29
|
+
def __init__(self, model: str, gateway: Optional[LLMGateway] = None,
|
|
30
|
+
tokenizer: Optional[TokenizerGateway] = None,
|
|
29
31
|
tracer: Optional[TracerSystem] = None):
|
|
30
32
|
"""
|
|
31
33
|
Create an instance of the LLMBroker.
|
|
@@ -35,10 +37,12 @@ class LLMBroker():
|
|
|
35
37
|
model
|
|
36
38
|
The name of the model to use.
|
|
37
39
|
gateway
|
|
38
|
-
The gateway to use for communication with the LLM. If None, a gateway is created that
|
|
40
|
+
The gateway to use for communication with the LLM. If None, a gateway is created that
|
|
41
|
+
will utilize a local
|
|
39
42
|
Ollama server.
|
|
40
43
|
tokenizer
|
|
41
|
-
The gateway to use for tokenization. This is used to log approximate token counts for
|
|
44
|
+
The gateway to use for tokenization. This is used to log approximate token counts for
|
|
45
|
+
the LLM calls. If
|
|
42
46
|
None, `mxbai-embed-large` is used on a local Ollama server.
|
|
43
47
|
tracer
|
|
44
48
|
Optional tracer system to record LLM calls and responses.
|
|
@@ -58,8 +62,9 @@ class LLMBroker():
|
|
|
58
62
|
else:
|
|
59
63
|
self.adapter = gateway
|
|
60
64
|
|
|
61
|
-
def generate(self, messages: List[LLMMessage], tools=None, temperature=1.0, num_ctx=32768,
|
|
62
|
-
|
|
65
|
+
def generate(self, messages: List[LLMMessage], tools=None, temperature=1.0, num_ctx=32768,
|
|
66
|
+
num_predict=-1, max_tokens=16384,
|
|
67
|
+
correlation_id: str = None) -> str:
|
|
63
68
|
"""
|
|
64
69
|
Generate a text response from the LLM.
|
|
65
70
|
|
|
@@ -68,7 +73,8 @@ class LLMBroker():
|
|
|
68
73
|
messages : LLMMessage
|
|
69
74
|
A list of messages to send to the LLM.
|
|
70
75
|
tools : List[Tool]
|
|
71
|
-
A list of tools to use with the LLM. If a tool call is requested, the tool will be
|
|
76
|
+
A list of tools to use with the LLM. If a tool call is requested, the tool will be
|
|
77
|
+
called and the output
|
|
72
78
|
will be included in the response.
|
|
73
79
|
temperature : float
|
|
74
80
|
The temperature to use for the response. Defaults to 1.0
|
|
@@ -91,10 +97,11 @@ class LLMBroker():
|
|
|
91
97
|
messages_for_tracer = [m.model_dump() for m in messages]
|
|
92
98
|
|
|
93
99
|
# Record LLM call in tracer
|
|
94
|
-
tools_for_tracer = [{"name": t.name, "description": t.description} for t in
|
|
100
|
+
tools_for_tracer = [{"name": t.name, "description": t.description} for t in
|
|
101
|
+
tools] if tools else None
|
|
95
102
|
self.tracer.record_llm_call(
|
|
96
|
-
self.model,
|
|
97
|
-
messages_for_tracer,
|
|
103
|
+
self.model,
|
|
104
|
+
messages_for_tracer,
|
|
98
105
|
temperature,
|
|
99
106
|
tools=tools_for_tracer,
|
|
100
107
|
source=type(self),
|
|
@@ -110,12 +117,14 @@ class LLMBroker():
|
|
|
110
117
|
tools=tools,
|
|
111
118
|
temperature=temperature,
|
|
112
119
|
num_ctx=num_ctx,
|
|
113
|
-
num_predict=num_predict
|
|
120
|
+
num_predict=num_predict,
|
|
121
|
+
max_tokens=max_tokens)
|
|
114
122
|
|
|
115
123
|
call_duration_ms = (time.time() - start_time) * 1000
|
|
116
124
|
|
|
117
125
|
# Record LLM response in tracer
|
|
118
|
-
tool_calls_for_tracer = [tc.model_dump() for tc in
|
|
126
|
+
tool_calls_for_tracer = [tc.model_dump() for tc in
|
|
127
|
+
result.tool_calls] if result.tool_calls else None
|
|
119
128
|
self.tracer.record_llm_response(
|
|
120
129
|
self.model,
|
|
121
130
|
result.content,
|
|
@@ -153,13 +162,17 @@ class LLMBroker():
|
|
|
153
162
|
logger.info('Function output', output=output)
|
|
154
163
|
messages.append(LLMMessage(role=MessageRole.Assistant, tool_calls=[tool_call]))
|
|
155
164
|
messages.append(
|
|
156
|
-
LLMMessage(role=MessageRole.Tool, content=json.dumps(output),
|
|
157
|
-
|
|
158
|
-
|
|
165
|
+
LLMMessage(role=MessageRole.Tool, content=json.dumps(output),
|
|
166
|
+
tool_calls=[tool_call]))
|
|
167
|
+
# {'role': 'tool', 'content': str(output), 'name': tool_call.name,
|
|
168
|
+
# 'tool_call_id': tool_call.id})
|
|
169
|
+
return self.generate(messages, tools, temperature, num_ctx, num_predict,
|
|
170
|
+
correlation_id=correlation_id)
|
|
159
171
|
else:
|
|
160
172
|
logger.warn('Function not found', function=tool_call.name)
|
|
161
173
|
logger.info('Expected usage of missing function', expected_usage=tool_call)
|
|
162
|
-
# raise Exception('Unknown tool function requested:',
|
|
174
|
+
# raise Exception('Unknown tool function requested:',
|
|
175
|
+
# requested_tool.function.name)
|
|
163
176
|
|
|
164
177
|
return result.content
|
|
165
178
|
|
|
@@ -170,8 +183,9 @@ class LLMBroker():
|
|
|
170
183
|
content += message.content
|
|
171
184
|
return content
|
|
172
185
|
|
|
173
|
-
def generate_object(self, messages: List[LLMMessage], object_model: Type[BaseModel],
|
|
174
|
-
num_predict=-1,
|
|
186
|
+
def generate_object(self, messages: List[LLMMessage], object_model: Type[BaseModel],
|
|
187
|
+
temperature=1.0, num_ctx=32768, num_predict=-1, max_tokens=16384,
|
|
188
|
+
correlation_id: str = None) -> BaseModel:
|
|
175
189
|
"""
|
|
176
190
|
Generate a structured response from the LLM and return it as an object.
|
|
177
191
|
|
|
@@ -203,8 +217,8 @@ class LLMBroker():
|
|
|
203
217
|
|
|
204
218
|
# Record LLM call in tracer
|
|
205
219
|
self.tracer.record_llm_call(
|
|
206
|
-
self.model,
|
|
207
|
-
messages_for_tracer,
|
|
220
|
+
self.model,
|
|
221
|
+
messages_for_tracer,
|
|
208
222
|
temperature,
|
|
209
223
|
tools=None,
|
|
210
224
|
source=type(self),
|
|
@@ -214,14 +228,18 @@ class LLMBroker():
|
|
|
214
228
|
# Measure call duration for audit
|
|
215
229
|
start_time = time.time()
|
|
216
230
|
|
|
217
|
-
result = self.adapter.complete(model=self.model, messages=messages,
|
|
218
|
-
|
|
231
|
+
result = self.adapter.complete(model=self.model, messages=messages,
|
|
232
|
+
object_model=object_model,
|
|
233
|
+
temperature=temperature, num_ctx=num_ctx,
|
|
234
|
+
num_predict=num_predict, max_tokens=max_tokens)
|
|
219
235
|
|
|
220
236
|
call_duration_ms = (time.time() - start_time) * 1000
|
|
221
237
|
|
|
222
238
|
# Record LLM response in tracer with object representation
|
|
223
239
|
# Convert object to string for tracer
|
|
224
|
-
object_str = str(result.object.model_dump()) if hasattr(result.object,
|
|
240
|
+
object_str = str(result.object.model_dump()) if hasattr(result.object,
|
|
241
|
+
"model_dump") else str(
|
|
242
|
+
result.object)
|
|
225
243
|
self.tracer.record_llm_response(
|
|
226
244
|
self.model,
|
|
227
245
|
f"Structured response: {object_str}",
|
|
@@ -216,7 +216,7 @@ class MessageBuilder():
|
|
|
216
216
|
|
|
217
217
|
return self
|
|
218
218
|
|
|
219
|
-
def add_files(self, *file_paths: Union[str, Path]) -> "MessageBuilder":
|
|
219
|
+
def add_files(self, *file_paths: List[Union[str, Path]]) -> "MessageBuilder":
|
|
220
220
|
"""
|
|
221
221
|
Add multiple text files to the message, ignoring binary files.
|
|
222
222
|
|
|
@@ -46,15 +46,16 @@ def register_llms_from_ollama(url: str, registry: LLMRegistry):
|
|
|
46
46
|
registry.register(entry)
|
|
47
47
|
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
print(f"
|
|
56
|
-
print(f"Fastest model
|
|
57
|
-
print(f"Fastest model with
|
|
58
|
-
print(f"
|
|
59
|
-
print(f"Smartest model
|
|
60
|
-
print(f"Smartest model with
|
|
49
|
+
if __name__ == "__main__":
|
|
50
|
+
# Example usage
|
|
51
|
+
ollama_url = "http://localhost:11434/api/tags"
|
|
52
|
+
registry = LLMRegistry()
|
|
53
|
+
register_llms_from_ollama(ollama_url, registry)
|
|
54
|
+
|
|
55
|
+
# print(f"Tool using: {registry.find_first(tools=True, structured_output=True).name}")
|
|
56
|
+
print(f"Fastest model: {registry.find_fastest().name}")
|
|
57
|
+
print(f"Fastest model with tools: {registry.find_fastest(tools=True).name}")
|
|
58
|
+
print(f"Fastest model with structured output: {registry.find_fastest(structured_output=True).name}")
|
|
59
|
+
print(f"Smartest model: {registry.find_smartest().name}")
|
|
60
|
+
print(f"Smartest model with tools: {registry.find_smartest(tools=True).name}")
|
|
61
|
+
print(f"Smartest model with structured output: {registry.find_smartest(structured_output=True).name}")
|
mojentic/llm/tools/__init__.py
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Mojentic LLM tools module for extending LLM capabilities.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
# Base tool class
|
|
6
|
+
from .llm_tool import LLMTool
|
|
7
|
+
from .tool_wrapper import ToolWrapper
|
|
8
|
+
|
|
9
|
+
# Common tools
|
|
10
|
+
from .ask_user_tool import AskUserTool
|
|
11
|
+
from .current_datetime import CurrentDateTimeTool
|
|
12
|
+
from .date_resolver import ResolveDateTool
|
|
13
|
+
from .organic_web_search import OrganicWebSearchTool
|
|
14
|
+
from .tell_user_tool import TellUserTool
|
|
15
|
+
|
|
16
|
+
# Import tool modules
|
|
17
|
+
from . import file_manager
|
|
18
|
+
from . import ephemeral_task_manager
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
from typing import Optional
|
|
1
|
+
from typing import Optional, TYPE_CHECKING
|
|
2
2
|
|
|
3
3
|
from parsedatetime import Calendar, VERSION_CONTEXT_STYLE
|
|
4
4
|
from pytz import timezone
|
|
5
5
|
|
|
6
|
-
from mojentic.llm import LLMBroker
|
|
7
6
|
from mojentic.llm.tools.llm_tool import LLMTool
|
|
8
7
|
|
|
8
|
+
# Avoid circular imports with TYPE_CHECKING
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from mojentic.llm.llm_broker import LLMBroker
|
|
11
|
+
|
|
9
12
|
|
|
10
13
|
class ResolveDateTool(LLMTool):
|
|
11
14
|
def run(self, relative_date_found: str, reference_date_in_iso8601: Optional[str] = None) -> dict[str, str]:
|
|
@@ -5,14 +5,14 @@ This module provides tools for appending, prepending, inserting, starting, compl
|
|
|
5
5
|
Tasks follow a state machine that transitions from PENDING through IN_PROGRESS to COMPLETED.
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
|
-
from
|
|
9
|
-
from
|
|
10
|
-
from
|
|
11
|
-
from
|
|
12
|
-
from
|
|
13
|
-
from
|
|
14
|
-
from
|
|
15
|
-
from
|
|
8
|
+
from .append_task_tool import AppendTaskTool
|
|
9
|
+
from .clear_tasks_tool import ClearTasksTool
|
|
10
|
+
from .complete_task_tool import CompleteTaskTool
|
|
11
|
+
from .insert_task_after_tool import InsertTaskAfterTool
|
|
12
|
+
from .list_tasks_tool import ListTasksTool
|
|
13
|
+
from .prepend_task_tool import PrependTaskTool
|
|
14
|
+
from .start_task_tool import StartTaskTool
|
|
15
|
+
from .ephemeral_task_list import EphemeralTaskList, Task
|
|
16
16
|
|
|
17
17
|
__all__ = [
|
|
18
18
|
"EphemeralTaskList",
|