Compare commits

...

3 Commits

Author SHA1 Message Date
Taranjeet Singh b1418ce93f fix(ts-sdk): format Together LLM provider to pass CI
Reformats the Together provider added in #5798 to match the DeepSeek
reference and satisfy the `prettier --check .` CI gate:

- Collapse manual line-wrapping in together.ts to Prettier style
- Add TOGETHER_API_BASE env override + trailing newline (parity with deepseek.ts)
- Remove stray leading blank line in factory.ts

Closes #5759

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-24 20:16:49 -07:00
niraja03 ab4c76f197 test(ts-sdk): add Together LLM unit tests 2026-06-24 06:24:06 +05:30
niraja03 14fa2ec49b feat(ts-sdk): add Together LLM provider 2026-06-24 05:09:27 +05:30
3 changed files with 170 additions and 0 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.xyz/v1",
model: config.model || "mistralai/Mixtral-8x7B-Instruct-v0.1",
});
}
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
@@ -22,6 +22,7 @@ import { RedisDB } from "../vector_stores/redis";
import { OllamaLLM } from "../llms/ollama";
import { LMStudioLLM } from "../llms/lmstudio";
import { DeepSeekLLM } from "../llms/deepseek";
import { TogetherLLM } from "../llms/together";
import { SupabaseDB } from "../vector_stores/supabase";
import { SQLiteManager } from "../storage/SQLiteManager";
import { MemoryHistoryManager } from "../storage/MemoryHistoryManager";
@@ -85,6 +86,8 @@ export class LLMFactory {
return new LangchainLLM(config);
case "deepseek":
return new DeepSeekLLM(config);
case "together":
return new TogetherLLM(config);
default:
throw new Error(`Unsupported LLM provider: ${provider}`);
}
+124
View File
@@ -0,0 +1,124 @@
/// <reference types="jest" />
/**
* Together LLM — unit tests (mocked OpenAI).
*/
import { TogetherLLM } from "../src/llms/together";
const mockCreate = jest.fn();
jest.mock("openai", () => {
return jest.fn().mockImplementation(() => ({
chat: { completions: { create: mockCreate } },
}));
});
describe("TogetherLLM (unit)", () => {
beforeEach(() => mockCreate.mockClear());
it("throws when no API key is provided", () => {
const original = process.env.TOGETHER_API_KEY;
delete process.env.TOGETHER_API_KEY;
try {
expect(() => new TogetherLLM({})).toThrow("Together API key is required");
} finally {
if (original !== undefined) process.env.TOGETHER_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 TogetherLLM({ 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 TogetherLLM({ 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 TogetherLLM({ apiKey: "test-key" });
await expect(
llm.generateResponse([{ role: "user", content: "Hi" }]),
).rejects.toThrow("Together 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 TogetherLLM({ 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 TogetherLLM({ apiKey: "test-key" });
await expect(
llm.generateChat([{ role: "user", content: "Hi" }]),
).rejects.toThrow("Together LLM failed: Timeout");
});
});