machine-thinking 0.0.1__tar.gz → 0.0.3__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.
@@ -0,0 +1,69 @@
1
+ Metadata-Version: 2.4
2
+ Name: machine-thinking
3
+ Version: 0.0.3
4
+ Summary: Package description
5
+ Author-email: Machina Ratiocinatrix <machina.ratio@gmail.com>, Alexander Fedotov <alex.fedotov@aol.com>
6
+ Project-URL: Homepage, https://github.com/machina-ratiocinatrix/machine-thinking
7
+ Project-URL: Bug Tracker, https://github.com/machina-ratiocinatrix/machine-thinking/issues
8
+ Keywords: machine-thinking
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.10
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: pyyaml==6.0.3
16
+ Dynamic: license-file
17
+
18
+ # Machine thinking
19
+ Tinker API calls to Inkling model without dependencies.
20
+ <pre>
21
+ pip install machine-thinking
22
+ </pre>
23
+ Then:
24
+ ```Python
25
+ # Python
26
+ from yaml import safe_load as yl
27
+ from machine_thinking.chat import chat_complete as cc
28
+
29
+ kwargs = """ # this is a string in YAML format
30
+ max_tokens: 32000
31
+ stop_sequences:
32
+ - STOP
33
+ - "\nTitle"
34
+ temperature: 1.0
35
+ top_k: 10
36
+ top_p: 0.5
37
+ reasoning_effort: high
38
+ """
39
+
40
+ instruction = 'You are a helpful assistant. Do not use markdown or lists in your responses.'
41
+
42
+ weather_tool = """ # YAML definition of a function (old format)
43
+ type: function
44
+ function:
45
+ name: get_weather
46
+ description: Determine weather in a location
47
+ parameters:
48
+ type: object
49
+ properties:
50
+ location:
51
+ type: string
52
+ description: The city and state, e.g. San Francisco, CA
53
+ additionalProperties: false
54
+ required:
55
+ - location
56
+ """
57
+
58
+ tools = [yl(weather_tool)]
59
+
60
+ msgs = [{'role': 'user', 'content': 'What is the weather in Chicago, IL and Paris, France?'}]
61
+
62
+ thoughts, text = cc(
63
+ messages=msgs,
64
+ instructions=instruction,
65
+ tools=tools,
66
+ **yl(kwargs)
67
+ )
68
+ ```
69
+
@@ -0,0 +1,52 @@
1
+ # Machine thinking
2
+ Tinker API calls to Inkling model without dependencies.
3
+ <pre>
4
+ pip install machine-thinking
5
+ </pre>
6
+ Then:
7
+ ```Python
8
+ # Python
9
+ from yaml import safe_load as yl
10
+ from machine_thinking.chat import chat_complete as cc
11
+
12
+ kwargs = """ # this is a string in YAML format
13
+ max_tokens: 32000
14
+ stop_sequences:
15
+ - STOP
16
+ - "\nTitle"
17
+ temperature: 1.0
18
+ top_k: 10
19
+ top_p: 0.5
20
+ reasoning_effort: high
21
+ """
22
+
23
+ instruction = 'You are a helpful assistant. Do not use markdown or lists in your responses.'
24
+
25
+ weather_tool = """ # YAML definition of a function (old format)
26
+ type: function
27
+ function:
28
+ name: get_weather
29
+ description: Determine weather in a location
30
+ parameters:
31
+ type: object
32
+ properties:
33
+ location:
34
+ type: string
35
+ description: The city and state, e.g. San Francisco, CA
36
+ additionalProperties: false
37
+ required:
38
+ - location
39
+ """
40
+
41
+ tools = [yl(weather_tool)]
42
+
43
+ msgs = [{'role': 'user', 'content': 'What is the weather in Chicago, IL and Paris, France?'}]
44
+
45
+ thoughts, text = cc(
46
+ messages=msgs,
47
+ instructions=instruction,
48
+ tools=tools,
49
+ **yl(kwargs)
50
+ )
51
+ ```
52
+
@@ -3,7 +3,7 @@ requires = ["setuptools>=67.0"]
3
3
  build-backend = "setuptools.build_meta"
