moltlang 0.1.0__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.
- mcp_server/__init__.py +13 -0
- mcp_server/endpoints.py +177 -0
- mcp_server/server.py +303 -0
- moltlang/__init__.py +64 -0
- moltlang/cli.py +247 -0
- moltlang/config.py +86 -0
- moltlang/openclaw/__init__.py +11 -0
- moltlang/openclaw/skill.py +77 -0
- moltlang/tokens.py +311 -0
- moltlang/training/__init__.py +12 -0
- moltlang/training/data_gen.py +118 -0
- moltlang/training/distill.py +86 -0
- moltlang/translator.py +965 -0
- moltlang/validator.py +378 -0
- moltlang-0.1.0.dist-info/METADATA +187 -0
- moltlang-0.1.0.dist-info/RECORD +20 -0
- moltlang-0.1.0.dist-info/WHEEL +5 -0
- moltlang-0.1.0.dist-info/entry_points.txt +2 -0
- moltlang-0.1.0.dist-info/licenses/LICENSE +23 -0
- moltlang-0.1.0.dist-info/top_level.txt +2 -0
moltlang/cli.py
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
"""
|
|
2
|
+
MoltLang Command Line Interface.
|
|
3
|
+
|
|
4
|
+
This module provides a CLI for interacting with MoltLang translation.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import argparse
|
|
8
|
+
import sys
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from moltlang import (
|
|
12
|
+
translate_to_molt,
|
|
13
|
+
translate_from_molt,
|
|
14
|
+
validate_translation,
|
|
15
|
+
MoltTranslator,
|
|
16
|
+
MoltValidator,
|
|
17
|
+
)
|
|
18
|
+
from moltlang.config import get_config
|
|
19
|
+
from moltlang.tokens import TokenType, TokenRegistry
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def cmd_translate(args: argparse.Namespace) -> int:
|
|
23
|
+
"""Handle translate command."""
|
|
24
|
+
translator = MoltTranslator()
|
|
25
|
+
|
|
26
|
+
if args.to_molt:
|
|
27
|
+
result = translator.translate_to_molt(args.text)
|
|
28
|
+
print(result.text)
|
|
29
|
+
if args.verbose:
|
|
30
|
+
print(f"\nTokens: {result.token_count}")
|
|
31
|
+
print(f"Original: {result.original_token_count}")
|
|
32
|
+
print(f"Efficiency: {result.token_efficiency:.2%}")
|
|
33
|
+
print(f"Confidence: {result.confidence:.2f}")
|
|
34
|
+
else:
|
|
35
|
+
result = translator.translate_from_molt(args.text)
|
|
36
|
+
print(result.text)
|
|
37
|
+
if args.verbose:
|
|
38
|
+
print(f"\nTokens: {result.token_count}")
|
|
39
|
+
print(f"Confidence: {result.confidence:.2f}")
|
|
40
|
+
|
|
41
|
+
return 0
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def cmd_validate(args: argparse.Namespace) -> int:
|
|
45
|
+
"""Handle validate command."""
|
|
46
|
+
quality = validate_translation(args.original, args.translated)
|
|
47
|
+
|
|
48
|
+
print(f"Valid: {quality.is_valid}")
|
|
49
|
+
print(f"Score: {quality.score:.2f}")
|
|
50
|
+
print(f"Token Efficiency: {quality.token_efficiency:.2%}")
|
|
51
|
+
print(f"Confidence: {quality.confidence:.2f}")
|
|
52
|
+
|
|
53
|
+
if quality.issues:
|
|
54
|
+
print(f"\nIssues ({len(quality.issues)}):")
|
|
55
|
+
for issue in quality.issues:
|
|
56
|
+
print(f" [{issue.severity}] {issue.type.value}: {issue.message}")
|
|
57
|
+
|
|
58
|
+
return 0 if quality.is_valid else 1
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def cmd_tokens(args: argparse.Namespace) -> int:
|
|
62
|
+
"""Handle tokens command."""
|
|
63
|
+
registry = TokenRegistry()
|
|
64
|
+
|
|
65
|
+
if args.type:
|
|
66
|
+
token_type_prefix = args.type.upper()
|
|
67
|
+
tokens = [t for t in TokenType if t.name.startswith(token_type_prefix)]
|
|
68
|
+
else:
|
|
69
|
+
tokens = list(TokenType)
|
|
70
|
+
|
|
71
|
+
print(f"Available tokens ({len(tokens)}):")
|
|
72
|
+
for token in tokens:
|
|
73
|
+
print(f" {token.name:30} {token.value}")
|
|
74
|
+
|
|
75
|
+
return 0
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def cmd_roundtrip(args: argparse.Namespace) -> int:
|
|
79
|
+
"""Handle roundtrip command."""
|
|
80
|
+
translator = MoltTranslator()
|
|
81
|
+
validator = MoltValidator()
|
|
82
|
+
|
|
83
|
+
# Forward translation
|
|
84
|
+
molt_result = translator.translate_to_molt(args.text)
|
|
85
|
+
print(f"Original: {args.text}")
|
|
86
|
+
print(f"MoltLang: {molt_result.text}")
|
|
87
|
+
|
|
88
|
+
# Back translation
|
|
89
|
+
human_result = translator.translate_from_molt(molt_result.text)
|
|
90
|
+
print(f"Roundtrip: {human_result.text}")
|
|
91
|
+
|
|
92
|
+
# Validate roundtrip
|
|
93
|
+
quality = validator.validate_roundtrip(args.text, translator)
|
|
94
|
+
print(f"\nRoundtrip Quality:")
|
|
95
|
+
print(f" Valid: {quality.is_valid}")
|
|
96
|
+
print(f" Similarity: {quality.metrics.get('roundtrip_similarity', 0):.2f}")
|
|
97
|
+
|
|
98
|
+
return 0
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def cmd_demo(args: argparse.Namespace) -> int:
|
|
102
|
+
"""Handle demo command."""
|
|
103
|
+
print("=" * 50)
|
|
104
|
+
print("MoltLang Demo")
|
|
105
|
+
print("=" * 50)
|
|
106
|
+
|
|
107
|
+
examples = [
|
|
108
|
+
"Fetch data from API and return JSON",
|
|
109
|
+
"Parse data from file",
|
|
110
|
+
"Search database for user",
|
|
111
|
+
"Transform and validate input",
|
|
112
|
+
]
|
|
113
|
+
|
|
114
|
+
translator = MoltTranslator()
|
|
115
|
+
|
|
116
|
+
for example in examples:
|
|
117
|
+
result = translator.translate_to_molt(example)
|
|
118
|
+
print(f"\nEnglish: {example}")
|
|
119
|
+
print(f"MoltLang: {result.text}")
|
|
120
|
+
print(f"Efficiency: {result.token_efficiency:.2%}")
|
|
121
|
+
|
|
122
|
+
print("\n" + "=" * 50)
|
|
123
|
+
return 0
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def main() -> int:
|
|
127
|
+
"""
|
|
128
|
+
Main CLI entry point.
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
Exit code (0 for success, non-zero for error)
|
|
132
|
+
"""
|
|
133
|
+
parser = argparse.ArgumentParser(
|
|
134
|
+
description="MoltLang - A Language for LLMs, by LLMs",
|
|
135
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
136
|
+
epilog="""
|
|
137
|
+
Examples:
|
|
138
|
+
moltlang translate "Fetch data from API" --to-molt
|
|
139
|
+
moltlang translate "[OP:FETCH][SRC:API]" --from-molt
|
|
140
|
+
moltlang validate "Fetch data" "[OP:FETCH][SRC:API]"
|
|
141
|
+
moltlang tokens --type OP
|
|
142
|
+
moltlang roundtrip "Parse data from database"
|
|
143
|
+
moltlang demo
|
|
144
|
+
""",
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
parser.add_argument(
|
|
148
|
+
"--version", action="version", version="MoltLang 0.1.0"
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
subparsers = parser.add_subparsers(
|
|
152
|
+
dest="command",
|
|
153
|
+
title="Commands",
|
|
154
|
+
description="Available commands",
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
# Translate command
|
|
158
|
+
translate_parser = subparsers.add_parser(
|
|
159
|
+
"translate",
|
|
160
|
+
help="Translate between human language and MoltLang",
|
|
161
|
+
)
|
|
162
|
+
translate_parser.add_argument(
|
|
163
|
+
"text",
|
|
164
|
+
help="Text to translate",
|
|
165
|
+
)
|
|
166
|
+
translate_parser.add_argument(
|
|
167
|
+
"--to-molt",
|
|
168
|
+
action="store_true",
|
|
169
|
+
help="Translate to MoltLang (default: from MoltLang)",
|
|
170
|
+
)
|
|
171
|
+
translate_parser.add_argument(
|
|
172
|
+
"--from-molt",
|
|
173
|
+
action="store_true",
|
|
174
|
+
help="Translate from MoltLang (default: to MoltLang)",
|
|
175
|
+
)
|
|
176
|
+
translate_parser.add_argument(
|
|
177
|
+
"-v", "--verbose",
|
|
178
|
+
action="store_true",
|
|
179
|
+
help="Show detailed output",
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
# Validate command
|
|
183
|
+
validate_parser = subparsers.add_parser(
|
|
184
|
+
"validate",
|
|
185
|
+
help="Validate a translation",
|
|
186
|
+
)
|
|
187
|
+
validate_parser.add_argument(
|
|
188
|
+
"original",
|
|
189
|
+
help="Original text",
|
|
190
|
+
)
|
|
191
|
+
validate_parser.add_argument(
|
|
192
|
+
"translated",
|
|
193
|
+
help="Translated text",
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
# Tokens command
|
|
197
|
+
tokens_parser = subparsers.add_parser(
|
|
198
|
+
"tokens",
|
|
199
|
+
help="List available tokens",
|
|
200
|
+
)
|
|
201
|
+
tokens_parser.add_argument(
|
|
202
|
+
"--type",
|
|
203
|
+
help="Filter by token type (e.g., OP, SRC, RET)",
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
# Roundtrip command
|
|
207
|
+
roundtrip_parser = subparsers.add_parser(
|
|
208
|
+
"roundtrip",
|
|
209
|
+
help="Test roundtrip translation",
|
|
210
|
+
)
|
|
211
|
+
roundtrip_parser.add_argument(
|
|
212
|
+
"text",
|
|
213
|
+
help="Text to translate",
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
# Demo command
|
|
217
|
+
subparsers.add_parser(
|
|
218
|
+
"demo",
|
|
219
|
+
help="Run interactive demo",
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
# Parse arguments
|
|
223
|
+
args = parser.parse_args()
|
|
224
|
+
|
|
225
|
+
# Handle no command
|
|
226
|
+
if args.command is None:
|
|
227
|
+
parser.print_help()
|
|
228
|
+
return 0
|
|
229
|
+
|
|
230
|
+
# Dispatch to command handler
|
|
231
|
+
handlers = {
|
|
232
|
+
"translate": cmd_translate,
|
|
233
|
+
"validate": cmd_validate,
|
|
234
|
+
"tokens": cmd_tokens,
|
|
235
|
+
"roundtrip": cmd_roundtrip,
|
|
236
|
+
"demo": cmd_demo,
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
handler = handlers.get(args.command)
|
|
240
|
+
if handler:
|
|
241
|
+
return handler(args)
|
|
242
|
+
|
|
243
|
+
return 1
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
if __name__ == "__main__":
|
|
247
|
+
sys.exit(main())
|
moltlang/config.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""
|
|
2
|
+
MoltLang configuration module.
|
|
3
|
+
|
|
4
|
+
This module handles configuration for the MoltLang translation system,
|
|
5
|
+
including token sets, optimization settings, and language preferences.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
from enum import Enum
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class OptimizationLevel(Enum):
|
|
14
|
+
"""Translation optimization levels."""
|
|
15
|
+
|
|
16
|
+
FAST = "fast" # Fast translation, lower accuracy
|
|
17
|
+
BALANCED = "balanced" # Balance between speed and accuracy
|
|
18
|
+
ACCURATE = "accurate" # Highest accuracy, slower
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class MoltConfig:
|
|
23
|
+
"""
|
|
24
|
+
Configuration for MoltLang translation.
|
|
25
|
+
|
|
26
|
+
Attributes:
|
|
27
|
+
max_tokens: Maximum tokens to generate in translation
|
|
28
|
+
temperature: Sampling temperature for translation (0.0-1.0)
|
|
29
|
+
optimization_level: Speed/accuracy tradeoff
|
|
30
|
+
enable_cache: Enable translation caching
|
|
31
|
+
target_token_reduction: Target token reduction percentage (0-100)
|
|
32
|
+
strict_mode: Enable strict validation of MoltLang syntax
|
|
33
|
+
human_language: Target human language for translation (default: English)
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
max_tokens: int = 4096
|
|
37
|
+
temperature: float = 0.3
|
|
38
|
+
optimization_level: OptimizationLevel = OptimizationLevel.BALANCED
|
|
39
|
+
enable_cache: bool = True
|
|
40
|
+
target_token_reduction: float = 0.6 # Target 60% token reduction
|
|
41
|
+
strict_mode: bool = False
|
|
42
|
+
human_language: str = "en"
|
|
43
|
+
custom_token_registry: dict[str, Any] = field(default_factory=dict)
|
|
44
|
+
|
|
45
|
+
# Token efficiency settings
|
|
46
|
+
min_token_efficiency: float = 0.5 # Minimum 50% token efficiency
|
|
47
|
+
prefer_high_density: bool = True
|
|
48
|
+
|
|
49
|
+
# Validation settings
|
|
50
|
+
validation_threshold: float = 0.95 # 95% translation accuracy threshold
|
|
51
|
+
enable_roundtrip_validation: bool = True
|
|
52
|
+
|
|
53
|
+
def __post_init__(self):
|
|
54
|
+
"""Validate configuration after initialization."""
|
|
55
|
+
if not 0.0 <= self.temperature <= 1.0:
|
|
56
|
+
raise ValueError("temperature must be between 0.0 and 1.0")
|
|
57
|
+
if not 0.0 <= self.target_token_reduction <= 1.0:
|
|
58
|
+
raise ValueError("target_token_reduction must be between 0.0 and 1.0")
|
|
59
|
+
if not 0.0 <= self.min_token_efficiency <= 1.0:
|
|
60
|
+
raise ValueError("min_token_efficiency must be between 0.0 and 1.0")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
# Default configuration instance
|
|
64
|
+
DEFAULT_CONFIG = MoltConfig()
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def get_config(**kwargs) -> MoltConfig:
|
|
68
|
+
"""
|
|
69
|
+
Get a MoltConfig instance with optional overrides.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
**kwargs: Configuration overrides
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
MoltConfig instance with overrides applied
|
|
76
|
+
|
|
77
|
+
Examples:
|
|
78
|
+
>>> config = get_config(temperature=0.5, strict_mode=True)
|
|
79
|
+
>>> config.temperature
|
|
80
|
+
0.5
|
|
81
|
+
"""
|
|
82
|
+
config = DEFAULT_CONFIG
|
|
83
|
+
for key, value in kwargs.items():
|
|
84
|
+
if hasattr(config, key):
|
|
85
|
+
setattr(config, key, value)
|
|
86
|
+
return config
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""
|
|
2
|
+
MoltLang OpenClaw Skill.
|
|
3
|
+
|
|
4
|
+
This module implements the OpenClaw skill for MoltLang translation.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from moltlang import translate_to_molt, translate_from_molt, MoltTranslator
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class MoltLangOpenClawSkill:
|
|
13
|
+
"""
|
|
14
|
+
OpenClaw skill for MoltLang translation.
|
|
15
|
+
|
|
16
|
+
Enables OpenClaw agents to use MoltLang for efficient communication.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
def __init__(self):
|
|
20
|
+
"""Initialize the skill."""
|
|
21
|
+
self.name = "moltlang"
|
|
22
|
+
self.version = "0.1.0"
|
|
23
|
+
self.description = "Translate between human language and MoltLang"
|
|
24
|
+
self.translator = MoltTranslator()
|
|
25
|
+
|
|
26
|
+
async def handle_message(self, message: str, context: dict[str, Any]) -> str:
|
|
27
|
+
"""
|
|
28
|
+
Handle incoming messages and translate to MoltLang.
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
message: Human language message
|
|
32
|
+
context: OpenClaw context
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
MoltLang translation
|
|
36
|
+
"""
|
|
37
|
+
return self.translator.translate_to_molt(message).text
|
|
38
|
+
|
|
39
|
+
async def handle_molt_message(self, molt: str, context: dict[str, Any]) -> str:
|
|
40
|
+
"""
|
|
41
|
+
Handle MoltLang messages and translate to human language.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
molt: MoltLang message
|
|
45
|
+
context: OpenClaw context
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
Human language translation
|
|
49
|
+
"""
|
|
50
|
+
return self.translator.translate_from_molt(molt).text
|
|
51
|
+
|
|
52
|
+
def get_commands(self) -> list[dict[str, Any]]:
|
|
53
|
+
"""Return available commands for OpenClaw."""
|
|
54
|
+
return [
|
|
55
|
+
{
|
|
56
|
+
"name": "molt",
|
|
57
|
+
"description": "Translate text to MoltLang",
|
|
58
|
+
"parameters": {
|
|
59
|
+
"text": {
|
|
60
|
+
"type": "string",
|
|
61
|
+
"description": "Text to translate",
|
|
62
|
+
"required": True,
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
"name": "unmolt",
|
|
68
|
+
"description": "Translate MoltLang to human language",
|
|
69
|
+
"parameters": {
|
|
70
|
+
"molt": {
|
|
71
|
+
"type": "string",
|
|
72
|
+
"description": "MoltLang text",
|
|
73
|
+
"required": True,
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
]
|