feat(ts-sdk): add Together LLM provider (#6049)

Co-authored-by: zhangsixiao <zhangsixiao@bytedance.com>
Co-authored-by: kartik-mem0 <kartik.labhshetwar@mem0.ai>
This commit is contained in:
张思孝
2026-07-07 21:08:26 +05:30
committed by GitHub
co-authored by zhangsixiao kartik-mem0
parent 803ff13bb8
commit 9b04509433
6 changed files with 230 additions and 5 deletions
+62 -4
View File
@@ -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
<CodeGroup>
```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' } });
```
</CodeGroup>
You can also configure the API base URL in the config:
<CodeGroup>
```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",
},
},
};
```
</CodeGroup>
## Config
All available parameters for the `together` config are present in [Master List of All Params in Config](../config).
+43
View File
@@ -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<string | LLMResponse> {
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<LLMResponse> {
try {
return await super.generateChat(messages);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
throw new Error(`Together LLM failed: ${message}`);
}
}
}
+3
View File
@@ -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:
@@ -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();
});
+114
View File
@@ -0,0 +1,114 @@
/// <reference types="jest" />
/**
* 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");
});
});
+1 -1
View File
@@ -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)