npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reviewer-lib

v1.4.0

Published

A library for code review using OpenAI API

Downloads

15

Readme

Reviewer lib

An automated code review tool that uses OpenAI to analyze and provide recommendations for code improvement and commenting in PR when received message from AI.

Build Status Downloads NPM Version Minified Size Open Issues

Installation

npm install -D reviewer-lib

Usage

Notes: Cheaper models give a lower quality result (Davinci, Curie, Ada, Babbage). To use less expensive models, OpenAI API requests should be directed to instance.submitCode.

import { Reviewer} from 'reviewer-lib';

const reviewer = new Reviewer(apiKey); // OpenAI apikey
const code = `
function exampleFunction(x, y) {
  let result = x + y;
  return result;
}
`;

reviewer.submitCodeAssistanceMode(code)
   .then((feedback: string) => {
      console.log('Code Review Feedback:');
      console.log(feedback);
   })
   .catch((error: Error | string) => {
      console.error('Error:', error);
   });

in CI/CD:

  1. Create file and set up instance. ./review.mjs
import { Reviewer} from 'reviewer-lib';

const apiKey = process.env.OPENAI_API_KEY;
if (!apiKey) {
  console.error('Error: OPENAI_API_KEY is not set');
  process.exit(1);
}

const reviewer = new Reviewer(apiKey);
let code = '';

process.stdin.on('data', chunk => {
  code += chunk;
});
process.stdin.on('end', () => {
  reviewer.submitCodeAssistanceMode(code)
    .then((feedback: string) => {
      console.log('Code Review Feedback:');
      console.log(feedback);
    })
    .catch((error: Error | string) => {
      console.error('Error:', error);
      process.exit(1);
    });
});
  1. Create workflow. .github/workflows/code-review.yml. Add $GITHUB_TOKEN and $OPENAI_API_KEY to your project secrets
name: Code Review with ChatGPT

on:
  pull_request:
    branches:
      - master

jobs:
  review:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
        with:
          fetch-depth: 2

      - name: Install jq
        run: sudo apt-get install -y jq

      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '18'

      - name: Install dependencies
        run: npm install

      - name: Get diff from PR
        id: diff
        run: |
          git fetch origin
          git diff origin/master HEAD > pr.diff

      - name: Run code review
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
        run: |
          npm run build
          node ./review.mjs < pr.diff > review_feedback.txt

      - name: Post review feedback as a comment
        env:
          GITHUB_TOKEN: ${{ secrets.API_GITHUB_TOKEN }}
        run: |
          REVIEW_FEEDBACK=$(cat review_feedback.txt)
          COMMENT_BODY=$(jq -n --arg body "$REVIEW_FEEDBACK" '{body: $body}')
          PULL_REQUEST_NUMBER=${{ github.event.pull_request.number }}
          curl -s -H "Authorization: token $GITHUB_TOKEN" -X POST -d "$COMMENT_BODY" \
            "https://api.github.com/repos/${{ github.repository }}/issues/$PULL_REQUEST_NUMBER/comments"
  1. Push code. Pipeline should create a job and comment the PR.

API

new Reviewer(apiKey, model, maxTokens). Creates a new Reviewer instance.

  1. Params:
  • apiKey (String): Your OpenAI API key.
  • model (String): The model you want to use (default 'gpt-3.5-turbo').
  • maxTokens (Number): The maximum number of tokens for the response (default 400).
  • code (String): The code to analyze. Returns Promise: Suggestions for improving the code.
  • temperature?: Controls the creativity and variety of the generated text. Values from 0 to 1.
  • n?: The number of text variants the model should generate. The default value is 1.
  • stop?: A list of sequences where the generation should stop. For example, ["\n", "END"].
  • top_p?: Controls cumulative probability sampling. Values from 0 to 1. This is an alternative way to control creativity that focuses on the most likely tokens.
  • frequency_penalty?: A number from 0 to 1. Reduces the probability of tokens that have already been used in the text. This helps reduce repetition.
  • presence_penalty?: A number from 0 to 1. Increases the probability of tokens that have not yet been used. This helps introduce new topics and ideas.
  • logprobs?: If set, returns the logarithms of the probabilities of all tokens when generating. This is useful for analyzing probabilities and choosing the best tokens.
  • echo?: If set to true, returns the request along with the response. This can be useful for debugging.
  • best_of?: Generate multiple variants and choose the best one. This is related to n, but allows you to get the best variant from a larger number of generated texts.
  • logit_bias?: A map of tokens and values to control the probability of certain tokens. This allows you to influence the generation by encouraging or disallowing the use of certain words.
  1. Methods:
  • submitCodeAssistanceMode(code: string): Function, analyzes and provides recommendations for improving the code.
reviewer.submitCodeAssistanceMode(code).then(suggestions => {
  console.log('Review Suggestions:', suggestions);
});

Other Functions

  • submitCode(code: string): Function, analyzes and provides recommendations for improving the code. Use '/engines/${model}/completions' endpoint. Work with cheaper models. Please, note while using this func with more expensive models, it may not work and throw an error. In this case use submitCodeAssistanceMode.
  • getCurrentModels: Function, gets list of available AI models.
  • historicalAnalysis(repoPath: string): A feature that analyzes the history of code changes and makes recommendations for improvements based on past changes.
  • codeStyleRecommendations(code: string): Add a feature that provides recommendations for improving code style by following established style guides.
  • securityAnalysis(code: string): A function that checks code for potential vulnerabilities and suggests solutions.
  • generateTests(code: string): Function for automatic test generation based on provided code.
  • optimizeCode(code: string): A function for suggesting optimizations in code in terms of performance and readability.
  • generateDocumentation(code: string): A function that automatically generates comments or documentation for code.

References