Skip to content

Commit 4732c49

Browse files
Pre-filter confluence search results
1 parent 82f951a commit 4732c49

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/functions/confluence.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { promises as fs } from 'node:fs';
22
import { join } from 'node:path';
33
import axios, { type AxiosInstance } from 'axios';
44
import { getSecretEnvVar } from 'src/config/secretConfig';
5+
import { llms } from '#agent/agentContextLocalStorage';
56
import { agentStorageDir } from '#app/appDirs';
67
import { func, funcClass } from '#functionSchema/functionDecorators';
7-
import { countTokens } from '#llm/tokens';
88
import { logger } from '#o11y/logger';
99
import { functionConfig } from '#user/userContext';
1010
import { envVar } from '#utils/env-var';
@@ -62,35 +62,42 @@ export class Confluence {
6262

6363
// https://developer.atlassian.com/cloud/confluence/rest/v1/api-group-search/#api-wiki-rest-api-search-get
6464
/**
65-
* Searches Confluence pages
65+
* Searches Confluence pages and pre-filters the results.
66+
* For example:
67+
* searchString: cloud-project-XYZ network
68+
* searchResultFilterQuery: Im looking for information specifically about the networking configuration of the cloud-project-XYZ
6669
* @param {string} searchString - the string to search for
70+
* @param {string} searchResultFilterQuery - the LLM generated query to filter the search results to
6771
* @returns {Promise<Array<{id: string, title: string, type: string, body: string, bodyTokens: number}>>}
6872
*/
6973
@func()
70-
async search(searchString: string): Promise<Array<{ id: string; title: string; body: string; bodyTokens: number }>> {
74+
async search(searchString: string, searchResultFilterQuery: string): Promise<Array<{ id: string; title: string; summary: string }>> {
7175
if (!searchString) throw new Error('searchString is required');
7276
// title ~ "release" OR text ~ "release" (escape " in searchString)
7377
searchString = searchString.replaceAll('"', '\\"');
7478
const cql = `title ~ "${searchString}" OR text ~ "${searchString}"`;
7579

7680
try {
7781
const response = await this.axios().get('/wiki/rest/api/content/search', {
78-
params: { cql, expand: 'type,title,body.export_view' },
82+
params: { cql, expand: 'type,title,body.export_view', limit: 10 },
7983
});
80-
84+
console.log(`Found ${response.data.results.length} results for query: ${searchString}`);
8185
const results = response.data.results.map((page: any) => ({
8286
id: page.id,
8387
title: page.title,
84-
type: page.type,
85-
body: turndownService.turndown(page.body.export_view.value),
86-
bodyTokens: null,
88+
// type: page.type,
89+
summary: turndownService.turndown(page.body.export_view.value),
8790
}));
88-
const tokenPromises = results.map(async (page: any) => {
89-
page.bodyTokens = await countTokens(page.body);
90-
});
91-
await Promise.all(tokenPromises);
9291

93-
return results;
92+
const filterCalls = results.map(async (page: any) => {
93+
const prompt = `<confluence-page>\n<page:id>${page.id}</page:id>\n<page:title>${page.title}</page:title>\n<page:body>\n${page.summary}\n</page:body>\n</confluence-page>\n\n<filter-query>\n${searchResultFilterQuery}\n</filter-query>\n\nFilter the confluence page contents to only include information that has some relevance to the query to reduce the number of tokens returned to the LLM performing the search\nWhere the page contents is highly relevant, return the page contents as is.\nWhere the page contents has some relevance, return extracts of the relevant sections and a short summary of the pages contents.\nWhere the page contents is not relevant, only return a short summary of the pages contents. Do not explain why or what is not relevant.\n`;
94+
const summarizedBody = await llms().medium.generateText(prompt, { id: 'Confluence search result filter' });
95+
page.summary = summarizedBody;
96+
return page;
97+
});
98+
const filteredPages = await Promise.all(filterCalls);
99+
logger.info({ filteredPages, searchResultFilterQuery }, `Returning filtered confluence search results for query: ${searchString}`);
100+
return filteredPages;
94101
} catch (error) {
95102
logger.error(error, `Error searching Confluence: ${cql}`);
96103
throw error;

0 commit comments

Comments
 (0)