Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed and added citations #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions app/api/conversations/route.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
export const dynamic = 'force-dynamic'

import { type NextRequest } from 'next/server'
import { NextResponse } from 'next/server'
import { client, getInfo, setSession } from '@/app/api/utils/common'

export const dynamic = 'force-dynamic'
export async function GET(request: NextRequest) {
const { sessionId, user } = getInfo(request)
try {
const { data }: any = await client.getConversations(user)
return NextResponse.json(data, {
headers: setSession(sessionId),
})
} catch (error) {
return NextResponse.json([]);
}
catch (error) {
return NextResponse.json([])
}
}
22 changes: 17 additions & 5 deletions app/components/chat/answer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'
import type { FC } from 'react'
import React from 'react'
import React, { useEffect, useState } from 'react'
import { HandThumbDownIcon, HandThumbUpIcon } from '@heroicons/react/24/outline'
import { useTranslation } from 'react-i18next'
import LoadingAnim from '../loading-anim'
Expand Down Expand Up @@ -76,6 +76,12 @@ const Answer: FC<IAnswerProps> = ({
config,
}) => {
const { id, content, feedback, agent_thoughts, workflowProcess, suggestedQuestions, citation } = item
const [localCitation, setLocalCitation] = useState(citation)

useEffect(() => {
setLocalCitation(citation)
}, [citation, id]) // Add id to dependencies to ensure update on new answers

const isAgentMode = !!agent_thoughts && agent_thoughts.length > 0
const { t } = useTranslation()

Expand Down Expand Up @@ -177,7 +183,7 @@ const Answer: FC<IAnswerProps> = ({
)

return (
<div key={id}>
<div key={`${id}-${content}-${JSON.stringify(citation)}`}>
<div className='flex items-start'>
<div className={`${s.answerIcon} w-10 h-10 shrink-0`}>
{isResponding
Expand Down Expand Up @@ -206,11 +212,11 @@ const Answer: FC<IAnswerProps> = ({
{suggestedQuestions
? (
<SuggestedQuestions
onHandleSend={onHandleSend}
onHandleSend={onHandleSend as (message: string) => void}
suggestedQuestions={suggestedQuestions}
/>
)
: null}
: undefined}
</>
))}
</div>
Expand All @@ -222,7 +228,13 @@ const Answer: FC<IAnswerProps> = ({
</div>
</div>
</div>
{localCitation && localCitation.length > 0 && (
<Citation
data={localCitation}
showHitInfo={config?.supportCitationHitInfo}
/>
)}
</div>
)
}
export default React.memo(Answer)
export default Answer // Remove React.memo() here
1 change: 1 addition & 0 deletions app/components/chat/citation/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// app/components/chat/citation/index.tsx
import { useEffect, useMemo, useRef, useState } from 'react'
import type { FC } from 'react'
import { useTranslation } from 'react-i18next'
Expand Down
1 change: 1 addition & 0 deletions app/components/chat/citation/popup.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// app/components/chat/citation/popup.tsx
import { Fragment, useState } from 'react'
import type { FC } from 'react'
import Link from 'next/link'
Expand Down
1 change: 1 addition & 0 deletions app/components/chat/citation/progress-tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// app/components/chat/citation/progress-tooltip.tsx
import { useState } from 'react'
import type { FC } from 'react'
import { useTranslation } from 'react-i18next'
Expand Down
1 change: 1 addition & 0 deletions app/components/chat/citation/tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// app/components/chat/citation/tooltip.tsx
import React, { useState } from 'react'
import type { FC } from 'react'
import {
Expand Down
14 changes: 10 additions & 4 deletions app/components/chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type { FeedbackFunc } from './type'
import VoiceInput from './answer/voice-input'
import { Microphone01 } from '@/app/components/base/icons/line/mediaAndDevices'
import { Microphone01 as Microphone01Solid } from '@/app/components/base/icons/solid/mediaAndDevices'

import type { ChatItem, VisionFile, VisionSettings } from '@/types/app'
import { TransferMethod } from '@/types/app'
import Tooltip from '@/app/components/base/tooltip'
Expand Down Expand Up @@ -63,6 +62,12 @@ const Chat: FC<IChatProps> = ({
const isUseInputMethod = useRef(false)

const [query, setQuery] = React.useState('')
const [updateCounter, setUpdateCounter] = useState(0)

useEffect(() => {
setUpdateCounter(prev => prev + 1)
}, [chatList])

const handleContentChange = (e: any) => {
const value = e.target.value
setQuery(value)
Expand Down Expand Up @@ -148,14 +153,14 @@ const Chat: FC<IChatProps> = ({
}

return (
<div className={cn(!feedbackDisabled && 'px-3.5', 'h-full')}>
<div className={cn(!feedbackDisabled && 'px-3.5', 'h-full')} key={updateCounter}>
{/* Chat List */}
<div className="h-full space-y-[30px]">
{chatList.map((item) => {
if (item.isAnswer) {
const isLast = item.id === chatList[chatList.length - 1].id
return <Answer
key={item.id}
key={`${item.id}-${item.content}-${JSON.stringify(item.citation)}`}
item={item}
onHandleSend={handleSend}
feedbackDisabled={feedbackDisabled}
Expand Down Expand Up @@ -263,4 +268,5 @@ const Chat: FC<IChatProps> = ({
)
}

export default React.memo(Chat)
export default Chat // Remove React.memo()

1 change: 1 addition & 0 deletions app/components/chat/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export type IChatItem = {
}

export type MessageEnd = {
retriever_resources: any
id: string
metadata: {
retriever_resources?: CitationItem[]
Expand Down
9 changes: 6 additions & 3 deletions app/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ const Main: FC = () => {
})
newChatList.push({
id: item.id,
content: item.answer + 22,
content: item.answer,
agent_thoughts: addFileInfos(item.agent_thoughts ? sortAgentSorts(item.agent_thoughts) : item.agent_thoughts, item.message_files),
feedback: item.feedback,
isAnswer: true,
Expand Down Expand Up @@ -500,13 +500,16 @@ const Main: FC = () => {

draft.push({
...responseItem,
citations: messageEnd.retriever_resources, // Add citations here
})
})
setChatList(newListWithAnswer)
return
}
// not support show citation
// responseItem.citation = messageEnd.retriever_resources
// Handle citations if they exist
if (messageEnd.retriever_resources)
responseItem.citation = messageEnd.retriever_resources // Store citations

const newListWithAnswer = produce(
getChatList().filter(item => item.id !== responseItem.id && item.id !== placeholderAnswerId),
(draft) => {
Expand Down