Skip to content

Commit 8f86ac1

Browse files
committed
fix(web): attribute a web source only when it survived into the prompt
1 parent 4fdfa64 commit 8f86ac1

3 files changed

Lines changed: 52 additions & 5 deletions

File tree

__tests__/messageSources.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,43 @@ describe('web search results (experimental)', () => {
410410
expect(usedByName['Cooking recipes']).toBe(false);
411411
});
412412

413+
it('does not flag a web result used when it was truncated out of the prompt', () => {
414+
const cited = [
415+
web(
416+
'Weather Warsaw',
417+
'https://w.com',
418+
'Warsaw temperature and rain forecast today'
419+
),
420+
];
421+
const answer =
422+
'The weather in Warsaw shows rain and a low temperature today.';
423+
424+
const result = pickCitationsByAnswer(cited, answer, [], new Set<string>());
425+
426+
expect(result[0].used).toBe(false);
427+
});
428+
429+
it('flags a web result used only when its name is present in the prompt', () => {
430+
const cited = [
431+
web(
432+
'Weather Warsaw',
433+
'https://w.com',
434+
'Warsaw temperature and rain forecast today'
435+
),
436+
];
437+
const answer =
438+
'The weather in Warsaw shows rain and a low temperature today.';
439+
440+
const result = pickCitationsByAnswer(
441+
cited,
442+
answer,
443+
[],
444+
new Set(['Weather Warsaw'])
445+
);
446+
447+
expect(result[0].used).toBe(true);
448+
});
449+
413450
it('marks no web result used when the answer is a refusal', () => {
414451
const cited = [
415452
web('Weather Warsaw', 'https://w.com', 'Warsaw temperature and rain'),

hooks/useMessageSources.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ export const useMessageSources = (sourceDocuments?: SourceDocument[]) => {
88

99
const seen = new Set<string>();
1010
return sourceDocuments.filter((source) => {
11-
const key = sourceKey(source.documentId, source.name);
11+
const key =
12+
source.kind === 'web' && source.url
13+
? `web:${source.url}`
14+
: sourceKey(source.documentId, source.name);
1215
if (seen.has(key)) return false;
1316
seen.add(key);
1417
return true;

utils/messageSources.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ export const answerCitationOverlaps = (
169169
export const pickCitationsByAnswer = (
170170
sourceDocuments: SourceDocument[],
171171
answer: string,
172-
preferred: SourceDocument[]
172+
preferred: SourceDocument[],
173+
presentNames?: Set<string>
173174
): SourceDocument[] => {
174175
const webDocuments = sourceDocuments.filter(
175176
(doc) => sourceKind(doc) === 'web'
@@ -182,12 +183,16 @@ export const pickCitationsByAnswer = (
182183
answer,
183184
preferred
184185
);
185-
return [...citedLocal, ...flagUsedWebDocuments(webDocuments, answer)];
186+
return [
187+
...citedLocal,
188+
...flagUsedWebDocuments(webDocuments, answer, presentNames),
189+
];
186190
};
187191

188192
const flagUsedWebDocuments = (
189193
webDocuments: SourceDocument[],
190-
answer: string
194+
answer: string,
195+
presentNames?: Set<string>
191196
): SourceDocument[] => {
192197
if (webDocuments.length === 0) return webDocuments;
193198

@@ -205,7 +210,9 @@ const flagUsedWebDocuments = (
205210
return scored.map((s) => ({
206211
...s.doc,
207212
used:
208-
maxOverlap > 0 && s.overlap >= maxOverlap * ANSWER_CITATION_OVERLAP_RATIO,
213+
maxOverlap > 0 &&
214+
s.overlap >= maxOverlap * ANSWER_CITATION_OVERLAP_RATIO &&
215+
(presentNames === undefined || presentNames.has(s.doc.name)),
209216
}));
210217
};
211218

0 commit comments

Comments
 (0)