mirror of
https://github.com/jina-ai/node-DeepResearch.git
synced 2026-03-22 07:29:35 +08:00
refactor: move LLM client implementations to utils directory
Co-Authored-By: Han Xiao <han.xiao@jina.ai>
This commit is contained in:
106
src/config.ts
106
src/config.ts
@@ -1,5 +1,6 @@
|
|||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
import { ProxyAgent, setGlobalDispatcher } from 'undici';
|
import { ProxyAgent, setGlobalDispatcher } from 'undici';
|
||||||
|
import { GoogleAIWrapper, LocalLLMClient, LLMClient } from './utils/llm-client';
|
||||||
|
|
||||||
interface ModelConfig {
|
interface ModelConfig {
|
||||||
model: string;
|
model: string;
|
||||||
@@ -15,111 +16,6 @@ interface ToolConfigs {
|
|||||||
agentBeastMode: ModelConfig;
|
agentBeastMode: ModelConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
import { GoogleGenerativeAI } from '@google/generative-ai';
|
|
||||||
|
|
||||||
interface LLMClientConfig {
|
|
||||||
model: string;
|
|
||||||
temperature: number;
|
|
||||||
generationConfig?: {
|
|
||||||
responseMimeType?: string;
|
|
||||||
responseSchema?: any;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
interface LLMResponse {
|
|
||||||
text(): string;
|
|
||||||
usageMetadata: {
|
|
||||||
totalTokenCount: number;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
interface LLMClient {
|
|
||||||
getGenerativeModel(config: LLMClientConfig): {
|
|
||||||
generateContent(prompt: string): Promise<{
|
|
||||||
response: LLMResponse;
|
|
||||||
}>;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
class GoogleAIWrapper implements LLMClient {
|
|
||||||
private client: GoogleGenerativeAI;
|
|
||||||
|
|
||||||
constructor(apiKey: string) {
|
|
||||||
this.client = new GoogleGenerativeAI(apiKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
getGenerativeModel(config: LLMClientConfig) {
|
|
||||||
const model = this.client.getGenerativeModel({
|
|
||||||
model: config.model,
|
|
||||||
generationConfig: {
|
|
||||||
temperature: config.temperature,
|
|
||||||
...(config.generationConfig || {})
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
generateContent: async (prompt: string) => {
|
|
||||||
const result = await model.generateContent(prompt);
|
|
||||||
return {
|
|
||||||
response: {
|
|
||||||
text: () => result.response.text(),
|
|
||||||
usageMetadata: {
|
|
||||||
totalTokenCount: result.response.usageMetadata?.totalTokenCount ?? 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class LocalLLMClient implements LLMClient {
|
|
||||||
constructor(
|
|
||||||
private hostname: string,
|
|
||||||
private port: string,
|
|
||||||
private model: string
|
|
||||||
) {}
|
|
||||||
|
|
||||||
getGenerativeModel(config: LLMClientConfig) {
|
|
||||||
return {
|
|
||||||
generateContent: async (prompt: string) => {
|
|
||||||
const response = await fetch(`http://${this.hostname}:${this.port}/v1/chat/completions`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
model: this.model,
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
role: 'user',
|
|
||||||
content: prompt,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
temperature: config.temperature,
|
|
||||||
response_format: {
|
|
||||||
type: 'json_schema',
|
|
||||||
json_schema: config.generationConfig?.responseSchema,
|
|
||||||
},
|
|
||||||
max_tokens: 1000,
|
|
||||||
stream: false,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
return {
|
|
||||||
response: {
|
|
||||||
text: () => data.choices[0].message.content,
|
|
||||||
usageMetadata: {
|
|
||||||
totalTokenCount: data.usage?.total_tokens || 0,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
|
|||||||
104
src/utils/llm-client.ts
Normal file
104
src/utils/llm-client.ts
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
import { GoogleGenerativeAI } from '@google/generative-ai';
|
||||||
|
|
||||||
|
export interface LLMClientConfig {
|
||||||
|
model: string;
|
||||||
|
temperature: number;
|
||||||
|
generationConfig?: {
|
||||||
|
responseMimeType?: string;
|
||||||
|
responseSchema?: any;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LLMResponse {
|
||||||
|
text(): string;
|
||||||
|
usageMetadata: {
|
||||||
|
totalTokenCount: number;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LLMClient {
|
||||||
|
getGenerativeModel(config: LLMClientConfig): {
|
||||||
|
generateContent(prompt: string): Promise<{
|
||||||
|
response: LLMResponse;
|
||||||
|
}>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export class GoogleAIWrapper implements LLMClient {
|
||||||
|
private client: GoogleGenerativeAI;
|
||||||
|
|
||||||
|
constructor(apiKey: string) {
|
||||||
|
this.client = new GoogleGenerativeAI(apiKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
getGenerativeModel(config: LLMClientConfig) {
|
||||||
|
const model = this.client.getGenerativeModel({
|
||||||
|
model: config.model,
|
||||||
|
generationConfig: {
|
||||||
|
temperature: config.temperature,
|
||||||
|
...(config.generationConfig || {})
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
generateContent: async (prompt: string) => {
|
||||||
|
const result = await model.generateContent(prompt);
|
||||||
|
return {
|
||||||
|
response: {
|
||||||
|
text: () => result.response.text(),
|
||||||
|
usageMetadata: {
|
||||||
|
totalTokenCount: result.response.usageMetadata?.totalTokenCount ?? 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class LocalLLMClient implements LLMClient {
|
||||||
|
constructor(
|
||||||
|
private hostname: string,
|
||||||
|
private port: string,
|
||||||
|
private model: string
|
||||||
|
) {}
|
||||||
|
|
||||||
|
getGenerativeModel(config: LLMClientConfig) {
|
||||||
|
return {
|
||||||
|
generateContent: async (prompt: string) => {
|
||||||
|
const response = await fetch(`http://${this.hostname}:${this.port}/v1/chat/completions`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
model: this.model,
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: 'user',
|
||||||
|
content: prompt,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
temperature: config.temperature,
|
||||||
|
response_format: {
|
||||||
|
type: 'json_schema',
|
||||||
|
json_schema: config.generationConfig?.responseSchema,
|
||||||
|
},
|
||||||
|
max_tokens: 1000,
|
||||||
|
stream: false,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
return {
|
||||||
|
response: {
|
||||||
|
text: () => data.choices[0].message.content,
|
||||||
|
usageMetadata: {
|
||||||
|
totalTokenCount: data.usage?.total_tokens || 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user