refactor: move general utils to shared
directory
This commit is contained in:
@ -272,6 +272,7 @@
|
||||
retryNode,
|
||||
currentDepth,
|
||||
breadth,
|
||||
aiConfig: config.ai,
|
||||
maxDepth: form.value.depth,
|
||||
languageCode: locale.value,
|
||||
searchLanguageCode: config.webSearch.searchLanguage,
|
||||
|
@ -1,9 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
const { locale: globalLocale, availableLocales, t, setLocale } = useI18n()
|
||||
|
||||
export type Locale = (typeof globalLocale)['value']
|
||||
export type AvailableLocales = Locale[]
|
||||
|
||||
const props = defineProps<{
|
||||
/** Override display locale */
|
||||
value?: Locale
|
||||
|
@ -19,7 +19,7 @@
|
||||
}>()
|
||||
|
||||
const { t, locale } = useI18n()
|
||||
const { showConfigManager, isConfigValid } = storeToRefs(useConfigStore())
|
||||
const { showConfigManager, isConfigValid, config } = storeToRefs(useConfigStore())
|
||||
const toast = useToast()
|
||||
|
||||
const reasoningContent = ref('')
|
||||
@ -57,6 +57,7 @@
|
||||
query: form.value.query,
|
||||
numQuestions: form.value.numQuestions,
|
||||
language: t('language', {}, { locale: locale.value }),
|
||||
aiConfig: config.value.ai,
|
||||
})) {
|
||||
if (f.type === 'reasoning') {
|
||||
reasoningContent.value += f.delta
|
||||
|
@ -8,6 +8,7 @@
|
||||
} from '~/constants/injection-keys'
|
||||
|
||||
const { t, locale } = useI18n()
|
||||
const { config } = storeToRefs(useConfigStore())
|
||||
const toast = useToast()
|
||||
|
||||
const error = ref('')
|
||||
@ -156,6 +157,7 @@
|
||||
prompt: getCombinedQuery(form.value, feedback.value),
|
||||
language: t('language', {}, { locale: locale.value }),
|
||||
learnings,
|
||||
aiConfig: config.value.ai,
|
||||
})
|
||||
for await (const chunk of fullStream) {
|
||||
if (chunk.type === 'reasoning') {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { skipHydrate } from 'pinia'
|
||||
import type { Locale } from '@/components/LangSwitcher.vue'
|
||||
import { getApiBase } from '~~/shared/utils/ai-model'
|
||||
|
||||
export type ConfigAiProvider =
|
||||
| 'openai-compatible'
|
||||
@ -71,25 +71,7 @@ export const useConfigStore = defineStore('config', () => {
|
||||
)
|
||||
const isConfigValid = computed(() => validateConfig(config.value))
|
||||
|
||||
const aiApiBase = computed(() => {
|
||||
const { ai } = config.value
|
||||
if (ai.provider === 'openrouter') {
|
||||
return ai.apiBase || 'https://openrouter.ai/api/v1'
|
||||
}
|
||||
if (ai.provider === 'deepseek') {
|
||||
return ai.apiBase || 'https://api.deepseek.com/v1'
|
||||
}
|
||||
if (ai.provider === 'ollama') {
|
||||
return ai.apiBase || 'http://localhost:11434/v1'
|
||||
}
|
||||
if (ai.provider === 'siliconflow') {
|
||||
return ai.apiBase || 'https://api.siliconflow.cn/v1'
|
||||
}
|
||||
if (ai.provider === 'infiniai') {
|
||||
return ai.apiBase || 'https://cloud.infini-ai.com/maas/v1'
|
||||
}
|
||||
return ai.apiBase || 'https://api.openai.com/v1'
|
||||
})
|
||||
const aiApiBase = computed(() => getApiBase(config.value.ai))
|
||||
const webSearchApiBase = computed(() => {
|
||||
const { webSearch } = config.value
|
||||
if (webSearch.provider === 'tavily') {
|
||||
|
@ -1,14 +1,12 @@
|
||||
import { streamText } from 'ai'
|
||||
import { z } from 'zod'
|
||||
import { parseStreamingJson, type DeepPartial } from '~~/utils/json'
|
||||
import { parseStreamingJson, type DeepPartial } from '~~/shared/utils/json'
|
||||
|
||||
import { trimPrompt } from './ai/providers'
|
||||
import { languagePrompt, systemPrompt } from './prompt'
|
||||
import zodToJsonSchema from 'zod-to-json-schema'
|
||||
import { useAiModel } from '@/composables/useAiProvider'
|
||||
import type { Locale } from '@/components/LangSwitcher.vue'
|
||||
import type { DeepResearchNode } from '@/components/DeepResearch/DeepResearch.vue'
|
||||
import { throwAiError } from '~~/utils/errors'
|
||||
import { throwAiError } from '~~/shared/utils/errors'
|
||||
|
||||
export type ResearchResult = {
|
||||
learnings: ProcessedSearchResult['learnings']
|
||||
@ -18,6 +16,7 @@ export interface WriteFinalReportParams {
|
||||
prompt: string
|
||||
learnings: ProcessedSearchResult['learnings']
|
||||
language: string
|
||||
aiConfig: ConfigAi
|
||||
}
|
||||
|
||||
// Used for streaming response
|
||||
@ -28,36 +27,36 @@ export type PartialProcessedSearchResult = DeepPartial<ProcessedSearchResult>
|
||||
|
||||
export type ResearchStep =
|
||||
| {
|
||||
type: 'generating_query'
|
||||
result: PartialSearchQuery
|
||||
nodeId: string
|
||||
parentNodeId?: string
|
||||
}
|
||||
type: 'generating_query'
|
||||
result: PartialSearchQuery
|
||||
nodeId: string
|
||||
parentNodeId?: string
|
||||
}
|
||||
| { type: 'generating_query_reasoning'; delta: string; nodeId: string }
|
||||
| {
|
||||
type: 'generated_query'
|
||||
query: string
|
||||
result: PartialSearchQuery
|
||||
nodeId: string
|
||||
}
|
||||
type: 'generated_query'
|
||||
query: string
|
||||
result: PartialSearchQuery
|
||||
nodeId: string
|
||||
}
|
||||
| { type: 'searching'; query: string; nodeId: string }
|
||||
| { type: 'search_complete'; results: WebSearchResult[]; nodeId: string }
|
||||
| {
|
||||
type: 'processing_serach_result'
|
||||
query: string
|
||||
result: PartialProcessedSearchResult
|
||||
nodeId: string
|
||||
}
|
||||
type: 'processing_serach_result'
|
||||
query: string
|
||||
result: PartialProcessedSearchResult
|
||||
nodeId: string
|
||||
}
|
||||
| {
|
||||
type: 'processing_serach_result_reasoning'
|
||||
delta: string
|
||||
nodeId: string
|
||||
}
|
||||
type: 'processing_serach_result_reasoning'
|
||||
delta: string
|
||||
nodeId: string
|
||||
}
|
||||
| {
|
||||
type: 'node_complete'
|
||||
result?: ProcessedSearchResult
|
||||
nodeId: string
|
||||
}
|
||||
type: 'node_complete'
|
||||
result?: ProcessedSearchResult
|
||||
nodeId: string
|
||||
}
|
||||
| { type: 'error'; message: string; nodeId: string }
|
||||
| { type: 'complete'; learnings: ProcessedSearchResult['learnings'] }
|
||||
|
||||
@ -80,6 +79,7 @@ export function generateSearchQueries({
|
||||
learnings,
|
||||
language,
|
||||
searchLanguage,
|
||||
aiConfig,
|
||||
}: {
|
||||
query: string
|
||||
language: string
|
||||
@ -88,6 +88,7 @@ export function generateSearchQueries({
|
||||
learnings?: string[]
|
||||
/** Force the LLM to generate serp queries in a certain language */
|
||||
searchLanguage?: string
|
||||
aiConfig: ConfigAi
|
||||
}) {
|
||||
const schema = z.object({
|
||||
queries: z
|
||||
@ -115,14 +116,14 @@ export function generateSearchQueries({
|
||||
`Given the following prompt from the user, generate a list of highly effective Google search queries to research the topic. Return a maximum of ${numQueries} queries, but feel free to return less if the original prompt is clear. Make sure each query is creative, unique and not similar to each other: <prompt>${query}</prompt>`,
|
||||
learnings
|
||||
? `Here are some learnings from previous research, use them to generate more specific queries: ${learnings.join(
|
||||
'\n',
|
||||
)}`
|
||||
'\n',
|
||||
)}`
|
||||
: '',
|
||||
`You MUST respond in JSON matching this JSON schema: ${jsonSchema}`,
|
||||
lp,
|
||||
].join('\n\n')
|
||||
return streamText({
|
||||
model: useAiModel(),
|
||||
model: getLanguageModel(aiConfig),
|
||||
system: systemPrompt(),
|
||||
prompt,
|
||||
onError({ error }) {
|
||||
@ -149,12 +150,14 @@ function processSearchResult({
|
||||
numLearnings = 5,
|
||||
numFollowUpQuestions = 3,
|
||||
language,
|
||||
aiConfig,
|
||||
}: {
|
||||
query: string
|
||||
results: WebSearchResult[]
|
||||
language: string
|
||||
numLearnings?: number
|
||||
numFollowUpQuestions?: number
|
||||
aiConfig: ConfigAi
|
||||
}) {
|
||||
const schema = z.object({
|
||||
learnings: z
|
||||
@ -194,7 +197,7 @@ function processSearchResult({
|
||||
].join('\n\n')
|
||||
|
||||
return streamText({
|
||||
model: useAiModel(),
|
||||
model: getLanguageModel(aiConfig),
|
||||
system: systemPrompt(),
|
||||
prompt,
|
||||
onError({ error }) {
|
||||
@ -207,6 +210,7 @@ export function writeFinalReport({
|
||||
prompt,
|
||||
learnings,
|
||||
language,
|
||||
aiConfig,
|
||||
}: WriteFinalReportParams) {
|
||||
const learningsString = trimPrompt(
|
||||
learnings
|
||||
@ -229,7 +233,7 @@ ${learning.learning}
|
||||
].join('\n\n')
|
||||
|
||||
return streamText({
|
||||
model: useAiModel(),
|
||||
model: getLanguageModel(aiConfig),
|
||||
system: systemPrompt(),
|
||||
prompt: _prompt,
|
||||
onError({ error }) {
|
||||
@ -247,6 +251,7 @@ export async function deepResearch({
|
||||
breadth,
|
||||
maxDepth,
|
||||
languageCode,
|
||||
aiConfig,
|
||||
searchLanguageCode,
|
||||
learnings,
|
||||
onProgress,
|
||||
@ -259,6 +264,8 @@ export async function deepResearch({
|
||||
maxDepth: number
|
||||
/** The language of generated response */
|
||||
languageCode: Locale
|
||||
/** The AI model configuration */
|
||||
aiConfig: ConfigAi
|
||||
/** The language of SERP query */
|
||||
searchLanguageCode?: Locale
|
||||
/** Accumulated learnings from all nodes visited so far */
|
||||
@ -299,6 +306,7 @@ export async function deepResearch({
|
||||
numQueries: breadth,
|
||||
language,
|
||||
searchLanguage,
|
||||
aiConfig,
|
||||
})
|
||||
|
||||
for await (const chunk of parseStreamingJson(
|
||||
@ -370,7 +378,6 @@ export async function deepResearch({
|
||||
if (!searchQuery?.query) {
|
||||
return {
|
||||
learnings: [],
|
||||
visitedUrls: [],
|
||||
}
|
||||
}
|
||||
onProgress({
|
||||
@ -404,6 +411,7 @@ export async function deepResearch({
|
||||
results,
|
||||
numFollowUpQuestions: nextBreadth,
|
||||
language,
|
||||
aiConfig,
|
||||
})
|
||||
let searchResult: PartialProcessedSearchResult = {}
|
||||
|
||||
@ -479,8 +487,8 @@ export async function deepResearch({
|
||||
const nextQuery = `
|
||||
Previous research goal: ${searchQuery.researchGoal}
|
||||
Follow-up research directions: ${searchResult.followUpQuestions
|
||||
.map((q) => `\n${q}`)
|
||||
.join('')}
|
||||
.map((q) => `\n${q}`)
|
||||
.join('')}
|
||||
`.trim()
|
||||
|
||||
// Add concurrency by 1, and do next recursive search
|
||||
@ -495,6 +503,7 @@ export async function deepResearch({
|
||||
currentDepth: nextDepth,
|
||||
nodeId: searchQuery.nodeId,
|
||||
languageCode,
|
||||
aiConfig,
|
||||
})
|
||||
return r
|
||||
} catch (error) {
|
||||
|
@ -3,9 +3,9 @@ import { z } from 'zod'
|
||||
import { zodToJsonSchema } from 'zod-to-json-schema'
|
||||
|
||||
import { languagePrompt, systemPrompt } from './prompt'
|
||||
import { useAiModel } from '~/composables/useAiProvider'
|
||||
import { parseStreamingJson, type DeepPartial } from '~~/utils/json'
|
||||
import { throwAiError } from '~~/utils/errors'
|
||||
import { parseStreamingJson, type DeepPartial } from '~~/shared/utils/json'
|
||||
import { throwAiError } from '~~/shared/utils/errors'
|
||||
import { getLanguageModel } from '~~/shared/utils/ai-model'
|
||||
|
||||
type PartialFeedback = DeepPartial<z.infer<typeof feedbackTypeSchema>>
|
||||
|
||||
@ -17,9 +17,11 @@ export function generateFeedback({
|
||||
query,
|
||||
language,
|
||||
numQuestions = 3,
|
||||
aiConfig,
|
||||
}: {
|
||||
query: string
|
||||
language: string
|
||||
aiConfig: ConfigAi
|
||||
numQuestions?: number
|
||||
}) {
|
||||
const schema = z.object({
|
||||
@ -36,7 +38,7 @@ export function generateFeedback({
|
||||
].join('\n\n')
|
||||
|
||||
const stream = streamText({
|
||||
model: useAiModel(),
|
||||
model: getLanguageModel(aiConfig),
|
||||
system: systemPrompt(),
|
||||
prompt,
|
||||
onError({ error }) {
|
||||
|
4
shared/types/types.d.ts
vendored
Normal file
4
shared/types/types.d.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
import type { NuxtApp } from "#app";
|
||||
|
||||
export type AvailableLocales = NuxtApp['$i18n']['availableLocales']
|
||||
export type Locale = AvailableLocales[number]
|
63
shared/utils/ai-model.ts
Normal file
63
shared/utils/ai-model.ts
Normal file
@ -0,0 +1,63 @@
|
||||
import { createDeepSeek } from "@ai-sdk/deepseek"
|
||||
import { createOpenAI } from "@ai-sdk/openai"
|
||||
import { createOpenRouter } from "@openrouter/ai-sdk-provider"
|
||||
import { wrapLanguageModel, extractReasoningMiddleware } from "ai"
|
||||
import type { LanguageModelV1 } from "ai"
|
||||
|
||||
export function getLanguageModel(config: ConfigAi) {
|
||||
const apiBase = getApiBase(config)
|
||||
let model: LanguageModelV1
|
||||
|
||||
if (config.provider === 'openrouter') {
|
||||
const openRouter = createOpenRouter({
|
||||
apiKey: config.apiKey,
|
||||
baseURL: apiBase,
|
||||
})
|
||||
model = openRouter(config.model, {
|
||||
includeReasoning: true,
|
||||
})
|
||||
} else if (
|
||||
config.provider === 'deepseek' ||
|
||||
config.provider === 'siliconflow' ||
|
||||
config.provider === 'infiniai' ||
|
||||
// Special case if model name includes 'deepseek'
|
||||
// This ensures compatibilty with providers like Siliconflow
|
||||
config.model?.toLowerCase().includes('deepseek')
|
||||
) {
|
||||
const deepSeek = createDeepSeek({
|
||||
apiKey: config.apiKey,
|
||||
baseURL: apiBase,
|
||||
})
|
||||
model = deepSeek(config.model)
|
||||
} else {
|
||||
const openai = createOpenAI({
|
||||
apiKey: config.apiKey,
|
||||
baseURL: apiBase,
|
||||
})
|
||||
model = openai(config.model)
|
||||
}
|
||||
|
||||
return wrapLanguageModel({
|
||||
model,
|
||||
middleware: extractReasoningMiddleware({ tagName: 'think' }),
|
||||
})
|
||||
}
|
||||
|
||||
export function getApiBase(config: ConfigAi) {
|
||||
if (config.provider === 'openrouter') {
|
||||
return config.apiBase || 'https://openrouter.ai/api/v1'
|
||||
}
|
||||
if (config.provider === 'deepseek') {
|
||||
return config.apiBase || 'https://api.deepseek.com/v1'
|
||||
}
|
||||
if (config.provider === 'ollama') {
|
||||
return config.apiBase || 'http://localhost:11434/v1'
|
||||
}
|
||||
if (config.provider === 'siliconflow') {
|
||||
return config.apiBase || 'https://api.siliconflow.cn/v1'
|
||||
}
|
||||
if (config.provider === 'infiniai') {
|
||||
return config.apiBase || 'https://cloud.infini-ai.com/maas/v1'
|
||||
}
|
||||
return config.apiBase || 'https://api.openai.com/v1'
|
||||
}
|
Reference in New Issue
Block a user