forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Obs AI Assistant] Expose recall function as API (elastic#185058)
Exposes a `POST /internal/observability_ai_assistant/chat/recall` endpoint for [Investigate UI ](elastic#183293). It is mostly just moving stuff around, some small refactorings and a new way to generate short ids. Previously we were using indexes for scoring suggestions, we are now generating a short but unique id (ie 4-5 chars) which generates a fairly unique token which strengthens the relationship between the id and the object but still allows for quick output. LLMs are slow to generate UUIDs, but indexes are very generic and the LLM might not pay a lot of attention to it.
- Loading branch information
1 parent
ee15561
commit 1338287
Showing
35 changed files
with
897 additions
and
509 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...ins/observability_solution/observability_ai_assistant/common/utils/short_id_table.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import { ShortIdTable } from './short_id_table'; | ||
|
||
describe('shortIdTable', () => { | ||
it('generates at least 10k unique ids consistently', () => { | ||
const ids = new Set(); | ||
|
||
const table = new ShortIdTable(); | ||
|
||
let i = 10_000; | ||
while (i--) { | ||
const id = table.take(String(i)); | ||
ids.add(id); | ||
} | ||
|
||
expect(ids.size).toBe(10_000); | ||
}); | ||
|
||
it('returns the original id based on the generated id', () => { | ||
const table = new ShortIdTable(); | ||
|
||
const idsByOriginal = new Map<string, string>(); | ||
|
||
let i = 100; | ||
while (i--) { | ||
const id = table.take(String(i)); | ||
idsByOriginal.set(String(i), id); | ||
} | ||
|
||
expect(idsByOriginal.size).toBe(100); | ||
|
||
expect(() => { | ||
Array.from(idsByOriginal.entries()).forEach(([originalId, shortId]) => { | ||
const returnedOriginalId = table.lookup(shortId); | ||
if (returnedOriginalId !== originalId) { | ||
throw Error( | ||
`Expected shortId ${shortId} to return ${originalId}, but ${returnedOriginalId} was returned instead` | ||
); | ||
} | ||
}); | ||
}).not.toThrow(); | ||
}); | ||
}); |
56 changes: 56 additions & 0 deletions
56
.../plugins/observability_solution/observability_ai_assistant/common/utils/short_id_table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
const ALPHABET = 'abcdefghijklmnopqrstuvwxyz'; | ||
|
||
function generateShortId(size: number): string { | ||
let id = ''; | ||
let i = size; | ||
while (i--) { | ||
const index = Math.floor(Math.random() * ALPHABET.length); | ||
id += ALPHABET[index]; | ||
} | ||
return id; | ||
} | ||
|
||
const MAX_ATTEMPTS_AT_LENGTH = 100; | ||
|
||
export class ShortIdTable { | ||
private byShortId: Map<string, string> = new Map(); | ||
private byOriginalId: Map<string, string> = new Map(); | ||
|
||
constructor() {} | ||
|
||
take(originalId: string) { | ||
if (this.byOriginalId.has(originalId)) { | ||
return this.byOriginalId.get(originalId)!; | ||
} | ||
|
||
let uniqueId: string | undefined; | ||
let attemptsAtLength = 0; | ||
let length = 4; | ||
while (!uniqueId) { | ||
const nextId = generateShortId(length); | ||
attemptsAtLength++; | ||
if (!this.byShortId.has(nextId)) { | ||
uniqueId = nextId; | ||
} else if (attemptsAtLength >= MAX_ATTEMPTS_AT_LENGTH) { | ||
attemptsAtLength = 0; | ||
length++; | ||
} | ||
} | ||
|
||
this.byShortId.set(uniqueId, originalId); | ||
this.byOriginalId.set(originalId, uniqueId); | ||
|
||
return uniqueId; | ||
} | ||
|
||
lookup(shortId: string) { | ||
return this.byShortId.get(shortId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...k/plugins/observability_solution/observability_ai_assistant/common/utils/until_aborted.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { Observable, OperatorFunction, takeUntil } from 'rxjs'; | ||
import { AbortError } from '@kbn/kibana-utils-plugin/common'; | ||
|
||
export function untilAborted<T>(signal: AbortSignal): OperatorFunction<T, T> { | ||
return (source$) => { | ||
const signal$ = new Observable((subscriber) => { | ||
if (signal.aborted) { | ||
subscriber.error(new AbortError()); | ||
} | ||
signal.addEventListener('abort', () => { | ||
subscriber.error(new AbortError()); | ||
}); | ||
}); | ||
|
||
return source$.pipe(takeUntil(signal$)); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.