Incant——为你的代码添加魔法咒语
Incant – add magic spells to your code

原始链接: https://github.com/montyanderson/incant

`incant` 库提供了一种安全地将语言模型调用集成到代码中的方法。它使用一些原生方法来确保输出类型安全且无幻觉,这意味着输出保证在预期类型内,并且是提供的输入的子集。 代码演示了两个关键函数:`createSelector` 和 `createFilter`。`createSelector` 使用语言模型根据提示(例如,“选择最大的数字”)从给定数组中选择最佳元素。结果保证是数组中的一个数字,防止出现幻觉。 `createFilter` 使用语言模型根据给定提示(例如,“返回男性姓名”)从数组中过滤元素。输出保证是输入数组的子集,保留原始顺序并防止模型编造名称。 `incant` 依赖于环境变量(如 `OPENAI_API_KEY`)进行配置,使用方便。但是,务必避免向底层的推理提供程序发送个人或敏感数据,因为所有输入数据都会被传输到上游。

Hacker News 最新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Incant – 为你的代码添加魔法咒语 (github.com/montyanderson) 5 分,来自 montyanderson,1小时前 | 隐藏 | 过去 | 收藏 | 讨论 考虑申请 YC 2025 秋季批次!申请截止日期为 8 月 4 日 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系我们 搜索:
相关文章

原文

JSR

Add magic spells to your code.

dancing

Incant gives you primtivies to integrate language model invocations safely.

Warning

All data provided as input to incant primitives will be sent to upstream inferene providers. Avoid sending personal and sensitive information.

// incant looks in your env array for an OPENAI_API_KEY & other vars
// easy for end users to configure for your cli
const { createSelector, createFilter } = createIncant({
	env: Deno.env.toObject(),
});
// create a function to pick the highest number
const pickHighestNumber = createSelector<number>("Pick the highest number");

const input = [
	1,
	10,
	3,
	10000,
	5,
	999,
];

// type-safe and hallucination-safe – output is guaranteed to be one of input array
const highestNumber = await pickHighestNumber(input);
// make a function to filter only male names
const filterMaleNames = createFilter<string>("Return male names");

const maleNames = await filterMaleNames([
	"John",
	"Jack",
	"Jane",
	"Beatrice",
	"Mike",
	"Emily",
	"Charlie",
	"Robin",
	"Alex",
]);

// original array ordering is preserved
// no hallucinations possible: output is guaranteed to be subset of input array
// [ "John", "Jack", "Mike", "Charlie", "Alex" ]
联系我们 contact @ memedata.com