Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 3x 2x 2x 2x 2x | import type { ChatParams, ChatResult, ChatStreamChunk } from '../types.ts';
import { OpenV1BaseService } from './base.ts';
/**
* Creates an Open /v1-compatible service for Node.js environments.
*
* Requires `fetch` to be explicitly endowed for object-capability security.
*
* Pass `stream: true` in params for SSE streaming; omit for a single JSON response.
*
* @param config - Configuration for the service.
* @param config.endowments - Required endowments.
* @param config.endowments.fetch - The fetch implementation to use for HTTP requests.
* @param config.baseUrl - Base URL of the API (e.g. `'https://api.openai.com'`).
* @param config.apiKey - Optional API key sent as a Bearer token.
* @returns An object with a `chat` method. Raw sampling is not supported by this backend.
*/
export const makeOpenV1NodejsService = (config: {
endowments: { fetch: typeof globalThis.fetch };
baseUrl: string;
apiKey?: string;
}): {
chat: {
(params: ChatParams & { stream: true }): AsyncIterable<ChatStreamChunk>;
(params: ChatParams & { stream?: false }): Promise<ChatResult>;
};
listModels: () => Promise<string[]>;
} => {
const { endowments, baseUrl, apiKey } = config;
Iif (!endowments?.fetch) {
throw new Error('Must endow a fetch implementation.');
}
const service = new OpenV1BaseService(endowments.fetch, baseUrl, apiKey);
return harden({
chat: service.chat.bind(service) as {
(params: ChatParams & { stream: true }): AsyncIterable<ChatStreamChunk>;
(params: ChatParams & { stream?: false }): Promise<ChatResult>;
},
listModels: service.listModels.bind(service),
});
};
|