Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ea94ffa63b | ||
|
|
a3d1a1068b | ||
|
|
227c02da9f | ||
|
|
d64d7b525b |
@@ -2,8 +2,6 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
...require("./jest.config"),
|
...require("./jest.config"),
|
||||||
testMatch: ["**/integration/**/*.test.ts"],
|
testMatch: ["**/integration/**/*.test.ts"],
|
||||||
globalSetup: "<rootDir>/src/client/tests/integration/global-setup.ts",
|
|
||||||
globalTeardown: "<rootDir>/src/client/tests/integration/global-teardown.ts",
|
|
||||||
// Run integration tests serially to avoid rate limiting and race conditions
|
// Run integration tests serially to avoid rate limiting and race conditions
|
||||||
maxWorkers: 1,
|
maxWorkers: 1,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,40 +0,0 @@
|
|||||||
/**
|
|
||||||
* Jest global setup for integration tests.
|
|
||||||
*
|
|
||||||
* Runs a full project cleanup before any integration test starts,
|
|
||||||
* then waits 10 seconds for the async cleanup to propagate.
|
|
||||||
*/
|
|
||||||
import { MemoryClient } from "../../mem0";
|
|
||||||
|
|
||||||
export default async function globalSetup() {
|
|
||||||
const apiKey = process.env.MEM0_API_KEY;
|
|
||||||
if (!apiKey) return; // skip if no key — tests will be skipped too
|
|
||||||
|
|
||||||
const client = new MemoryClient({ apiKey });
|
|
||||||
await client.ping();
|
|
||||||
|
|
||||||
console.log("[integration] Running pre-test cleanup...");
|
|
||||||
|
|
||||||
// Full project wipe — all four filters set explicitly
|
|
||||||
try {
|
|
||||||
await client.deleteAll({
|
|
||||||
userId: "*",
|
|
||||||
agentId: "*",
|
|
||||||
appId: "*",
|
|
||||||
runId: "*",
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// ignore — may 404 if no data exists
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
await client.deleteUsers();
|
|
||||||
} catch {
|
|
||||||
// ignore — may throw "No entities to delete"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait 10 seconds for async cleanup to propagate
|
|
||||||
console.log("[integration] Waiting 10s for cleanup to propagate...");
|
|
||||||
await new Promise((r) => setTimeout(r, 10_000));
|
|
||||||
console.log("[integration] Pre-test cleanup done.");
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
/**
|
|
||||||
* Jest global teardown for integration tests.
|
|
||||||
*
|
|
||||||
* Runs a full project cleanup after all integration tests complete
|
|
||||||
* so no test data is left behind.
|
|
||||||
*/
|
|
||||||
import { MemoryClient } from "../../mem0";
|
|
||||||
|
|
||||||
export default async function globalTeardown() {
|
|
||||||
const apiKey = process.env.MEM0_API_KEY;
|
|
||||||
if (!apiKey) return;
|
|
||||||
|
|
||||||
const client = new MemoryClient({ apiKey });
|
|
||||||
await client.ping();
|
|
||||||
|
|
||||||
console.log("[integration] Running post-test cleanup...");
|
|
||||||
|
|
||||||
try {
|
|
||||||
await client.deleteAll({
|
|
||||||
userId: "*",
|
|
||||||
agentId: "*",
|
|
||||||
appId: "*",
|
|
||||||
runId: "*",
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
await client.deleteUsers();
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("[integration] Post-test cleanup done.");
|
|
||||||
}
|
|
||||||
@@ -52,13 +52,17 @@ export async function withRetry<T>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Poll getAll until memories appear for a user.
|
* Poll getAll until a user's memories reach minCount and the ID set
|
||||||
* The Mem0 API processes memories asynchronously — after add()
|
* stops changing between polls.
|
||||||
* we need to wait for them to be available.
|
*
|
||||||
|
* The Mem0 API processes memories asynchronously, and consolidating a
|
||||||
|
* new fact replaces a memory (delete + add) instead of editing it in
|
||||||
|
* place. Returning on the first non-empty read hands callers IDs the
|
||||||
|
* pipeline then invalidates, so compare IDs rather than length: a
|
||||||
|
* replacement leaves the count unchanged.
|
||||||
*
|
*
|
||||||
* Polls every 15 seconds with a maximum of 4 retries to avoid
|
* Polls every 15 seconds with a maximum of 4 retries to avoid
|
||||||
* hitting rate limits. Throws if results aren't available after
|
* hitting rate limits. Throws if minCount is never reached.
|
||||||
* all retries.
|
|
||||||
*/
|
*/
|
||||||
export async function waitForMemories(
|
export async function waitForMemories(
|
||||||
client: MemoryClient,
|
client: MemoryClient,
|
||||||
@@ -66,18 +70,32 @@ export async function waitForMemories(
|
|||||||
minCount: number,
|
minCount: number,
|
||||||
maxRetries = 4,
|
maxRetries = 4,
|
||||||
): Promise<Memory[]> {
|
): Promise<Memory[]> {
|
||||||
|
let memories: Memory[] = [];
|
||||||
|
let previousIds = "";
|
||||||
|
|
||||||
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
||||||
const response = await withRetry(() =>
|
const response = await withRetry(() =>
|
||||||
client.getAll({ filters: { user_id: userId } }),
|
client.getAll({ filters: { user_id: userId } }),
|
||||||
);
|
);
|
||||||
const memories = response.results ?? [];
|
memories = response.results ?? [];
|
||||||
if (memories.length >= minCount) {
|
|
||||||
|
const ids = memories
|
||||||
|
.map((m) => m.id)
|
||||||
|
.sort()
|
||||||
|
.join(",");
|
||||||
|
if (memories.length >= minCount && ids === previousIds) {
|
||||||
return memories;
|
return memories;
|
||||||
}
|
}
|
||||||
|
previousIds = ids;
|
||||||
|
|
||||||
if (attempt < maxRetries) {
|
if (attempt < maxRetries) {
|
||||||
await new Promise((r) => setTimeout(r, 15_000));
|
await new Promise((r) => setTimeout(r, 15_000));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (memories.length >= minCount) {
|
||||||
|
return memories;
|
||||||
|
}
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`waitForMemories: expected at least ${minCount} memories for user "${userId}" but did not get them after ${maxRetries} attempts`,
|
`waitForMemories: expected at least ${minCount} memories for user "${userId}" but did not get them after ${maxRetries} attempts`,
|
||||||
);
|
);
|
||||||
@@ -202,32 +220,3 @@ export async function cleanupTestUser(
|
|||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Full project wipe — deletes all memories and all entities.
|
|
||||||
* Equivalent to Python SDK's:
|
|
||||||
* client.delete_all(user_id="*", agent_id="*", app_id="*", run_id="*")
|
|
||||||
*
|
|
||||||
* Used as cleanup before and after integration test runs so tests
|
|
||||||
* start from a clean slate and don't leave data behind.
|
|
||||||
*/
|
|
||||||
export async function fullProjectCleanup(client: MemoryClient): Promise<void> {
|
|
||||||
// Delete all memories — all four filters set explicitly
|
|
||||||
try {
|
|
||||||
await client.deleteAll({
|
|
||||||
userId: "*",
|
|
||||||
agentId: "*",
|
|
||||||
appId: "*",
|
|
||||||
runId: "*",
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// ignore — may 404 if no data exists
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete all entities (users, agents, apps, runs)
|
|
||||||
try {
|
|
||||||
await client.deleteUsers();
|
|
||||||
} catch {
|
|
||||||
// ignore — may throw "No entities to delete"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -100,6 +100,7 @@ describeIntegration("MemoryClient Integration — Users & Project", () => {
|
|||||||
const hookName = `test-hook-${randomUUID().slice(0, 8)}`;
|
const hookName = `test-hook-${randomUUID().slice(0, 8)}`;
|
||||||
const hookUrl = `https://example.com/webhook/${randomUUID().slice(0, 8)}`;
|
const hookUrl = `https://example.com/webhook/${randomUUID().slice(0, 8)}`;
|
||||||
const updatedName = `updated-hook-${randomUUID().slice(0, 8)}`;
|
const updatedName = `updated-hook-${randomUUID().slice(0, 8)}`;
|
||||||
|
const updatedUrl = `https://example.com/updated/${randomUUID().slice(0, 8)}`;
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
if (createdWebhookId) {
|
if (createdWebhookId) {
|
||||||
@@ -168,7 +169,7 @@ describeIntegration("MemoryClient Integration — Users & Project", () => {
|
|||||||
client.updateWebhook({
|
client.updateWebhook({
|
||||||
webhookId: createdWebhookId,
|
webhookId: createdWebhookId,
|
||||||
name: updatedName,
|
name: updatedName,
|
||||||
url: "https://example.com/updated",
|
url: updatedUrl,
|
||||||
eventTypes: [WebhookEvent.MEMORY_DELETED],
|
eventTypes: [WebhookEvent.MEMORY_DELETED],
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
import { waitForMemories } from "./integration/helpers";
|
||||||
|
|
||||||
|
const clientReturning = (polls: Array<Array<{ id: string }>>) => {
|
||||||
|
let call = 0;
|
||||||
|
return {
|
||||||
|
getAll: jest.fn(async () => ({
|
||||||
|
results: polls[Math.min(call++, polls.length - 1)],
|
||||||
|
})),
|
||||||
|
} as any;
|
||||||
|
};
|
||||||
|
|
||||||
|
const settle = async <T>(pending: Promise<T>): Promise<T> => {
|
||||||
|
pending.catch(() => {});
|
||||||
|
for (let i = 0; i < 4; i++) {
|
||||||
|
await jest.advanceTimersByTimeAsync(15_000);
|
||||||
|
}
|
||||||
|
return pending;
|
||||||
|
};
|
||||||
|
|
||||||
|
describe("waitForMemories", () => {
|
||||||
|
beforeEach(() => jest.useFakeTimers());
|
||||||
|
afterEach(() => jest.useRealTimers());
|
||||||
|
|
||||||
|
test("waits for the id set to stop changing", async () => {
|
||||||
|
const client = clientReturning([
|
||||||
|
[{ id: "a" }],
|
||||||
|
[{ id: "a" }, { id: "b" }],
|
||||||
|
[{ id: "a" }, { id: "b" }],
|
||||||
|
]);
|
||||||
|
|
||||||
|
const memories = await settle(waitForMemories(client, "user", 1));
|
||||||
|
|
||||||
|
expect(memories.map((m) => m.id)).toEqual(["a", "b"]);
|
||||||
|
expect(client.getAll).toHaveBeenCalledTimes(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("keeps polling when consolidation replaces a memory at a constant count", async () => {
|
||||||
|
const client = clientReturning([
|
||||||
|
[{ id: "a" }, { id: "b" }],
|
||||||
|
[{ id: "a" }, { id: "c" }],
|
||||||
|
[{ id: "a" }, { id: "c" }],
|
||||||
|
]);
|
||||||
|
|
||||||
|
const memories = await settle(waitForMemories(client, "user", 1));
|
||||||
|
|
||||||
|
expect(memories.map((m) => m.id)).toEqual(["a", "c"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns the last read when the set never settles but minCount is met", async () => {
|
||||||
|
const client = clientReturning([
|
||||||
|
[{ id: "a" }],
|
||||||
|
[{ id: "b" }],
|
||||||
|
[{ id: "c" }],
|
||||||
|
[{ id: "d" }],
|
||||||
|
]);
|
||||||
|
|
||||||
|
const memories = await settle(waitForMemories(client, "user", 1));
|
||||||
|
|
||||||
|
expect(memories.map((m) => m.id)).toEqual(["d"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("throws when minCount is never reached", async () => {
|
||||||
|
const client = clientReturning([[]]);
|
||||||
|
|
||||||
|
await expect(settle(waitForMemories(client, "user", 1))).rejects.toThrow(
|
||||||
|
/expected at least 1 memories/,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -12,11 +12,7 @@ export class LMStudioEmbedder implements Embedder {
|
|||||||
private model: string;
|
private model: string;
|
||||||
|
|
||||||
constructor(config: EmbeddingConfig) {
|
constructor(config: EmbeddingConfig) {
|
||||||
const baseURL =
|
const baseURL = config.baseURL ?? config.url ?? DEFAULT_BASE_URL;
|
||||||
config.baseURL ??
|
|
||||||
config.url ??
|
|
||||||
process.env.LMSTUDIO_BASE_URL ??
|
|
||||||
DEFAULT_BASE_URL;
|
|
||||||
const apiKey = config.apiKey || DEFAULT_LMSTUDIO_API_KEY;
|
const apiKey = config.apiKey || DEFAULT_LMSTUDIO_API_KEY;
|
||||||
this.openai = new OpenAI({ apiKey, baseURL: String(baseURL) });
|
this.openai = new OpenAI({ apiKey, baseURL: String(baseURL) });
|
||||||
this.model = config.model || DEFAULT_MODEL;
|
this.model = config.model || DEFAULT_MODEL;
|
||||||
|
|||||||
@@ -12,8 +12,7 @@ export class LMStudioLLM extends OpenAILLM {
|
|||||||
super({
|
super({
|
||||||
...config,
|
...config,
|
||||||
apiKey: config.apiKey || DEFAULT_LMSTUDIO_API_KEY,
|
apiKey: config.apiKey || DEFAULT_LMSTUDIO_API_KEY,
|
||||||
baseURL:
|
baseURL: config.baseURL ?? DEFAULT_BASE_URL,
|
||||||
config.baseURL ?? process.env.LMSTUDIO_BASE_URL ?? DEFAULT_BASE_URL,
|
|
||||||
model: config.model || DEFAULT_MODEL,
|
model: config.model || DEFAULT_MODEL,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,64 +10,14 @@ const mockCreate = jest.fn().mockResolvedValue({
|
|||||||
data: [{ embedding: mockEmbedding }],
|
data: [{ embedding: mockEmbedding }],
|
||||||
});
|
});
|
||||||
|
|
||||||
let capturedConstructorArgs: any;
|
|
||||||
|
|
||||||
jest.mock("openai", () => {
|
jest.mock("openai", () => {
|
||||||
return jest.fn().mockImplementation((args: any) => {
|
return jest.fn().mockImplementation(() => ({
|
||||||
capturedConstructorArgs = args;
|
embeddings: { create: mockCreate },
|
||||||
return {
|
}));
|
||||||
embeddings: { create: mockCreate },
|
|
||||||
};
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("LMStudioEmbedder (unit)", () => {
|
describe("LMStudioEmbedder (unit)", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => mockCreate.mockClear());
|
||||||
mockCreate.mockClear();
|
|
||||||
capturedConstructorArgs = undefined;
|
|
||||||
delete process.env.LMSTUDIO_BASE_URL;
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
delete process.env.LMSTUDIO_BASE_URL;
|
|
||||||
});
|
|
||||||
|
|
||||||
it("uses the default base URL when neither config nor env var is set", async () => {
|
|
||||||
const embedder = new LMStudioEmbedder({ model: "test-model" });
|
|
||||||
|
|
||||||
await embedder.embed("hello");
|
|
||||||
|
|
||||||
expect(capturedConstructorArgs).toMatchObject({
|
|
||||||
baseURL: "http://localhost:1234/v1",
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("uses LMSTUDIO_BASE_URL when config does not provide a base URL", async () => {
|
|
||||||
process.env.LMSTUDIO_BASE_URL = "http://example.test:5678/v1";
|
|
||||||
|
|
||||||
const embedder = new LMStudioEmbedder({ model: "test-model" });
|
|
||||||
|
|
||||||
await embedder.embed("hello");
|
|
||||||
|
|
||||||
expect(capturedConstructorArgs).toMatchObject({
|
|
||||||
baseURL: "http://example.test:5678/v1",
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("prefers an explicit config baseURL over LMSTUDIO_BASE_URL", async () => {
|
|
||||||
process.env.LMSTUDIO_BASE_URL = "http://example.test:5678/v1";
|
|
||||||
|
|
||||||
const embedder = new LMStudioEmbedder({
|
|
||||||
model: "test-model",
|
|
||||||
baseURL: "http://config.test:9999/v1",
|
|
||||||
});
|
|
||||||
|
|
||||||
await embedder.embed("hello");
|
|
||||||
|
|
||||||
expect(capturedConstructorArgs).toMatchObject({
|
|
||||||
baseURL: "http://config.test:9999/v1",
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("embed() calls OpenAI with encoding_format float and returns vector", async () => {
|
it("embed() calls OpenAI with encoding_format float and returns vector", async () => {
|
||||||
const embedder = new LMStudioEmbedder({
|
const embedder = new LMStudioEmbedder({
|
||||||
|
|||||||
@@ -5,56 +5,16 @@
|
|||||||
|
|
||||||
import { LMStudioLLM } from "../src/llms/lmstudio";
|
import { LMStudioLLM } from "../src/llms/lmstudio";
|
||||||
|
|
||||||
let capturedConstructorArgs: any;
|
|
||||||
const mockCreate = jest.fn();
|
const mockCreate = jest.fn();
|
||||||
|
|
||||||
jest.mock("openai", () => {
|
jest.mock("openai", () => {
|
||||||
return jest.fn().mockImplementation((args: any) => {
|
return jest.fn().mockImplementation(() => ({
|
||||||
capturedConstructorArgs = args;
|
chat: { completions: { create: mockCreate } },
|
||||||
return {
|
}));
|
||||||
chat: { completions: { create: mockCreate } },
|
|
||||||
};
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("LMStudioLLM (unit)", () => {
|
describe("LMStudioLLM (unit)", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => mockCreate.mockClear());
|
||||||
mockCreate.mockClear();
|
|
||||||
capturedConstructorArgs = undefined;
|
|
||||||
delete process.env.LMSTUDIO_BASE_URL;
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
delete process.env.LMSTUDIO_BASE_URL;
|
|
||||||
});
|
|
||||||
|
|
||||||
it("uses the default base URL when neither config nor env var is set", () => {
|
|
||||||
new LMStudioLLM({});
|
|
||||||
|
|
||||||
expect(capturedConstructorArgs).toMatchObject({
|
|
||||||
baseURL: "http://localhost:1234/v1",
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("uses LMSTUDIO_BASE_URL when config does not provide a base URL", () => {
|
|
||||||
process.env.LMSTUDIO_BASE_URL = "http://example.test:5678/v1";
|
|
||||||
|
|
||||||
new LMStudioLLM({});
|
|
||||||
|
|
||||||
expect(capturedConstructorArgs).toMatchObject({
|
|
||||||
baseURL: "http://example.test:5678/v1",
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("prefers an explicit config baseURL over LMSTUDIO_BASE_URL", () => {
|
|
||||||
process.env.LMSTUDIO_BASE_URL = "http://example.test:5678/v1";
|
|
||||||
|
|
||||||
new LMStudioLLM({ baseURL: "http://config.test:9999/v1" });
|
|
||||||
|
|
||||||
expect(capturedConstructorArgs).toMatchObject({
|
|
||||||
baseURL: "http://config.test:9999/v1",
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("generateResponse() returns a text response", async () => {
|
it("generateResponse() returns a text response", async () => {
|
||||||
mockCreate.mockResolvedValueOnce({
|
mockCreate.mockResolvedValueOnce({
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ class BaseEmbedderConfig(ABC):
|
|||||||
# Gemini specific
|
# Gemini specific
|
||||||
output_dimensionality: Optional[str] = None,
|
output_dimensionality: Optional[str] = None,
|
||||||
# LM Studio specific
|
# LM Studio specific
|
||||||
lmstudio_base_url: Optional[str] = None,
|
lmstudio_base_url: Optional[str] = "http://localhost:1234/v1",
|
||||||
# AWS Bedrock specific
|
# AWS Bedrock specific
|
||||||
aws_access_key_id: Optional[str] = None,
|
aws_access_key_id: Optional[str] = None,
|
||||||
aws_secret_access_key: Optional[str] = None,
|
aws_secret_access_key: Optional[str] = None,
|
||||||
@@ -70,8 +70,7 @@ class BaseEmbedderConfig(ABC):
|
|||||||
:type memory_update_embedding_type: Optional[str], optional
|
:type memory_update_embedding_type: Optional[str], optional
|
||||||
:param memory_search_embedding_type: The type of embedding to use for the search memory action, defaults to None
|
:param memory_search_embedding_type: The type of embedding to use for the search memory action, defaults to None
|
||||||
:type memory_search_embedding_type: Optional[str], optional
|
:type memory_search_embedding_type: Optional[str], optional
|
||||||
:param lmstudio_base_url: LM Studio base URL to be use, defaults to None
|
:param lmstudio_base_url: LM Studio base URL to be use, defaults to "http://localhost:1234/v1"
|
||||||
(resolved by the provider from LMSTUDIO_BASE_URL, then "http://localhost:1234/v1")
|
|
||||||
:type lmstudio_base_url: Optional[str], optional
|
:type lmstudio_base_url: Optional[str], optional
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ class LMStudioConfig(BaseLlmConfig):
|
|||||||
vision_details: Vision detail level, defaults to "auto"
|
vision_details: Vision detail level, defaults to "auto"
|
||||||
http_client_proxies: HTTP client proxy settings, defaults to None
|
http_client_proxies: HTTP client proxy settings, defaults to None
|
||||||
lmstudio_base_url: LM Studio base URL, defaults to None
|
lmstudio_base_url: LM Studio base URL, defaults to None
|
||||||
(resolved by the provider from LMSTUDIO_BASE_URL, then "http://localhost:1234/v1")
|
|
||||||
lmstudio_response_format: LM Studio response format, defaults to None
|
lmstudio_response_format: LM Studio response format, defaults to None
|
||||||
"""
|
"""
|
||||||
# Initialize base parameters
|
# Initialize base parameters
|
||||||
@@ -56,5 +55,5 @@ class LMStudioConfig(BaseLlmConfig):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# LM Studio-specific parameters
|
# LM Studio-specific parameters
|
||||||
self.lmstudio_base_url = lmstudio_base_url
|
self.lmstudio_base_url = lmstudio_base_url or "http://localhost:1234/v1"
|
||||||
self.lmstudio_response_format = lmstudio_response_format
|
self.lmstudio_response_format = lmstudio_response_format
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import os
|
|
||||||
from typing import Literal, Optional
|
from typing import Literal, Optional
|
||||||
|
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
@@ -14,9 +13,6 @@ class LMStudioEmbedding(EmbeddingBase):
|
|||||||
self.config.model = self.config.model or "nomic-ai/nomic-embed-text-v1.5-GGUF/nomic-embed-text-v1.5.f16.gguf"
|
self.config.model = self.config.model or "nomic-ai/nomic-embed-text-v1.5-GGUF/nomic-embed-text-v1.5.f16.gguf"
|
||||||
self.config.embedding_dims = self.config.embedding_dims or 1536
|
self.config.embedding_dims = self.config.embedding_dims or 1536
|
||||||
self.config.api_key = self.config.api_key or "lm-studio"
|
self.config.api_key = self.config.api_key or "lm-studio"
|
||||||
self.config.lmstudio_base_url = (
|
|
||||||
self.config.lmstudio_base_url or os.getenv("LMSTUDIO_BASE_URL") or "http://localhost:1234/v1"
|
|
||||||
)
|
|
||||||
|
|
||||||
self.client = OpenAI(base_url=self.config.lmstudio_base_url, api_key=self.config.api_key)
|
self.client = OpenAI(base_url=self.config.lmstudio_base_url, api_key=self.config.api_key)
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
|
||||||
from typing import Dict, List, Optional, Union
|
from typing import Dict, List, Optional, Union
|
||||||
|
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
@@ -38,9 +37,6 @@ class LMStudioLLM(LLMBase):
|
|||||||
or "lmstudio-community/Meta-Llama-3.1-70B-Instruct-GGUF/Meta-Llama-3.1-70B-Instruct-IQ2_M.gguf"
|
or "lmstudio-community/Meta-Llama-3.1-70B-Instruct-GGUF/Meta-Llama-3.1-70B-Instruct-IQ2_M.gguf"
|
||||||
)
|
)
|
||||||
self.config.api_key = self.config.api_key or "lm-studio"
|
self.config.api_key = self.config.api_key or "lm-studio"
|
||||||
self.config.lmstudio_base_url = (
|
|
||||||
self.config.lmstudio_base_url or os.getenv("LMSTUDIO_BASE_URL") or "http://localhost:1234/v1"
|
|
||||||
)
|
|
||||||
|
|
||||||
self.client = OpenAI(base_url=self.config.lmstudio_base_url, api_key=self.config.api_key)
|
self.client = OpenAI(base_url=self.config.lmstudio_base_url, api_key=self.config.api_key)
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from mem0.configs.embeddings.base import BaseEmbedderConfig
|
|||||||
from mem0.embeddings.lmstudio import LMStudioEmbedding
|
from mem0.embeddings.lmstudio import LMStudioEmbedding
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_lm_studio_client():
|
def mock_lm_studio_client():
|
||||||
with patch("mem0.embeddings.lmstudio.OpenAI") as mock_openai:
|
with patch("mem0.embeddings.lmstudio.OpenAI") as mock_openai:
|
||||||
@@ -79,31 +80,3 @@ def test_embed_batch_count_mismatch_raises(mock_lm_studio_client):
|
|||||||
|
|
||||||
with pytest.raises(ValueError, match="returned 1 embeddings for 2 texts"):
|
with pytest.raises(ValueError, match="returned 1 embeddings for 2 texts"):
|
||||||
embedder.embed_batch(["first text", "second text"])
|
embedder.embed_batch(["first text", "second text"])
|
||||||
|
|
||||||
|
|
||||||
def test_base_url_honors_lmstudio_base_url_env(mock_lm_studio_client, monkeypatch):
|
|
||||||
monkeypatch.setenv("LMSTUDIO_BASE_URL", "http://lmstudio.internal:9000/v1")
|
|
||||||
|
|
||||||
with patch("mem0.embeddings.lmstudio.OpenAI") as mock_openai:
|
|
||||||
LMStudioEmbedding(BaseEmbedderConfig())
|
|
||||||
|
|
||||||
assert mock_openai.call_args.kwargs["base_url"] == "http://lmstudio.internal:9000/v1"
|
|
||||||
|
|
||||||
|
|
||||||
def test_base_url_prefers_explicit_config_over_env(mock_lm_studio_client, monkeypatch):
|
|
||||||
monkeypatch.setenv("LMSTUDIO_BASE_URL", "http://lmstudio.internal:9000/v1")
|
|
||||||
|
|
||||||
with patch("mem0.embeddings.lmstudio.OpenAI") as mock_openai:
|
|
||||||
LMStudioEmbedding(BaseEmbedderConfig(lmstudio_base_url="http://explicit:1234/v1"))
|
|
||||||
|
|
||||||
assert mock_openai.call_args.kwargs["base_url"] == "http://explicit:1234/v1"
|
|
||||||
|
|
||||||
|
|
||||||
def test_base_url_falls_back_to_localhost_default(mock_lm_studio_client, monkeypatch):
|
|
||||||
monkeypatch.delenv("LMSTUDIO_BASE_URL", raising=False)
|
|
||||||
|
|
||||||
with patch("mem0.embeddings.lmstudio.OpenAI") as mock_openai:
|
|
||||||
embedder = LMStudioEmbedding(BaseEmbedderConfig())
|
|
||||||
|
|
||||||
assert mock_openai.call_args.kwargs["base_url"] == "http://localhost:1234/v1"
|
|
||||||
assert embedder.config.lmstudio_base_url == "http://localhost:1234/v1"
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ from unittest.mock import Mock, patch
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from mem0.configs.llms.base import BaseLlmConfig
|
|
||||||
from mem0.configs.llms.lmstudio import LMStudioConfig
|
from mem0.configs.llms.lmstudio import LMStudioConfig
|
||||||
from mem0.llms.lmstudio import LMStudioLLM
|
from mem0.llms.lmstudio import LMStudioLLM
|
||||||
|
|
||||||
@@ -71,40 +70,3 @@ def test_generate_response_specifying_response_format(mock_lm_studio_client):
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert response == "I'm doing well, thank you for asking!"
|
assert response == "I'm doing well, thank you for asking!"
|
||||||
|
|
||||||
|
|
||||||
def test_base_url_honors_lmstudio_base_url_env(monkeypatch):
|
|
||||||
monkeypatch.setenv("LMSTUDIO_BASE_URL", "http://lmstudio.internal:9000/v1")
|
|
||||||
|
|
||||||
with patch("mem0.llms.lmstudio.OpenAI") as mock_openai:
|
|
||||||
LMStudioLLM(LMStudioConfig())
|
|
||||||
|
|
||||||
assert mock_openai.call_args.kwargs["base_url"] == "http://lmstudio.internal:9000/v1"
|
|
||||||
|
|
||||||
|
|
||||||
def test_base_url_prefers_explicit_config_over_env(monkeypatch):
|
|
||||||
monkeypatch.setenv("LMSTUDIO_BASE_URL", "http://lmstudio.internal:9000/v1")
|
|
||||||
|
|
||||||
with patch("mem0.llms.lmstudio.OpenAI") as mock_openai:
|
|
||||||
LMStudioLLM(LMStudioConfig(lmstudio_base_url="http://explicit:1234/v1"))
|
|
||||||
|
|
||||||
assert mock_openai.call_args.kwargs["base_url"] == "http://explicit:1234/v1"
|
|
||||||
|
|
||||||
|
|
||||||
def test_base_url_falls_back_to_localhost_default(monkeypatch):
|
|
||||||
monkeypatch.delenv("LMSTUDIO_BASE_URL", raising=False)
|
|
||||||
|
|
||||||
with patch("mem0.llms.lmstudio.OpenAI") as mock_openai:
|
|
||||||
llm = LMStudioLLM(LMStudioConfig())
|
|
||||||
|
|
||||||
assert mock_openai.call_args.kwargs["base_url"] == "http://localhost:1234/v1"
|
|
||||||
assert llm.config.lmstudio_base_url == "http://localhost:1234/v1"
|
|
||||||
|
|
||||||
|
|
||||||
def test_base_url_honors_env_via_base_llm_config(monkeypatch):
|
|
||||||
monkeypatch.setenv("LMSTUDIO_BASE_URL", "http://lmstudio.internal:9000/v1")
|
|
||||||
|
|
||||||
with patch("mem0.llms.lmstudio.OpenAI") as mock_openai:
|
|
||||||
LMStudioLLM(BaseLlmConfig(model="some-model"))
|
|
||||||
|
|
||||||
assert mock_openai.call_args.kwargs["base_url"] == "http://lmstudio.internal:9000/v1"
|
|
||||||
|
|||||||
Reference in New Issue
Block a user