为了创造AI-nativeprogramminglanguage,我hack了CPython的str

admin 2026-03-27 01:14:53 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文介绍了一种名为IntentLang的编程方法,它通过修改CPython的字符串类型,让自然语言描述直接成为可执行的代码。开发者可以使用类似计算1到100的和.o(int)这样的语法来执行操作、处理数据、控制条件分支,甚至创建和操作复杂对象。该方法还支持缓存已执行的意图以提高效率。 综合评分: 75 文章分类: AI安全,安全开发,技术标准,其他


cover_image

为了创造 AI-native programming language, 我 hack 了 CPython 的 str

原创

l3yx l3yx

淚笑的赛博日记

2026年1月11日 16:36 浙江

上一篇文章 《IntentLang:以第一性原理与意图工程重建 AI Agent》 刚发布没几天,就收到很多师傅的反馈说晦涩难懂。我重新审视了一下,确实文章直接以我个人的视角去描述了很多只有我自己在做 Agent 开发时遇到的痛点,另外我文中举的例子又太过简单,不足以描述我到底在用 IntentLang 解决什么样的问题。而且也用了 AI 进行润色,确实让文章比较抽象。

另外就是大多数人其实不关注什么是 LLM 的原始推理结果,什么是模型厂商在此之上对外提供的 API 协议封装,支持工具调用的模型究竟和普通模型有什么不一样,Function Calling 到底是一种模型能力、协议约定还是运行时机制,让 LLM 看起来 “拥有记忆” 的本质究竟是什么,在应用层又该如何实现。

但我还是不想放弃介绍 IntentLang,于是我想到了一个很酷,也很 Geek 和 Hacker 的方法:直接在语言层面 “hack” Python,让字符串本身成为一个可执行的 Intent,贯彻人类意图就是一等公民

而这么做之后,IntentLang 也似乎向 ANPL(AI-native programming language)又更近了一步。

Vibe Coding 与 Intent Coding

目前的 Vibe Coding 指的是开发者通过自然语言描述需求,驱动 AI 编程工具去生成代码,开发者只需要关注需求和方向,甚至可以不在意代码细节,专注于最终目标。

Intent Coding

所以 Vibe Coding 是自然语言驱动 AI 进行编程,而 IntentLang 或者说 Intent Coding 是以 AI 为基础的自然语言编程自然语言编程不是生成代码,而是直接成为代码。这篇文章我也不想再讲抽象的概念,直接给出代码示例:

from intentlang import MagicIntent
MagicIntent.hack_str()

result = "计算1到100的和".o(int)
print(result)
$ python3 test.py
5050

是的,你没有看错,上述的代码是真实可运行的,你要做的仅仅是描述意图(自然语言的字符串就是意图),指定输出类型,然后就足够了,可以让开发者在 Python 世界里感受到 “言出法随”。

而且不仅如此,你还可以处理已有数据:

from intentlang import MagicIntent
MagicIntent.hack_str()

data = {"name": "leixiao", "age": 26, "city": "Hangzhou"}
result = "转成一个结构清晰的表格".i(data).o(str)
print(result)
$ python3 test.py
| Key | Value |
|-----|-------|
| name | leixiao |
| age | 26 |
| city | Hangzhou |

通过自然语言控制条件分支:

from intentlang import MagicIntent
MagicIntent.hack_str()

login_data = {"login_failures": 12}
if "这个用户是否应该被封禁?".i(login_data).o(bool).c("登陆失败次数超过10次的需要封建").r('先看一下登陆数据结构,不要猜测 key'):
    print("Yes")
else:
    print("No")
$ python3 test.py
Yes

还可以无中生有:

import socket
from intentlang import MagicIntent
MagicIntent.hack_str()

