Skip to content

Async suffix added so the intent is clear and there's no ambiguity. #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion OpenAI_API/APIAuthentication.cs
Original file line number Diff line number Diff line change
@@ -163,7 +163,7 @@ public static APIAuthentication LoadFromPath(string directory = null, string fil
/// Tests the api key against the OpenAI API, to ensure it is valid. This hits the models endpoint so should not be charged for usage.
/// </summary>
/// <returns><see langword="true"/> if the api key is valid, or <see langword="false"/> if empty or not accepted by the OpenAI API.</returns>
public async Task<bool> ValidateAPIKey()
public async Task<bool> ValidateAPIKeyAsync()
{
if (string.IsNullOrEmpty(ApiKey))
return false;
4 changes: 2 additions & 2 deletions OpenAI_API/Chat/ChatEndpoint.cs
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ public Conversation CreateConversation()
/// <returns>Asynchronously returns the completion result. Look in its <see cref="ChatResult.Choices"/> property for the results.</returns>
public async Task<ChatResult> CreateChatCompletionAsync(ChatRequest request)
{
return await HttpPost<ChatResult>(postData: request);
return await HttpPostAsync<ChatResult>(postData: request);
}

/// <summary>
@@ -167,7 +167,7 @@ public async Task StreamChatAsync(ChatRequest request, Action<ChatResult> result
public IAsyncEnumerable<ChatResult> StreamChatEnumerableAsync(ChatRequest request)
{
request = new ChatRequest(request) { Stream = true };
return HttpStreamingRequest<ChatResult>(Url, HttpMethod.Post, request);
return HttpStreamingRequestAsync<ChatResult>(Url, HttpMethod.Post, request);
}

/// <summary>
4 changes: 2 additions & 2 deletions OpenAI_API/Chat/Conversation.cs
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ public OpenAI_API.Models.Model Model
}

/// <summary>
/// After calling <see cref="GetResponseFromChatbot"/>, this contains the full response object which can contain useful metadata like token usages, <see cref="ChatChoice.FinishReason"/>, etc. This is overwritten with every call to <see cref="GetResponseFromChatbot"/> and only contains the most recent result.
/// After calling <see cref="GetResponseFromChatbotAsync"/>, this contains the full response object which can contain useful metadata like token usages, <see cref="ChatChoice.FinishReason"/>, etc. This is overwritten with every call to <see cref="GetResponseFromChatbotAsync"/> and only contains the most recent result.
/// </summary>
public ChatResult MostResentAPIResult { get; private set; }

@@ -106,7 +106,7 @@ public void AppendMessage(ChatMessage message)
/// Calls the API to get a response, which is appended to the current chat's <see cref="Messages"/> as an <see cref="ChatMessageRole.Assistant"/> <see cref="ChatMessage"/>.
/// </summary>
/// <returns>The string of the response from the chatbot API</returns>
public async Task<string> GetResponseFromChatbot()
public async Task<string> GetResponseFromChatbotAsync()
{
ChatRequest req = new ChatRequest(RequestParameters);
req.Messages = _Messages.ToList();
8 changes: 4 additions & 4 deletions OpenAI_API/Completions/CompletionEndpoint.cs
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ internal CompletionEndpoint(OpenAIAPI api) : base(api) { }
/// <returns>Asynchronously returns the completion result. Look in its <see cref="CompletionResult.Completions"/> property for the completions.</returns>
public async Task<CompletionResult> CreateCompletionAsync(CompletionRequest request)
{
return await HttpPost<CompletionResult>(postData: request);
return await HttpPostAsync<CompletionResult>(postData: request);
}

/// <summary>
@@ -153,7 +153,7 @@ public async Task StreamCompletionAsync(CompletionRequest request, Action<Comple
public IAsyncEnumerable<CompletionResult> StreamCompletionEnumerableAsync(CompletionRequest request)
{
request = new CompletionRequest(request) { Stream = true };
return HttpStreamingRequest<CompletionResult>(Url, HttpMethod.Post, request);
return HttpStreamingRequestAsync<CompletionResult>(Url, HttpMethod.Post, request);
}

/// <summary>
@@ -211,7 +211,7 @@ public IAsyncEnumerable<CompletionResult> StreamCompletionEnumerableAsync(string
/// </summary>
/// <param name="request">The request to send to the API. This does not fall back to default values specified in <see cref="DefaultCompletionRequestArgs"/>.</param>
/// <returns>A string of the prompt followed by the best completion</returns>
public async Task<string> CreateAndFormatCompletion(CompletionRequest request)
public async Task<string> CreateAndFormatCompletionAsync(CompletionRequest request)
{
string prompt = request.Prompt;
var result = await CreateCompletionAsync(request);
@@ -223,7 +223,7 @@ public async Task<string> CreateAndFormatCompletion(CompletionRequest request)
/// </summary>
/// <param name="prompt">The prompt to complete</param>
/// <returns>The best completion</returns>
public async Task<string> GetCompletion(string prompt)
public async Task<string> GetCompletionAsync(string prompt)
{
CompletionRequest request = new CompletionRequest(DefaultCompletionRequestArgs)
{
4 changes: 2 additions & 2 deletions OpenAI_API/Completions/ICompletionEndpoint.cs
Original file line number Diff line number Diff line change
@@ -123,13 +123,13 @@ IAsyncEnumerable<CompletionResult> StreamCompletionEnumerableAsync(string prompt
/// </summary>
/// <param name="request">The request to send to the API. This does not fall back to default values specified in <see cref="DefaultCompletionRequestArgs"/>.</param>
/// <returns>A string of the prompt followed by the best completion</returns>
Task<string> CreateAndFormatCompletion(CompletionRequest request);
Task<string> CreateAndFormatCompletionAsync(CompletionRequest request);

/// <summary>
/// Simply returns the best completion
/// </summary>
/// <param name="prompt">The prompt to complete</param>
/// <returns>The best completion</returns>
Task<string> GetCompletion(string prompt);
Task<string> GetCompletionAsync(string prompt);
}
}
2 changes: 1 addition & 1 deletion OpenAI_API/Embedding/EmbeddingEndpoint.cs
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ public async Task<EmbeddingResult> CreateEmbeddingAsync(string input)
/// <returns>Asynchronously returns the embedding result. Look in its <see cref="Data.Embedding"/> property of <see cref="EmbeddingResult.Data"/> to find the vector of floating point numbers</returns>
public async Task<EmbeddingResult> CreateEmbeddingAsync(EmbeddingRequest request)
{
return await HttpPost<EmbeddingResult>(postData: request);
return await HttpPostAsync<EmbeddingResult>(postData: request);
}

/// <summary>
30 changes: 15 additions & 15 deletions OpenAI_API/EndpointBase.cs
Original file line number Diff line number Diff line change
@@ -101,7 +101,7 @@ protected string GetErrorMessage(string resultAsString, HttpResponseMessage resp
/// <param name="streaming">(optional) If true, streams the response. Otherwise waits for the entire response before returning.</param>
/// <returns>The HttpResponseMessage of the response, which is confirmed to be successful.</returns>
/// <exception cref="HttpRequestException">Throws an exception if a non-success HTTP response was returned</exception>
private async Task<HttpResponseMessage> HttpRequestRaw(string url = null, HttpMethod verb = null, object postData = null, bool streaming = false)
private async Task<HttpResponseMessage> HttpRequestRawAsync(string url = null, HttpMethod verb = null, object postData = null, bool streaming = false)
{
if (string.IsNullOrEmpty(url))
url = this.Url;
@@ -166,9 +166,9 @@ private async Task<HttpResponseMessage> HttpRequestRaw(string url = null, HttpMe
/// <param name="url">(optional) If provided, overrides the url endpoint for this request. If omitted, then <see cref="Url"/> will be used.</param>
/// <returns>The text string of the response, which is confirmed to be successful.</returns>
/// <exception cref="HttpRequestException">Throws an exception if a non-success HTTP response was returned</exception>
internal async Task<string> HttpGetContent<T>(string url = null)
internal async Task<string> HttpGetContentAsync<T>(string url = null)
{
var response = await HttpRequestRaw(url);
var response = await HttpRequestRawAsync(url);
return await response.Content.ReadAsStringAsync();
}

@@ -182,9 +182,9 @@ internal async Task<string> HttpGetContent<T>(string url = null)
/// <param name="postData">(optional) A json-serializable object to include in the request body.</param>
/// <returns>An awaitable Task with the parsed result of type <typeparamref name="T"/></returns>
/// <exception cref="HttpRequestException">Throws an exception if a non-success HTTP response was returned or if the result couldn't be parsed.</exception>
private async Task<T> HttpRequest<T>(string url = null, HttpMethod verb = null, object postData = null) where T : ApiResultBase
private async Task<T> HttpRequestAsync<T>(string url = null, HttpMethod verb = null, object postData = null) where T : ApiResultBase
{
var response = await HttpRequestRaw(url, verb, postData);
var response = await HttpRequestRawAsync(url, verb, postData);
string resultAsString = await response.Content.ReadAsStringAsync();

var res = JsonConvert.DeserializeObject<T>(resultAsString);
@@ -246,9 +246,9 @@ private async Task<T> StreamingHttpRequest<T>(string url = null, HttpMethod verb
/// <param name="url">(optional) If provided, overrides the url endpoint for this request. If omitted, then <see cref="Url"/> will be used.</param>
/// <returns>An awaitable Task with the parsed result of type <typeparamref name="T"/></returns>
/// <exception cref="HttpRequestException">Throws an exception if a non-success HTTP response was returned or if the result couldn't be parsed.</exception>
internal async Task<T> HttpGet<T>(string url = null) where T : ApiResultBase
internal async Task<T> HttpGetAsync<T>(string url = null) where T : ApiResultBase
{
return await HttpRequest<T>(url, HttpMethod.Get);
return await HttpRequestAsync<T>(url, HttpMethod.Get);
}

/// <summary>
@@ -259,9 +259,9 @@ internal async Task<T> HttpGet<T>(string url = null) where T : ApiResultBase
/// <param name="postData">(optional) A json-serializable object to include in the request body.</param>
/// <returns>An awaitable Task with the parsed result of type <typeparamref name="T"/></returns>
/// <exception cref="HttpRequestException">Throws an exception if a non-success HTTP response was returned or if the result couldn't be parsed.</exception>
internal async Task<T> HttpPost<T>(string url = null, object postData = null) where T : ApiResultBase
internal async Task<T> HttpPostAsync<T>(string url = null, object postData = null) where T : ApiResultBase
{
return await HttpRequest<T>(url, HttpMethod.Post, postData);
return await HttpRequestAsync<T>(url, HttpMethod.Post, postData);
}

/// <summary>
@@ -272,9 +272,9 @@ internal async Task<T> HttpPost<T>(string url = null, object postData = null) wh
/// <param name="postData">(optional) A json-serializable object to include in the request body.</param>
/// <returns>An awaitable Task with the parsed result of type <typeparamref name="T"/></returns>
/// <exception cref="HttpRequestException">Throws an exception if a non-success HTTP response was returned or if the result couldn't be parsed.</exception>
internal async Task<T> HttpDelete<T>(string url = null, object postData = null) where T : ApiResultBase
internal async Task<T> HttpDeleteAsync<T>(string url = null, object postData = null) where T : ApiResultBase
{
return await HttpRequest<T>(url, HttpMethod.Delete, postData);
return await HttpRequestAsync<T>(url, HttpMethod.Delete, postData);
}


@@ -286,9 +286,9 @@ internal async Task<T> HttpDelete<T>(string url = null, object postData = null)
/// <param name="postData">(optional) A json-serializable object to include in the request body.</param>
/// <returns>An awaitable Task with the parsed result of type <typeparamref name="T"/></returns>
/// <exception cref="HttpRequestException">Throws an exception if a non-success HTTP response was returned or if the result couldn't be parsed.</exception>
internal async Task<T> HttpPut<T>(string url = null, object postData = null) where T : ApiResultBase
internal async Task<T> HttpPutAsync<T>(string url = null, object postData = null) where T : ApiResultBase
{
return await HttpRequest<T>(url, HttpMethod.Put, postData);
return await HttpRequestAsync<T>(url, HttpMethod.Put, postData);
}


@@ -336,9 +336,9 @@ private async IAsyncEnumerable<string> HttpStreamingRequestRaw(string url = null
/// <param name="postData">(optional) A json-serializable object to include in the request body.</param>
/// <returns>The HttpResponseMessage of the response, which is confirmed to be successful.</returns>
/// <exception cref="HttpRequestException">Throws an exception if a non-success HTTP response was returned</exception>
protected async IAsyncEnumerable<T> HttpStreamingRequest<T>(string url = null, HttpMethod verb = null, object postData = null) where T : ApiResultBase
protected async IAsyncEnumerable<T> HttpStreamingRequestAsync<T>(string url = null, HttpMethod verb = null, object postData = null) where T : ApiResultBase
{
var response = await HttpRequestRaw(url, verb, postData, true);
var response = await HttpRequestRawAsync(url, verb, postData, true);

string organization = null;
string requestId = null;
10 changes: 5 additions & 5 deletions OpenAI_API/Files/FilesEndpoint.cs
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ internal FilesEndpoint(OpenAIAPI api) : base(api) { }
/// <exception cref="HttpRequestException"></exception>
public async Task<List<File>> GetFilesAsync()
{
return (await HttpGet<FilesData>()).Data;
return (await HttpGetAsync<FilesData>()).Data;
}

/// <summary>
@@ -39,7 +39,7 @@ public async Task<List<File>> GetFilesAsync()
/// <returns></returns>
public async Task<File> GetFileAsync(string fileId)
{
return await HttpGet<File>($"{Url}/{fileId}");
return await HttpGetAsync<File>($"{Url}/{fileId}");
}


@@ -50,7 +50,7 @@ public async Task<File> GetFileAsync(string fileId)
/// <returns></returns>
public async Task<string> GetFileContentAsStringAsync(string fileId)
{
return await HttpGetContent<File>($"{Url}/{fileId}/content");
return await HttpGetContentAsync<File>($"{Url}/{fileId}/content");
}

/// <summary>
@@ -60,7 +60,7 @@ public async Task<string> GetFileContentAsStringAsync(string fileId)
/// <returns></returns>
public async Task<File> DeleteFileAsync(string fileId)
{
return await HttpDelete<File>($"{Url}/{fileId}");
return await HttpDeleteAsync<File>($"{Url}/{fileId}");
}


@@ -77,7 +77,7 @@ public async Task<File> UploadFileAsync(string filePath, string purpose = "fine-
{ new ByteArrayContent(System.IO.File.ReadAllBytes(filePath)), "file", Path.GetFileName(filePath) }
};

return await HttpPost<File>(Url, content);
return await HttpPostAsync<File>(Url, content);
}

/// <summary>
2 changes: 1 addition & 1 deletion OpenAI_API/Images/ImageGenerationEndpoint.cs
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ public async Task<ImageResult> CreateImageAsync(string input)
/// <returns>Asynchronously returns the image result. Look in its <see cref="Data.Url"/> </returns>
public async Task<ImageResult> CreateImageAsync(ImageGenerationRequest request)
{
return await HttpPost<ImageResult>(postData: request);
return await HttpPostAsync<ImageResult>(postData: request);
}
}
}
4 changes: 2 additions & 2 deletions OpenAI_API/Model/ModelsEndpoint.cs
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ internal ModelsEndpoint(OpenAIAPI api) : base(api) { }
/// <returns>Asynchronously returns the <see cref="Model"/> with all available properties</returns>
public async Task<Model> RetrieveModelDetailsAsync(string id)
{
string resultAsString = await HttpGetContent<JsonHelperRoot>($"{Url}/{id}");
string resultAsString = await HttpGetContentAsync<JsonHelperRoot>($"{Url}/{id}");
var model = JsonConvert.DeserializeObject<Model>(resultAsString);
return model;
}
@@ -39,7 +39,7 @@ public async Task<Model> RetrieveModelDetailsAsync(string id)
/// <returns>Asynchronously returns the list of all <see cref="Model"/>s</returns>
public async Task<List<Model>> GetModelsAsync()
{
return (await HttpGet<JsonHelperRoot>()).data;
return (await HttpGetAsync<JsonHelperRoot>()).data;
}

/// <summary>
2 changes: 1 addition & 1 deletion OpenAI_API/Moderation/ModerationEndpoint.cs
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ public async Task<ModerationResult> CallModerationAsync(string input)
/// <returns>Asynchronously returns the classification result</returns>
public async Task<ModerationResult> CallModerationAsync(ModerationRequest request)
{
return await HttpPost<ModerationResult>(postData: request);
return await HttpPostAsync<ModerationResult>(postData: request);
}
}
}
6 changes: 3 additions & 3 deletions OpenAI_Tests/AuthTests.cs
Original file line number Diff line number Diff line change
@@ -110,17 +110,17 @@ public void ParseKey()
public async Task TestBadKey()
{
var auth = new OpenAI_API.APIAuthentication("pk-testAA");
Assert.IsFalse(await auth.ValidateAPIKey());
Assert.IsFalse(await auth.ValidateAPIKeyAsync());

auth = new OpenAI_API.APIAuthentication(null);
Assert.IsFalse(await auth.ValidateAPIKey());
Assert.IsFalse(await auth.ValidateAPIKeyAsync());
}

[Test]
public async Task TestValidateGoodKey()
{
var auth = new OpenAI_API.APIAuthentication(Environment.GetEnvironmentVariable("TEST_OPENAI_SECRET_KEY"));
Assert.IsTrue(await auth.ValidateAPIKey());
Assert.IsTrue(await auth.ValidateAPIKeyAsync());
}

}
4 changes: 2 additions & 2 deletions OpenAI_Tests/ChatEndpointTests.cs
Original file line number Diff line number Diff line change
@@ -97,12 +97,12 @@ public void ChatBackAndForth()
chat.AppendUserInput("Is this an animal? House");
chat.AppendExampleChatbotOutput("No");
chat.AppendUserInput("Is this an animal? Dog");
string res = chat.GetResponseFromChatbot().Result;
string res = chat.GetResponseFromChatbotAsync().Result;
Assert.NotNull(res);
Assert.IsNotEmpty(res);
Assert.AreEqual("Yes", res.Trim());
chat.AppendUserInput("Is this an animal? Chair");
res = chat.GetResponseFromChatbot().Result;
res = chat.GetResponseFromChatbotAsync().Result;
Assert.NotNull(res);
Assert.IsNotEmpty(res);
Assert.AreEqual("No", res.Trim());