OpenAiChatClient constructor

OpenAiChatClient({
  1. required Uri endpoint,
  2. required String model,
  3. String? apiKey,
  4. Map<String, String> headers = const {},
  5. Map<String, Object?> extraBody = const {},
  6. Duration requestTimeout = const Duration(minutes: 2),
  7. int maxEventBytes = 1024 * 1024,
  8. int maxResponseBytes = 16 * 1024 * 1024,
  9. Client clientFactory()?,
})

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.',
    );
  }
}