4
4
  [project]
5
5
  name = "machine-thinking"
6
- version = "0.0.1"
6
+ version = "0.0.3"
7
7
  authors = [
8
8
  {name="Machina Ratiocinatrix", email="machina.ratio@gmail.com"},
9
9
  {name="Alexander Fedotov", email="alex.fedotov@aol.com"}
@@ -0,0 +1,80 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Python
3
+
4
+ """Copyright (c) Alexander Fedotov.
5
+ This source code is licensed under the license found in the
6
+ LICENSE file in the root directory of this source tree.
7
+ """
8
+ from .utils import (query,
9
+ default_model,
10
+ get_function,
11
+ get_func_args,
12
+ call_function)
13
+
14
+
15
+ def get_weather(location):
16
+ return {"temperature": "72F", "condition": "Sunny"}
17
+
18
+
19
+ def chat_complete(messages=None, instructions=None, tools=None, **kwargs):
20
+ """
21
+ """
22
+ instruction = kwargs.get('system_instruction', instructions)
23
+ first_message = [dict(role='system', content=instruction)] if instruction else []
24
+
25
+ # contents can come in kwards or as an argument
26
+ messages = kwargs.get('messages', messages)
27
+
28
+ first_message.extend(messages)
29
+ instruction_and_contents = first_message
30
+
31
+ payload = {
32
+ 'model': kwargs.get('model', default_model),
33
+ 'messages': instruction_and_contents,
34
+ # 'response_format': kwargs.get('response_format',{'type': 'text'}),
35
+ 'temperature': kwargs.get('temperature', 1), # 0.0 to 2.0
36
+ 'max_tokens': kwargs.get('max_tokens', 4096),
37
+ 'n': kwargs.get('n', 1),
38
+ 'top_p': kwargs.get('top_p', 0.9),
39
+ 'reasoning_effort': kwargs.get('reasoning_effort', 'high'), # 'low', 'medium', 'high'
40
+ 'stream': False
41
+ }
42
+ if tools:
43
+ payload['tools'] = tools
44
+ payload['parallel_tool_calls'] = True
45
+ payload['tool_choice'] = 'auto'
46
+
47
+ while True:
48
+ result = query(payload, '/chat/completions')
49
+ completion_message = result['choices'][0]['message']
50
+ instruction_and_contents.append(completion_message)
51
+ thoughts = completion_message.get('reasoning_content', '')
52
+ text = completion_message.get('content', '')
53
+ function_calls = completion_message.get('tool_calls', [])
54
+
55
+ if function_calls:
56
+ # Call all requested functions and create response messages.
57
+ for function_call in function_calls:
58
+ call_id = function_call.get('id')
59
+ func_def = function_call.get('function')
60
+ func_name = func_def.get('name', '')
61
+
62
+ # Look up tool by name in globals and caller frames
63
+ func = get_function(func_name)
64
+ func_args = get_func_args(func_def)
65
+ result = call_function(func, func_args)
66
+
67
+ tool_message = {
68
+ "role": "tool",
69
+ "tool_call_id": call_id,
70
+ "content": result
71
+ }
72
+ instruction_and_contents.append(tool_message)
73
+ else:
74
+ break
75
+
76
+ return thoughts, text
77
+
78
+
79
+ if __name__ == '__main__':
80
+ ...
@@ -0,0 +1,102 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Python
3
+
4
+ """Copyright (c) Alexander Fedotov.
5
+ This source code is licensed under the license found in the
6
+ LICENSE file in the root directory of this source tree.
7
+ """
8
+ import json
9
+ import urllib.request
10
+ import urllib.error
11
+ from os import environ
12
+
13
+
14
+ api_key = environ.get("TINKER_API_KEY", '')
15
+ default_model = environ.get("TINKER_DEFAULT_MODEL", 'thinkingmachines/Inkling')
16
+ api_base = environ.get("TINKER_API_BASE", 'https://tinker.thinkingmachines.dev/services/tinker-prod/oai/api/v1')
17
+
18
+
19
+ # Set the mandatory headers
20
+ headers = {
21
+ "Content-Type": "application/json",
22
+ "Authorization": f"Bearer {api_key}",
23
+ "User-Agent": "machine-thinking"
24
+ }
25
+
26
+
27
+ def get_function(func_name):
28
+ # Look up tool by name in globals
29
+ func = globals().get(func_name)
30
+ # Look up in the caller frames
31
+ if not func:
32
+ import inspect
33
+ frame = inspect.currentframe().f_back
34
+ while frame:
35
+ if func_name in frame.f_globals:
36
+ func = frame.f_globals[func_name]
37
+ break
38
+ frame = frame.f_back
39
+
40
+ return func
41
+
42
+
43
+ def get_func_args(func_def):
44
+ try:
45
+ func_args_str = func_def.get('arguments')
46
+ if isinstance(func_args_str, str):
47
+ func_args = json.loads(func_args_str)
48
+ else:
49
+ func_args = func_args_str
50
+ except Exception as e:
51
+ func_args = {}
52
+ print(f"Error parsing tool arguments: {e}")
53
+
54
+ return func_args
55
+
56
+
57
+ def call_function(func, func_args):
58
+ if func and callable(func):
59
+ try:
60
+ tool_result = func(**func_args)
61
+ if isinstance(tool_result, (dict, list)):
62
+ result = json.dumps(tool_result)
63
+ else:
64
+ result = str(tool_result)
65
+ except Exception as e:
66
+ result = f"Error executing tool: {str(e)}"
67
+ print(result)
68
+ else:
69
+ result = f"Error: Tool function not callable."
70
+ print(result)
71
+
72
+ return result
73
+
74
+
75
+ def query(payload, url_suffix):
76
+ # Convert data dictionary to JSON and encode it to bytes
77
+ data_bytes = json.dumps(payload).encode('utf-8')
78
+ # Create the Request object
79
+ req = urllib.request.Request(
80
+ f'{api_base}{url_suffix}',
81
+ data=data_bytes,
82
+ headers=headers,
83
+ method="POST")
84
+ # Try to query
85
+ try:
86
+ # Execute the request
87
+ with urllib.request.urlopen(req, timeout=3000) as response:
88
+ response_data = response.read().decode('utf-8')
89
+ output = json.loads(response_data)
90
+ return output
91
+
92
+ except urllib.error.HTTPError as e:
93
+ # Handle HTTP errors (e.g., 401 Unauthorized, 400 Bad Request)
94
+ error_info = e.read().decode('utf-8', errors='ignore')
95
+ print(f"HTTP Error {e.code}: {e.reason}")
96
+ print(f"Error Details: {error_info}")
97
+ return {}
98
+
99
+ except urllib.error.URLError as e:
100
+ # Handle network/connection errors
101
+ print(f"Failed to reach the server: {e.reason}")
102
+ return {}
@@ -0,0 +1,69 @@
1
+ Metadata-Version: 2.4
2
+ Name: machine-thinking
3
+ Version: 0.0.3
4
+ Summary: Package description
5
+ Author-email: Machina Ratiocinatrix <machina.ratio@gmail.com>, Alexander Fedotov <alex.fedotov@aol.com>
6
+ Project-URL: Homepage, https://github.com/machina-ratiocinatrix/machine-thinking
7
+ Project-URL: Bug Tracker, https://github.com/machina-ratiocinatrix/machine-thinking/issues
8
+ Keywords: machine-thinking
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.10
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: pyyaml==6.0.3
16
+ Dynamic: license-file
17
+
18
+ # Machine thinking
19
+ Tinker API calls to Inkling model without dependencies.
20
+ <pre>
21
+ pip install machine-thinking
22
+ </pre>
23
+ Then:
24
+ ```Python
25
+ # Python
26
+ from yaml import safe_load as yl
27
+ from machine_thinking.chat import chat_complete as cc
28
+
29
+ kwargs = """ # this is a string in YAML format
30
+ max_tokens: 32000
31
+ stop_sequences:
32
+ - STOP
33
+ - "\nTitle"
34
+ temperature: 1.0
35
+ top_k: 10
36
+ top_p: 0.5
37
+ reasoning_effort: high
38
+ """
39
+
40
+ instruction = 'You are a helpful assistant. Do not use markdown or lists in your responses.'
41
+
42
+ weather_tool = """ # YAML definition of a function (old format)
43
+ type: function
44
+ function:
45
+ name: get_weather
46
+ description: Determine weather in a location
47
+ parameters:
48
+ type: object
49
+ properties:
50
+ location:
51
+ type: string
52
+ description: The city and state, e.g. San Francisco, CA
53
+ additionalProperties: false
54
+ required:
55
+ - location
56
+ """
57
+
58
+ tools = [yl(weather_tool)]
59
+
60
+ msgs = [{'role': 'user', 'content': 'What is the weather in Chicago, IL and Paris, France?'}]
61
+
62
+ thoughts, text = cc(
63
+ messages=msgs,
64
+ instructions=instruction,
65
+ tools=tools,
66
+ **yl(kwargs)
67
+ )
68
+ ```
69
+
@@ -2,8 +2,10 @@ LICENSE
2
2
  README.md
3
3
  pyproject.toml
4
4
  src/machine_thinking/__init__.py
5
+ src/machine_thinking/chat.py
5
6
  src/machine_thinking/cli.py
6
7
  src/machine_thinking/main.py
8
+ src/machine_thinking/utils.py
7
9
  src/machine_thinking.egg-info/PKG-INFO
8
10
  src/machine_thinking.egg-info/SOURCES.txt
9
11
  src/machine_thinking.egg-info/dependency_links.txt
@@ -1,26 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: machine-thinking
3
- Version: 0.0.1
4
- Summary: Package description
5
- Author-email: Machina Ratiocinatrix <machina.ratio@gmail.com>, Alexander Fedotov <alex.fedotov@aol.com>
6
- Project-URL: Homepage, https://github.com/machina-ratiocinatrix/machine-thinking
7
- Project-URL: Bug Tracker, https://github.com/machina-ratiocinatrix/machine-thinking/issues
8
- Keywords: machine-thinking
9
- Classifier: Programming Language :: Python :: 3
10
- Classifier: License :: OSI Approved :: MIT License
11
- Classifier: Operating System :: OS Independent
12
- Requires-Python: >=3.10
13
- Description-Content-Type: text/markdown
14
- License-File: LICENSE
15
- Requires-Dist: pyyaml==6.0.3
16
- Dynamic: license-file
17
-
18
- # Machine thinking
19
- <pre>
20
- pip install machine-thinking
21
- </pre>
22
- Then:
23
- ```Python
24
- # Python
25
- import machine_thinking
26
- ```
@@ -1,9 +0,0 @@
1
- # Machine thinking
2
- <pre>
3
- pip install machine-thinking
4
- </pre>
5
- Then:
6
- ```Python
7
- # Python
8
- import machine_thinking
9
- ```
@@ -1,26 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: machine-thinking
3
- Version: 0.0.1
4
- Summary: Package description
5
- Author-email: Machina Ratiocinatrix <machina.ratio@gmail.com>, Alexander Fedotov <alex.fedotov@aol.com>
6
- Project-URL: Homepage, https://github.com/machina-ratiocinatrix/machine-thinking
7
- Project-URL: Bug Tracker, https://github.com/machina-ratiocinatrix/machine-thinking/issues
8
- Keywords: machine-thinking
9
- Classifier: Programming Language :: Python :: 3
10
- Classifier: License :: OSI Approved :: MIT License
11
- Classifier: Operating System :: OS Independent
12
- Requires-Python: >=3.10
13
- Description-Content-Type: text/markdown
14
- License-File: LICENSE
15
- Requires-Dist: pyyaml==6.0.3
16
- Dynamic: license-file
17
-
18
- # Machine thinking
19
- <pre>
20
- pip install machine-thinking
21
- </pre>
22
- Then:
23
- ```Python
24
- # Python
25
- import machine_thinking
26
- ```