Add deepseek.ts to src/llms with corresponding unit tests. Updates fa… (#4613)
Co-authored-by: kartik-mem0 <kartik.labhshetwar@mem0.ai>
This commit is contained in:
co-authored by
kartik-mem0
parent
d926f3697c
commit
e59e3d5f0c
@@ -0,0 +1,43 @@
|
|||||||
|
import { OpenAILLM } from "./openai";
|
||||||
|
import { LLMConfig, Message } from "../types";
|
||||||
|
import { LLMResponse } from "./base";
|
||||||
|
|
||||||
|
export class DeepSeekLLM extends OpenAILLM {
|
||||||
|
constructor(config: LLMConfig) {
|
||||||
|
const apiKey = config.apiKey || process.env.DEEPSEEK_API_KEY;
|
||||||
|
if (!apiKey) {
|
||||||
|
throw new Error("DeepSeek API key is required");
|
||||||
|
}
|
||||||
|
super({
|
||||||
|
...config,
|
||||||
|
apiKey,
|
||||||
|
baseURL:
|
||||||
|
config.baseURL ||
|
||||||
|
process.env.DEEPSEEK_API_BASE ||
|
||||||
|
"https://api.deepseek.com",
|
||||||
|
model: config.model || "deepseek-chat",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async generateResponse(
|
||||||
|
messages: Message[],
|
||||||
|
responseFormat?: { type: string },
|
||||||
|
tools?: any[],
|
||||||
|
): Promise<string | LLMResponse> {
|
||||||
|
try {
|
||||||
|
return await super.generateResponse(messages, responseFormat, tools);
|
||||||
|
} catch (err) {
|
||||||
|
const message = err instanceof Error ? err.message : String(err);
|
||||||
|
throw new Error(`DeepSeek LLM failed: ${message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async generateChat(messages: Message[]): Promise<LLMResponse> {
|
||||||
|
try {
|
||||||
|
return await super.generateChat(messages);
|
||||||
|
} catch (err) {
|
||||||
|
const message = err instanceof Error ? err.message : String(err);
|
||||||
|
throw new Error(`DeepSeek LLM failed: ${message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,6 +35,7 @@ import { LangchainEmbedder } from "../embeddings/langchain";
|
|||||||
import { LangchainVectorStore } from "../vector_stores/langchain";
|
import { LangchainVectorStore } from "../vector_stores/langchain";
|
||||||
import { AzureAISearch } from "../vector_stores/azure_ai_search";
|
import { AzureAISearch } from "../vector_stores/azure_ai_search";
|
||||||
import { PGVector } from "../vector_stores/pgvector";
|
import { PGVector } from "../vector_stores/pgvector";
|
||||||
|
import { DeepSeekLLM } from "../llms/deepseek";
|
||||||
|
|
||||||
export class EmbedderFactory {
|
export class EmbedderFactory {
|
||||||
static create(provider: string, config: EmbeddingConfig): Embedder {
|
static create(provider: string, config: EmbeddingConfig): Embedder {
|
||||||
@@ -82,6 +83,8 @@ export class LLMFactory {
|
|||||||
return new MistralLLM(config);
|
return new MistralLLM(config);
|
||||||
case "langchain":
|
case "langchain":
|
||||||
return new LangchainLLM(config);
|
return new LangchainLLM(config);
|
||||||
|
case "deepseek":
|
||||||
|
return new DeepSeekLLM(config);
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unsupported LLM provider: ${provider}`);
|
throw new Error(`Unsupported LLM provider: ${provider}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,124 @@
|
|||||||
|
/// <reference types="jest" />
|
||||||
|
/**
|
||||||
|
* DeepSeek LLM — unit tests (mocked OpenAI).
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { DeepSeekLLM } from "../src/llms/deepseek";
|
||||||
|
|
||||||
|
const mockCreate = jest.fn();
|
||||||
|
|
||||||
|
jest.mock("openai", () => {
|
||||||
|
return jest.fn().mockImplementation(() => ({
|
||||||
|
chat: { completions: { create: mockCreate } },
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("DeepSeekLLM (unit)", () => {
|
||||||
|
beforeEach(() => mockCreate.mockClear());
|
||||||
|
|
||||||
|
it("throws when no API key is provided", () => {
|
||||||
|
const original = process.env.DEEPSEEK_API_KEY;
|
||||||
|
delete process.env.DEEPSEEK_API_KEY;
|
||||||
|
try {
|
||||||
|
expect(() => new DeepSeekLLM({})).toThrow("DeepSeek API key is required");
|
||||||
|
} finally {
|
||||||
|
if (original !== undefined) process.env.DEEPSEEK_API_KEY = original;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("generateResponse() returns a text response", async () => {
|
||||||
|
mockCreate.mockResolvedValueOnce({
|
||||||
|
choices: [
|
||||||
|
{
|
||||||
|
message: {
|
||||||
|
content: "Hello, world!",
|
||||||
|
role: "assistant",
|
||||||
|
tool_calls: null,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const llm = new DeepSeekLLM({ apiKey: "test-key" });
|
||||||
|
const result = await llm.generateResponse([
|
||||||
|
{ role: "user", content: "Hi" },
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(mockCreate).toHaveBeenCalledTimes(1);
|
||||||
|
expect(result).toBe("Hello, world!");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("generateResponse() handles tool calls", async () => {
|
||||||
|
mockCreate.mockResolvedValueOnce({
|
||||||
|
choices: [
|
||||||
|
{
|
||||||
|
message: {
|
||||||
|
content: "",
|
||||||
|
role: "assistant",
|
||||||
|
tool_calls: [
|
||||||
|
{
|
||||||
|
function: {
|
||||||
|
name: "get_weather",
|
||||||
|
arguments: '{"city": "London"}',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const llm = new DeepSeekLLM({ apiKey: "test-key" });
|
||||||
|
const result = await llm.generateResponse(
|
||||||
|
[{ role: "user", content: "What is the weather?" }],
|
||||||
|
undefined,
|
||||||
|
[{ type: "function", function: { name: "get_weather" } }],
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
content: "",
|
||||||
|
role: "assistant",
|
||||||
|
toolCalls: [{ name: "get_weather", arguments: '{"city": "London"}' }],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("generateResponse() wraps API errors with a clear message", async () => {
|
||||||
|
mockCreate.mockRejectedValueOnce(new Error("Connection refused"));
|
||||||
|
|
||||||
|
const llm = new DeepSeekLLM({ apiKey: "test-key" });
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
llm.generateResponse([{ role: "user", content: "Hi" }]),
|
||||||
|
).rejects.toThrow("DeepSeek LLM failed: Connection refused");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("generateChat() returns LLMResponse shape", async () => {
|
||||||
|
mockCreate.mockResolvedValueOnce({
|
||||||
|
choices: [
|
||||||
|
{
|
||||||
|
message: { content: "I can help with that.", role: "assistant" },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const llm = new DeepSeekLLM({ apiKey: "test-key" });
|
||||||
|
const result = await llm.generateChat([
|
||||||
|
{ role: "user", content: "Help me" },
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
content: "I can help with that.",
|
||||||
|
role: "assistant",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("generateChat() wraps API errors with a clear message", async () => {
|
||||||
|
mockCreate.mockRejectedValueOnce(new Error("Timeout"));
|
||||||
|
|
||||||
|
const llm = new DeepSeekLLM({ apiKey: "test-key" });
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
llm.generateChat([{ role: "user", content: "Hi" }]),
|
||||||
|
).rejects.toThrow("DeepSeek LLM failed: Timeout");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -87,6 +87,11 @@ jest.mock("../src/llms/lmstudio", () => ({
|
|||||||
.fn()
|
.fn()
|
||||||
.mockImplementation((config) => ({ type: "lmstudio-llm", config })),
|
.mockImplementation((config) => ({ type: "lmstudio-llm", config })),
|
||||||
}));
|
}));
|
||||||
|
jest.mock("../src/llms/deepseek", () => ({
|
||||||
|
DeepSeekLLM: jest
|
||||||
|
.fn()
|
||||||
|
.mockImplementation((config) => ({ type: "deepseek-llm", config })),
|
||||||
|
}));
|
||||||
|
|
||||||
jest.mock("../src/vector_stores/qdrant", () => ({
|
jest.mock("../src/vector_stores/qdrant", () => ({
|
||||||
Qdrant: jest
|
Qdrant: jest
|
||||||
@@ -200,6 +205,7 @@ describe("LLMFactory", () => {
|
|||||||
["mistral"],
|
["mistral"],
|
||||||
["langchain"],
|
["langchain"],
|
||||||
["lmstudio"],
|
["lmstudio"],
|
||||||
|
["deepseek"],
|
||||||
])("creates LLM for provider '%s'", (provider) => {
|
])("creates LLM for provider '%s'", (provider) => {
|
||||||
expect(() => LLMFactory.create(provider, dummyLLMConfig)).not.toThrow();
|
expect(() => LLMFactory.create(provider, dummyLLMConfig)).not.toThrow();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user