mojentic 0.5.3__py3-none-any.whl → 0.5.4__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.
@@ -0,0 +1,9 @@
1
+ import os
2
+
3
+ from mojentic.llm.gateways import OllamaGateway, OpenAIGateway
4
+
5
+ ollama = OllamaGateway()
6
+ print(len(ollama.calculate_embeddings("Hello, world! " * 5000)))
7
+
8
+ openai = OpenAIGateway(os.environ["OPENAI_API_KEY"])
9
+ print(len(openai.calculate_embeddings("Hello, world! " * 5000)))
@@ -26,7 +26,8 @@ class IterativeProblemSolver:
26
26
  max_iterations: int
27
27
  chat: ChatSession
28
28
 
29
- def __init__(self, llm: LLMBroker, available_tools: Optional[List[LLMTool]] = None, max_iterations: int = 3):
29
+ def __init__(self, llm: LLMBroker, available_tools: Optional[List[LLMTool]] = None, max_iterations: int = 3,
30
+ system_prompt: Optional[str] = None):
30
31
  """Initialize the IterativeProblemSolver.
31
32
 
32
33
  Parameters
@@ -42,7 +43,9 @@ class IterativeProblemSolver:
42
43
  self.available_tools = available_tools or []
43
44
  self.chat = ChatSession(
44
45
  llm=llm,
45
- system_prompt="You are a helpful assistant, working on behalf of the user on a specific user request.",
46
+ system_prompt=system_prompt or "You are a problem-solving assistant that can solve complex problems step by step. "
47
+ "You analyze problems, break them down into smaller parts, and solve them systematically. "
48
+ "If you cannot solve a problem completely in one step, you make progress and identify what to do next.",
46
49
  tools=self.available_tools,
47
50
  )
48
51
 
@@ -140,7 +140,7 @@ class SimpleRecursiveAgent:
140
140
  emitter: EventEmitter
141
141
  chat: ChatSession
142
142
 
143
- def __init__(self, llm: LLMBroker, available_tools: Optional[List[LLMTool]] = None, max_iterations: int = 5):
143
+ def __init__(self, llm: LLMBroker, available_tools: Optional[List[LLMTool]] = None, max_iterations: int = 5, system_prompt: Optional[str] = None):
144
144
  """
145
145
  Initialize the SimpleRecursiveAgent.
146
146
 
@@ -161,7 +161,7 @@ class SimpleRecursiveAgent:
161
161
  # Initialize the chat session
