comprehending-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.
- comprehending_machine/__init__.py +20 -0
- comprehending_machine/cli.py +157 -0
- comprehending_machine/config.py +32 -0
- comprehending_machine/githf.py +69 -0
- comprehending_machine/machina.yaml +7 -0
- comprehending_machine/machine.py +197 -0
- comprehending_machine/providers/__init__.py +7 -0
- comprehending_machine/providers/basta.py +82 -0
- comprehending_machine/providers/camelids.py +87 -0
- comprehending_machine/providers/castor_pollux.py +127 -0
- comprehending_machine/providers/depsek.py +82 -0
- comprehending_machine/providers/electroid.py +78 -0
- comprehending_machine/providers/openai.py +82 -0
- comprehending_machine/providers/qrog.py +82 -0
- comprehending_machine/providers/strangelove.py +81 -0
- comprehending_machine/utilities.py +418 -0
- comprehending_machine-0.0.1.dist-info/METADATA +54 -0
- comprehending_machine-0.0.1.dist-info/RECORD +22 -0
- comprehending_machine-0.0.1.dist-info/WHEEL +5 -0
- comprehending_machine-0.0.1.dist-info/entry_points.txt +2 -0
- comprehending_machine-0.0.1.dist-info/licenses/LICENSE +21 -0
- comprehending_machine-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,127 @@
|
|
|
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 urllib.parse
|
|
11
|
+
import json
|
|
12
|
+
from os import environ
|
|
13
|
+
from ..utilities import messages_to_mpj
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def respond(messages=None, instructions=None, **kwargs):
|
|
17
|
+
"""
|
|
18
|
+
"""
|
|
19
|
+
api_key = environ.get('GEMINI_API_KEY', '')
|
|
20
|
+
api_base = environ.get('GEMINI_API_BASE', 'https://generativelanguage.googleapis.com/v1beta')
|
|
21
|
+
content_model = environ.get('GEMINI_DEFAULT_CONTENT_MODEL', 'gemma-4-31b-it')
|
|
22
|
+
|
|
23
|
+
garbage = [
|
|
24
|
+
{'category': 'HARM_CATEGORY_HATE_SPEECH', 'threshold': 'BLOCK_NONE'},
|
|
25
|
+
{'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'threshold': 'BLOCK_NONE'},
|
|
26
|
+
{'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'threshold': 'BLOCK_NONE'},
|
|
27
|
+
{'category': 'HARM_CATEGORY_HARASSMENT', 'threshold': 'BLOCK_NONE'},
|
|
28
|
+
{'category': 'HARM_CATEGORY_CIVIC_INTEGRITY', 'threshold': 'BLOCK_NONE'}
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
instructions = kwargs.get('system_instruction', instructions)
|
|
32
|
+
system_instruction = dict(role='system', parts=[dict(text=instructions)]) if instructions else None
|
|
33
|
+
|
|
34
|
+
# Trickery for thinking models
|
|
35
|
+
thinking_config = None
|
|
36
|
+
model = kwargs.get("model", content_model)
|
|
37
|
+
if model.startswith('gemini-2.5'):
|
|
38
|
+
thinking_config = {
|
|
39
|
+
'includeThoughts': kwargs.get('include_thoughts', True),
|
|
40
|
+
'thinkingBudget': kwargs.get('thinking_budget', 10000)
|
|
41
|
+
}
|
|
42
|
+
elif model.startswith('gemini-3'):
|
|
43
|
+
thinking_config = {
|
|
44
|
+
'includeThoughts': kwargs.get('include_thoughts', True),
|
|
45
|
+
'thinkingLevel': kwargs.get('thinking_level', 'high')
|
|
46
|
+
}
|
|
47
|
+
elif model.startswith('gemma-4'):
|
|
48
|
+
thinking_config = {
|
|
49
|
+
'includeThoughts': kwargs.get('include_thoughts', True)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
# Define the payload
|
|
53
|
+
payload = {
|
|
54
|
+
'systemInstruction': system_instruction,
|
|
55
|
+
'contents': messages,
|
|
56
|
+
'safetySettings': garbage,
|
|
57
|
+
'generationConfig': {
|
|
58
|
+
'stopSequences': kwargs.get('stop_sequences', ['STOP', 'Title']),
|
|
59
|
+
'responseMimeType': kwargs.get('mime_type', 'text/plain'),
|
|
60
|
+
'responseModalities': kwargs.get('modalities', ['TEXT']),
|
|
61
|
+
'temperature': kwargs.get('temperature', 1.0),
|
|
62
|
+
'maxOutputTokens': kwargs.get('max_tokens', 10000),
|
|
63
|
+
'topP': kwargs.get('top_p', 0.9),
|
|
64
|
+
'topK': kwargs.get('top_k', 10),
|
|
65
|
+
'enableEnhancedCivicAnswers': False,
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
if thinking_config:
|
|
69
|
+
payload['generationConfig']['thinkingConfig'] = thinking_config
|
|
70
|
+
if kwargs.get('sources'):
|
|
71
|
+
payload['tools'].append(
|
|
72
|
+
{
|
|
73
|
+
"url_context": {}
|
|
74
|
+
}
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
# Convert data dictionary to JSON and encode it to bytes
|
|
78
|
+
data_bytes = json.dumps(payload).encode('utf-8')
|
|
79
|
+
|
|
80
|
+
# Set the mandatory headers
|
|
81
|
+
headers = {
|
|
82
|
+
"Content-Type": "application/json",
|
|
83
|
+
"User-Agent": "Comprehending-Machine"
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
# urlencode parameter
|
|
87
|
+
params = urllib.parse.urlencode({'key': api_key})
|
|
88
|
+
|
|
89
|
+
# Create the Request object
|
|
90
|
+
req = urllib.request.Request(
|
|
91
|
+
f'{api_base}/models/{kwargs.get("model", content_model)}:generateContent?{params}',
|
|
92
|
+
data=data_bytes,
|
|
93
|
+
headers=headers,
|
|
94
|
+
method="POST")
|
|
95
|
+
|
|
96
|
+
try:
|
|
97
|
+
# Execute the request
|
|
98
|
+
with urllib.request.urlopen(req, timeout=300) as response:
|
|
99
|
+
response_data = response.read().decode('utf-8')
|
|
100
|
+
output = json.loads(response_data)
|
|
101
|
+
text = ''
|
|
102
|
+
thoughts = ''
|
|
103
|
+
if output['candidates'][0]['finishReason'] == 'SAFETY':
|
|
104
|
+
raise Exception('Answer censored by Google.')
|
|
105
|
+
for part in output['candidates'][0]['content']['parts']:
|
|
106
|
+
if part.get('thought'):
|
|
107
|
+
thoughts += part['text']
|
|
108
|
+
else:
|
|
109
|
+
text += part['text']
|
|
110
|
+
|
|
111
|
+
return thoughts, text
|
|
112
|
+
|
|
113
|
+
except urllib.error.HTTPError as e:
|
|
114
|
+
# Handle HTTP errors (e.g., 401 Unauthorized, 400 Bad Request)
|
|
115
|
+
error_info = e.read().decode('utf-8', errors='ignore')
|
|
116
|
+
print(f"HTTP Error {e.code}: {e.reason}")
|
|
117
|
+
print(f"Error Details: {error_info}")
|
|
118
|
+
return '', ''
|
|
119
|
+
|
|
120
|
+
except urllib.error.URLError as e:
|
|
121
|
+
# Handle network/connection errors
|
|
122
|
+
print(f"Failed to reach the server: {e.reason}")
|
|
123
|
+
return '', ''
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
if __name__ == '__main__':
|
|
127
|
+
...
|
|
@@ -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, instructions, **kwargs):
|
|
15
|
+
"""
|
|
16
|
+
"""
|
|
17
|
+
api_key = environ.get("DEPSEK_API_KEY")
|
|
18
|
+
api_base = environ.get("DEPSEK_API_BASE", "https://api.deepseek.com")
|
|
19
|
+
default_model = environ.get("DEPSEK_DEFAULT_MODEL", "deepseek-v4-pro")
|
|
20
|
+
|
|
21
|
+
instruction = kwargs.get('system_instruction', instructions)
|
|
22
|
+
first_message = [dict(role='system', content=instruction)] if instruction else []
|
|
23
|
+
|
|
24
|
+
# add contents and user text to the first (instruction) message
|
|
25
|
+
first_message.extend(messages)
|
|
26
|
+
instruction_and_contents = first_message
|
|
27
|
+
|
|
28
|
+
# Define the payload
|
|
29
|
+
payload = {
|
|
30
|
+
"model": kwargs.get("model", default_model),
|
|
31
|
+
"messages": instruction_and_contents,
|
|
32
|
+
"max_tokens": kwargs.get("max_tokens", 32000),
|
|
33
|
+
"temperature": kwargs.get("temperature", 1.0),
|
|
34
|
+
"reasoning_effort": kwargs.get("reasoning_effort", "max"),
|
|
35
|
+
"thinking": {
|
|
36
|
+
"type": "enabled"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
# Convert data dictionary to JSON and encode it to bytes
|
|
41
|
+
data_bytes = json.dumps(payload).encode('utf-8')
|
|
42
|
+
|
|
43
|
+
# Set the mandatory headers
|
|
44
|
+
headers = {
|
|
45
|
+
"Content-Type": "application/json",
|
|
46
|
+
"Authorization": f"Bearer {api_key}",
|
|
47
|
+
"User-Agent": "Comprehending-Machine"
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
# Create the Request object
|
|
51
|
+
req = urllib.request.Request(
|
|
52
|
+
f'{api_base}/chat/completions',
|
|
53
|
+
data=data_bytes,
|
|
54
|
+
headers=headers,
|
|
55
|
+
method="POST")
|
|
56
|
+
|
|
57
|
+
try:
|
|
58
|
+
# Execute the request
|
|
59
|
+
with urllib.request.urlopen(req, timeout=300) as response:
|
|
60
|
+
response_data = response.read().decode('utf-8')
|
|
61
|
+
output = json.loads(response_data)
|
|
62
|
+
message = output['choices'][0]['message']
|
|
63
|
+
text = message['content']
|
|
64
|
+
thoughts = message['reasoning_content']
|
|
65
|
+
|
|
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,78 @@
|
|
|
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("ANTHROPIC_API_KEY")
|
|
18
|
+
api_base = environ.get("ANTHROPIC_API_BASE", "https://api.anthropic.com/v1")
|
|
19
|
+
api_type = environ.get("ANTHROPIC_VERSION", "2023-06-01")
|
|
20
|
+
default_model = environ.get("ANTHROPIC_DEFAULT_MODEL", 'claude-opus-4-6')
|
|
21
|
+
|
|
22
|
+
# Define the payload
|
|
23
|
+
payload = {
|
|
24
|
+
"model": kwargs.get("model", default_model),
|
|
25
|
+
"thinking": {"type": "adaptive"},
|
|
26
|
+
"system": kwargs.get("system_instruction", instructions),
|
|
27
|
+
"messages": messages,
|
|
28
|
+
"max_tokens": kwargs.get("max_tokens", 100000),
|
|
29
|
+
"stop_sequences": kwargs.get("stop_sequences", ['stop']),
|
|
30
|
+
"stream": kwargs.get("stream", False),
|
|
31
|
+
"temperature": 1.0,
|
|
32
|
+
"output_config": kwargs.get("output_config", {"effort": "low"}),
|
|
33
|
+
"metadata": kwargs.get("metadata", None)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
# Convert data dictionary to JSON and encode it to bytes
|
|
37
|
+
data_bytes = json.dumps(payload).encode('utf-8')
|
|
38
|
+
|
|
39
|
+
headers = {
|
|
40
|
+
"x-api-key": api_key,
|
|
41
|
+
"anthropic-version": api_type,
|
|
42
|
+
"content-type": "application/json",
|
|
43
|
+
"User-Agent": "Comprehending-Machine"
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
# Create the Request object
|
|
47
|
+
req = urllib.request.Request(
|
|
48
|
+
f'{api_base}/messages',
|
|
49
|
+
data=data_bytes,
|
|
50
|
+
headers=headers,
|
|
51
|
+
method="POST")
|
|
52
|
+
|
|
53
|
+
try:
|
|
54
|
+
# Execute the request
|
|
55
|
+
with urllib.request.urlopen(req, timeout=300) as response:
|
|
56
|
+
response_data = response.read().decode('utf-8')
|
|
57
|
+
output = json.loads(response_data)
|
|
58
|
+
text = ''
|
|
59
|
+
thoughts = ''
|
|
60
|
+
for chunk in output.get("content"):
|
|
61
|
+
if chunk['type'] == 'text':
|
|
62
|
+
addition = chunk['text']
|
|
63
|
+
if addition not in ('\n\n', '\n'):
|
|
64
|
+
text += addition
|
|
65
|
+
|
|
66
|
+
elif chunk['type'] == 'thinking':
|
|
67
|
+
thoughts += chunk['thinking']
|
|
68
|
+
|
|
69
|
+
return thoughts, text
|
|
70
|
+
|
|
71
|
+
except Exception as e:
|
|
72
|
+
print("Unable to generate Message response")
|
|
73
|
+
print(f"Exception: {e}")
|
|
74
|
+
return ['', '']
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
if __name__ == "__main__":
|
|
78
|
+
...
|
|
@@ -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": "Comprehending-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": "Comprehending-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": "Comprehending-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
|
+
...
|