mirror of
https://github.com/dzhng/deep-research.git
synced 2026-03-22 16:07:17 +08:00
Merge pull request #10 from nasedkinpv/main
feat: add custom OpenAI endpoint and custom model support
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -39,3 +39,4 @@ yarn-error.log*
|
||||
# Misc
|
||||
.DS_Store
|
||||
*.pem
|
||||
bun.lockb
|
||||
@@ -1,32 +1,53 @@
|
||||
import { createOpenAI } from '@ai-sdk/openai';
|
||||
import { createOpenAI, type OpenAIProviderSettings } from '@ai-sdk/openai';
|
||||
import { getEncoding } from 'js-tiktoken';
|
||||
|
||||
import { RecursiveCharacterTextSplitter } from './text-splitter';
|
||||
|
||||
// Providers
|
||||
interface CustomOpenAIProviderSettings extends OpenAIProviderSettings {
|
||||
baseURL?: string;
|
||||
}
|
||||
|
||||
// Providers
|
||||
const openai = createOpenAI({
|
||||
apiKey: process.env.OPENAI_KEY!,
|
||||
});
|
||||
baseURL: process.env.OPENAI_ENDPOINT || 'https://api.openai.com/v1',
|
||||
} as CustomOpenAIProviderSettings);
|
||||
|
||||
const isCustomEndpoint =
|
||||
process.env.OPENAI_ENDPOINT &&
|
||||
process.env.OPENAI_ENDPOINT !== 'https://api.openai.com/v1';
|
||||
const customModel = process.env.OPENAI_MODEL;
|
||||
|
||||
// Models
|
||||
|
||||
export const gpt4Model = openai('gpt-4o', {
|
||||
structuredOutputs: true,
|
||||
});
|
||||
export const gpt4MiniModel = openai('gpt-4o-mini', {
|
||||
structuredOutputs: true,
|
||||
});
|
||||
export const o3MiniModel = openai('o3-mini', {
|
||||
reasoningEffort: 'medium',
|
||||
structuredOutputs: true,
|
||||
});
|
||||
export const gpt4Model = openai(
|
||||
isCustomEndpoint && customModel ? customModel : 'gpt-4o',
|
||||
{
|
||||
structuredOutputs: true,
|
||||
},
|
||||
);
|
||||
export const gpt4MiniModel = openai(
|
||||
isCustomEndpoint && customModel ? customModel : 'gpt-4o-mini',
|
||||
{
|
||||
structuredOutputs: true,
|
||||
},
|
||||
);
|
||||
export const o3MiniModel = openai(
|
||||
isCustomEndpoint && customModel ? customModel : 'o3-mini',
|
||||
{
|
||||
reasoningEffort: 'medium',
|
||||
structuredOutputs: true,
|
||||
},
|
||||
);
|
||||
|
||||
const MinChunkSize = 140;
|
||||
const encoder = getEncoding('o200k_base');
|
||||
|
||||
// trim prompt to maximum context size
|
||||
export function trimPrompt(prompt: string, contextSize = 120_000) {
|
||||
export function trimPrompt(
|
||||
prompt: string,
|
||||
contextSize = Number(process.env.CONTEXT_SIZE) || 128_000,
|
||||
) {
|
||||
if (!prompt) {
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -207,8 +207,15 @@ export async function deepResearch({
|
||||
visitedUrls: allUrls,
|
||||
};
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`Error running query: ${serpQuery.query}: `, e);
|
||||
} catch (e: any) {
|
||||
if (e.message && e.message.includes('Timeout')) {
|
||||
console.error(
|
||||
`Timeout error running query: ${serpQuery.query}: `,
|
||||
e,
|
||||
);
|
||||
} else {
|
||||
console.error(`Error running query: ${serpQuery.query}: `, e);
|
||||
}
|
||||
return {
|
||||
learnings: [],
|
||||
visitedUrls: [],
|
||||
|
||||
Reference in New Issue
Block a user