Skip to content

Commit

Permalink
chore: tidy up fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Oct 28, 2024
1 parent d777871 commit 300aa86
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 37 deletions.
67 changes: 32 additions & 35 deletions src/handlers/ask-llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,43 +40,40 @@ export async function askGpt(context: Context, question: string, formattedChat:
const {
env: { UBIQUITY_OS_APP_NAME },
config: { model, similarityThreshold, maxTokens },
adapters: {
supabase: { comment, issue },
voyage: { reranker },
openai: { completions },
},
logger,
} = context;
let similarComments: CommentSimilaritySearchResult[] = [];
let similarIssues: IssueSimilaritySearchResult[] = [];
try {
similarComments = (await context.adapters.supabase.comment.findSimilarComments(question, 1 - similarityThreshold, "")) || [];
} catch (error) {
throw bubbleUpErrorComment(context, error, false);
}
try {
similarIssues = (await context.adapters.supabase.issue.findSimilarIssues(question, 1 - similarityThreshold, "")) || [];
} catch (error) {
throw bubbleUpErrorComment(context, error, false);
}
let similarText = similarComments.map((comment: CommentSimilaritySearchResult) => comment.comment_plaintext);
similarText.push(...similarIssues.map((issue: IssueSimilaritySearchResult) => issue.issue_plaintext));
// Remove Null Results (Private Comments)
similarText = similarText.filter((text) => text !== null);
formattedChat = formattedChat.filter((text) => text !== null);
similarText = similarText.filter((text) => text !== "");
const rerankedText = similarText.length > 0 ? await context.adapters.voyage.reranker.reRankResults(similarText, question) : [];
const languages = await fetchRepoLanguageStats(context);
let dependencies = {};
let devDependencies = {};

try {
const deps = await fetchRepoDependencies(context);
dependencies = deps.dependencies;
devDependencies = deps.devDependencies;
const [similarComments, similarIssues] = await Promise.all([
comment.findSimilarComments(question, 1 - similarityThreshold, ""),
issue.findSimilarIssues(question, 1 - similarityThreshold, "")
]);

const similarText = [
...similarComments?.map((comment: CommentSimilaritySearchResult) => comment.comment_plaintext) || [],
...similarIssues?.map((issue: IssueSimilaritySearchResult) => issue.issue_plaintext) || []
];

formattedChat = formattedChat.filter(text => text);

const rerankedText = similarText.length > 0 ? await reranker.reRankResults(similarText, question) : [];
const [languages, { dependencies, devDependencies }] = await Promise.all([
fetchRepoLanguageStats(context),
fetchRepoDependencies(context)
]);

const groundTruths = await findGroundTruths(context, "chat-bot", { languages, dependencies, devDependencies });

const numTokens = await completions.findTokenLength(question, rerankedText, formattedChat, groundTruths);
logger.info(`Number of tokens: ${numTokens}`);

return completions.createCompletion(question, model, rerankedText, formattedChat, groundTruths, UBIQUITY_OS_APP_NAME, maxTokens);
} catch (error) {
throw bubbleUpErrorComment(context, error, false);
}
const groundTruths = await findGroundTruths(context, "chat-bot", {
languages,
dependencies,
devDependencies,
});
//Calculate the current context size in tokens
const numTokens = await context.adapters.openai.completions.findTokenLength(question, rerankedText, formattedChat, groundTruths);
context.logger.info(`Number of tokens: ${numTokens}`);
return context.adapters.openai.completions.createCompletion(question, model, rerankedText, formattedChat, groundTruths, UBIQUITY_OS_APP_NAME, maxTokens);
}
}
3 changes: 1 addition & 2 deletions src/handlers/ground-truths/chat-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function fetchRepoDependencies(context: Context) {
return {
dependencies: {},
devDependencies: {},
}
};
}

export function extractDependencies(packageJson: Record<string, Record<string, string>>) {
Expand All @@ -51,7 +51,6 @@ export async function fetchRepoLanguageStats(context: Context) {
},
} = context;
try {

const { data: languages } = await octokit.repos.listLanguages({
owner,
repo,
Expand Down

0 comments on commit 300aa86

Please sign in to comment.