@completionhq/promptfactory-node

中文文档
0.0.3发布于 2024/04/01MITTypeScriptESM + CJS近 30 天下载 8154

The PromptFactory Node SDK is a fast and lightweight to create and manage prompts for your LLM application.

原始使用文档
文档目录9 节
  1. Key Objectives
  2. Features
  3. Installation
  4. Usage
  5. Basic Prompt Creation
  6. Basic Message Array Prompt Creation (OpenAI / Chat Completi…
  7. Integration with OpenAI
  8. Configuration Options
  9. Advanced Features

PromptFactory Library

The PromptFactory library provides a robust, efficient, and language-independent solution for constructing and managing prompt templates for use with various AI models, including but not limited to LLMs (Large Language Models) and text-to-image models. It bridges the gap between complex project structures like LangChain and straightforward string templating approaches, offering a streamlined and developer-friendly toolset for prompt management.

Key Objectives

  • Efficiency and Simplicity: Offer a rapid and straightforward interface for prompt management within software projects, without the need for extensive dependencies.
  • Model Compatibility: Ensure compatibility with a wide range of AI models, including OpenAI, Mistral, Llama, etc., facilitating easy integration and flexibility in usage.
  • Developer-Focused: Design with a focus on developer needs, allowing for prompt customization, argument validation, and easy storage and versioning of prompt templates within code repositories.

Features

  • Ease of Use: Intuitive API for prompt creation and management, supporting both simple and complex prompt configurations.
  • Argument Validation: Built-in support for validating and hydrating prompt arguments, ensuring dynamic prompts are generated accurately.
  • Model Agnostic: Works seamlessly with any AI model accepting text or structured message prompts.
  • Versatile Prompt Management: Allows for the storage of prompt templates and arguments in code, simplifying version control and collaboration.

Installation

Install the PromptFactory library using npm or yarn:

npm install @completionhq/promptfactory-node

# or

yarn add @completionhq/promptfactory-node

Usage

Below are examples demonstrating how to use PromptFactory for creating and utilizing prompts.

Basic Prompt Creation

// 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?

Basic Message Array Prompt Creation (OpenAI / Chat Completion format)

// 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

Integration with 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);

Configuration Options

Customize the behavior of PromptFactory with various configuration options:

  • promptTemplate: String template for generating simple text prompts.
  • messagesTemplate: Array of chat completion parameters for creating structured message prompts.
  • promptArguments: Object containing variables to be interpolated into the prompt or messages template.
  • parser: Specifies the template parser to use, defaulting to FString for simple string interpolation.
  • fileSerializationFormat: Determines the format used for loading and saving prompts from/to files, with JSON as the default format.

Advanced Features

  • File-based Prompt Management: Load and manage prompts directly from files, supporting both JSON and YAML formats for ease of use and version control.
  • Dynamic Argument Hydration: Seamlessly interpolate dynamic variables into prompts, ensuring accurate and contextually relevant prompt generation.
  • Comprehensive Validation: Utilize built-in validation mechanisms to ensure prompt templates and arguments are correctly formatted and error-free.

版本与发布记录

@completionhq/promptfactory-node 共发布 3 个版本,首次发布于 2024/03/30;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.3latest2024/04/012 年前
0.0.22024/03/302 年前
0.0.12024/03/302 年前

关于 @completionhq/promptfactory-node 的常见问题

@completionhq/promptfactory-node 是什么?

The PromptFactory Node SDK is a fast and lightweight to create and manage prompts for your LLM application.

如何安装 @completionhq/promptfactory-node?

在项目目录运行 npm install @completionhq/promptfactory-node,或使用 pnpm add / yarn add / bun add。

@completionhq/promptfactory-node 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@completionhq/promptfactory-node 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@completionhq/promptfactory-node 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=12.*。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@completionhq/promptfactory-node 可以商用吗?

@completionhq/promptfactory-node 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@completionhq/promptfactory-node 的下载量有多少?

根据 npm 官方下载统计,@completionhq/promptfactory-node 在 2026/10/11 – 2026/11/09 这 30 天内被下载 8,154 次,日均约 272 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@completionhq/promptfactory-node 还在维护吗?

最新版本 0.0.3 发布于 2024/04/01(2 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/03/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。