File

src/pull-requests/pull-request-description.service.ts

Index

Methods

Constructor

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

Methods

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

import { OpenAIWrappedService } from "../openai-wrapped/openai-wrapped.service";
import { GeneratePullRequestDescriptionDto } from "./dtos/create-pull-request-description.dto";

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

  private generatePrompt(language: string, maxLength: number, tone: string) {
    return [
      `Generate an apt github PR description written in present tense and ${tone} tone for the given code diff/commit-messages with the specifications mentioned below`,
      `Description language: ${language}`,
      `Description must be a maximum of ${maxLength} characters.`,
      "Exclude anything unnecessary such as translation. Your entire response will be passed directly into a pull request description",
    ].join("\n");
  }

  async generateDescription(options: GeneratePullRequestDescriptionDto) {
    const content = `${options.diff ? `Diff: ${options.diff}\n` : ""}${
      options.commitMessages ? `\nCommit Messages: ${options.commitMessages.join(",")}` : ""
    }`;

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

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

results matching ""

    No results matching ""