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
113 changes: 113 additions & 0 deletions packages/ai/infermatic/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,117 @@
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> = { INFERMATIC_API_KEY: 'test-key' },
dryRun = false,
) => ({
secret: (key: string) => secrets[key],
log: () => {},
dryRun,
});

describe('Infermatic chat completions 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({ INFERMATIC_API_KEY: 'test-key' }, true),
'hello',
{},
{},
);

expect(result).toEqual({
text: '[dry-run]',
model: 'Sao10K-72B-Qwen2.5-Kunou-v1-FP8-Dynamic',
});
expect(fetchMock).not.toHaveBeenCalled();
});

it('posts chat completions requests and maps usage tokens', async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
model: 'Sao10K-L3.3-70B-Euryale-v2.3-FP8-Dynamic',
choices: [{ message: { role: 'assistant', content: 'hi from infermatic' } }],
usage: { prompt_tokens: 9, completion_tokens: 6, total_tokens: 15 },
}),
});
vi.stubGlobal('fetch', fetchMock);

const result = await adapter.generate(
ctx(),
'hello',
{
model: 'Sao10K-L3.3-70B-Euryale-v2.3-FP8-Dynamic',
system: 'be concise',
maxTokens: 48,
temperature: 0.4,
extra: { top_k: 40, repetition_penalty: 1.1 },
},
{},
);

expect(fetchMock).toHaveBeenCalledOnce();
const call = fetchMock.mock.calls[0];
expect(call).toBeDefined();
const [url, request] = call!;
expect(url).toBe('https://api.totalgpt.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: 'Sao10K-L3.3-70B-Euryale-v2.3-FP8-Dynamic',
messages: [
{ role: 'system', content: 'be concise' },
{ role: 'user', content: 'hello' },
],
max_tokens: 48,
temperature: 0.4,
top_k: 40,
repetition_penalty: 1.1,
});
expect(result).toEqual({
text: 'hi from infermatic',
model: 'Sao10K-L3.3-70B-Euryale-v2.3-FP8-Dynamic',
inputTokens: 9,
outputTokens: 6,
});
});

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

const result = await adapter.generate(ctx(), 'hello', {}, { baseUrl: 'https://infermatic.test' });

expect(result).toEqual({
text: 'legacy text response',
model: 'TheDrummer-UnslopNemo-12B-v4.1',
});
});

it('includes status and response body excerpt on errors', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
ok: false,
status: 500,
text: async () => 'unsupported system prompt'.repeat(30),
}));

await expect(adapter.generate(ctx(), 'hello', {}, {})).rejects.toThrow(
/Infermatic 500: unsupported system prompt/,
);
});
});
82 changes: 70 additions & 12 deletions packages/ai/infermatic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,85 @@ interface Config {
baseUrl?: string;
}

const DEFAULT_BASE = 'https://api.totalgpt.ai';
const DEFAULT_MODEL = 'Sao10K-72B-Qwen2.5-Kunou-v1-FP8-Dynamic';

export default defineAi<Config>({
id: 'ai-infermatic',
label: 'Infermatic',
defaultModel: 'INFERMATIC_API_KEY',
models: ['INFERMATIC_API_KEY'],

async generate(ctx, prompt, _opts, _config) {
const apiKey = ctx.secret('https://infermatic.ai');
if (!apiKey) throw new Error('https://infermatic.ai not in vault — run `sh1pt promote ai setup`');
ctx.log(`[stub] ai-infermatic · ${prompt.length} chars in — integration pending`);
return { text: '[stub — ai-infermatic integration not yet implemented]', model: 'INFERMATIC_API_KEY' };
defaultModel: DEFAULT_MODEL,
models: [
DEFAULT_MODEL,
'Sao10K-L3.3-70B-Euryale-v2.3-FP8-Dynamic',
'TheDrummer-UnslopNemo-12B-v4.1',
],

async generate(ctx, prompt, opts, config) {
const apiKey = ctx.secret('INFERMATIC_API_KEY');
if (!apiKey) throw new Error('INFERMATIC_API_KEY not in vault');
const model = opts.model ?? DEFAULT_MODEL;
ctx.log(`infermatic · model=${model} · ${prompt.length} chars in`);
if (ctx.dryRun) return { text: '[dry-run]', model };

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

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

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

setup: tokenSetup<Config>({
secretKey: 'https://infermatic.ai',
secretKey: 'INFERMATIC_API_KEY',
label: 'Infermatic',
vendorDocUrl: '',
vendorDocUrl: 'https://ui.infermatic.ai/docs',
steps: [
'Sign in at and create an API key',
'Copy the key usually shown once',
'Sign in at https://ui.infermatic.ai and create an API key',
'Copy the key; it is usually shown once',
'Paste below; sh1pt encrypts it in the vault',
],
}),
});

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

interface InfermaticMessage {
role: InfermaticRole;
content: string;
}

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