162
162
  self.chat = ChatSession(
163
163
  llm=llm,
164
- system_prompt="You are a problem-solving assistant that can solve complex problems step by step. "
164
+ system_prompt=system_prompt or "You are a problem-solving assistant that can solve complex problems step by step. "
165
165
  "You analyze problems, break them down into smaller parts, and solve them systematically. "
166
166
  "If you cannot solve a problem completely in one step, you make progress and identify what to do next.",
167
167
  tools=self.available_tools
@@ -1,12 +1,15 @@
1
1
  import json
2
- from typing import Type, List
2
+ from itertools import islice
3
+ from typing import Type, List, Iterable
3
4
 
5
+ import numpy as np
4
6
  import structlog
5
7
  from openai import OpenAI
6
8
 
7
9
  from mojentic.llm.gateways.llm_gateway import LLMGateway
8
10
  from mojentic.llm.gateways.models import LLMToolCall, LLMGatewayResponse
9
11
  from mojentic.llm.gateways.openai_messages_adapter import adapt_messages_to_openai
12
+ from mojentic.llm.gateways.tokenizer_gateway import TokenizerGateway
10
13
 
11
14
  logger = structlog.get_logger()
12
15
 
@@ -121,8 +124,28 @@ class OpenAIGateway(LLMGateway):
121
124
  The embeddings for the text.
122
125
  """
123
126
  logger.debug("calculate_embeddings", text=text, model=model)
124
- response = self.client.embeddings.create(
125
- model=model,
126
- input=text
127
- )
128
- return response.data[0].embedding
127
+
128
+ embeddings = [self.client.embeddings.create(model=model, input=chunk).data[0].embedding
129
+ for chunk in self._chunked_tokens(text, 8191)]
130
+ lengths = [len(embedding) for embedding in embeddings]
131
+
132
+ average = np.average(embeddings, axis=0, weights=lengths)
133
+ average = average / np.linalg.norm(average)
134
+ average = average.tolist()
135
+
136
+ return average
137
+
138
+ def _batched(self, iterable: Iterable, n: int):
139
+ """Batch data into tuples of length n. The last batch may be shorter."""
140
+ # batched('ABCDEFG', 3) --> ABC DEF G
141
+ if n < 1:
142
+ raise ValueError('n must be at least one')
143
+ it = iter(iterable)
144
+ while batch := tuple(islice(it, n)):
145
+ yield batch
146
+
147
+ def _chunked_tokens(self, text, chunk_length):
148
+ tokenizer = TokenizerGateway()
149
+ tokens = tokenizer.encode(text)
150
+ chunks_iterator = self._batched(tokens, chunk_length)
151
+ yield from chunks_iterator
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mojentic
3
- Version: 0.5.3
3
+ Version: 0.5.4
4
4
  Summary: Mojentic is an agentic framework that aims to provide a simple and flexible way to assemble teams of agents to solve complex problems.
5
5
  Author-email: Stacey Vetzal <stacey@vetzal.com>
6
6
  Project-URL: Homepage, https://github.com/mojility/mojentic
@@ -13,6 +13,7 @@ Description-Content-Type: text/markdown
13
13
  License-File: LICENSE.md
14
14
  Requires-Dist: pydantic
15
15
  Requires-Dist: structlog
16
+ Requires-Dist: numpy
16
17
  Requires-Dist: ollama
17
18
  Requires-Dist: openai
18
19
  Requires-Dist: anthropic
@@ -17,6 +17,7 @@ _examples/image_broker.py,sha256=SoIphNj98zk1i7EQc7M2n2O_9ShDHwErmJ6jf67mzX0,355
17
17
  _examples/image_broker_splat.py,sha256=O7rzTFUka32if4G4VuXvhu1O-2lRMWfi0r8gjIE8-0Y,1934
18
18
  _examples/iterative_solver.py,sha256=ANGdC74ymHosVt6xUBjplkJl_W3ALTGxOkDpPLEDcm8,1331
19
19
  _examples/list_models.py,sha256=8noMpGeXOdX5Pf0NXCt_CRurOKEg_5luhWveGntBhe8,578
20
+ _examples/oversized_embeddings.py,sha256=_z2JoqZn0g7VtRsFVWIkngVqzjhQQvCEUYWVxs1I7MM,284
20
21
  _examples/raw.py,sha256=Y2wvgynFuoUs28agE4ijsLYec8VRjiReklqlCH2lERs,442
21
22
  _examples/react.py,sha256=VQ-5MmjUXoHzBFPTV_JrocuOkDzZ8oyUUSYLlEToJ_0,939
22
23
  _examples/recursive_agent.py,sha256=NHmX8iQc1y43AKh1ZlAdV9OZ18qVaRRmSaKALE_KI6k,2966
@@ -47,9 +48,9 @@ mojentic/agents/base_agent.py,sha256=m1SRyzY9ou4tesFDszonC4Y8moQMg9eofvJeuhoXmM0
47
48
  mojentic/agents/base_llm_agent.py,sha256=26pe0nS7UD1x1-NZjGMQMguxxQBjcRDyXMzcSmJOd2c,3094
48
49
  mojentic/agents/base_llm_agent_spec.py,sha256=1kzayCtAZY7oWloaFMh7-NkTtiKihDCLD3i9HwH3_Uc,2571
49
50
  mojentic/agents/correlation_aggregator_agent.py,sha256=okdq92rMNwIXTgnuwwn3zp4JzSxTh05vGWzTGocOgbU,1134
50
- mojentic/agents/iterative_problem_solver.py,sha256=dU0Zd-cYOyuZ2GM36_oRK1KD3_gEZJpMqvLqPhbbxGo,3980
51
+ mojentic/agents/iterative_problem_solver.py,sha256=RLt8MCM99d-Z26EhC5UnmE0M97EM475VGBVsNio8DSA,4329
51
52
  mojentic/agents/output_agent.py,sha256=I9GHcWvQhWxEcvjzj47scKo_QF8_6yR17ZUEHhwC94Y,231
52
- mojentic/agents/simple_recursive_agent.py,sha256=10L4MshVn9s2MtBUxZfJeXZakOSxfKQh0EkcwPLbfiE,9896
53
+ mojentic/agents/simple_recursive_agent.py,sha256=DcJFttTAYurRIObTL8kfbHUs5uvuqh6prEgA5nmxCLo,9950
53
54
  mojentic/audit/event_store.py,sha256=uw5ploNym9CvI-WDeW3Y4qHJupPWBQaGrfrubxOfmpA,130
54
55
  mojentic/audit/event_store_spec.py,sha256=mCryZGWNXP9CmukvD3hUyQGs_7afjwbTK3RdT831Lzc,594
55
56
  mojentic/context/__init__.py,sha256=MKMP7ViQg8gMtLFvn9pf47XMc5beA5Wx95K4dEw93z8,55
@@ -71,7 +72,7 @@ mojentic/llm/gateways/models.py,sha256=rHoXkaK5FOgWV5X-nkFhqubwk3sUQceJj3rBnxgqB
71
72
  mojentic/llm/gateways/ollama.py,sha256=629fpZhC0zVCYqj360-PKTT4mQOLec5nzzvfMtS_mLQ,7581
72
73
  mojentic/llm/gateways/ollama_messages_adapter.py,sha256=kUN_p2FyN88_trXMcL-Xsn9xPBU7pGKlJwTUEUCf6G4,1404
73
74
  mojentic/llm/gateways/ollama_messages_adapter_spec.py,sha256=gVRbWDrHOa1EiZ0CkEWe0pGn-GKRqdGb-x56HBQeYSE,4981
74
- mojentic/llm/gateways/openai.py,sha256=lYD2-DWt2W3fnmO7k8m29PD-wx4UDvcpPLduanj7tzc,4473
75
+ mojentic/llm/gateways/openai.py,sha256=NQECA41PS21RhXjwQdbH3DAP15WqkEl22UlS1ka_lLM,5434
75
76
  mojentic/llm/gateways/openai_message_adapter_spec.py,sha256=IgXyio15nXn11MiFpUnAxm5fFpPU9m-c4qerd9gbpBA,6473
76
77
  mojentic/llm/gateways/openai_messages_adapter.py,sha256=Qwidv7C-wXDOEV8NYNjgIbHnJSlkcoK10FaeLos5zTc,2882
77
78
  mojentic/llm/gateways/tokenizer_gateway.py,sha256=ztuqfunlJ6xmyUPPHcC_69-kegiNJD6jdSEde7hDh2w,485
@@ -91,8 +92,8 @@ mojentic/llm/tools/tool_wrapper_spec.py,sha256=LGqtre-g8SzOy3xtpbMdgTnw2EdYutmFO
91
92
  mojentic/llm/tools/web_search.py,sha256=L1accST2xMhGAkwHCLlIvKihTLiaYxl0NI6IqCJWGCw,1102
92
93
  mojentic/utils/__init__.py,sha256=lqECkkoFvHFttDnafRE1vvh0Dmna_lwupMToP5VvX5k,115
93
94
  mojentic/utils/formatting.py,sha256=bPrwwdluXdQ8TsFxfWtHNOeMWKNvAfABSoUnnA1g7c8,947
94
- mojentic-0.5.3.dist-info/licenses/LICENSE.md,sha256=txSgV8n5zY1W3NiF5HHsCwlaW0e8We1cSC6TuJUqxXA,1060
95
- mojentic-0.5.3.dist-info/METADATA,sha256=0WlHklNeQXHa9joHQTemZmBVRYeI12UVTHjZ46Ao9c4,4935
96
- mojentic-0.5.3.dist-info/WHEEL,sha256=SmOxYU7pzNKBqASvQJ7DjX3XGUF92lrGhMb3R6_iiqI,91
97
- mojentic-0.5.3.dist-info/top_level.txt,sha256=Q-BvPQ8Eu1jnEqK8Xkr6A9C8Xa1z38oPZRHuA5MCTqg,19
98
- mojentic-0.5.3.dist-info/RECORD,,
95
+ mojentic-0.5.4.dist-info/licenses/LICENSE.md,sha256=txSgV8n5zY1W3NiF5HHsCwlaW0e8We1cSC6TuJUqxXA,1060
96
+ mojentic-0.5.4.dist-info/METADATA,sha256=nMGgFMtdzLGe0h4j8Ohyl_DnetZnoObcZvhORCXA8Ec,4956
97
+ mojentic-0.5.4.dist-info/WHEEL,sha256=SmOxYU7pzNKBqASvQJ7DjX3XGUF92lrGhMb3R6_iiqI,91
98
+ mojentic-0.5.4.dist-info/top_level.txt,sha256=Q-BvPQ8Eu1jnEqK8Xkr6A9C8Xa1z38oPZRHuA5MCTqg,19
99
+ mojentic-0.5.4.dist-info/RECORD,,