Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions packages/ai/together/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,118 @@
import { smokeTest } from '@profullstack/sh1pt-core/testing';
import { afterEach, describe, expect, it, vi } from 'vitest';
import adapter from './index.js';

smokeTest(adapter, { idPrefix: 'ai' });

const ctx = (
secrets: Record<string, string> = { TOGETHER_API_KEY: 'test-key' },
dryRun = false,
) => ({
secret: (key: string) => secrets[key],
log: () => {},
dryRun,
});

describe('Together AI generation', () => {
afterEach(() => {
vi.unstubAllGlobals();
});

it('short-circuits dry-run before network calls', async () => {
const fetchMock = vi.fn();
vi.stubGlobal('fetch', fetchMock);

const result = await adapter.generate(
ctx({ TOGETHER_API_KEY: 'test-key' }, true),
'hello',
{},
{},
);

expect(result).toEqual({ text: '[dry-run]', model: 'meta-llama/Llama-3.3-70B-Instruct-Turbo' });
expect(fetchMock).not.toHaveBeenCalled();
});

it('posts chat completions requests and maps usage tokens', async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
model: 'meta-llama/Llama-3.3-70B-Instruct-Turbo',
choices: [{ message: { role: 'assistant', content: 'hi from together' } }],
usage: { prompt_tokens: 12, completion_tokens: 5, total_tokens: 17 },
}),
});
vi.stubGlobal('fetch', fetchMock);

const result = await adapter.generate(
ctx(),
'hello',
{
system: 'be direct',
maxTokens: 60,
temperature: 0.6,
extra: { top_p: 0.8, request_id: 'req-test' },
},
{},
);

expect(fetchMock).toHaveBeenCalledOnce();
const call = fetchMock.mock.calls[0];
expect(call).toBeDefined();
const [url, request] = call!;
expect(url).toBe('https://api.together.ai/v1/chat/completions');
expect(request.headers.authorization).toBe('Bearer test-key');
expect(request.headers['content-type']).toBe('application/json');
expect(JSON.parse(request.body)).toEqual({
model: 'meta-llama/Llama-3.3-70B-Instruct-Turbo',
messages: [
{ role: 'system', content: 'be direct' },
{ role: 'user', content: 'hello' },
],
stream: false,
max_tokens: 60,
temperature: 0.6,
top_p: 0.8,
request_id: 'req-test',
});
expect(result).toEqual({
text: 'hi from together',
model: 'meta-llama/Llama-3.3-70B-Instruct-Turbo',
inputTokens: 12,
outputTokens: 5,
});
});

it('supports text-style choices from compatible responses', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
choices: [{ text: 'legacy text response' }],
}),
}));

const result = await adapter.generate(
ctx(),
'hello',
{ model: 'Qwen/Qwen3.5-9B' },
{ baseUrl: 'https://together.test/v1' },
);

expect(result).toEqual({
text: 'legacy text response',
model: 'Qwen/Qwen3.5-9B',
});
});

it('includes status and response body excerpt on errors', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
ok: false,
status: 401,
text: async () => 'invalid api key'.repeat(30),
}));

await expect(adapter.generate(ctx(), 'hello', {}, {})).rejects.toThrow(
/Together AI 401: invalid api key/,
);
});
});
75 changes: 67 additions & 8 deletions packages/ai/together/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,86 @@ interface Config {
baseUrl?: string;
}

const DEFAULT_BASE = 'https://api.together.ai/v1';
const DEFAULT_MODEL = 'meta-llama/Llama-3.3-70B-Instruct-Turbo';

export default defineAi<Config>({
id: 'ai-together',
label: 'Together AI',
defaultModel: 'meta-llama/Llama-3.3-70B-Instruct-Turbo',
models: ['meta-llama/Llama-3.3-70B-Instruct-Turbo'],
defaultModel: DEFAULT_MODEL,
models: [
DEFAULT_MODEL,
'Qwen/Qwen3.5-9B',
'deepseek-ai/DeepSeek-V3',
],

async generate(ctx, prompt, _opts, _config) {
async generate(ctx, prompt, opts, config) {
const apiKey = ctx.secret('TOGETHER_API_KEY');
if (!apiKey) throw new Error('TOGETHER_API_KEY not in vault — run `sh1pt promote ai setup`');
ctx.log(`[stub] ai-together · ${prompt.length} chars in — integration pending`);
return { text: '[stub — ai-together integration not yet implemented]', model: 'meta-llama/Llama-3.3-70B-Instruct-Turbo' };
if (!apiKey) throw new Error('TOGETHER_API_KEY not in vault');
const model = opts.model ?? DEFAULT_MODEL;
ctx.log(`together · model=${model} · ${prompt.length} chars in`);
if (ctx.dryRun) return { text: '[dry-run]', model };

const messages: TogetherMessage[] = [];
if (opts.system) messages.push({ role: 'system', content: opts.system });
messages.push({ role: 'user', content: prompt });

const res = await fetch(`${config.baseUrl ?? DEFAULT_BASE}/chat/completions`, {
method: 'POST',
headers: {
authorization: `Bearer ${apiKey}`,
'content-type': 'application/json',
},
body: JSON.stringify({
model,
messages,
stream: false,
...(opts.maxTokens !== undefined ? { max_tokens: opts.maxTokens } : {}),
...(opts.temperature !== undefined ? { temperature: opts.temperature } : {}),
...opts.extra,
}),
});
if (!res.ok) throw new Error(`Together AI ${res.status}: ${(await res.text()).slice(0, 200)}`);

const data = await res.json() as TogetherChatResponse;
const choice = data.choices[0];
return {
text: choice?.message?.content ?? choice?.text ?? '',
model: data.model ?? model,
inputTokens: data.usage?.prompt_tokens,
outputTokens: data.usage?.completion_tokens,
};
},

setup: tokenSetup<Config>({
secretKey: 'TOGETHER_API_KEY',
label: 'Together AI',
vendorDocUrl: 'https://api.together.xyz',
vendorDocUrl: 'https://docs.together.ai/reference/chat-completions',
steps: [
'Sign in at https://api.together.xyz and create an API key',
'Sign in at https://api.together.ai and create an API key',
'Copy the key — usually shown once',
'Paste below; sh1pt encrypts it in the vault',
],
}),
});

type TogetherRole = 'system' | 'user' | 'assistant' | 'tool';

interface TogetherMessage {
role: TogetherRole;
content: string;
}

interface TogetherChatResponse {
model?: string;
choices: Array<{
message?: {
content?: string;
};
text?: string;
}>;
usage?: {
prompt_tokens?: number;
completion_tokens?: number;
};
}
Loading