Skip to content

Commit

Permalink
Merge branch 'main' into fix-sse
Browse files Browse the repository at this point in the history
  • Loading branch information
wliu6v committed Nov 22, 2023
2 parents 7cab0d6 + aa842b7 commit a17b4fd
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 36 deletions.
1 change: 1 addition & 0 deletions example_app/openai_app/lib/bloc/openai/openai_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class OpenAIBloc extends Cubit<OpenAIState> {
void initOpenAISdk() async {
_openAI = OpenAI.instance.build(
token: getToken(),
apiUrl: 'https://api.openai.com/v1/', // you can replace with your api url
enableLog: true,
baseOption: HttpSetup(
receiveTimeout: const Duration(seconds: 30),
Expand Down
4 changes: 2 additions & 2 deletions lib/src/audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Audio {
final mRequest = await request.toJson();

return _client.postFormData(
kURL + kTranscription,
_client.apiUrl + kTranscription,
mRequest,
onCancel: (it) => onCancel != null ? onCancel(it) : null,
complete: (it) => AudioResponse.fromJson(it),
Expand All @@ -31,7 +31,7 @@ class Audio {
final mRequest = await request.toJson();

return _client.postFormData(
kURL + kTranslations,
_client.apiUrl + kTranslations,
mRequest,
onCancel: (it) => onCancel != null ? onCancel(it) : null,
complete: (it) => AudioResponse.fromJson(it),
Expand Down
15 changes: 10 additions & 5 deletions lib/src/client/openai_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import 'package:chat_gpt_sdk/src/model/error/openai_error.dart';
import 'package:dio/dio.dart';

class OpenAIClient extends OpenAIWrapper {
OpenAIClient({required Dio dio, bool isLogging = false}) {
OpenAIClient({required Dio dio, required String apiUrl, bool isLogging = false}) {
_dio = dio;
_apiUrl = apiUrl;
log = Logger.instance.builder(isLogging: isLogging);
}

Expand All @@ -22,6 +23,10 @@ class OpenAIClient extends OpenAIWrapper {
///[log]
late Logger log;

late String _apiUrl;

String get apiUrl => _apiUrl;

Future<T> get<T>(
String url, {
required T Function(Map<String, dynamic>) onSuccess,
Expand Down Expand Up @@ -179,7 +184,7 @@ class OpenAIClient extends OpenAIWrapper {
final cancelData = CancelData(cancelToken: CancelToken());
onCancel(cancelData);

log.log("starting request");
log.log("starting request $url");
log.log("request body :$request");

final response = await _dio.post(
Expand Down Expand Up @@ -220,7 +225,7 @@ class OpenAIClient extends OpenAIWrapper {
final cancelData = CancelData(cancelToken: CancelToken());
onCancel(cancelData);

log.log("starting request");
log.log("starting request $url");
log.log("request body :$request");
final response = _dio
.post(
Expand All @@ -239,7 +244,7 @@ class OpenAIClient extends OpenAIWrapper {
required T Function(Map<String, dynamic> value) complete,
required void Function(CancelData cancelData) onCancel,
}) {
log.log("starting request");
log.log("starting request $url");
log.log("request body :$request");
final controller = StreamController<T>.broadcast();
final cancelData = CancelData(cancelToken: CancelToken());
Expand Down Expand Up @@ -335,7 +340,7 @@ class OpenAIClient extends OpenAIWrapper {
final cancelData = CancelData(cancelToken: CancelToken());
onCancel(cancelData);

log.log("starting request");
log.log("starting request $url");
log.log("request body :$request");
final response = await _dio.post(
url,
Expand Down
6 changes: 3 additions & 3 deletions lib/src/edit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Edit {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.post(
kURL + kEditPrompt,
_client.apiUrl + kEditPrompt,
request.toJson(),
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) => EditResponse.fromJson(it),
Expand All @@ -36,7 +36,7 @@ class Edit {
final mRequest = await request.convert();

return _client.postFormData(
kURL + kImageEdit,
_client.apiUrl + kImageEdit,
mRequest,
onCancel: (it) => onCancel != null ? onCancel(it) : null,
complete: (it) => GenImgResponse.fromJson(it),
Expand All @@ -51,7 +51,7 @@ class Edit {
final mRequest = await request.convert();

return _client.postFormData(
kURL + kVariation,
_client.apiUrl + kVariation,
mRequest,
onCancel: (it) => onCancel != null ? onCancel(it) : null,
complete: (it) => GenImgResponse.fromJson(it),
Expand Down
2 changes: 1 addition & 1 deletion lib/src/embedding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Embedding {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.post(
kURL + kEmbedding,
_client.apiUrl + kEmbedding,
request.toJson(),
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) => EmbedResponse.fromJson(it),
Expand Down
22 changes: 11 additions & 11 deletions lib/src/fine_tuned.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class FineTuned {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.post(
kURL + kFineTune,
_client.apiUrl + kFineTune,
request.toJson(),
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) => FineTuneModel.fromJson(it),
Expand All @@ -33,7 +33,7 @@ class FineTuned {
void Function(CancelData cancelData)? onCancel,
}) async {
return _client.get(
kURL + kFineTune,
_client.apiUrl + kFineTune,
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) {
final data = it['data'] as List;
Expand All @@ -49,7 +49,7 @@ class FineTuned {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.get(
"$kURL$kFineTune/$fineTuneId",
"$_client.apiUrl$kFineTune/$fineTuneId",
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) => FineTuneModel.fromJson(it),
);
Expand All @@ -61,7 +61,7 @@ class FineTuned {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.post(
"$kURL$kFineTune/$fineTuneId/cancel",
"$_client.apiUrl$kFineTune/$fineTuneId/cancel",
{},
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) => FineTuneModel.fromJson(it),
Expand All @@ -74,7 +74,7 @@ class FineTuned {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.delete(
"$kURL$kFineTuneModel/$model",
"$_client.apiUrl$kFineTuneModel/$model",
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) => FineTuneDelete.fromJson(it),
);
Expand All @@ -86,7 +86,7 @@ class FineTuned {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.getStream(
"$kURL$kFineTune/$fineTuneId/events",
"$_client.apiUrl$kFineTune/$fineTuneId/events",
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) {
final data = it['data'] as List;
Expand All @@ -108,7 +108,7 @@ class FineTuned {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.post(
kURL + kFineTuneJob,
_client.apiUrl + kFineTuneJob,
request.toJson(),
onSuccess: FineTuneModelJob.fromJson,
onCancel: (it) => onCancel != null ? onCancel(it) : null,
Expand All @@ -121,7 +121,7 @@ class FineTuned {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.get(
kURL + kFineTuneJob + "/$fineTuneId",
_client.apiUrl + kFineTuneJob + "/$fineTuneId",
onSuccess: FineTuneList.fromJson,
onCancel: (it) => onCancel != null ? onCancel(it) : null,
);
Expand All @@ -132,7 +132,7 @@ class FineTuned {
void Function(CancelData cancelData)? onCancel,
}) async {
return _client.get(
kURL + kFineTune,
_client.apiUrl + kFineTune,
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) {
final data = it['data'] as List;
Expand All @@ -148,7 +148,7 @@ class FineTuned {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.post(
"$kURL$kFineTuneJob/$fineTuneId/cancel",
"$_client.apiUrl$kFineTuneJob/$fineTuneId/cancel",
{},
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: FineTuneList.fromJson,
Expand All @@ -172,7 +172,7 @@ class FineTuned {
}

return _client.getStream(
"$kURL$kFineTuneJob/$fineTuneId/events",
"$_client.apiUrl$kFineTuneJob/$fineTuneId/events",
queryParameters: query,
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/moderation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Moderation {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.post(
kURL + kModeration,
_client.apiUrl + kModeration,
{"input": input, "model": model.model},
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) => ModerationData.fromJson(it),
Expand Down
18 changes: 10 additions & 8 deletions lib/src/openai.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class OpenAI implements IOpenAI {
OpenAI build({
String? token,
String? orgId,
String? apiUrl,
HttpSetup? baseOption,
bool enableLog = false,
}) {
Expand Down Expand Up @@ -86,7 +87,8 @@ class OpenAI implements IOpenAI {
}
dio.interceptors.add(InterceptorWrapper());

_client = OpenAIClient(dio: dio, isLogging: enableLog);
final _apiUrl = (apiUrl?.isNotEmpty ?? false) ? apiUrl! : kURL;
_client = OpenAIClient(dio: dio, apiUrl: _apiUrl , isLogging: enableLog);

return instance;
}
Expand All @@ -97,7 +99,7 @@ class OpenAI implements IOpenAI {
void Function(CancelData cancelData)? onCancel,
}) async {
return _client.get<OpenAiModel>(
"$kURL$kModelList",
"${_client.apiUrl}$kModelList",
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) => OpenAiModel.fromJson(it),
);
Expand All @@ -109,7 +111,7 @@ class OpenAI implements IOpenAI {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.get<EngineModel>(
"$kURL$kEngineList",
"${_client.apiUrl}$kEngineList",
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) {
return EngineModel.fromJson(it);
Expand All @@ -129,7 +131,7 @@ class OpenAI implements IOpenAI {
void Function(CancelData cancelData)? onCancel,
}) =>
_client.post(
"$kURL$kCompletion",
"${_client.apiUrl}$kCompletion",
request.toJson(),
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) {
Expand All @@ -145,7 +147,7 @@ class OpenAI implements IOpenAI {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.post(
"$kURL$kChatGptTurbo",
"${_client.apiUrl}$kChatGptTurbo",
request.toJson(),
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) {
Expand All @@ -161,7 +163,7 @@ class OpenAI implements IOpenAI {
void Function(CancelData cancelData)? onCancel,
}) async {
return _client.post(
"$kURL$kGenerateImage",
"${_client.apiUrl}$kGenerateImage",
request.toJson(),
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) {
Expand All @@ -179,7 +181,7 @@ class OpenAI implements IOpenAI {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.sse(
"$kURL$kChatGptTurbo",
"${_client.apiUrl}$kChatGptTurbo",
request.toJson()..addAll({"stream": true}),
onCancel: (it) => onCancel != null ? onCancel(it) : null,
complete: (it) {
Expand All @@ -200,7 +202,7 @@ class OpenAI implements IOpenAI {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.sse(
'$kURL$kCompletion',
'${_client.apiUrl}$kCompletion',
request.toJson()..addAll({"stream": true}),
onCancel: (it) => onCancel != null ? onCancel(it) : null,
complete: (it) {
Expand Down
10 changes: 5 additions & 5 deletions lib/src/openai_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class OpenAIFile {
///Returns a list of files that belong to the user's organization.
Future<FileResponse> get({void Function(CancelData cancelData)? onCancel}) {
return _client.get(
kURL + kFile,
_client.apiUrl + kFile,
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) => FileResponse.fromJson(it),
);
Expand All @@ -32,7 +32,7 @@ class OpenAIFile {
final mRequest = await request.getForm();

return _client.postFormData(
kURL + kFile,
_client.apiUrl + kFile,
mRequest,
onCancel: (it) => onCancel != null ? onCancel(it) : null,
complete: (it) => UploadResponse.fromJson(it),
Expand All @@ -45,7 +45,7 @@ class OpenAIFile {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.delete(
"$kURL$kFile/{$fileId}",
"${_client.apiUrl}$kFile/{$fileId}",
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) => DeleteFile.fromJson(it),
);
Expand All @@ -57,7 +57,7 @@ class OpenAIFile {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.get(
"$kURL$kFile/$fileId",
"${_client.apiUrl}$kFile/$fileId",
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) => UploadResponse.fromJson(it),
);
Expand All @@ -69,7 +69,7 @@ class OpenAIFile {
void Function(CancelData cancelData)? onCancel,
}) {
return _client.get(
'$kURL$kFile/$fileId/content',
'${_client.apiUrl}$kFile/$fileId/content',
returnRawData: true,
onCancel: (it) => onCancel != null ? onCancel(it) : null,
onSuccess: (it) => it,
Expand Down
8 changes: 8 additions & 0 deletions test/client/openai_wrapper_test.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ class MockOpenAIClient extends _i1.Mock implements _i5.OpenAIClient {
),
returnValueForMissingStub: null,
);

@override
String get apiUrl => (super.noSuchMethod(
Invocation.getter(#apiUrl),
returnValue: '',
returnValueForMissingStub: '',
) as String);

@override
_i3.Future<T> get<T>(
String? url, {
Expand Down
Loading

0 comments on commit a17b4fd

Please sign in to comment.