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
+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");
});
});