Skip to content

Commit

Permalink
OpenAI-DotNet 6.5.3 (RageAgainstThePixel#75)
Browse files Browse the repository at this point in the history
- Added missing ConfigureAwait to await calls
- Updated docs

---------

Co-authored-by: Michael Washington <[email protected]>
Co-authored-by: Samuel John Makin <[email protected]>
  • Loading branch information
3 people committed Mar 29, 2023
1 parent 08b1094 commit 40c3fc2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
14 changes: 7 additions & 7 deletions OpenAI-DotNet/Audio/AudioEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task<string> CreateTranscriptionAsync(AudioTranscriptionRequest req
{
using var content = new MultipartFormDataContent();
using var audioData = new MemoryStream();
await request.Audio.CopyToAsync(audioData, cancellationToken);
await request.Audio.CopyToAsync(audioData, cancellationToken).ConfigureAwait(false);
content.Add(new ByteArrayContent(audioData.ToArray()), "file", request.AudioName);
content.Add(new StringContent(request.Model), "model");

Expand All @@ -64,16 +64,16 @@ public async Task<string> CreateTranscriptionAsync(AudioTranscriptionRequest req

request.Dispose();

var response = await Api.Client.PostAsync(GetUrl("/transcriptions"), content, cancellationToken);
var responseAsString = await response.ReadAsStringAsync(cancellationToken);
var response = await Api.Client.PostAsync(GetUrl("/transcriptions"), content, cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);

return responseFormat == AudioResponseFormat.Json
? JsonSerializer.Deserialize<AudioResponse>(responseAsString)?.Text
: responseAsString;
}

/// <summary>
/// Translates audio into into English.
/// Translates audio into English.
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
Expand All @@ -82,7 +82,7 @@ public async Task<string> CreateTranslationAsync(AudioTranslationRequest request
{
using var content = new MultipartFormDataContent();
using var audioData = new MemoryStream();
await request.Audio.CopyToAsync(audioData, cancellationToken);
await request.Audio.CopyToAsync(audioData, cancellationToken).ConfigureAwait(false);
content.Add(new ByteArrayContent(audioData.ToArray()), "file", request.AudioName);
content.Add(new StringContent(request.Model), "model");

Expand All @@ -101,8 +101,8 @@ public async Task<string> CreateTranslationAsync(AudioTranslationRequest request

request.Dispose();

var response = await Api.Client.PostAsync(GetUrl("/translations"), content, cancellationToken);
var responseAsString = await response.ReadAsStringAsync(cancellationToken);
var response = await Api.Client.PostAsync(GetUrl("/translations"), content, cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);

return responseFormat == AudioResponseFormat.Json
? JsonSerializer.Deserialize<AudioResponse>(responseAsString)?.Text
Expand Down
12 changes: 6 additions & 6 deletions OpenAI-DotNet/Chat/ChatEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public async Task StreamCompletionAsync(ChatRequest chatRequest, Action<ChatResp
{
Content = jsonContent
};
var response = await Api.Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
await response.CheckResponseAsync(cancellationToken);
var response = await Api.Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
await response.CheckResponseAsync(cancellationToken).ConfigureAwait(false);
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
using var reader = new StreamReader(stream);

while (await reader.ReadLineAsync() is { } line)
while (await reader.ReadLineAsync().ConfigureAwait(false) is { } line)
{
if (line.StartsWith("data: "))
{
Expand Down Expand Up @@ -93,12 +93,12 @@ public async IAsyncEnumerable<ChatResponse> StreamCompletionEnumerableAsync(Chat
{
Content = jsonContent
};
var response = await Api.Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
await response.CheckResponseAsync(cancellationToken);
var response = await Api.Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
await response.CheckResponseAsync(cancellationToken).ConfigureAwait(false);
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
using var reader = new StreamReader(stream);

while (await reader.ReadLineAsync() is { } line &&
while (await reader.ReadLineAsync().ConfigureAwait(false) is { } line &&
!cancellationToken.IsCancellationRequested)
{
if (line.StartsWith("data: "))
Expand Down
2 changes: 1 addition & 1 deletion OpenAI-DotNet/Images/ImagesEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public async Task<IReadOnlyList<string>> CreateImageEditAsync(ImageEditRequest r
if (request.Mask != null)
{
using var maskData = new MemoryStream();
await request.Mask.CopyToAsync(maskData, cancellationToken);
await request.Mask.CopyToAsync(maskData, cancellationToken).ConfigureAwait(false);
content.Add(new ByteArrayContent(maskData.ToArray()), "mask", request.MaskName);
}

Expand Down
6 changes: 4 additions & 2 deletions OpenAI-DotNet/OpenAI-DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ More context [on Roger Pincombe's blog](https://rogerpincombe.com/openai-dotnet-
<RepositoryUrl>https://github.com/RageAgainstThePixel/OpenAI-DotNet</RepositoryUrl>
<PackageTags>OpenAI, AI, ML, API, gpt-4, gpt-3.5-tubo, gpt-3, chatGPT, chat-gpt, gpt-2, gpt</PackageTags>
<Title>OpenAI API</Title>
<PackageReleaseNotes>Version 6.5.2
<PackageReleaseNotes>Version 6.5.3
- Added missing ConfigureAwait to await calls
Version 6.5.2
- Updated SetResponseData to better reflect the difference between OpenAI and Azure responses.
- Updated ProcessingTime parsing from int to double
Version 6.5.1
Expand Down Expand Up @@ -102,7 +104,7 @@ Version 4.4.0
<AssemblyOriginatorKeyFile>OpenAI-DotNet.pfx</AssemblyOriginatorKeyFile>
<DelaySign>true</DelaySign>
<PackageId>OpenAI-DotNet</PackageId>
<Version>6.5.2</Version>
<Version>6.5.3</Version>
<Company>RageAgainstThePixel</Company>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>Assets\OpenAI-DotNet-Icon.png</PackageIcon>
Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,22 @@ var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv());
### [Azure OpenAI](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/)

You can also choose to use Microsoft's Azure OpenAI deployments as well.

You can find the required information in the Azure Playground by clicking the `View Code` button and view a URL like this:

```markdown
https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}/chat/completions?api-version={api-version}
```

- `your-resource-name` The name of your Azure OpenAI Resource.
- `deployment-id` The deployment name you chose when you deployed the model.
- `api-version` The API version to use for this operation. This follows the YYYY-MM-DD format.

To setup the client to use your deployment, you'll need to pass in `OpenAIClientSettings` into the client constructor.

```csharp
var auth = new OpenAIAuthentication("sk-apiKey");
var settings = new OpenAIClientSettings(resourceName: "your-resource", deploymentId: "your-deployment-id");
var settings = new OpenAIClientSettings(resourceName: "your-resource-name", deploymentId: "deployment-id", apiVersion: "api-version");
var api = new OpenAIClient(auth, settings);
```

Expand All @@ -161,7 +172,7 @@ var api = new OpenAIClient(auth, settings);
// get your access token using any of the MSAL methods
var accessToken = result.AccessToken;
var auth = new OpenAIAuthentication(accessToken);
var settings = new OpenAIClientSettings(resourceName: "your-resource", deploymentId: "your-deployment-id", useActiveDirectoryAuthentication: true);
var settings = new OpenAIClientSettings(resourceName: "your-resource", deploymentId: "deployment-id", apiVersion: "api-version", useActiveDirectoryAuthentication: true);
var api = new OpenAIClient(auth, settings);
```

Expand Down

0 comments on commit 40c3fc2

Please sign in to comment.