@tsdiapi/gpt v0.1.1
š 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
Option | Type | Default | ENV Variable | Description |
---|---|---|---|---|
apiKey | string | "" | OPENAI_API_KEY | OpenAI API Key (Required) |
model | string | "gpt-4o-mini" | OPENAI_MODEL_ID | Default 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! š
4 months ago
4 months ago
5 months ago
5 months ago
5 months ago
5 months ago