feat: respond in user's language, style fixes and research depth fixes
This commit is contained in:
@ -8,7 +8,7 @@
|
|||||||
import type { TreeNode } from './Tree.vue'
|
import type { TreeNode } from './Tree.vue'
|
||||||
import { marked } from 'marked'
|
import { marked } from 'marked'
|
||||||
|
|
||||||
const { t } = useI18n()
|
const { t, locale } = useI18n()
|
||||||
const emit = defineEmits<{
|
const emit = defineEmits<{
|
||||||
(e: 'complete', results: ResearchResult): void
|
(e: 'complete', results: ResearchResult): void
|
||||||
}>()
|
}>()
|
||||||
@ -150,6 +150,7 @@
|
|||||||
query,
|
query,
|
||||||
maxDepth: depth,
|
maxDepth: depth,
|
||||||
breadth,
|
breadth,
|
||||||
|
language: t('language', {}, { locale: locale.value }),
|
||||||
onProgress: handleResearchProgress,
|
onProgress: handleResearchProgress,
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -181,7 +182,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div v-if="selectedNode" class="p-4">
|
<div v-if="selectedNode" class="p-4">
|
||||||
<USeparator :label="t('webBrowsing.nodeDetails')" />
|
<USeparator :label="t('webBrowsing.nodeDetails')" />
|
||||||
<h2 class="text-xl font-bold mt-2">{{ selectedNode.label }}</h2>
|
<h2 class="text-xl font-bold my-2">{{ selectedNode.label }}</h2>
|
||||||
|
|
||||||
<!-- Root node has no additional information -->
|
<!-- Root node has no additional information -->
|
||||||
<p v-if="selectedNode.id === '0'">
|
<p v-if="selectedNode.id === '0'">
|
||||||
@ -191,15 +192,23 @@
|
|||||||
<h3 class="text-lg font-semibold mt-2">
|
<h3 class="text-lg font-semibold mt-2">
|
||||||
{{ t('webBrowsing.researchGoal') }}
|
{{ t('webBrowsing.researchGoal') }}
|
||||||
</h3>
|
</h3>
|
||||||
<p class="whitespace-pre-wrap">{{ selectedNode.researchGoal }}</p>
|
<p
|
||||||
|
v-if="selectedNode.researchGoal"
|
||||||
|
class="prose max-w-none"
|
||||||
|
v-html="marked(selectedNode.researchGoal, { gfm: true })"
|
||||||
|
/>
|
||||||
|
|
||||||
<h3 class="text-lg font-semibold mt-2">
|
<h3 class="text-lg font-semibold mt-2">
|
||||||
{{ t('webBrowsing.visitedUrls') }}
|
{{ t('webBrowsing.visitedUrls') }}
|
||||||
</h3>
|
</h3>
|
||||||
<ul class="list-disc list-inside">
|
<ul class="list-disc list-inside">
|
||||||
<li v-for="(url, index) in selectedNode.visitedUrls" :key="index">
|
<li
|
||||||
|
v-for="(url, index) in selectedNode.visitedUrls"
|
||||||
|
class="whitespace-pre-wrap break-all"
|
||||||
|
:key="index"
|
||||||
|
>
|
||||||
<UButton
|
<UButton
|
||||||
class="!p-0 break-all whitespace-pre-wrap"
|
class="!p-0 contents"
|
||||||
variant="link"
|
variant="link"
|
||||||
:href="url"
|
:href="url"
|
||||||
target="_blank"
|
target="_blank"
|
||||||
@ -212,13 +221,12 @@
|
|||||||
<h3 class="text-lg font-semibold mt-2">
|
<h3 class="text-lg font-semibold mt-2">
|
||||||
{{ t('webBrowsing.learnings') }}
|
{{ t('webBrowsing.learnings') }}
|
||||||
</h3>
|
</h3>
|
||||||
<ul class="list-disc list-inside">
|
<p
|
||||||
<li
|
v-for="(learning, index) in selectedNode.learnings"
|
||||||
v-for="(learning, index) in selectedNode.learnings"
|
class="prose max-w-none"
|
||||||
:key="index"
|
:key="index"
|
||||||
v-html="marked(learning)"
|
v-html="marked(`- ${learning}`, { gfm: true })"
|
||||||
></li>
|
/>
|
||||||
</ul>
|
|
||||||
</template>
|
</template>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
(e: 'submit', feedback: ResearchFeedbackResult[]): void
|
(e: 'submit', feedback: ResearchFeedbackResult[]): void
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
const { t } = useI18n()
|
const { t, locale } = useI18n()
|
||||||
const feedback = ref<ResearchFeedbackResult[]>([])
|
const feedback = ref<ResearchFeedbackResult[]>([])
|
||||||
|
|
||||||
const isLoading = ref(false)
|
const isLoading = ref(false)
|
||||||
@ -37,6 +37,7 @@
|
|||||||
for await (const f of generateFeedback({
|
for await (const f of generateFeedback({
|
||||||
query,
|
query,
|
||||||
numQuestions,
|
numQuestions,
|
||||||
|
language: t('language', {}, { locale: locale.value }),
|
||||||
})) {
|
})) {
|
||||||
const questions = f.questions!.filter((s) => typeof s === 'string')
|
const questions = f.questions!.filter((s) => typeof s === 'string')
|
||||||
// Incrementally update modelValue
|
// Incrementally update modelValue
|
||||||
|
@ -27,14 +27,19 @@
|
|||||||
const icon = computed(() => {
|
const icon = computed(() => {
|
||||||
const result = { name: '', pulse: false }
|
const result = { name: '', pulse: false }
|
||||||
if (!props.node.status) return result
|
if (!props.node.status) return result
|
||||||
|
|
||||||
switch (props.node.status) {
|
switch (props.node.status) {
|
||||||
case 'generating_query':
|
case 'generating_query':
|
||||||
result.name = 'i-lucide-clipboard-list'
|
result.name = 'i-lucide-clipboard-list'
|
||||||
result.pulse = true
|
result.pulse = true
|
||||||
break
|
break
|
||||||
case 'generated_query':
|
case 'generated_query':
|
||||||
result.name = 'i-lucide-pause'
|
// FIXME: 因为 deepResearch 有并发限制,这个 case 是为了明确区分状态。
|
||||||
break
|
// 但是目前进入这个状态之后再进入 searching 状态,图标不会更新成 search,不知道原因
|
||||||
|
// 暂时禁用了这个 case
|
||||||
|
// result.name = 'i-lucide-pause'
|
||||||
|
// result.pulse = true
|
||||||
|
// break
|
||||||
case 'searching':
|
case 'searching':
|
||||||
result.name = 'i-lucide-search'
|
result.name = 'i-lucide-search'
|
||||||
result.pulse = true
|
result.pulse = true
|
||||||
@ -70,7 +75,7 @@
|
|||||||
>
|
>
|
||||||
{{ node.label }}
|
{{ node.label }}
|
||||||
</UButton>
|
</UButton>
|
||||||
<ol v-if="node.children.length > 0" class="flex flex-col gap-x-2">
|
<ol v-if="node.children.length > 0" class="flex flex-col gap-y-2">
|
||||||
<li v-for="node in node.children" :key="node.id">
|
<li v-for="node in node.children" :key="node.id">
|
||||||
<Tree
|
<Tree
|
||||||
class="ml-2"
|
class="ml-2"
|
||||||
|
@ -4,7 +4,7 @@ import { z } from 'zod'
|
|||||||
import { parseStreamingJson, type DeepPartial } from '~/utils/json'
|
import { parseStreamingJson, type DeepPartial } from '~/utils/json'
|
||||||
|
|
||||||
import { trimPrompt } from './ai/providers'
|
import { trimPrompt } from './ai/providers'
|
||||||
import { systemPrompt } from './prompt'
|
import { languagePrompt, systemPrompt } from './prompt'
|
||||||
import zodToJsonSchema from 'zod-to-json-schema'
|
import zodToJsonSchema from 'zod-to-json-schema'
|
||||||
import { type TavilySearchResponse } from '@tavily/core'
|
import { type TavilySearchResponse } from '@tavily/core'
|
||||||
import { useTavily } from '~/composables/useTavily'
|
import { useTavily } from '~/composables/useTavily'
|
||||||
@ -18,6 +18,7 @@ export type ResearchResult = {
|
|||||||
export interface WriteFinalReportParams {
|
export interface WriteFinalReportParams {
|
||||||
prompt: string
|
prompt: string
|
||||||
learnings: string[]
|
learnings: string[]
|
||||||
|
language: string
|
||||||
}
|
}
|
||||||
// useRuntimeConfig()
|
// useRuntimeConfig()
|
||||||
// Used for streaming response
|
// Used for streaming response
|
||||||
@ -71,8 +72,10 @@ export function generateSearchQueries({
|
|||||||
query,
|
query,
|
||||||
numQueries = 3,
|
numQueries = 3,
|
||||||
learnings,
|
learnings,
|
||||||
|
language,
|
||||||
}: {
|
}: {
|
||||||
query: string
|
query: string
|
||||||
|
language: string
|
||||||
numQueries?: number
|
numQueries?: number
|
||||||
// optional, if provided, the research will continue from the last learning
|
// optional, if provided, the research will continue from the last learning
|
||||||
learnings?: string[]
|
learnings?: string[]
|
||||||
@ -101,6 +104,7 @@ export function generateSearchQueries({
|
|||||||
)}`
|
)}`
|
||||||
: '',
|
: '',
|
||||||
`You MUST respond in JSON with the following schema: ${jsonSchema}`,
|
`You MUST respond in JSON with the following schema: ${jsonSchema}`,
|
||||||
|
languagePrompt(language),
|
||||||
].join('\n\n')
|
].join('\n\n')
|
||||||
return streamText({
|
return streamText({
|
||||||
model: useAiModel(),
|
model: useAiModel(),
|
||||||
@ -118,9 +122,11 @@ function processSearchResult({
|
|||||||
result,
|
result,
|
||||||
numLearnings = 3,
|
numLearnings = 3,
|
||||||
numFollowUpQuestions = 3,
|
numFollowUpQuestions = 3,
|
||||||
|
language,
|
||||||
}: {
|
}: {
|
||||||
query: string
|
query: string
|
||||||
result: TavilySearchResponse
|
result: TavilySearchResponse
|
||||||
|
language: string
|
||||||
numLearnings?: number
|
numLearnings?: number
|
||||||
numFollowUpQuestions?: number
|
numFollowUpQuestions?: number
|
||||||
}) {
|
}) {
|
||||||
@ -135,15 +141,17 @@ function processSearchResult({
|
|||||||
),
|
),
|
||||||
})
|
})
|
||||||
const jsonSchema = JSON.stringify(zodToJsonSchema(schema))
|
const jsonSchema = JSON.stringify(zodToJsonSchema(schema))
|
||||||
const contents = result.results.map((item) => item.content).filter(Boolean).map(
|
const contents = result.results
|
||||||
(content) => trimPrompt(content, 25_000),
|
.map((item) => item.content)
|
||||||
)
|
.filter(Boolean)
|
||||||
|
.map((content) => trimPrompt(content, 25_000))
|
||||||
const prompt = [
|
const prompt = [
|
||||||
`Given the following contents from a SERP search for the query <query>${query}</query>, generate a list of learnings from the contents. Return a maximum of ${numLearnings} learnings, but feel free to return less if the contents are clear. Make sure each learning is unique and not similar to each other. The learnings should be concise and to the point, as detailed and information dense as possible. Make sure to include any entities like people, places, companies, products, things, etc in the learnings, as well as any exact metrics, numbers, or dates. The learnings will be used to research the topic further.`,
|
`Given the following contents from a SERP search for the query <query>${query}</query>, generate a list of learnings from the contents. Return a maximum of ${numLearnings} learnings, but feel free to return less if the contents are clear. Make sure each learning is unique and not similar to each other. The learnings should be concise and to the point, as detailed and information dense as possible. Make sure to include any entities like people, places, companies, products, things, etc in the learnings, as well as any exact metrics, numbers, or dates. The learnings will be used to research the topic further.`,
|
||||||
`<contents>${contents
|
`<contents>${contents
|
||||||
.map((content) => `<content>\n${content}\n</content>`)
|
.map((content) => `<content>\n${content}\n</content>`)
|
||||||
.join('\n')}</contents>`,
|
.join('\n')}</contents>`,
|
||||||
`You MUST respond in JSON with the following schema: ${jsonSchema}`,
|
`You MUST respond in JSON with the following schema: ${jsonSchema}`,
|
||||||
|
languagePrompt(language),
|
||||||
].join('\n\n')
|
].join('\n\n')
|
||||||
|
|
||||||
return streamText({
|
return streamText({
|
||||||
@ -157,6 +165,7 @@ function processSearchResult({
|
|||||||
export function writeFinalReport({
|
export function writeFinalReport({
|
||||||
prompt,
|
prompt,
|
||||||
learnings,
|
learnings,
|
||||||
|
language,
|
||||||
}: WriteFinalReportParams) {
|
}: WriteFinalReportParams) {
|
||||||
const learningsString = trimPrompt(
|
const learningsString = trimPrompt(
|
||||||
learnings
|
learnings
|
||||||
@ -169,7 +178,8 @@ export function writeFinalReport({
|
|||||||
`<prompt>${prompt}</prompt>`,
|
`<prompt>${prompt}</prompt>`,
|
||||||
`Here are all the learnings from previous research:`,
|
`Here are all the learnings from previous research:`,
|
||||||
`<learnings>\n${learningsString}\n</learnings>`,
|
`<learnings>\n${learningsString}\n</learnings>`,
|
||||||
`Write the report in Markdown.`,
|
`Write the report using Markdown.`,
|
||||||
|
languagePrompt(language),
|
||||||
`## Deep Research Report`,
|
`## Deep Research Report`,
|
||||||
].join('\n\n')
|
].join('\n\n')
|
||||||
|
|
||||||
@ -188,6 +198,7 @@ export async function deepResearch({
|
|||||||
query,
|
query,
|
||||||
breadth,
|
breadth,
|
||||||
maxDepth,
|
maxDepth,
|
||||||
|
language,
|
||||||
learnings = [],
|
learnings = [],
|
||||||
visitedUrls = [],
|
visitedUrls = [],
|
||||||
onProgress,
|
onProgress,
|
||||||
@ -197,6 +208,7 @@ export async function deepResearch({
|
|||||||
query: string
|
query: string
|
||||||
breadth: number
|
breadth: number
|
||||||
maxDepth: number
|
maxDepth: number
|
||||||
|
language: string
|
||||||
learnings?: string[]
|
learnings?: string[]
|
||||||
visitedUrls?: string[]
|
visitedUrls?: string[]
|
||||||
onProgress: (step: ResearchStep) => void
|
onProgress: (step: ResearchStep) => void
|
||||||
@ -208,6 +220,7 @@ export async function deepResearch({
|
|||||||
query,
|
query,
|
||||||
learnings,
|
learnings,
|
||||||
numQueries: breadth,
|
numQueries: breadth,
|
||||||
|
language,
|
||||||
})
|
})
|
||||||
const limit = pLimit(ConcurrencyLimit)
|
const limit = pLimit(ConcurrencyLimit)
|
||||||
|
|
||||||
@ -242,22 +255,18 @@ export async function deepResearch({
|
|||||||
const results = await Promise.all(
|
const results = await Promise.all(
|
||||||
searchQueries.map((searchQuery, i) =>
|
searchQueries.map((searchQuery, i) =>
|
||||||
limit(async () => {
|
limit(async () => {
|
||||||
if (!searchQuery?.query)
|
if (!searchQuery?.query) {
|
||||||
return {
|
return {
|
||||||
learnings: [],
|
learnings: [],
|
||||||
visitedUrls: [],
|
visitedUrls: [],
|
||||||
}
|
}
|
||||||
|
}
|
||||||
onProgress({
|
onProgress({
|
||||||
type: 'searching',
|
type: 'searching',
|
||||||
query: searchQuery.query,
|
query: searchQuery.query,
|
||||||
nodeId: childNodeId(nodeId, i),
|
nodeId: childNodeId(nodeId, i),
|
||||||
})
|
})
|
||||||
try {
|
try {
|
||||||
// const result = await firecrawl.search(searchQuery.query, {
|
|
||||||
// timeout: 15000,
|
|
||||||
// limit: 5,
|
|
||||||
// scrapeOptions: { formats: ['markdown'] },
|
|
||||||
// });
|
|
||||||
const result = await useTavily().search(searchQuery.query, {
|
const result = await useTavily().search(searchQuery.query, {
|
||||||
maxResults: 5,
|
maxResults: 5,
|
||||||
})
|
})
|
||||||
@ -266,7 +275,9 @@ export async function deepResearch({
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Collect URLs from this search
|
// Collect URLs from this search
|
||||||
const newUrls = result.results.map((item) => item.url).filter(Boolean)
|
const newUrls = result.results
|
||||||
|
.map((item) => item.url)
|
||||||
|
.filter(Boolean)
|
||||||
onProgress({
|
onProgress({
|
||||||
type: 'search_complete',
|
type: 'search_complete',
|
||||||
urls: newUrls,
|
urls: newUrls,
|
||||||
@ -279,6 +290,7 @@ export async function deepResearch({
|
|||||||
query: searchQuery.query,
|
query: searchQuery.query,
|
||||||
result,
|
result,
|
||||||
numFollowUpQuestions: nextBreadth,
|
numFollowUpQuestions: nextBreadth,
|
||||||
|
language,
|
||||||
})
|
})
|
||||||
let searchResult: PartialSearchResult = {}
|
let searchResult: PartialSearchResult = {}
|
||||||
|
|
||||||
@ -317,7 +329,7 @@ export async function deepResearch({
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (
|
if (
|
||||||
nextDepth < maxDepth &&
|
nextDepth <= maxDepth &&
|
||||||
searchResult.followUpQuestions?.length
|
searchResult.followUpQuestions?.length
|
||||||
) {
|
) {
|
||||||
console.warn(
|
console.warn(
|
||||||
@ -340,6 +352,7 @@ export async function deepResearch({
|
|||||||
onProgress,
|
onProgress,
|
||||||
currentDepth: nextDepth,
|
currentDepth: nextDepth,
|
||||||
nodeId: childNodeId(nodeId, i),
|
nodeId: childNodeId(nodeId, i),
|
||||||
|
language,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
return {
|
return {
|
||||||
|
@ -2,7 +2,7 @@ import { streamText } from 'ai'
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { zodToJsonSchema } from 'zod-to-json-schema'
|
import { zodToJsonSchema } from 'zod-to-json-schema'
|
||||||
|
|
||||||
import { systemPrompt } from './prompt'
|
import { languagePrompt, systemPrompt } from './prompt'
|
||||||
import { useAiModel } from '~/composables/useAiProvider'
|
import { useAiModel } from '~/composables/useAiProvider'
|
||||||
|
|
||||||
type PartialFeedback = DeepPartial<z.infer<typeof feedbackTypeSchema>>
|
type PartialFeedback = DeepPartial<z.infer<typeof feedbackTypeSchema>>
|
||||||
@ -13,9 +13,11 @@ export const feedbackTypeSchema = z.object({
|
|||||||
|
|
||||||
export function generateFeedback({
|
export function generateFeedback({
|
||||||
query,
|
query,
|
||||||
|
language,
|
||||||
numQuestions = 3,
|
numQuestions = 3,
|
||||||
}: {
|
}: {
|
||||||
query: string
|
query: string
|
||||||
|
language: string
|
||||||
numQuestions?: number
|
numQuestions?: number
|
||||||
}) {
|
}) {
|
||||||
const schema = z.object({
|
const schema = z.object({
|
||||||
@ -27,6 +29,7 @@ export function generateFeedback({
|
|||||||
const prompt = [
|
const prompt = [
|
||||||
`Given the following query from the user, ask ${numQuestions} follow up questions to clarify the research direction. Return a maximum of ${numQuestions} questions, but feel free to return less if the original query is clear: <query>${query}</query>`,
|
`Given the following query from the user, ask ${numQuestions} follow up questions to clarify the research direction. Return a maximum of ${numQuestions} questions, but feel free to return less if the original query is clear: <query>${query}</query>`,
|
||||||
`You MUST respond in JSON with the following schema: ${jsonSchema}`,
|
`You MUST respond in JSON with the following schema: ${jsonSchema}`,
|
||||||
|
languagePrompt(language),
|
||||||
].join('\n\n')
|
].join('\n\n')
|
||||||
|
|
||||||
const stream = streamText({
|
const stream = streamText({
|
||||||
|
@ -13,3 +13,18 @@ export const systemPrompt = () => {
|
|||||||
- Consider new technologies and contrarian ideas, not just the conventional wisdom.
|
- Consider new technologies and contrarian ideas, not just the conventional wisdom.
|
||||||
- You may use high levels of speculation or prediction, just flag it for me.`
|
- You may use high levels of speculation or prediction, just flag it for me.`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct the language requirement prompt for LLMs.
|
||||||
|
* Placing this at the end of the prompt makes it easier for the LLM to pay attention to.
|
||||||
|
* @param language the language of the prompt, e.g. `English`
|
||||||
|
*/
|
||||||
|
export const languagePrompt = (language: string) => {
|
||||||
|
let languagePrompt = `- Respond in ${language}.`
|
||||||
|
|
||||||
|
if (language === '中文') {
|
||||||
|
languagePrompt +=
|
||||||
|
' Add appropriate spaces between Chinese and Latin characters / numbers to improve readability.'
|
||||||
|
}
|
||||||
|
return languagePrompt
|
||||||
|
}
|
||||||
|
@ -54,7 +54,7 @@
|
|||||||
import type { ResearchFeedbackResult } from '~/components/ResearchFeedback.vue'
|
import type { ResearchFeedbackResult } from '~/components/ResearchFeedback.vue'
|
||||||
import type { ResearchResult } from '~/lib/deep-research'
|
import type { ResearchResult } from '~/lib/deep-research'
|
||||||
|
|
||||||
const { t } = useI18n()
|
const { t, locale } = useI18n()
|
||||||
const { config } = useConfigStore()
|
const { config } = useConfigStore()
|
||||||
const toast = useToast()
|
const toast = useToast()
|
||||||
|
|
||||||
@ -113,6 +113,7 @@ ${feedback.value
|
|||||||
prompt: getCombinedQuery(),
|
prompt: getCombinedQuery(),
|
||||||
learnings: researchResult.value?.learnings ?? [],
|
learnings: researchResult.value?.learnings ?? [],
|
||||||
visitedUrls: researchResult.value?.visitedUrls ?? [],
|
visitedUrls: researchResult.value?.visitedUrls ?? [],
|
||||||
|
language: t('language', {}, { locale: locale.value }),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@ -37,15 +37,15 @@ export async function* parseStreamingJson<T extends z.ZodType>(
|
|||||||
let isParseSuccessful = false
|
let isParseSuccessful = false
|
||||||
|
|
||||||
for await (const chunk of textStream) {
|
for await (const chunk of textStream) {
|
||||||
rawText = removeJsonMarkdown(rawText + chunk)
|
rawText += chunk
|
||||||
const parsed = parsePartialJson(rawText)
|
const parsed = parsePartialJson(removeJsonMarkdown(rawText))
|
||||||
|
|
||||||
isParseSuccessful =
|
isParseSuccessful =
|
||||||
parsed.state === 'repaired-parse' || parsed.state === 'successful-parse'
|
parsed.state === 'repaired-parse' || parsed.state === 'successful-parse'
|
||||||
if (isParseSuccessful && isValid(parsed.value as any)) {
|
if (isParseSuccessful && isValid(parsed.value as any)) {
|
||||||
yield parsed.value as DeepPartial<z.infer<T>>
|
yield parsed.value as DeepPartial<z.infer<T>>
|
||||||
} else {
|
} else {
|
||||||
console.dir(parsed, { depth: null, colors: true })
|
console.debug(`Failed to parse JSON:`, rawText)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user