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] More lenient parsing of suggestion scores (elastic…
…#177898) Closes elastic#177855. Also adds the scores to `data` for easier debugging.
- Loading branch information
1 parent
00ca8d4
commit 70c0b23
Showing
3 changed files
with
109 additions
and
11 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
69 changes: 69 additions & 0 deletions
69
...lity_solution/observability_ai_assistant/server/functions/parse_suggestion_scores.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,69 @@ | ||
/* | ||
* 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 dedent from 'dedent'; | ||
import { parseSuggestionScores } from './parse_suggestion_scores'; | ||
|
||
describe('parseSuggestionScores', () => { | ||
it('parses newlines as separators', () => { | ||
expect( | ||
parseSuggestionScores( | ||
dedent( | ||
`0,1 | ||
2,7 | ||
3,10` | ||
) | ||
) | ||
).toEqual([ | ||
{ | ||
index: 0, | ||
score: 1, | ||
}, | ||
{ | ||
index: 2, | ||
score: 7, | ||
}, | ||
{ | ||
index: 3, | ||
score: 10, | ||
}, | ||
]); | ||
}); | ||
|
||
it('parses semi-colons as separators', () => { | ||
expect(parseSuggestionScores(`0,1;2,7;3,10`)).toEqual([ | ||
{ | ||
index: 0, | ||
score: 1, | ||
}, | ||
{ | ||
index: 2, | ||
score: 7, | ||
}, | ||
{ | ||
index: 3, | ||
score: 10, | ||
}, | ||
]); | ||
}); | ||
|
||
it('parses spaces as separators', () => { | ||
expect(parseSuggestionScores(`0,1 2,7 3,10`)).toEqual([ | ||
{ | ||
index: 0, | ||
score: 1, | ||
}, | ||
{ | ||
index: 2, | ||
score: 7, | ||
}, | ||
{ | ||
index: 3, | ||
score: 10, | ||
}, | ||
]); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
...rvability_solution/observability_ai_assistant/server/functions/parse_suggestion_scores.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. | ||
*/ | ||
|
||
export function parseSuggestionScores(scoresAsString: string) { | ||
// make sure that spaces, semi-colons etc work as separators as well | ||
const scores = scoresAsString | ||
.replace(/[^0-9,]/g, ' ') | ||
.trim() | ||
.split(/\s+/) | ||
.map((pair) => { | ||
const [index, score] = pair.split(',').map((str) => parseInt(str, 10)); | ||
|
||
return { | ||
index, | ||
score, | ||
}; | ||
}); | ||
|
||
return scores; | ||
} |