sock: socket.socket = "创造一个 socket 对象,并向 example.com 发起 GET 请求".o(socket.socket).r("不要读取数据,不要关闭 socket")
print(sock)
print(sock.recv(15).decode())
sock.close()
$ python3 test.py
<socket.socket fd=10, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('192.168.1.6', 58427), raddr=('104.18.27.120', 80)>
HTTP/1.1 200 OK

接力操作复杂对象:

import&nbsp;socket
from&nbsp;intentlang&nbsp;import&nbsp;MagicIntent
MagicIntent.hack_str()

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('example.com',&nbsp;80))
request =&nbsp;b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"

"用 socket 对象发送这段数据请求, 不需要读取".i({'sock': sock,&nbsp;'request': request})()

print(sock.recv(15).decode())
sock.close()
$&nbsp;python3 test.py
HTTP/1.1 200 OK

最后再来做个算法题吧:

from&nbsp;intentlang&nbsp;import&nbsp;MagicIntent
MagicIntent.hack_str()

s =&nbsp;"abcabcbb"
length =&nbsp;"找出其中不含有重复字符的最长子串的长度".i(s).o(int)
print(length)
$&nbsp;python3 test.py
3

Intent 的缓存与复用

前文的例子中,其实大多数意图都是固定的逻辑,也就是说意图被 AI 推理执行后,意图的执行过程是可以缓存的,下次再次执行同样的意图无需再次经过 AI 推理,直接复用上一次 AI 生成的代码即可。在 MagicIntent 中可以用 MagicIntent.hack_str(cache=True) 语句设置。

以算法的代码为例,会在当前目录下的 .intent_cache 目录生成缓存文件:

而且代码逻辑与输入的数据是解耦的,所以可以随意更换数据,之前的缓存代码依然可复用:

from&nbsp;intentlang&nbsp;import&nbsp;MagicIntent
MagicIntent.hack_str(cache=True)

s =&nbsp;"abcabcbb"
length =&nbsp;"找出其中不含有重复字符的最长子串的长度".i(s).o(int)
print(length)

s =&nbsp;"bbbbb"
length =&nbsp;"找出其中不含有重复字符的最长子串的长度".i(s).o(int)
print(length)

s =&nbsp;"pwwkew"
length =&nbsp;"找出其中不含有重复字符的最长子串的长度".i(s).o(int)
print(length)
$&nbsp;python3 test.py
3
1
3

MagicIntent.hack_str(cache=True)  会控制所有魔法意图(由 Python 字符串直接书写的意图)的缓存行为,如果要差异化控制每一个意图的缓存行为,或者有异步调用的需求,还是建议用 IntentLang 的 Intent 类,具体使用方法可以参考仓库 README: https://github.com/l3yx/intentlang#compilation-and-execution 。

黑客的语法糖

A Geek Idea, Implemented in a Hacker Way.

其实以上把自然语言字符串本身变成一个可计算对象只算是 IntentLang 的一个 “语法糖”,以下两段代码是等价的:

from&nbsp;intentlang&nbsp;import&nbsp;MagicIntent
MagicIntent.hack_str()

result =&nbsp;"计算1到100的和".o(int)
print(result)
from&nbsp;intentlang&nbsp;import&nbsp;Intent

intent = Intent().goal("计算1到100的和").output(result=(int,&nbsp;""))
result = intent.run_sync().output.result
print(result)

但后者远没有前者 Cool,也会被质疑 “这和其他 Agent 框架有什么区别?”。

然后这一小节本来还想讲一下我是如何 “hack” CPython 的 str 的,但想了下其实有了 AI 之后也没必要,大概提几个关键点吧:

  • 我在代码层,修改了 CPython 类型对象的内部结构,为 str 插入了自定义的几个方法
  • 在字符串上调用 .i() .o() .c() ... 这些方法其实是在构造一个 MagicIntent  对象
  • MagicIntent 相当于一个 “Value Proxy”,本身不保存值,保存的是 Intent 对象
  • 为 MagicIntent 重载了如 __str__, __float__ ,__add____getattr__ 等魔术方法
  • 在代码中进行任何对 MagicIntent 的观察和计算的行为,都会最终触发 Intent.compile(...).run_sync()

