lmcc 0.8.0__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.
- lmcc-0.8.0/LICENSE +202 -0
- lmcc-0.8.0/PKG-INFO +54 -0
- lmcc-0.8.0/README.md +29 -0
- lmcc-0.8.0/lmcc/__init__.py +46 -0
- lmcc-0.8.0/lmcc/adapter.py +214 -0
- lmcc-0.8.0/lmcc/core.py +684 -0
- lmcc-0.8.0/lmcc/errors.py +30 -0
- lmcc-0.8.0/lmcc/extensions.py +227 -0
- lmcc-0.8.0/lmcc/fn.py +115 -0
- lmcc-0.8.0/lmcc/formats.py +272 -0
- lmcc-0.8.0/lmcc/helpers.py +149 -0
- lmcc-0.8.0/lmcc/plan.py +1465 -0
- lmcc-0.8.0/lmcc/reader.py +406 -0
- lmcc-0.8.0/lmcc/registry.py +204 -0
- lmcc-0.8.0/lmcc/serde.py +221 -0
- lmcc-0.8.0/lmcc/stream.py +839 -0
- lmcc-0.8.0/lmcc/template.py +308 -0
- lmcc-0.8.0/lmcc/transport.py +392 -0
- lmcc-0.8.0/lmcc/turn.py +303 -0
- lmcc-0.8.0/lmcc.egg-info/PKG-INFO +54 -0
- lmcc-0.8.0/lmcc.egg-info/SOURCES.txt +57 -0
- lmcc-0.8.0/lmcc.egg-info/dependency_links.txt +1 -0
- lmcc-0.8.0/lmcc.egg-info/requires.txt +7 -0
- lmcc-0.8.0/lmcc.egg-info/top_level.txt +4 -0
- lmcc-0.8.0/lmcc_dspy/__init__.py +255 -0
- lmcc-0.8.0/lmcc_lm15/__init__.py +107 -0
- lmcc-0.8.0/lmcc_std/__init__.py +29 -0
- lmcc-0.8.0/lmcc_std/code.py +124 -0
- lmcc-0.8.0/lmcc_std/formats.py +255 -0
- lmcc-0.8.0/lmcc_std/jsontext.py +162 -0
- lmcc-0.8.0/lmcc_std/readers.py +129 -0
- lmcc-0.8.0/lmcc_std/reasoning.py +70 -0
- lmcc-0.8.0/lmcc_std/tools.py +262 -0
- lmcc-0.8.0/pyproject.toml +33 -0
- lmcc-0.8.0/setup.cfg +4 -0
- lmcc-0.8.0/tests/test_agent_surface.py +73 -0
- lmcc-0.8.0/tests/test_audit_holes.py +304 -0
- lmcc-0.8.0/tests/test_coherence.py +237 -0
- lmcc-0.8.0/tests/test_corpus.py +29 -0
- lmcc-0.8.0/tests/test_docs_howto.py +55 -0
- lmcc-0.8.0/tests/test_driver_protocol.py +21 -0
- lmcc-0.8.0/tests/test_extensions.py +155 -0
- lmcc-0.8.0/tests/test_fix_hints.py +223 -0
- lmcc-0.8.0/tests/test_formats.py +196 -0
- lmcc-0.8.0/tests/test_guide.py +14 -0
- lmcc-0.8.0/tests/test_harness_checks.py +71 -0
- lmcc-0.8.0/tests/test_helpers.py +71 -0
- lmcc-0.8.0/tests/test_heredoc_turns.py +192 -0
- lmcc-0.8.0/tests/test_kernel.py +170 -0
- lmcc-0.8.0/tests/test_package_version.py +11 -0
- lmcc-0.8.0/tests/test_repairs.py +301 -0
- lmcc-0.8.0/tests/test_research_cross_design.py +168 -0
- lmcc-0.8.0/tests/test_response_parts.py +49 -0
- lmcc-0.8.0/tests/test_std.py +121 -0
- lmcc-0.8.0/tests/test_streaming.py +460 -0
- lmcc-0.8.0/tests/test_text_rules.py +247 -0
- lmcc-0.8.0/tests/test_tools_citations.py +163 -0
- lmcc-0.8.0/tests/test_transports_serde.py +103 -0
- lmcc-0.8.0/tests/test_turns.py +271 -0
lmcc-0.8.0/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
lmcc-0.8.0/PKG-INFO
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lmcc
|
|
3
|
+
Version: 0.8.0
|
|
4
|
+
Summary: The language model calling convention: typed signatures to messages and replies to typed values
|
|
5
|
+
Author: Maxime Rivest
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/MaximeRivest/lmcc
|
|
8
|
+
Project-URL: Source, https://github.com/MaximeRivest/lmcc
|
|
9
|
+
Project-URL: Specification, https://github.com/MaximeRivest/lmcc/blob/master/contract/spec/kernel.md
|
|
10
|
+
Keywords: llm,language models,prompting,parsing,structured output
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Provides-Extra: dspy
|
|
20
|
+
Requires-Dist: dspy>=3.0; extra == "dspy"
|
|
21
|
+
Requires-Dist: pydantic>=2; extra == "dspy"
|
|
22
|
+
Provides-Extra: lm15
|
|
23
|
+
Requires-Dist: lm15==1.0.0rc1; extra == "lm15"
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
# lmcc — the calling convention for calling a model
|
|
27
|
+
|
|
28
|
+
When a program calls a function in another language, a calling
|
|
29
|
+
convention says where each argument goes, how the result comes back, and
|
|
30
|
+
how each type crosses. A model is another language; lmcc is its calling
|
|
31
|
+
convention. It lays out each call as an [lm15](https://pypi.org/project/lm15/)
|
|
32
|
+
request and reads each reply back into typed values. It never touches
|
|
33
|
+
the network.
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
import lmcc
|
|
37
|
+
|
|
38
|
+
@lmcc.fn
|
|
39
|
+
def answer(question: str) -> str:
|
|
40
|
+
"""Answer the question in one sentence."""
|
|
41
|
+
|
|
42
|
+
xml = lmcc.adapter(messages=[
|
|
43
|
+
lmcc.system("{instruction}\n{% for f in outputs %}<{f.name}>\n{f.value}\n</{f.name}>\n{% endfor %}"),
|
|
44
|
+
lmcc.user("{question}"),
|
|
45
|
+
])
|
|
46
|
+
plan = answer.bind(xml)
|
|
47
|
+
request = plan.render(question="Why is the sky blue?").request("gpt-4.1-mini") # an lm15 request
|
|
48
|
+
plan.read("<Answer>\nRayleigh scattering.\n</Answer>").values # {'answer': ...}, misspelling repaired and reported
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`pip install "lmcc[lm15]"` adds the typed bridge to lm15 (`lmcc_lm15`).
|
|
52
|
+
The tutorial, how-to guides, the normative specification and the
|
|
53
|
+
conformance corpus are in the repository:
|
|
54
|
+
https://github.com/MaximeRivest/lmcc
|
lmcc-0.8.0/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# lmcc — the calling convention for calling a model
|
|
2
|
+
|
|
3
|
+
When a program calls a function in another language, a calling
|
|
4
|
+
convention says where each argument goes, how the result comes back, and
|
|
5
|
+
how each type crosses. A model is another language; lmcc is its calling
|
|
6
|
+
convention. It lays out each call as an [lm15](https://pypi.org/project/lm15/)
|
|
7
|
+
request and reads each reply back into typed values. It never touches
|
|
8
|
+
the network.
|
|
9
|
+
|
|
10
|
+
```python
|
|
11
|
+
import lmcc
|
|
12
|
+
|
|
13
|
+
@lmcc.fn
|
|
14
|
+
def answer(question: str) -> str:
|
|
15
|
+
"""Answer the question in one sentence."""
|
|
16
|
+
|
|
17
|
+
xml = lmcc.adapter(messages=[
|
|
18
|
+
lmcc.system("{instruction}\n{% for f in outputs %}<{f.name}>\n{f.value}\n</{f.name}>\n{% endfor %}"),
|
|
19
|
+
lmcc.user("{question}"),
|
|
20
|
+
])
|
|
21
|
+
plan = answer.bind(xml)
|
|
22
|
+
request = plan.render(question="Why is the sky blue?").request("gpt-4.1-mini") # an lm15 request
|
|
23
|
+
plan.read("<Answer>\nRayleigh scattering.\n</Answer>").values # {'answer': ...}, misspelling repaired and reported
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
`pip install "lmcc[lm15]"` adds the typed bridge to lm15 (`lmcc_lm15`).
|
|
27
|
+
The tutorial, how-to guides, the normative specification and the
|
|
28
|
+
conformance corpus are in the repository:
|
|
29
|
+
https://github.com/MaximeRivest/lmcc
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""lmcc — the language model calling convention.
|
|
2
|
+
|
|
3
|
+
A typed signature and an adapter (a template, a parse rule, transports by
|
|
4
|
+
purpose, formats by type) bind into a plan that renders messages and parses
|
|
5
|
+
replies. The kernel ships no vocabulary beyond its scalar and media
|
|
6
|
+
defaults; everything else registers through the sockets.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from .adapter import (Adapter, adapter, assistant, developer, message, system, turns, use,
|
|
10
|
+
user)
|
|
11
|
+
from .core import (Field, SignatureCore, Capture, field, signature, signature_from_dict,
|
|
12
|
+
signature_to_dict, typename)
|
|
13
|
+
from .errors import Refusal, refuse
|
|
14
|
+
from .extensions import ExtensionBinding, PatternBinding, native_extensions
|
|
15
|
+
from .fn import Fn, One, Purpose, fn
|
|
16
|
+
from .formats import Format, make as make_format, ship
|
|
17
|
+
from .reader import Reader
|
|
18
|
+
from .plan import Plan, Reading, RenderResult, bind
|
|
19
|
+
from .helpers import choose, find, put, when
|
|
20
|
+
from .registry import Registry, default_registry
|
|
21
|
+
from .serde import KERNEL_VERSION, dump, load
|
|
22
|
+
from .transport import Transport
|
|
23
|
+
from .stream import Stream, StreamResult
|
|
24
|
+
from .turn import ModelStep, ToolStep, Turn, signature_fingerprint
|
|
25
|
+
|
|
26
|
+
__version__ = KERNEL_VERSION
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def format(host_type, **kw):
|
|
31
|
+
"""``lmcc.format(Person, write=..., read=..., describe=...)`` — bind a
|
|
32
|
+
type to a format in the default registry (per runtime, never
|
|
33
|
+
serialized; ``ship`` does that on request)."""
|
|
34
|
+
return default_registry.format(host_type, **kw)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
__all__ = [
|
|
38
|
+
"Adapter", "ExtensionBinding", "Field", "Fn", "Format", "KERNEL_VERSION", "Reader",
|
|
39
|
+
"ModelStep", "One", "PatternBinding", "Plan", "Reading",
|
|
40
|
+
"Refusal", "Registry", "RenderResult", "Purpose", "SignatureCore", "Capture", "Transport",
|
|
41
|
+
"Stream", "StreamResult", "ToolStep", "Turn",
|
|
42
|
+
"adapter", "assistant", "bind", "choose", "find", "put", "when", "default_registry", "developer", "dump",
|
|
43
|
+
"field", "fn", "format", "load", "make_format", "message", "native_extensions",
|
|
44
|
+
"refuse", "ship", "signature", "signature_fingerprint",
|
|
45
|
+
"signature_from_dict", "signature_to_dict", "system", "turns", "typename", "use", "user",
|
|
46
|
+
]
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"""The Adapter: a template, a reader, transports by purpose, formats by
|
|
2
|
+
type — never a field name. It meets a signature only at ``bind``.
|
|
3
|
+
|
|
4
|
+
Construction surfaces:
|
|
5
|
+
- ``lmcc.adapter(messages=[...], reader=..., transports=..., formats=...)``
|
|
6
|
+
- ``lmcc.load(entry, registry=...)`` from serialized data (serde.py)
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from dataclasses import dataclass, field as dc_field
|
|
12
|
+
|
|
13
|
+
from .errors import refuse
|
|
14
|
+
from .transport import Transport
|
|
15
|
+
from .template import RESERVED_SLOTS, compile_template, turn_slots
|
|
16
|
+
|
|
17
|
+
_SLOT_NAME = __import__("re").compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
|
|
18
|
+
REPLAY = ("recorded", "values")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def system(text: str) -> dict:
|
|
22
|
+
return {"role": "system", "text": text}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def developer(text: str) -> dict:
|
|
26
|
+
return {"role": "developer", "text": text}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def user(text: str) -> dict:
|
|
30
|
+
return {"role": "user", "text": text}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def assistant(text: str) -> dict:
|
|
34
|
+
return {"role": "assistant", "text": text}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def message(role: str, text: str) -> dict:
|
|
38
|
+
if role not in ("system", "developer", "user", "assistant"):
|
|
39
|
+
refuse("entry-malformed", f"message role {role!r} must be system/developer/user/assistant",
|
|
40
|
+
fix={"action": "edit-entry", "path": "template"})
|
|
41
|
+
return {"role": role, "text": text}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def turns(slot: str = "turns") -> dict:
|
|
45
|
+
"""A turn slot in messages form (kernel §3a): the slot's turns become
|
|
46
|
+
ordinary messages here. ``lmcc.turns()`` is the slot ``turns``."""
|
|
47
|
+
return {"directive": "turns"} if slot == "turns" else {"directive": "turns", "slot": slot}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def use(name: str, **options) -> dict:
|
|
51
|
+
"""A reference to a named format or transport: ``lmcc.use("table", columns=[...])``."""
|
|
52
|
+
return {"use": name, "options": options}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@dataclass
|
|
56
|
+
class Adapter:
|
|
57
|
+
template: list # [{role, text} | {directive}]
|
|
58
|
+
reader: dict # {"kind": "derived", ...}
|
|
59
|
+
transports: dict[str, object] = dc_field(default_factory=dict) # purpose -> Transport | {"use", "options"}
|
|
60
|
+
formats: dict[str, object] = dc_field(default_factory=dict) # type/structural key -> {"use", "options"} | shipped dict | Format
|
|
61
|
+
name: str = "adapter"
|
|
62
|
+
extensions: dict[str, str] = dc_field(default_factory=dict) # "<family>/<name>" -> version needed (kernel §10)
|
|
63
|
+
replay: str = "recorded" # how written model steps are spelled (kernel §3a)
|
|
64
|
+
strict: bool = False # True: read replies exactly, no repairs (kernel §4a)
|
|
65
|
+
|
|
66
|
+
def bind(self, signature, capabilities: dict | None = None, *, registry=None):
|
|
67
|
+
from .plan import bind as _bind
|
|
68
|
+
from .registry import default_registry
|
|
69
|
+
return _bind(self, signature, capabilities or {}, registry or default_registry)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def dump(self, *, registry=None) -> dict:
|
|
73
|
+
from .serde import dump as _dump
|
|
74
|
+
from .registry import default_registry
|
|
75
|
+
return _dump(self, registry or default_registry)
|
|
76
|
+
|
|
77
|
+
def compiled_messages(self) -> list[tuple[dict, list | None]]:
|
|
78
|
+
cached = getattr(self, "_compiled", None)
|
|
79
|
+
if cached is not None and cached[0] == self.template:
|
|
80
|
+
return cached[1]
|
|
81
|
+
out = []
|
|
82
|
+
for i, msg in enumerate(self.template):
|
|
83
|
+
if "directive" in msg:
|
|
84
|
+
out.append((msg, None))
|
|
85
|
+
else:
|
|
86
|
+
out.append((msg, compile_template(msg["text"], where=f"template[{i}]")))
|
|
87
|
+
object.__setattr__(self, "_compiled", ([dict(m) for m in self.template], out))
|
|
88
|
+
return out
|
|
89
|
+
|
|
90
|
+
def turn_slots(self) -> dict[str, tuple[str, int]]:
|
|
91
|
+
"""Every placed turn slot: name -> ("messages" | "text", template index)
|
|
92
|
+
(kernel §3a). Placing one twice, or guarding an unplaced one, is
|
|
93
|
+
``template-syntax``."""
|
|
94
|
+
slots: dict[str, tuple[str, int]] = {}
|
|
95
|
+
guards: list[tuple[str, int]] = []
|
|
96
|
+
for i, (msg, nodes) in enumerate(self.compiled_messages()):
|
|
97
|
+
if nodes is None:
|
|
98
|
+
placed, guarded = [msg.get("slot", "turns")], []
|
|
99
|
+
form = "messages"
|
|
100
|
+
else:
|
|
101
|
+
placed, guarded = turn_slots(nodes)
|
|
102
|
+
form = "text"
|
|
103
|
+
for name in placed:
|
|
104
|
+
if name in slots:
|
|
105
|
+
refuse("template-syntax",
|
|
106
|
+
f"template[{i}]: turn slot {name!r} is already placed at "
|
|
107
|
+
f"template[{slots[name][1]}]; a slot is placed once",
|
|
108
|
+
fix={"action": "edit-template", "path": f"template[{i}]"})
|
|
109
|
+
slots[name] = (form, i)
|
|
110
|
+
guards += [(g, i) for g in guarded]
|
|
111
|
+
self._guards = guards # checked at bind: a guard may name an input (kernel §3a)
|
|
112
|
+
return slots
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def adapter(*, messages: list[dict] | None = None, template: list[dict] | dict | None = None,
|
|
116
|
+
reader: dict | None = None, transports: dict | None = None,
|
|
117
|
+
formats: dict | None = None, name: str = "adapter",
|
|
118
|
+
extensions: dict[str, str] | None = None, replay: str = "recorded",
|
|
119
|
+
strict: bool = False, declare_defaults: bool = True) -> Adapter:
|
|
120
|
+
"""Build an adapter. ``transports`` values: a name, a :class:`Transport`,
|
|
121
|
+
a data dict, or ``use(...)``. ``formats`` keys: type names or structural
|
|
122
|
+
keys; values: a name, ``use(...)``, a shipped dict, or a Format.
|
|
123
|
+
``extensions``: ``{"<family>/<name>": version}`` the adapter needs
|
|
124
|
+
(kernel §10); checked against the registry at bind. With
|
|
125
|
+
``declare_defaults`` (the constructor's convenience, not the loader's)
|
|
126
|
+
an inline ``pattern`` rule declares ``pattern/legacy-re2`` for you;
|
|
127
|
+
the dumped artifact carries the line either way. ``replay``:
|
|
128
|
+
``"recorded"`` writes a past model step's recorded reply when this plan
|
|
129
|
+
reads it back into the same values, ``"values"`` always writes it from
|
|
130
|
+
its values (kernel §3a). ``strict=True`` reads replies exactly as the
|
|
131
|
+
template spells them: no marker, delimiter or value repairs (kernel §4a)."""
|
|
132
|
+
if messages is None:
|
|
133
|
+
messages = template.get("messages") if isinstance(template, dict) else template
|
|
134
|
+
if not isinstance(messages, list):
|
|
135
|
+
refuse("entry-malformed", "template must be a list of messages and directives",
|
|
136
|
+
fix={"action": "edit-entry", "path": "template"})
|
|
137
|
+
for i, m in enumerate(messages):
|
|
138
|
+
if not isinstance(m, dict) or not ({"role", "text"} <= set(m) or "directive" in m):
|
|
139
|
+
refuse("entry-malformed", f"template[{i}]: a message is {{role, text}} or {{directive}}",
|
|
140
|
+
fix={"action": "edit-entry", "path": f"template[{i}]"})
|
|
141
|
+
if "directive" in m:
|
|
142
|
+
slot = m.get("slot", "turns")
|
|
143
|
+
if (m["directive"] != "turns" or set(m) - {"directive", "slot"}
|
|
144
|
+
or not isinstance(slot, str) or not _SLOT_NAME.match(slot)):
|
|
145
|
+
refuse("entry-malformed",
|
|
146
|
+
f"template[{i}]: a directive is {{\"directive\": \"turns\", \"slot\"?: "
|
|
147
|
+
f"name}} (demos and history are turn slots since kernel 0.7)",
|
|
148
|
+
fix={"action": "edit-entry", "path": f"template[{i}]"})
|
|
149
|
+
if slot in RESERVED_SLOTS:
|
|
150
|
+
refuse("template-syntax", f"template[{i}]: {slot!r} is reserved, not a turn slot",
|
|
151
|
+
fix={"action": "edit-template", "path": f"template[{i}]"})
|
|
152
|
+
if "role" in m and m["role"] not in ("system", "developer", "user", "assistant"):
|
|
153
|
+
refuse("entry-malformed", f"template[{i}]: role must be system/developer/user/assistant",
|
|
154
|
+
fix={"action": "edit-entry", "path": f"template[{i}]"})
|
|
155
|
+
if m.get("role") == "system" and any("role" in x and x["role"] != "system" or "directive" in x
|
|
156
|
+
for x in messages[:i]):
|
|
157
|
+
refuse("entry-malformed",
|
|
158
|
+
f"template[{i}]: system messages lead the template (they become the lm15 "
|
|
159
|
+
f"request's system field); put later instructions in a developer message",
|
|
160
|
+
fix={"action": "edit-entry", "path": f"template[{i}]"})
|
|
161
|
+
if replay not in REPLAY:
|
|
162
|
+
refuse("entry-malformed", f"replay must be one of {REPLAY}, not {replay!r}",
|
|
163
|
+
fix={"action": "edit-entry", "path": "replay"})
|
|
164
|
+
reader = reader or {"kind": "derived"}
|
|
165
|
+
kind = reader.get("kind")
|
|
166
|
+
if not isinstance(kind, str) or not kind:
|
|
167
|
+
refuse("unknown-reader", "reader.kind must name a reader",
|
|
168
|
+
fix={"action": "edit-entry", "path": "reader"})
|
|
169
|
+
if kind == "derived" and set(reader) != {"kind"}:
|
|
170
|
+
refuse("entry-malformed", f"reader: the derived reader takes only 'kind', not "
|
|
171
|
+
f"{sorted(set(reader) - {'kind'})}",
|
|
172
|
+
fix={"action": "edit-entry", "path": "reader"})
|
|
173
|
+
if not isinstance(strict, bool):
|
|
174
|
+
refuse("entry-malformed", f"strict must be true or false, not {strict!r}",
|
|
175
|
+
fix={"action": "edit-entry", "path": "strict"})
|
|
176
|
+
s_bindings: dict[str, object] = {}
|
|
177
|
+
for purpose, value in (transports or {}).items():
|
|
178
|
+
where = f"transports[{purpose!r}]"
|
|
179
|
+
if isinstance(value, str):
|
|
180
|
+
s_bindings[purpose] = {"use": value, "options": {}}
|
|
181
|
+
elif isinstance(value, Transport):
|
|
182
|
+
value.validate(where=where)
|
|
183
|
+
s_bindings[purpose] = value
|
|
184
|
+
elif isinstance(value, dict) and "use" in value:
|
|
185
|
+
s_bindings[purpose] = {"use": value["use"], "options": dict(value.get("options", {}))}
|
|
186
|
+
elif isinstance(value, dict):
|
|
187
|
+
s_bindings[purpose] = Transport.from_dict(value, where=where)
|
|
188
|
+
else:
|
|
189
|
+
refuse("entry-malformed", f"{where}: expected a name, Transport, use(...), or dict",
|
|
190
|
+
fix={"action": "edit-entry", "path": where})
|
|
191
|
+
f_bindings: dict[str, object] = {}
|
|
192
|
+
for key, value in (formats or {}).items():
|
|
193
|
+
where = f"formats[{key!r}]"
|
|
194
|
+
if isinstance(value, str):
|
|
195
|
+
f_bindings[key] = {"use": value, "options": {}}
|
|
196
|
+
elif isinstance(value, dict) and "use" in value:
|
|
197
|
+
f_bindings[key] = {"use": value["use"], "options": dict(value.get("options", {}))}
|
|
198
|
+
elif isinstance(value, dict) and "language" in value:
|
|
199
|
+
f_bindings[key] = dict(value)
|
|
200
|
+
elif hasattr(value, "write"):
|
|
201
|
+
f_bindings[key] = value
|
|
202
|
+
else:
|
|
203
|
+
refuse("entry-malformed", f"{where}: expected a name, use(...), a shipped format, or a Format",
|
|
204
|
+
fix={"action": "edit-entry", "path": where})
|
|
205
|
+
from .extensions import default_declaration, validate_declaration
|
|
206
|
+
declared = validate_declaration(extensions)
|
|
207
|
+
if declare_defaults:
|
|
208
|
+
declared = default_declaration(s_bindings, declared)
|
|
209
|
+
adp = Adapter(template=list(messages), reader=dict(reader), transports=s_bindings,
|
|
210
|
+
formats=f_bindings, name=name, extensions=declared, replay=replay,
|
|
211
|
+
strict=strict)
|
|
212
|
+
adp.compiled_messages() # surface template syntax errors immediately
|
|
213
|
+
adp.turn_slots() # and turn-slot put errors
|
|
214
|
+
return adp
|