Giter Club home page Giter Club logo

gpt-wrapper's Introduction

GPT-wrapper

This repository contains the TypeScript wrapper code for the OpenAI API. The wrapper is designed using object-oriented programming (OOP) principles. With this wrapper, you can replace long boilerplate code with a few lines of code, making it easier to integrate GPT functionality into your projects.

Development Setup

  • Prerequisites: Make sure you have Node.js (>= v18) and npm installed. (Learn more about Node.js and npm here.)
  • Run npm install to install all dependencies.
  • Create a .env file in the root directory of the project and add your OpenAI API key to it. You may refer to the .env.example file for an example.
  • Run npm run sample to run the sample code in src/sample.ts.

Example

Without wrapper

(Code sample with reference to OpenAI official documentation)

import OpenAI from "openai";

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

const code = "def foo():\n    print('Hello, world!')\n\nfoo()\n";

const response = await openai.chat.completions.create({
  model: "gpt-4",
  messages: [
    {
      role: "system",
      content:
        "You will be provided with a piece of code, and your task is to explain it in a concise way.",
    },
    {
      role: "user",
      content: code,
    },
  ],
  temperature: 0.7,
  max_tokens: 64,
  top_p: 1,
});

const explanation = response.choices[0].message.content;
console.log(explanation);

With wrapper

import { CodeExplainer } from "@src/agent";

const codeExplainer = new CodeExplainer();

const code = "def foo():\n    print('Hello, world!')\n\nfoo()\n";
const explanation = await codeExplainer.explainCode(code);
console.log(explanation);

Prompt Engineering

To align with best practices and improve code maintainability, this repository stores all prompt constants in a centralized location. System persona prompts are saved in src/prompt/SystemPersona.ts while user prompt templates are saved in src/prompt/UserPromptTemplate.ts

This repository draws inspiration from the following materials to design effective prompts:

gpt-wrapper's People

Contributors

hskpeter avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.