1.1.3 • Published 5 months ago

@n-promptstudio/sdk v1.1.3

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

README

PromptStudio SDK

This Node.js SDK provides a convenient way to interact with N-PromptStudio's API, specifically for managing and chatting with prompts.

Installation

To install the N-PromptStudio SDK, run the following command:

npm install @n-promptstudio/sdk

Usage

Initializing the SDK

First, import and initialize the PromptStudioManager:

import { PromptStudioManager } from '@n-promptstudio/sdk';

const promptManager = new PromptStudioManager({
    apiKey: 'YOUR_API_KEY',
    bypass: false,  // Optional: Defaults to false
    isLogging: true  // Optional: Defaults to true - enables/disables saving logs with N-PromptStudio
});

When isLogging is set to true (default), all chat interactions will be logged and saved in N-PromptStudio's system for future reference and analytics. If you want to disable this logging functionality, set isLogging to false.

Chatting with a Prompt

You can use the chatWithPrompt method to interact with a prompt:

const response = await promptManager.chatWithPrompt('PROMPT_ID', {
    user_message: [{ type: 'text', text: 'Hello, how are you?' }],
    memory_type: "windowMemory",
    window_size: 10,
    session_id: "",
    variables: {},
    isSessionEnabled: true  // Optional: Defaults to true
});

console.log(response);

Shot Control

The shot parameter controls how many conversation turns are included from the prompt's initial messages:

  • shot: 0: Uses no messages from the prompt (empty context)
  • shot: 1: Uses first turn (2 messages: 1 user + 1 assistant)
  • shot: 2: Uses first two turns (4 messages: 2 user + 2 assistant)
  • shot: -1: Uses all messages from the prompt

Session Management

By default, isSessionEnabled is set to true. This parameter determines whether the chat should maintain conversation history:

  • When isSessionEnabled: true (default):

    • Creates and maintains a session for the conversation
    • Includes previous chat history based on the memory_type
    • Caches responses for future use
    • Returns a valid session_id in the response
  • When isSessionEnabled: false:

    • Operates in stateless mode
    • Uses only the prompt's initial messages and current user message
    • Ignores any existing chat history
    • No caching of responses
    • Returns an empty session_id

Example with sessions disabled:

const response = await promptManager.chatWithPrompt('PROMPT_ID', {
    user_message: [{ type: 'text', text: 'Hello, how are you?' }],
    memory_type: "windowMemory",
    window_size: 10,
    session_id: "",
    variables: {},
    isSessionEnabled: false  // Each interaction will be independent
});

You can remove a specific chat session using the removeSession method:

const sessionId = 'YOUR_SESSION_ID';
await promptManager.removeSession(sessionId);

This is useful for cleaning up chat history when you're done with a conversation or need to free up resources.

Example in TypeScript

import { PromptStudioManager } from '@n-promptstudio/sdk';

(async () => {
    const promptManager = new PromptStudioManager({
        apiKey: 'YOUR_API_KEY',
        env: 'test'
    });

    try {
        const response = await promptManager.chatWithPrompt('PROMPT_ID', {
            user_message: [{ type: 'text', text: 'Hello, how are you?' }],
            memory_type: "windowMemory",
            window_size: 10,
            session_id: "",
            variables: {},
            isSessionEnabled: true
        });

        console.log(response);
    } catch (error) {
        console.log(error);
    }
})();

Example in JavaScript

const { PromptStudioManager } = require('@n-promptstudio/sdk');

(async () => {
    const promptManager = new PromptStudioManager({
        apiKey: 'YOUR_API_KEY',
        env: 'test'
    });

    try {
        const response = await promptManager.chatWithPrompt('PROMPT_ID', {
            user_message: [{ type: 'text', text: 'Hello, how are you?' }],
            memory_type: "windowMemory",
            window_size: 10,
            session_id: "",
            variables: {},
            isSessionEnabled: true
        });

        console.log(response);
    } catch (error) {
        console.log(error);
    }
})();

Types

The SDK includes TypeScript definitions for better type safety. Here are some key types:

type FileMessage = {
    type: 'file';
    file_url: {
        url: string;
    };
};

type TextMessage = {
    type: 'text';
    text: string;
};

type UserMessage = Array<FileMessage | TextMessage>;

type Memory = "fullMemory" | "windowMemory" | "summarizedMemory";

interface RequestPayload {
    user_message: UserMessage,
    memory_type: Memory,
    window_size: number,
    session_id: string,
    variables: Record<string, string>,
    version?: number
}

Memory Types

These are the only valid memory types that can be used with the memory_type parameter:

  • "fullMemory": Maintains the complete conversation history
  • "windowMemory": Keeps a sliding window of recent messages
  • "summarizedMemory": Maintains a summarized version of the conversation history

For more detailed type information, refer to the src/Prompt/types.d.ts file in the SDK.

License

This SDK is released under the MIT License.

1.1.1

9 months ago

1.1.0

9 months ago

1.0.9

11 months ago

1.1.3

5 months ago

1.1.2

9 months ago

1.0.9-beta.1

12 months ago

1.0.10

11 months ago

1.0.9-beta

12 months ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.0

1 year ago