diff --git a/components/DeepResearch.vue b/components/DeepResearch.vue
index e145c3d..bb06a60 100644
--- a/components/DeepResearch.vue
+++ b/components/DeepResearch.vue
@@ -8,7 +8,7 @@
import type { TreeNode } from './Tree.vue'
import { marked } from 'marked'
- const { t } = useI18n()
+ const { t, locale } = useI18n()
const emit = defineEmits<{
(e: 'complete', results: ResearchResult): void
}>()
@@ -150,6 +150,7 @@
query,
maxDepth: depth,
breadth,
+ language: t('language', {}, { locale: locale.value }),
onProgress: handleResearchProgress,
})
} catch (error) {
@@ -181,7 +182,7 @@
-
{{ selectedNode.label }}
+
{{ selectedNode.label }}
@@ -191,15 +192,23 @@
{{ t('webBrowsing.researchGoal') }}
-
{{ selectedNode.researchGoal }}
+
{{ t('webBrowsing.visitedUrls') }}
- -
+
-
{{ t('webBrowsing.learnings') }}
-
+
diff --git a/components/ResearchFeedback.vue b/components/ResearchFeedback.vue
index 880ada2..93cea03 100644
--- a/components/ResearchFeedback.vue
+++ b/components/ResearchFeedback.vue
@@ -14,7 +14,7 @@
(e: 'submit', feedback: ResearchFeedbackResult[]): void
}>()
- const { t } = useI18n()
+ const { t, locale } = useI18n()
const feedback = ref([])
const isLoading = ref(false)
@@ -37,6 +37,7 @@
for await (const f of generateFeedback({
query,
numQuestions,
+ language: t('language', {}, { locale: locale.value }),
})) {
const questions = f.questions!.filter((s) => typeof s === 'string')
// Incrementally update modelValue
diff --git a/components/Tree.vue b/components/Tree.vue
index 82dba3e..de2a20f 100644
--- a/components/Tree.vue
+++ b/components/Tree.vue
@@ -27,14 +27,19 @@
const icon = computed(() => {
const result = { name: '', pulse: false }
if (!props.node.status) return result
+
switch (props.node.status) {
case 'generating_query':
result.name = 'i-lucide-clipboard-list'
result.pulse = true
break
case 'generated_query':
- result.name = 'i-lucide-pause'
- break
+ // FIXME: 因为 deepResearch 有并发限制,这个 case 是为了明确区分状态。
+ // 但是目前进入这个状态之后再进入 searching 状态,图标不会更新成 search,不知道原因
+ // 暂时禁用了这个 case
+ // result.name = 'i-lucide-pause'
+ // result.pulse = true
+ // break
case 'searching':
result.name = 'i-lucide-search'
result.pulse = true
@@ -70,7 +75,7 @@
>
{{ node.label }}
-
+
-
item.content).filter(Boolean).map(
- (content) => trimPrompt(content, 25_000),
- )
+ const contents = result.results
+ .map((item) => item.content)
+ .filter(Boolean)
+ .map((content) => trimPrompt(content, 25_000))
const prompt = [
`Given the following contents from a SERP search for the 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
.map((content) => `\n${content}\n`)
.join('\n')}`,
`You MUST respond in JSON with the following schema: ${jsonSchema}`,
+ languagePrompt(language),
].join('\n\n')
return streamText({
@@ -157,6 +165,7 @@ function processSearchResult({
export function writeFinalReport({
prompt,
learnings,
+ language,
}: WriteFinalReportParams) {
const learningsString = trimPrompt(
learnings
@@ -169,7 +178,8 @@ export function writeFinalReport({
`${prompt}`,
`Here are all the learnings from previous research:`,
`\n${learningsString}\n`,
- `Write the report in Markdown.`,
+ `Write the report using Markdown.`,
+ languagePrompt(language),
`## Deep Research Report`,
].join('\n\n')
@@ -188,6 +198,7 @@ export async function deepResearch({
query,
breadth,
maxDepth,
+ language,
learnings = [],
visitedUrls = [],
onProgress,
@@ -197,6 +208,7 @@ export async function deepResearch({
query: string
breadth: number
maxDepth: number
+ language: string
learnings?: string[]
visitedUrls?: string[]
onProgress: (step: ResearchStep) => void
@@ -208,6 +220,7 @@ export async function deepResearch({
query,
learnings,
numQueries: breadth,
+ language,
})
const limit = pLimit(ConcurrencyLimit)
@@ -242,22 +255,18 @@ export async function deepResearch({
const results = await Promise.all(
searchQueries.map((searchQuery, i) =>
limit(async () => {
- if (!searchQuery?.query)
+ if (!searchQuery?.query) {
return {
learnings: [],
visitedUrls: [],
}
+ }
onProgress({
type: 'searching',
query: searchQuery.query,
nodeId: childNodeId(nodeId, i),
})
try {
- // const result = await firecrawl.search(searchQuery.query, {
- // timeout: 15000,
- // limit: 5,
- // scrapeOptions: { formats: ['markdown'] },
- // });
const result = await useTavily().search(searchQuery.query, {
maxResults: 5,
})
@@ -266,7 +275,9 @@ export async function deepResearch({
)
// 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({
type: 'search_complete',
urls: newUrls,
@@ -279,6 +290,7 @@ export async function deepResearch({
query: searchQuery.query,
result,
numFollowUpQuestions: nextBreadth,
+ language,
})
let searchResult: PartialSearchResult = {}
@@ -317,7 +329,7 @@ export async function deepResearch({
})
if (
- nextDepth < maxDepth &&
+ nextDepth <= maxDepth &&
searchResult.followUpQuestions?.length
) {
console.warn(
@@ -340,6 +352,7 @@ export async function deepResearch({
onProgress,
currentDepth: nextDepth,
nodeId: childNodeId(nodeId, i),
+ language,
})
} else {
return {
diff --git a/lib/feedback.ts b/lib/feedback.ts
index e47e04b..0dac09a 100644
--- a/lib/feedback.ts
+++ b/lib/feedback.ts
@@ -2,7 +2,7 @@ import { streamText } from 'ai'
import { z } from 'zod'
import { zodToJsonSchema } from 'zod-to-json-schema'
-import { systemPrompt } from './prompt'
+import { languagePrompt, systemPrompt } from './prompt'
import { useAiModel } from '~/composables/useAiProvider'
type PartialFeedback = DeepPartial>
@@ -13,9 +13,11 @@ export const feedbackTypeSchema = z.object({
export function generateFeedback({
query,
+ language,
numQuestions = 3,
}: {
query: string
+ language: string
numQuestions?: number
}) {
const schema = z.object({
@@ -27,6 +29,7 @@ export function generateFeedback({
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}`,
`You MUST respond in JSON with the following schema: ${jsonSchema}`,
+ languagePrompt(language),
].join('\n\n')
const stream = streamText({
diff --git a/lib/prompt.ts b/lib/prompt.ts
index ee6dfbf..50d0ce6 100644
--- a/lib/prompt.ts
+++ b/lib/prompt.ts
@@ -13,3 +13,18 @@ export const systemPrompt = () => {
- 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.`
}
+
+/**
+ * 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
+}
diff --git a/pages/index.vue b/pages/index.vue
index 97ee0da..e2ae1b1 100644
--- a/pages/index.vue
+++ b/pages/index.vue
@@ -54,7 +54,7 @@
import type { ResearchFeedbackResult } from '~/components/ResearchFeedback.vue'
import type { ResearchResult } from '~/lib/deep-research'
- const { t } = useI18n()
+ const { t, locale } = useI18n()
const { config } = useConfigStore()
const toast = useToast()
@@ -113,6 +113,7 @@ ${feedback.value
prompt: getCombinedQuery(),
learnings: researchResult.value?.learnings ?? [],
visitedUrls: researchResult.value?.visitedUrls ?? [],
+ language: t('language', {}, { locale: locale.value }),
})
}
diff --git a/utils/json.ts b/utils/json.ts
index 385caea..275841d 100644
--- a/utils/json.ts
+++ b/utils/json.ts
@@ -37,15 +37,15 @@ export async function* parseStreamingJson(
let isParseSuccessful = false
for await (const chunk of textStream) {
- rawText = removeJsonMarkdown(rawText + chunk)
- const parsed = parsePartialJson(rawText)
+ rawText += chunk
+ const parsed = parsePartialJson(removeJsonMarkdown(rawText))
isParseSuccessful =
parsed.state === 'repaired-parse' || parsed.state === 'successful-parse'
if (isParseSuccessful && isValid(parsed.value as any)) {
yield parsed.value as DeepPartial>
} else {
- console.dir(parsed, { depth: null, colors: true })
+ console.debug(`Failed to parse JSON:`, rawText)
}
}