Skip to content

Commit

Permalink
fix: gpt context answer only
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed May 19, 2024
1 parent a25afd7 commit 44ac7bf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
24 changes: 15 additions & 9 deletions src/gpt/research.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getAllIssueComments, getAllLinkedIssuesAndPullsInBody } from "../utils/
import { StreamlinedComment, UserType } from "../types/response";
import { Issue } from "@octokit/webhooks-types";
import { Context } from "../types/context";
import { addCommentToIssue } from "../utils/addComment";

export const sysMsg = `You are the UbiquityAI, designed to provide accurate technical answers. \n
Whenever appropriate, format your response using GitHub Flavored Markdown. Utilize tables, lists, and code blocks for clear and organized answers. \n
Expand Down Expand Up @@ -73,15 +74,16 @@ export async function decideContextGPT(
) {
const logger = console;
if (!issue) {
return `Payload issue is undefined`;
logger.info(`Error getting issue or pr`);
return;
}

// standard comments
const comments = await getAllIssueComments(context, repository, issue.number);

if (!comments) {
logger.info(`Error getting issue comments`);
return `Error getting issue comments`;
return;
}

// add the first comment of the issue/pull request
Expand All @@ -105,7 +107,6 @@ export async function decideContextGPT(

if (typeof links === "string" || !links) {
logger.info(`Error getting linked issues or prs: ${links}`);
return `Error getting linked issues or prs: ${links}`;
}

linkedIssueStreamlined = links.linkedIssues;
Expand Down Expand Up @@ -144,14 +145,20 @@ export async function decideContextGPT(
* @param chatHistory the conversational context to provide to GPT
*/
export async function askGPT(context: Context, question: string, chatHistory: CreateChatCompletionRequestMessage[]) {
const key = context.config.keys.openAi;
if (!key) {
console.error(`No OpenAI API Key provided`);
return errorDiff("You must configure the `openai-api-key` property in the bot configuration in order to use AI powered features.");
const {
logger,
config: {
keys: { openAi },
},
} = context;
if (!openAi) {
logger.error(`No OpenAI API Key provided`);
await addCommentToIssue(context, errorDiff("No OpenAI API Key provided to the /reseach plugin."));
return;
}

const openAI = new OpenAI({
apiKey: key,
apiKey: openAi,
});

const res: OpenAI.Chat.Completions.ChatCompletion = await openAI.chat.completions.create({
Expand All @@ -170,7 +177,6 @@ export async function askGPT(context: Context, question: string, chatHistory: Cr

if (!res) {
console.info(`No answer found for question: ${question}`);
return `No answer found for question: ${question}`;
}

return { answer, tokenUsage };
Expand Down
43 changes: 17 additions & 26 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,40 +75,31 @@ export async function run() {
}

async function research(context: Context) {
const { payload, config } = context;
const sender = payload.sender;

const issue = payload.issue as (typeof payload)["issue"];
const { payload } = context;
const body = payload.comment.body;
const repository = payload.repository;

const chatHistory: CreateChatCompletionRequestMessage[] = [];
const streamlined: StreamlinedComment[] = [];
const linkedPRStreamlined: StreamlinedComment[] = [];
const linkedIssueStreamlined: StreamlinedComment[] = [];

const regex = /^\/research\s(.+)$/;
const matches = body?.match(regex);

if (matches) {
return await processComment(context, repository, issue, sender, chatHistory, streamlined, linkedPRStreamlined, linkedIssueStreamlined, config, matches);
} else {
return "Invalid syntax for research \n usage: '/research What is pi?";
return await processComment(context, matches);
}

return "Invalid syntax for research \n usage: '/research What is pi?";
}

async function processComment(
context: Context,
repository: Context["payload"]["repository"],
issue: Context["payload"]["issue"],
sender: Context["payload"]["sender"],
chatHistory: CreateChatCompletionRequestMessage[],
streamlined: StreamlinedComment[],
linkedPRStreamlined: StreamlinedComment[],
linkedIssueStreamlined: StreamlinedComment[],
config: PluginInputs["settings"],
matches: RegExpMatchArray
) {
async function processComment(context: Context, matches: RegExpMatchArray) {
const { payload } = context;
const sender = payload.sender;

const issue = payload.issue as (typeof payload)["issue"];
const repository = payload.repository;

const chatHistory: CreateChatCompletionRequestMessage[] = [];
const streamlined: StreamlinedComment[] = [];
let linkedPRStreamlined: StreamlinedComment[] = [];
let linkedIssueStreamlined: StreamlinedComment[] = [];

const { logger } = context;
const [, body] = matches;
// standard comments
Expand Down Expand Up @@ -170,7 +161,7 @@ async function processComment(
},
{
role: "system",
content: "Original Context: " + JSON.stringify(gptDecidedContext), // provide the context
content: "Original Context: " + JSON.stringify(gptDecidedContext?.answer), // provide the context
name: "system",
},
{
Expand Down

0 comments on commit 44ac7bf

Please sign in to comment.