Skip to content

Commit

Permalink
fix(nuxt3): qa chain response data type
Browse files Browse the repository at this point in the history
  • Loading branch information
LarchLiu committed Feb 3, 2024
1 parent cb07444 commit f7c39a0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
24 changes: 18 additions & 6 deletions server/nuxt3/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import type { ReturnStorageData } from '@stargram/core/storage'
import { v4 as uuidv4 } from 'uuid'
import { errorMessage } from '@stargram/core/utils'

interface QARes {
answer: string
source: string[]
}
const textInput = ref<HTMLInputElement>()
const text = ref('')
const toast = useToast()
Expand All @@ -30,7 +34,7 @@ const selectedData = ref<ReturnStorageData>()
const modalOpen = ref(false)
const qaModalOpen = ref(false)
const qaQuestion = ref('')
const qaAnswer = ref('')
const qaAnswer = ref<QARes>()
async function getDataList() {
loadMoreStatus.value = 'loading'

Expand Down Expand Up @@ -100,15 +104,15 @@ function handleInputText() {
else {
qaModalOpen.value = true
qaQuestion.value = question
$fetch('/api/stargram/qa-chain', {
$fetch<QARes>('/api/stargram/qa-chain', {
method: 'POST',
body: {
userId: userId.value,
question,
},
})
.then((data) => {
qaAnswer.value = data as string
qaAnswer.value = data
})
.catch((err) => {
toast.add({
Expand Down Expand Up @@ -174,7 +178,7 @@ function handleInputText() {

watch(qaModalOpen, () => {
if (!qaModalOpen.value) {
qaAnswer.value = ''
qaAnswer.value = undefined
qaQuestion.value = ''
}
})
Expand Down Expand Up @@ -310,10 +314,18 @@ onMounted(async () => {
<div flex flex-col gap-2 p-4>
<button uno-carbon-close h-24px btn @click="qaModalOpen = false" />
<div>
Question: {{ qaQuestion }}
<span font-bold>Question: </span>{{ qaQuestion }}
</div>
<div v-if="qaAnswer" mb-4>
{{ qaAnswer }}
<div>
<span font-bold>Answer: </span>{{ qaAnswer.answer }}
</div>
<div v-if="qaAnswer.source && qaAnswer.source.length">
<span font-bold>Source: </span>
</div>
<div v-for="item in qaAnswer.source" :key="item" truncate>
<a class="text-14px text-[#37352f] underline decoration-solid" :href="item" target="_blank"> {{ item }} </a>
</div>
</div>
</div>
</UModal>
Expand Down
8 changes: 6 additions & 2 deletions server/nuxt3/server/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,20 @@ export async function MakeQAChain(question: string, context: { USER_CONFIG: User

let message = 'Answer: '
let meta = 'Source: '
let answer = ''
let source: string[] = []
if (typeof info === 'string') {
message = `Error Info: ${info}\n`
}
else {
message += info.text
answer = info.text
if (info.sourceDocuments) {
const uniqueUrls = new Set(info.sourceDocuments.map((d) => {
return d.metadata.source
return d.metadata.source!
}))
const uniqueMatchs = [...uniqueUrls]
source = [...uniqueUrls]
meta += uniqueMatchs.join('\n') || ''
message += `\n${meta}`
}
Expand All @@ -147,7 +151,7 @@ export async function MakeQAChain(question: string, context: { USER_CONFIG: User
else if (appName === 'slack')
return (await sendMessageToSlackBot(config.app.config.webhook, message))
else if (appName === 'stargram')
return message
return { answer, source }
}
catch (error) {
console.error(error)
Expand Down

0 comments on commit f7c39a0

Please sign in to comment.