openai-api-helper is a user-friendly library designed to simplify interactions with the OpenAI API. With minimal setup, you can quickly start making requests to OpenAI's chat completions endpoint. This package provides a straightforward way to integrate OpenAI's powerful language models into your applications with just a few lines of code.
- Easy Setup: Initialize with your OpenAI API key and get started in minutes.
- Simplified API Calls: Use a single method to make requests and handle responses effortlessly.
- Flexible Response Handling: Choose between complete JSON responses or simplified message string outputs.
- PromptBuilder (Beta): Efficiently construct prompts with background, context, instructions, and examples.
Click here for Documentation
Install openai-api-helper using npm:
npm install openai-api-helperHere's how to use openai-api-helper to interact with the OpenAI API:
const OpenAIAPIHelper = require("openai-api-helper").default;
async function main() {
const apiKey = "YOUR_OPENAI_API_KEY";
const model = "gpt-3.5-turbo"; // or your preferred model
const prompt =
"Explain the concept of artificial intelligence in simple terms.";
const openai = new OpenAIAPIHelper(apiKey);
try {
const response = await openai.call(model, prompt, 0.7, 1);
console.log("Response from OpenAI:", response);
} catch (error) {
console.error("Error:", error);
}
}
main();import OpenAIAPIHelper from "openai-api-helper";
const apiKey = "YOUR_OPENAI_API_KEY";
const openai = new OpenAIAPIHelper(apiKey);
async function test() {
const model = "gpt-3.5-turbo";
const prompt =
"What are the main differences between machine learning and deep learning?";
try {
const response = await openai.call(model, prompt, 0.7, 1);
console.log("Response from OpenAI:", response);
} catch (error) {
console.error("Error calling OpenAI API:", error);
}
}
test();The PromptBuilder class allows users to construct prompts efficiently. It includes options for adding background, context, instructions, and examples combined with user input.
const { PromptBuilder } = require("openai-api-helper");
const builder = new PromptBuilder()
.addBackground(
"You are an AI assistant specialized in explaining technical concepts."
)
.addContext("The user is a beginner in computer science.")
.addInstructions(
"Explain the concept in simple terms, using analogies where appropriate."
)
.addExample(
"Q: What is a variable?\nA: A variable is like a labeled box where you can store information. Just as you might put toys in a box labeled 'Toys', you can put data in a variable with a specific name."
)
.addUserInput("What is an algorithm?");
console.log(builder.build());Refer to the test files in the repository for more detailed usage examples.
new OpenAIAPIHelper(apiKey: string)apiKey: Your OpenAI API key.
call(model: string, prompt: string, temperature?: number, responseType?: number): Promise<string | object>model: Specify the GPT model (e.g., 'gpt-3.5-turbo', 'gpt-4').prompt: Provide the text prompt for the model.temperature(optional): Adjust the randomness of the response (default is 0.7).responseType(optional): Format your response (0 for complete JSON response, 1 for message string response. Default is 1).
addBackground(text: string): PromptBuilderaddContext(text: string): PromptBuilderaddInstructions(text: string): PromptBuilderaddExample(text: string): PromptBuilderaddUserInput(text: string): PromptBuilderbuild(): string
Sample usage files are available in the test directory:
test.js: Example JavaScript code demonstrating how to use theopenai-api-helperlibrary.test.ts: Example TypeScript code demonstrating how to use theopenai-api-helperlibrary.
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please follow these steps to contribute:
- Fork the repository
- Create a new branch:
git checkout -b feature-branch-name - Make your changes and commit them:
git commit -m 'Add some feature' - Push to the branch:
git push origin feature-branch-name - Submit a pull request
Please ensure that your code follows the project's coding standards and includes appropriate tests.
If you encounter any issues or have questions, please open an issue on GitHub.
This project is not officially associated with OpenAI. It is a community-driven effort to simplify the use of OpenAI's API.