UNPKG

snowcell

Version:

Official Snowcell Node.js/TypeScript SDK

46 lines 1.38 kB
import { iterSSE } from './util.js'; export class Chat { _client; constructor(_client) { this._client = _client; } async create(params) { const { model, messages, max_tokens = 512, temperature = 0.7, stream = false, ...extra } = params; const body = JSON.stringify({ model, messages, max_tokens, temperature, stream, ...extra, }); const res = await this._client['_inference']('POST', '/v1/chat/completions', { body }); return (await res.json()); } async *stream(params) { const { model, messages, max_tokens = 512, temperature = 0.7, ...extra } = params; const body = JSON.stringify({ model, messages, max_tokens, temperature, stream: true, ...extra, }); const res = await this._client['_inference']('POST', '/v1/chat/completions', { body, headers: { Accept: 'text/event-stream' }, }); for await (const data of iterSSE(res)) { if (data === '[DONE]') break; try { yield JSON.parse(data); } catch { // swallow bad lines / keepalives } } } } //# sourceMappingURL=chat.js.map