快速事实库
即时Factory库为构建和管理即时模板提供了强大,高效,语言独立的解决方案,用于各种AI模型,包括但不限于LLM(大型语言模型)和文本到图像模型. 它弥补了LangChain等复杂项目结构与直截了当的弦式诱导方法之间的差距,为迅速管理提供了精简和开发者友好的工具.
关键目标
- 效率和简化: 提供快速和直截了当的接口,以便在软件项目内进行迅速管理,而不需要大量依赖.
- 模型兼容性 : 确保与包括OpenAI,Mistral,Llama等在内的各种AI模型相兼容,为方便整合和灵活使用提供便利.
- 开发者 : 以开发者需求为重点的设计,允许快速定制,参数验证,并方便地存储和改版代码寄存器内的即时模板.
特征
- 易用性 : 直观API用于即时创建和管理,支持简单而复杂的即时配置.
- 参数验证 : 内置支持验证和水分快速参数,确保动态提示生成准确.
- 模型不可知论 : 与任何接受文本或结构化消息提示的AI模型无缝工作.
- Versatile 快速管理 : 允许在代码中存储即时模板和参数,简化版本控制和协作.
安装
使用 npm 或纱线安装 PointFactory 库 :
npm install @completionhq/promptfactory-node
# or
yarn add @completionhq/promptfactory-node
使用量
下面的例子说明如何使用即时事实来创建和使用即时信息.
基本快速创建
// Import required classes and types
import { StringPrompt, MessageArrayPrompt } from '@completionhq/promptfactory-node'; // Assuming the classes are exported from 'promptClasses.js'
// Create a StringPrompt instance
const stringPrompt = new StringPrompt("ExamplePrompt", {
template: "Hello, {{name}}! How can I assist you today?",
promptArguments: { name: "John Doe" },
});
// Or set the template and arguments separately
stringPrompt.setTemplate("Hello, {{name}}! How can I assist you today?");
stringPrompt.setArguments({ name: "John Doe" });
// Hydrate the string template to produce the final prompt
const hydratedStringPrompt = stringPrompt.hydrate();
console.log(hydratedStringPrompt); // Output: Hello, John Doe! How can I assist you today?
基本信件阵列快速创建( OpenAI / Chat 补全格式)
// Create a MessageArrayPrompt instance
const messageArrayPrompt = new MessageArrayPrompt("ExamplePrompt", {
template: [
{
role: "system",
content: "Hello, {{name}}! How can I assist you today?",
}
],
promptArguments: { name: "John Doe" },
});
// Or set the template and arguments separately
messageArrayPrompt.setTemplate([
{
role: "system",
content: "Hello, {{name}}! How can I assist you today?",
}
]);
messageArrayPrompt.setArguments({ name: "John Doe" });
// Hydrate the message array template to produce the final array of messages
const hydratedMessages = messageArrayPrompt.hydrate();
console.log(hydratedMessages); // Output: [{ role: 'system', content: 'Hello, John Doe! How can I assist you today?' }]
// For serialization and deserialization, refer to the utilities provided in the new interfaces
与 OpenAI 合并
import { OpenAI } from 'openai-node';
const prompt = new MessageArrayPromptFactory("OpenAIPrompt", {
messagesTemplate: [
{ role: "system", content: "Hello, {{name}}! How can I assist you today?" }
],
promptArguments: { name: "John Doe" },
});
const openai = new OpenAI({
apiKey: config.openaiApiKey,
});
const result = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: prompt.getHydratedMessagesArray(),
});
console.log(result.choices[0].message.content);
配置选项
自定义具有各种配置选项的即时事实的行为 :
promptTemplate:用于生成简单文本提示的字符串模板.messagesTemplate: 用于创建结构化消息提示的聊天补全参数阵列.promptArguments: 含有要插入即时或信件模板的变量的对象.parser:指定要使用的模板解析器,默认为FString用于简单的字符串插值.fileSerializationFormat: 确定从文件加载和保存提示所用的格式,以JSON为默认格式.
高级特性
- 基于文件的快速管理:直接从文件装入并管理提示,支持JSON和YAML两种格式,以便于使用和版本控制.
- 动态参数水分:无缝地将动态变量插入提示,确保准确和符合具体情况的快速生成.
- 全面验证:利用内置验证机制,确保及时模板和参数格式正确,无出错.