OpenAiChatClient constructor
OpenAiChatClient({})
Implementation
OpenAiChatClient({
required this.endpoint,
required this.model,
this.apiKey,
this.headers = const {},
this.extraBody = const {},
this.requestTimeout = const Duration(minutes: 2),
this.maxEventBytes = 1024 * 1024,
this.maxResponseBytes = 16 * 1024 * 1024,
http.Client Function()? clientFactory,
}) : clientFactory = clientFactory ?? http.Client.new {
if (!endpoint.hasAuthority ||
!['https', 'http'].contains(endpoint.scheme) ||
endpoint.userInfo.isNotEmpty ||
endpoint.hasFragment) {
throw ArgumentError(
'Use an absolute HTTP(S) completion endpoint without embedded credentials or a fragment.',
);
}
if (model.trim().isEmpty ||
requestTimeout <= Duration.zero ||
maxEventBytes <= 0 ||
maxResponseBytes < maxEventBytes) {
throw ArgumentError('Invalid model, timeout, or response budget.');
}
if (extraBody.keys.any(
(key) =>
const {'model', 'messages', 'stream', 'tools', 'n'}.contains(key),
)) {
throw ArgumentError('extraBody cannot override managed request fields.');
}
if (headers.keys.any(
(key) => [
'authorization',
'content-type',
'accept',
].contains(key.toLowerCase()),
)) {
throw ArgumentError(
'Use apiKey for authorization; content type and streaming headers are managed by the client.',
);
}
}