0.1.1 • Published 4 months ago

@tsdiapi/gpt v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

šŸš€ TSDIAPI GPT Plugin

A TSDIAPI plugin that provides seamless integration with OpenAI's GPT models, allowing structured responses, JSON-based AI output, and customizable chat interactions.


šŸ“¦ Installation

Install the plugin via npm:

npm install --save @tsdiapi/gpt

Then, register the plugin in your TSDIAPI project:

import { createApp } from "@tsdiapi/server";
import createPlugin from "@tsdiapi/gpt";

createApp({
  plugins: [
    createPlugin({
      apiKey: "your-openai-api-key", // Required
      model: "gpt-4o-mini", // Optional (default: gpt-4o-mini)
    }),
  ],
});

šŸš€ Features

  • šŸ”„ Structured JSON Responses – Generate structured AI responses based on DTO classes.
  • šŸ›  Automatic Schema Generation – Uses class-validator-jsonschema to create JSON schemas from DTOs.
  • ⚔ Multi-Model Support – Works with various OpenAI models like gpt-4o, gpt-3.5-turbo, and more.
  • šŸŽÆ Text and JSON Output – Supports standard chat completion and structured JSON responses.

šŸ”§ Configuration Options

OptionTypeDefaultENV VariableDescription
apiKeystring""OPENAI_API_KEYOpenAI API Key (Required)
modelstring"gpt-4o-mini"OPENAI_MODEL_IDDefault model to use

šŸ“Œ How to Use

āœ… Structured JSON Output (DTO-based)

Generate structured JSON responses from OpenAI using DTOs.

import { getGPTProvider } from "@tsdiapi/gpt";
import { IsString, IsEmail } from "class-validator";
import { Expose } from "class-transformer";

class UserDTO {
  @IsString()
  @Expose()
  name: string;

  @IsEmail()
  @Expose()
  email: string;
}

async function run() {
  const gpt = getGPTProvider();
  const response = await gpt.jsonDTO("Generate a random user", UserDTO);
  console.log(response);
}

run();

šŸ”¹ GPT will return a structured response following UserDTO schema.


āœ… Basic Chat Completion

Perform a simple text-based GPT completion:

import { getGPTProvider } from "@tsdiapi/gpt";

async function run() {
  const gpt = getGPTProvider();
  const response = await gpt.chat("Tell me a joke");
  console.log(response.result);
}

run();

āœ… Raw JSON Response

Send a JSON schema manually:

import { getGPTProvider } from "@tsdiapi/gpt";

const jsonSchema = {
  type: "object",
  properties: {
    title: { type: "string" },
    description: { type: "string" },
  },
  required: ["title", "description"],
};

async function run() {
  const gpt = getGPTProvider();
  const response = await gpt.JsonString("Describe a cat", jsonSchema);
  console.log(response);
}

run();

šŸ“Œ Example Response (Structured Output)

{
  "message": {
    "role": "assistant",
    "content": "{ \"name\": \"John Doe\", \"email\": \"johndoe@example.com\" }"
  },
  "usage": {
    "prompt_tokens": 20,
    "completion_tokens": 15,
    "total_tokens": 35
  },
  "result": {
    "name": "John Doe",
    "email": "johndoe@example.com"
  }
}

šŸ“Œ Related Plugins

Explore other TSDIAPI plugins: šŸ”— Available Plugins


šŸ‘Øā€šŸ’» Contributing

Contributions are welcome! If you have ideas for improvements, open an issue or submit a pull request.

Author: unbywyd
šŸ“§ Contact: unbywyd@gmail.com

šŸš€ Happy coding with TSDIAPI GPT Plugin! šŸŽ‰

0.1.0

4 months ago

0.1.1

4 months ago

0.0.1-alpha.4

5 months ago

0.0.1-alpha.3

5 months ago

0.0.1-alpha.2

5 months ago

0.0.1-alpha.1

5 months ago