Skip to content

Commit

Permalink
feat: Support Anyscale API in openai_dart client (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz authored Jan 20, 2024
1 parent cc6538b commit e0a3651
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 29 deletions.
33 changes: 33 additions & 0 deletions packages/openai_dart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,39 @@ final client = OpenAIClient(
- `YOUR_API_KEY`: This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal.
- `API_VERSION`: The Azure OpenAI API version to use (e.g. `2023-05-15`). Try to use the [latest version available](https://github.com/Azure/azure-rest-api-specs/tree/main/specification/cognitiveservices/data-plane/AzureOpenAI/inference), it will probably be the closest to the official OpenAI API.

### OpenAI-compatible APIs

This client can be used to consume APIs that are compatible with the OpenAI API spec.

[TogetherAI](https://www.together.ai/):

```dart
final client = OpenAIClient(
baseUrl: 'https://api.together.xyz/v1',
headers: { 'api-key': 'YOUR_TOGETHER_AI_API_KEY' },
);
```

[Anyscale](https://www.anyscale.com/):

```dart
final client = OpenAIClient(
baseUrl: 'https://api.endpoints.anyscale.com/v1',
headers: { 'api-key': 'YOUR_ANYSCALE_API_KEY' },
);
```

[OpenRouter](https://openrouter.ai):

```dart
final client = OpenAIClient(
baseUrl: 'https://openrouter.ai/api/v1',
headers: { 'api-key': 'YOUR_OPEN_ROUTER_API_KEY' },
);
```

Etc.

### Default HTTP client

By default, the client uses the following implementation of `http.Client`:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CreateChatCompletionResponse with _$CreateChatCompletionResponse {
String? systemFingerprint,

/// The object type, which is always `chat.completion`.
required CreateChatCompletionResponseObject object,
required String object,

/// Usage statistics for the completion request.
@JsonKey(includeIfNull: false) CompletionUsage? usage,
Expand Down Expand Up @@ -73,13 +73,3 @@ class CreateChatCompletionResponse with _$CreateChatCompletionResponse {
};
}
}

// ==========================================
// ENUM: CreateChatCompletionResponseObject
// ==========================================

/// The object type, which is always `chat.completion`.
enum CreateChatCompletionResponseObject {
@JsonValue('chat.completion')
chatCompletion,
}
Original file line number Diff line number Diff line change
Expand Up @@ -7436,8 +7436,7 @@ mixin _$CreateChatCompletionResponse {
String? get systemFingerprint => throw _privateConstructorUsedError;

/// The object type, which is always `chat.completion`.
CreateChatCompletionResponseObject get object =>
throw _privateConstructorUsedError;
String get object => throw _privateConstructorUsedError;

/// Usage statistics for the completion request.
@JsonKey(includeIfNull: false)
Expand All @@ -7464,7 +7463,7 @@ abstract class $CreateChatCompletionResponseCopyWith<$Res> {
String model,
@JsonKey(name: 'system_fingerprint', includeIfNull: false)
String? systemFingerprint,
CreateChatCompletionResponseObject object,
String object,
@JsonKey(includeIfNull: false) CompletionUsage? usage});

$CompletionUsageCopyWith<$Res>? get usage;
Expand Down Expand Up @@ -7516,7 +7515,7 @@ class _$CreateChatCompletionResponseCopyWithImpl<$Res,
object: null == object
? _value.object
: object // ignore: cast_nullable_to_non_nullable
as CreateChatCompletionResponseObject,
as String,
usage: freezed == usage
? _value.usage
: usage // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -7553,7 +7552,7 @@ abstract class _$$CreateChatCompletionResponseImplCopyWith<$Res>
String model,
@JsonKey(name: 'system_fingerprint', includeIfNull: false)
String? systemFingerprint,
CreateChatCompletionResponseObject object,
String object,
@JsonKey(includeIfNull: false) CompletionUsage? usage});

@override
Expand Down Expand Up @@ -7605,7 +7604,7 @@ class __$$CreateChatCompletionResponseImplCopyWithImpl<$Res>
object: null == object
? _value.object
: object // ignore: cast_nullable_to_non_nullable
as CreateChatCompletionResponseObject,
as String,
usage: freezed == usage
? _value.usage
: usage // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -7666,7 +7665,7 @@ class _$CreateChatCompletionResponseImpl extends _CreateChatCompletionResponse {

/// The object type, which is always `chat.completion`.
@override
final CreateChatCompletionResponseObject object;
final String object;

/// Usage statistics for the completion request.
@override
Expand Down Expand Up @@ -7730,7 +7729,7 @@ abstract class _CreateChatCompletionResponse
required final String model,
@JsonKey(name: 'system_fingerprint', includeIfNull: false)
final String? systemFingerprint,
required final CreateChatCompletionResponseObject object,
required final String object,
@JsonKey(includeIfNull: false) final CompletionUsage? usage}) =
_$CreateChatCompletionResponseImpl;
const _CreateChatCompletionResponse._() : super._();
Expand Down Expand Up @@ -7765,7 +7764,7 @@ abstract class _CreateChatCompletionResponse
@override

/// The object type, which is always `chat.completion`.
CreateChatCompletionResponseObject get object;
String get object;
@override

/// Usage statistics for the completion request.
Expand Down
9 changes: 2 additions & 7 deletions packages/openai_dart/lib/src/generated/schema/schema.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/openai_dart/oas/openapi_curated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1939,7 +1939,7 @@ components:
object:
type: string
description: The object type, which is always `chat.completion`.
enum: [ chat.completion ]
# enum: [ chat.completion ] # Anyscale API sends `text_completion` instead
usage:
$ref: "#/components/schemas/CompletionUsage"
required:
Expand Down
2 changes: 1 addition & 1 deletion packages/openai_dart/test/openai_client_chat_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void main() {
expect(res.id, isNotEmpty);
expect(res.created, greaterThan(0));
expect(res.model, startsWith('gpt-'));
expect(res.object, CreateChatCompletionResponseObject.chatCompletion);
expect(res.object, isNotEmpty);
expect(res.usage?.promptTokens, greaterThan(0));
expect(res.usage?.completionTokens, greaterThan(0));
expect(res.usage?.totalTokens, greaterThan(0));
Expand Down

0 comments on commit e0a3651

Please sign in to comment.