File

src/pull-requests/code-refactor-suggestion.service.ts

Index

Methods

Constructor

constructor(openAiService: OpenAIWrappedService)
Parameters :
Name Type Optional
openAiService OpenAIWrappedService No

Methods

Async generateDescription
generateDescription(options: GenerateCodeRefactorSuggestionDto)
Parameters :
Name Type Optional
options GenerateCodeRefactorSuggestionDto No
Returns : unknown
Private generatePrompt
generatePrompt(language: string, maxLength: number)
Parameters :
Name Type Optional
language string No
maxLength number No
Returns : any
import { Injectable } from "@nestjs/common";

import { OpenAIWrappedService } from "../openai-wrapped/openai-wrapped.service";
import { GenerateCodeRefactorSuggestionDto } from "./dtos/create-code-refactor-suggestion.dto";

@Injectable()
export class CodeRefactorSuggestionService {
  constructor(private openAiService: OpenAIWrappedService) {}

  private generatePrompt(language: string, maxLength: number) {
    return [
      `Generate a code refactor suggestion for a given code snippet written in ${language} with the specifications mentioned below`,
      `The code snippet must be a maximum of ${maxLength} characters.`,
      'Exclude anything unnecessary such as translation and instructions. The code snippet you suggest should start with "```suggestion" and end with ``` to create a valid GitHub suggestion codeblock. All non-code text or description should be outside of the codeblock.',
    ].join("\n");
  }

  async generateDescription(options: GenerateCodeRefactorSuggestionDto) {
    const content = `Code: ${options.code}\n`;

    try {
      const completion = await this.openAiService.generateCompletion(
        this.generatePrompt(options.language, options.descriptionLength),
        content,
        options.temperature
      );

      return completion;
    } catch (error: unknown) {
      if (error instanceof Error) {
        console.error("OpenAI error: ", error.message);
      }
    }
  }
}

results matching ""

    No results matching ""