完整的关键代码如下,如果对细节原理感兴趣的话可以拿这段代码问一下 AI :

import&nbsp;ctypes
import&nbsp;gc
from&nbsp;typing&nbsp;import&nbsp;Type, Callable, Tuple, Any
from&nbsp;intentlang&nbsp;import&nbsp;Intent

class&nbsp;MagicIntent:
&nbsp; &nbsp; _max_iterations: int =&nbsp;30
&nbsp; &nbsp; _cache: bool =&nbsp;False
&nbsp; &nbsp; _record: bool =&nbsp;True

&nbsp; &nbsp;&nbsp;def&nbsp;__init__(self, goal: str):
&nbsp; &nbsp; &nbsp; &nbsp; self.intent = Intent().goal(goal)
&nbsp; &nbsp; &nbsp; &nbsp; self._result: Any =&nbsp;None
&nbsp; &nbsp; &nbsp; &nbsp; self._executed: bool =&nbsp;False

&nbsp; &nbsp;&nbsp;def&nbsp;o(self, type: Type)&nbsp;-> "MagicIntent":
&nbsp; &nbsp; &nbsp; &nbsp; self.intent.output(result=(type,&nbsp;""))
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;self

&nbsp; &nbsp; ...

&nbsp; &nbsp;&nbsp;def&nbsp;__call__(self)&nbsp;-> Any:
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;ifnot&nbsp;self._executed:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;ifnot&nbsp;self.intent._output:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.intent.output(result=(bool,&nbsp;"success"))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self._result = self.intent.compile(
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max_iterations=MagicIntent._max_iterations,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cache=MagicIntent._cache,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; record=MagicIntent._record
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ).run_sync().output.result
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self._executed =&nbsp;True
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;self._result

&nbsp; &nbsp;&nbsp;def&nbsp;__bool__(self)&nbsp;-> bool:
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;bool(self())

&nbsp; &nbsp;&nbsp;def&nbsp;__int__(self)&nbsp;-> int:
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;int(self())

&nbsp; &nbsp; ...

&nbsp; &nbsp; @classmethod
&nbsp; &nbsp;&nbsp;def&nbsp;hack_str(cls, max_iterations: int =&nbsp;30, cache: bool = False, record: bool = True):
&nbsp; &nbsp; &nbsp; &nbsp; cls._max_iterations = max_iterations
&nbsp; &nbsp; &nbsp; &nbsp; cls._cache = cache
&nbsp; &nbsp; &nbsp; &nbsp; cls._record = record

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;def&nbsp;o(self, p)&nbsp;-> "MagicIntent":
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; magic_intent = cls(self)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; magic_intent.o(p)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;magic_intent

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;def&nbsp;i(self, p)&nbsp;-> "MagicIntent":
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; magic_intent = cls(self)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; magic_intent.i(p)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;magic_intent

&nbsp; &nbsp; &nbsp; &nbsp; ...

&nbsp; &nbsp; &nbsp; &nbsp; referents = gc.get_referents(str.__dict__)
&nbsp; &nbsp; &nbsp; &nbsp; real_dict = next(obj&nbsp;for&nbsp;obj&nbsp;in&nbsp;referents&nbsp;if&nbsp;isinstance(obj, dict))
&nbsp; &nbsp; &nbsp; &nbsp; real_dict['o'] = o
&nbsp; &nbsp; &nbsp; &nbsp; real_dict['i'] = i
&nbsp; &nbsp; &nbsp; &nbsp; ...
&nbsp; &nbsp; &nbsp; &nbsp; ctypes.pythonapi.PyType_Modified(ctypes.py_object(str))

免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:淚笑的赛博日记 l3yx l3yx《为了创造 AI-native programming language, 我 hack 了 CPython 的 str》

评论:0   参与:  0