fix: use multiple sources per message (#91)

* fix: use multiple sources per message

* remove node score threshhold and sorting

* Create silly-swans-swim.md
This commit is contained in:
Thuc Pham
2025-05-23 15:53:10 +07:00
committed by GitHub
parent 11956cb819
commit ef66c133ae
3 changed files with 19 additions and 16 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@llamaindex/chat-ui': patch
---
fix: use multiple sources per message
+7 -12
View File
@@ -98,8 +98,6 @@ export type MessageAnnotation = {
data: AnnotationData
}
const NODE_SCORE_THRESHOLD = 0.25
/**
* Gets custom message annotations that don't match any standard MessageAnnotationType
* @param annotations - Array of message annotations to filter
@@ -159,16 +157,13 @@ export function getSourceAnnotationData(
function preprocessSourceNodes(nodes: SourceNode[]): SourceNode[] {
// Filter source nodes has lower score
const processedNodes = nodes
.filter(node => (node.score ?? 1) > NODE_SCORE_THRESHOLD)
.sort((a, b) => (b.score ?? 1) - (a.score ?? 1))
.map(node => {
// remove trailing slash for node url if exists
if (node.url) {
node.url = node.url.replace(/\/$/, '')
}
return node
})
const processedNodes = nodes.map(node => {
// remove trailing slash for node url if exists
if (node.url) {
node.url = node.url.replace(/\/$/, '')
}
return node
})
return processedNodes
}
+7 -4
View File
@@ -1,5 +1,5 @@
import { Bot, Check, Copy, RefreshCw } from 'lucide-react'
import { ComponentType, memo } from 'react'
import { ComponentType, memo, useMemo } from 'react'
import { useCopyToClipboard } from '../hook/use-copy-to-clipboard'
import { cn } from '../lib/utils'
import { Button } from '../ui/button'
@@ -135,12 +135,15 @@ function ChatMarkdown(props: ChatMarkdownProps) {
const { message } = useChatMessage()
const annotations = message.annotations as MessageAnnotation[] | undefined
const allNodes = useMemo(() => {
const sourceData = getSourceAnnotationData(annotations ?? [])
return sourceData.flatMap(item => item.nodes)
}, [annotations])
return (
<Markdown
content={message.content}
sources={
annotations ? getSourceAnnotationData(annotations)[0] : undefined
}
sources={{ nodes: allNodes }}
citationComponent={props.citationComponent}
className={cn(
{