exploring-machine 0.0.1__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,82 @@
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 urllib.request
9
+ import urllib.error
10
+ import json
11
+ from os import environ
12
+
13
+
14
+ def respond(messages=None, instructions=None, **kwargs):
15
+ """ All parameters should be in kwargs, but they are optional
16
+ """
17
+ api_key = environ.get("OPENAI_API_KEY", '')
18
+ default_model = environ.get("OPENAI_DEFAULT_MODEL", 'gpt-5.4')
19
+ api_base = environ.get("OPENAI_API_BASE", "https://api.openai.com/v1")
20
+
21
+ instruction = kwargs.get('system_instruction', instructions)
22
+
23
+ # Define the payload
24
+ payload = {
25
+ "model": kwargs.get("model", default_model),
26
+ "instructions": instruction,
27
+ "input": messages,
28
+ "max_output_tokens": kwargs.get("max_tokens", 64000),
29
+ "reasoning": {
30
+ "effort": "xhigh",
31
+ "summary": "detailed"
32
+ }
33
+ }
34
+
35
+ # Convert data dictionary to JSON and encode it to bytes
36
+ data_bytes = json.dumps(payload).encode('utf-8')
37
+
38
+ # Set the mandatory headers
39
+ headers = {
40
+ "Content-Type": "application/json",
41
+ "Authorization": f"Bearer {api_key}",
42
+ "User-Agent": "Exploring-Machine"
43
+ }
44
+
45
+ # Create the Request object
46
+ req = urllib.request.Request(
47
+ f'{api_base}/responses',
48
+ data=data_bytes,
49
+ headers=headers,
50
+ method="POST")
51
+
52
+ try:
53
+ # Execute the request
54
+ with urllib.request.urlopen(req, timeout=300) as response:
55
+ response_data = response.read().decode('utf-8')
56
+ output = json.loads(response_data)
57
+ text = ''
58
+ thoughts = ''
59
+ for part in output['output']:
60
+ if part['type'] == 'message':
61
+ for chunk in part['content']:
62
+ text += chunk['text']
63
+ elif part['type'] == 'reasoning':
64
+ for chunk in part['summary']:
65
+ thoughts += chunk['text']
66
+ return thoughts, text
67
+
68
+ except urllib.error.HTTPError as e:
69
+ # Handle HTTP errors (e.g., 401 Unauthorized, 400 Bad Request)
70
+ error_info = e.read().decode('utf-8', errors='ignore')
71
+ print(f"HTTP Error {e.code}: {e.reason}")
72
+ print(f"Error Details: {error_info}")
73
+ return '', ''
74
+
75
+ except urllib.error.URLError as e:
76
+ # Handle network/connection errors
77
+ print(f"Failed to reach the server: {e.reason}")
78
+ return '', ''
79
+
80
+
81
+ if __name__ == "__main__":
82
+ ...
@@ -0,0 +1,82 @@
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 urllib.request
9
+ import urllib.error
10
+ import json
11
+ from os import environ
12
+
13
+
14
+ def respond(messages=None, instructions=None, **kwargs):
15
+ """
16
+ Sends a request to the Groq Responses API using only Python's built-in urllib.
17
+ """
18
+ api_base = environ.get('GROQ_API_BASE', 'https://api.groq.com/openai/v1')
19
+ api_key = environ.get('GROQ_API_KEY', '')
20
+ default_model = environ.get('GROQ_DEFAULT_MODEL', 'openai/gpt-oss-120b')
21
+
22
+ instruction = kwargs.get('system_instruction', instructions)
23
+
24
+ # Define the payload
25
+ payload = {
26
+ "model": kwargs.get("model", default_model),
27
+ "instructions": instruction,
28
+ "input": messages,
29
+ "max_output_tokens": kwargs.get("max_tokens", 65536),
30
+ "reasoning": {
31
+ "effort": "high"
32
+ }
33
+ }
34
+
35
+ # Convert data dictionary to JSON and encode it to bytes
36
+ data_bytes = json.dumps(payload).encode('utf-8')
37
+
38
+ # Set the mandatory headers
39
+ headers = {
40
+ "Content-Type": "application/json",
41
+ "Authorization": f"Bearer {api_key}",
42
+ "User-Agent": "Exploring-Machine"
43
+ }
44
+
45
+ # Create the Request object
46
+ req = urllib.request.Request(
47
+ f'{api_base}/responses',
48
+ data=data_bytes,
49
+ headers=headers,
50
+ method="POST")
51
+
52
+ try:
53
+ # Execute the request
54
+ with urllib.request.urlopen(req, timeout=300) as response:
55
+ response_data = response.read().decode('utf-8')
56
+ output = json.loads(response_data)
57
+ text = ''
58
+ thoughts = ''
59
+ for part in output['output']:
60
+ if part['type'] == 'message':
61
+ for chunk in part['content']:
62
+ text += chunk['text']
63
+ elif part['type'] == 'reasoning':
64
+ for chunk in part['content']:
65
+ thoughts += chunk['text']
66
+ return thoughts, text
67
+
68
+ except urllib.error.HTTPError as e:
69
+ # Handle HTTP errors (e.g., 401 Unauthorized, 400 Bad Request)
70
+ error_info = e.read().decode('utf-8', errors='ignore')
71
+ print(f"HTTP Error {e.code}: {e.reason}")
72
+ print(f"Error Details: {error_info}")
73
+ return '', ''
74
+
75
+ except urllib.error.URLError as e:
76
+ # Handle network/connection errors
77
+ print(f"Failed to reach the server: {e.reason}")
78
+ return '', ''
79
+
80
+
81
+ if __name__ == "__main__":
82
+ ...
@@ -0,0 +1,81 @@
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 urllib.request
9
+ import urllib.error
10
+ import json
11
+ from os import environ
12
+
13
+
14
+ def respond(messages, instructions, **kwargs):
15
+ """
16
+ """
17
+ api_key = environ.get("XAI_API_KEY")
18
+ api_base = environ.get("XAI_API_BASE", "https://api.x.ai/v1")
19
+ default_model = environ.get("XAI_DEFAULT_MODEL", "grok-4.20-reasoning")
20
+
21
+ instruction = kwargs.get('system_instruction', instructions)
22
+
23
+ # Define the payload
24
+ payload = {
25
+ "model": kwargs.get("model", default_model),
26
+ "instructions": instructions,
27
+ "input": messages,
28
+ "max_output_tokens": kwargs.get("max_tokens", 64000),
29
+ "reasoning": {
30
+ "summary": "detailed"
31
+ }
32
+ }
33
+
34
+ # Convert data dictionary to JSON and encode it to bytes
35
+ data_bytes = json.dumps(payload).encode('utf-8')
36
+
37
+ headers = {
38
+ "Content-Type": "application/json",
39
+ "Authorization": "Bearer " + api_key,
40
+ "User-Agent": "Exploring-Machine"
41
+ }
42
+
43
+ # Create the Request object
44
+ req = urllib.request.Request(
45
+ f'{api_base}/responses',
46
+ data=data_bytes,
47
+ headers=headers,
48
+ method="POST")
49
+
50
+ try:
51
+ # Execute the request
52
+ with urllib.request.urlopen(req, timeout=300) as response:
53
+ response_data = response.read().decode('utf-8')
54
+ output = json.loads(response_data)
55
+ text = ''
56
+ thoughts = ''
57
+ for part in output['output']:
58
+ if part['type'] == 'message':
59
+ for chunk in part['content']:
60
+ text += chunk['text']
61
+ elif part['type'] == 'reasoning_content':
62
+ for chunk in part['summary']:
63
+ thoughts += chunk['text']
64
+
65
+ return thoughts, text
66
+
67
+ except urllib.error.HTTPError as e:
68
+ # Handle HTTP errors (e.g., 401 Unauthorized, 400 Bad Request)
69
+ error_info = e.read().decode('utf-8', errors='ignore')
70
+ print(f"HTTP Error {e.code}: {e.reason}")
71
+ print(f"Error Details: {error_info}")
72
+ return '', ''
73
+
74
+ except urllib.error.URLError as e:
75
+ # Handle network/connection errors
76
+ print(f"Failed to reach the server: {e.reason}")
77
+ return '', ''
78
+
79
+
80
+ if __name__ == '__main__':
81
+ ...
@@ -0,0 +1,83 @@
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
+ def respond(messages=None, instructions=None, tools=None, **kwargs):
15
+ """ They don't have responses, emulation through chat completions.
16
+ """
17
+ api_key = environ.get("TINKER_API_KEY", '')
18
+ default_model = environ.get("TINKER_DEFAULT_MODEL", 'thinkingmachines/Inkling')
19
+ api_base = environ.get("TINKER_OAI_API_BASE", 'https://tinker.thinkingmachines.dev/services/tinker-prod/oai/api/v1')
20
+
21
+ # Set the mandatory headers
22
+ headers = {
23
+ "Content-Type": "application/json",
24
+ "Authorization": f"Bearer {api_key}",
25
+ "User-Agent": "Exploring-Machine"
26
+ }
27
+
28
+ instruction = kwargs.get('system_instruction', instructions)
29
+ first_message = [dict(role='system', content=instruction)] if instruction else []
30
+
31
+ # contents can come in kwards or as an argument
32
+ messages = kwargs.get('messages', messages)
33
+
34
+ first_message.extend(messages)
35
+ instruction_and_contents = first_message
36
+
37
+ payload = {
38
+ 'model': kwargs.get('model', default_model),
39
+ 'messages': instruction_and_contents,
40
+ # 'response_format': kwargs.get('response_format',{'type': 'text'}),
41
+ 'temperature': kwargs.get('temperature', 1), # 0.0 to 2.0
42
+ 'max_tokens': kwargs.get('max_tokens', 4096),
43
+ 'n': kwargs.get('n', 1),
44
+ 'top_p': kwargs.get('top_p', 0.9),
45
+ 'separate_reasoning': True,
46
+ 'reasoning_effort': kwargs.get('reasoning_effort', 'high'), # 'low', 'medium', 'high'
47
+ 'stream': False
48
+ }
49
+
50
+ data_bytes = json.dumps(payload).encode('utf-8')
51
+ # Create the Request object
52
+ req = urllib.request.Request(
53
+ f'{api_base}/chat/completions',
54
+ data=data_bytes,
55
+ headers=headers,
56
+ method="POST")
57
+
58
+ # Try to query
59
+ try:
60
+ # Execute the request
61
+ with urllib.request.urlopen(req, timeout=3000) as response:
62
+ response_data = response.read().decode('utf-8')
63
+ output = json.loads(response_data)
64
+ completion_message = output['choices'][0]['message']
65
+ thoughts = completion_message.get('reasoning_content', '')
66
+ text = completion_message.get('content', '')
67
+ return thoughts, text
68
+
69
+ except urllib.error.HTTPError as e:
70
+ # Handle HTTP errors (e.g., 401 Unauthorized, 400 Bad Request)
71
+ error_info = e.read().decode('utf-8', errors='ignore')
72
+ print(f"HTTP Error {e.code}: {e.reason}")
73
+ print(f"Error Details: {error_info}")
74
+ return '',''
75
+
76
+ except urllib.error.URLError as e:
77
+ # Handle network/connection errors
78
+ print(f"Failed to reach the server: {e.reason}")
79
+ return '', ''
80
+
81
+
82
+ if __name__ == '__main__':
83
+ ...