diff --git a/docs/components/llms/models/together.mdx b/docs/components/llms/models/together.mdx index 2f4148c06..20aaf739f 100644 --- a/docs/components/llms/models/together.mdx +++ b/docs/components/llms/models/together.mdx @@ -1,13 +1,15 @@ --- title: Together -description: "Configure Together AI as an LLM provider in Mem0 with API key setup and Mixtral model configuration." +description: "Configure Together AI as an LLM provider in Mem0 with API key setup and optional custom endpoint configuration." --- To use Together LLM models, you have to set the `TOGETHER_API_KEY` environment variable. You can obtain the Together API key from their [Account settings page](https://api.together.ai/settings/projects/~current/api-keys). +In the TypeScript SDK, you can optionally set `TOGETHER_API_BASE` or pass `baseURL` in the config (defaults to `https://api.together.ai/v1`). ## Usage -```python + +```python Python import os from mem0 import Memory @@ -18,7 +20,7 @@ config = { "llm": { "provider": "together", "config": { - "model": "mistralai/Mixtral-8x7B-Instruct-v0.1", + "model": "MiniMaxAI/MiniMax-M3", "temperature": 0.2, "max_tokens": 2000, } @@ -29,12 +31,68 @@ m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, - {"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."}, + {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"}) ``` +```typescript TypeScript +import { Memory } from 'mem0ai/oss'; + +const config = { + llm: { + provider: 'together', + config: { + apiKey: process.env.TOGETHER_API_KEY || '', + model: 'MiniMaxAI/MiniMax-M3', + temperature: 0.2, + maxTokens: 2000, + }, + }, +}; + +const memory = new Memory(config); +const messages = [ + { role: "user", content: "I'm planning to watch a movie tonight. Any recommendations?" }, + { role: "assistant", content: "How about thriller movies? They can be quite engaging." }, + { role: "user", content: "I'm not a big fan of thriller movies but I love sci-fi movies." }, + { role: "assistant", content: "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future." }, +]; +await memory.add(messages, { userId: 'alice', metadata: { category: 'movies' } }); +``` + + +You can also configure the API base URL in the config: + + +```python Python +config = { + "llm": { + "provider": "together", + "config": { + "model": "MiniMaxAI/MiniMax-M3", + "api_key": "your-api-key" + } + } +} +``` + +```typescript TypeScript +const config = { + llm: { + provider: "together", + config: { + model: "MiniMaxAI/MiniMax-M3", + baseURL: "https://api.together.ai/v1", + apiKey: "your-api-key", + }, + }, +}; +``` + + + ## Config All available parameters for the `together` config are present in [Master List of All Params in Config](../config). diff --git a/mem0-ts/src/oss/src/llms/together.ts b/mem0-ts/src/oss/src/llms/together.ts new file mode 100644 index 000000000..d06514496 --- /dev/null +++ b/mem0-ts/src/oss/src/llms/together.ts @@ -0,0 +1,43 @@ +import { OpenAILLM } from "./openai"; +import { LLMConfig, Message } from "../types"; +import { LLMResponse } from "./base"; + +export class TogetherLLM extends OpenAILLM { + constructor(config: LLMConfig) { + const apiKey = config.apiKey || process.env.TOGETHER_API_KEY; + if (!apiKey) { + throw new Error("Together API key is required"); + } + super({ + ...config, + apiKey, + baseURL: + config.baseURL || + process.env.TOGETHER_API_BASE || + "https://api.together.ai/v1", + model: config.model || "MiniMaxAI/MiniMax-M3", + }); + } + + async generateResponse( + messages: Message[], + responseFormat?: { type: string }, + tools?: any[], + ): Promise { + try { + return await super.generateResponse(messages, responseFormat, tools); + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + throw new Error(`Together LLM failed: ${message}`); + } + } + + async generateChat(messages: Message[]): Promise { + try { + return await super.generateChat(messages); + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + throw new Error(`Together LLM failed: ${message}`); + } + } +} diff --git a/mem0-ts/src/oss/src/utils/factory.ts b/mem0-ts/src/oss/src/utils/factory.ts index 66998aa28..908067b38 100644 --- a/mem0-ts/src/oss/src/utils/factory.ts +++ b/mem0-ts/src/oss/src/utils/factory.ts @@ -27,6 +27,7 @@ import { DeepSeekLLM } from "../llms/deepseek"; import { XAILLM } from "../llms/xai"; import { LiteLLM } from "../llms/litellm"; import { MiniMaxLLM } from "../llms/minimax"; +import { TogetherLLM } from "../llms/together"; import { VllmLLM } from "../llms/vllm"; import { SupabaseDB } from "../vector_stores/supabase"; import { SQLiteManager } from "../storage/SQLiteManager"; @@ -112,6 +113,8 @@ export class LLMFactory { return new LiteLLM(config); case "minimax": return new MiniMaxLLM(config); + case "together": + return new TogetherLLM(config); case "vllm": return new VllmLLM(config); default: diff --git a/mem0-ts/src/oss/tests/factory.unit.test.ts b/mem0-ts/src/oss/tests/factory.unit.test.ts index 490ca43f9..92ca560fb 100644 --- a/mem0-ts/src/oss/tests/factory.unit.test.ts +++ b/mem0-ts/src/oss/tests/factory.unit.test.ts @@ -117,6 +117,11 @@ jest.mock("../src/llms/minimax", () => ({ .fn() .mockImplementation((config) => ({ type: "minimax-llm", config })), })); +jest.mock("../src/llms/together", () => ({ + TogetherLLM: jest + .fn() + .mockImplementation((config) => ({ type: "together-llm", config })), +})); jest.mock("../src/llms/vllm", () => ({ VllmLLM: jest .fn() @@ -266,6 +271,7 @@ describe("LLMFactory", () => { ["xai"], ["litellm"], ["minimax"], + ["together"], ["vllm"], ])("creates LLM for provider '%s'", (provider) => { expect(() => LLMFactory.create(provider, dummyLLMConfig)).not.toThrow(); @@ -297,6 +303,7 @@ describe("VectorStoreFactory", () => { VectorStoreFactory.create("memory", { collectionName: "test", dimension: 4, + dbPath: ":memory:", }), ).not.toThrow(); }); diff --git a/mem0-ts/src/oss/tests/together.test.ts b/mem0-ts/src/oss/tests/together.test.ts new file mode 100644 index 000000000..7fdb90bd9 --- /dev/null +++ b/mem0-ts/src/oss/tests/together.test.ts @@ -0,0 +1,114 @@ +/// +/** + * Together LLM - unit tests (mocked OpenAI). + */ + +let capturedConstructorArgs: any; +const mockCreate = jest.fn(); + +jest.mock("openai", () => { + return jest.fn().mockImplementation((args: any) => { + capturedConstructorArgs = args; + return { + chat: { completions: { create: mockCreate } }, + }; + }); +}); + +import { TogetherLLM } from "../src/llms/together"; + +describe("TogetherLLM (unit)", () => { + beforeEach(() => { + capturedConstructorArgs = undefined; + mockCreate.mockClear(); + delete process.env.TOGETHER_API_KEY; + delete process.env.TOGETHER_API_BASE; + }); + + it("throws when no API key is provided", () => { + expect(() => new TogetherLLM({})).toThrow("Together API key is required"); + }); + + it("uses Together defaults with an explicit API key", () => { + new TogetherLLM({ apiKey: "test-key" }); + + expect(capturedConstructorArgs).toMatchObject({ + apiKey: "test-key", + baseURL: "https://api.together.ai/v1", + }); + }); + + it("uses environment variables when config does not provide credentials", () => { + process.env.TOGETHER_API_KEY = "env-key"; + process.env.TOGETHER_API_BASE = "https://example.together.test/v1"; + + new TogetherLLM({}); + + expect(capturedConstructorArgs).toMatchObject({ + apiKey: "env-key", + baseURL: "https://example.together.test/v1", + }); + }); + + it("config values take precedence over environment variables", () => { + process.env.TOGETHER_API_KEY = "env-key"; + process.env.TOGETHER_API_BASE = "https://env.together.test/v1"; + + new TogetherLLM({ + apiKey: "config-key", + baseURL: "https://config.together.test/v1", + model: "meta-llama/Llama-3.3-70B-Instruct-Turbo", + }); + + expect(capturedConstructorArgs).toMatchObject({ + apiKey: "config-key", + baseURL: "https://config.together.test/v1", + }); + }); + + it("generateResponse() returns a text response using the default Together model", async () => { + mockCreate.mockResolvedValueOnce({ + choices: [ + { + message: { + content: "Hello from Together", + role: "assistant", + tool_calls: null, + }, + }, + ], + }); + + const llm = new TogetherLLM({ apiKey: "test-key" }); + const result = await llm.generateResponse([ + { role: "user", content: "Hi" }, + ]); + + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ + model: "MiniMaxAI/MiniMax-M3", + }), + ); + expect(result).toBe("Hello from Together"); + }); + + it("generateResponse() wraps API errors with a clear message", async () => { + mockCreate.mockRejectedValueOnce(new Error("Connection refused")); + + const llm = new TogetherLLM({ apiKey: "test-key" }); + + await expect( + llm.generateResponse([{ role: "user", content: "Hi" }]), + ).rejects.toThrow("Together LLM failed: Connection refused"); + }); + + it("generateChat() wraps API errors with a clear message", async () => { + mockCreate.mockRejectedValueOnce(new Error("Timeout")); + + const llm = new TogetherLLM({ apiKey: "test-key" }); + + await expect( + llm.generateChat([{ role: "user", content: "Hi" }]), + ).rejects.toThrow("Together LLM failed: Timeout"); + }); +}); diff --git a/mem0/llms/together.py b/mem0/llms/together.py index 0ec08f00f..72a197bc3 100644 --- a/mem0/llms/together.py +++ b/mem0/llms/together.py @@ -17,7 +17,7 @@ class TogetherLLM(LLMBase): super().__init__(config) if not self.config.model: - self.config.model = "mistralai/Mixtral-8x7B-Instruct-v0.1" + self.config.model = "MiniMaxAI/MiniMax-M3" api_key = self.config.api_key or os.getenv("TOGETHER_API_KEY") self.client = Together(api_key=api_key)