我过去几个月在和 Claude 合作使用 Rust 开发了一门新语言 AIScript,现在正式开源。这门语言最大的特点是内置 AI 特性以及把语言和 Web 框架合二为一。
在 AIScript 里 prompt/ai/agent 都是关键字,使用 prompt "text"
就可以跟 AI 交互,任何使用了 prompt
的函数都需要显示声明是 ai
函数。定义一个 agent
跟定义一个 class
一样简单,支持类似 OpenAI Swarm 那种多 agent 编排。
只需要使用简单明了的 DSL 就可以写 web 接口,并且自动生成 OpenAPI docs ,JWT 或者 Google 社交账号登录用一个 @auth
或 @sso
指令即可。
$ export OPENAI_API_KEY=<your-openai-api-key>
$ cat web.ai
get / {
"""An api to ask LLM"""
query {
"""the question to ask"""
@string(min_len=3, max_len=100) // validate params with builtin directive @string
question: str
}
// `ai` and `prompt` are keywords
ai fn ask(question: str) -> str {
let answer = prompt question;
return answer;
}
// use query.name or query["name"] to access query parameter
let answer = ask(query.question);
return { answer };
}
$ aiscript serve web.ai
Listening on http://localhost:8080
$ curl http://localhost:8080
{
"error": "Missing required field: question"
}
$ curl http://localhost:8080?question=Hi
{
"error": "Field validation failed: question: String length is less than the minimum length of 3"
}
$ curl http://localhost:8080?question=What is the capital of France?
{
"answer": "The capital of France is Paris."
}
agent Researcher {
instructions: "You are a research assistant...",
fn search_web(query: str) -> str {
"""
Search the web for information about a topic.
"""
// Implementation
return "search results";
}
fn save_notes(content: str) -> bool {
"""
Save notes about a topic.
"""
// Implementation
return true;
}
}
@sso(provider="google")
get /auth/google {
let url = sso.authority_url();
print(url);
return temporary_redirect(target=url);
}
@sso(provider="google")
get /auth/google/callback {
query {
code: str,
state: str,
}
print("code", query.code);
print("state", query.state);
let user_info = sso.verify(code=query.code);
print(user_info);
return { user_info };
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.