Skip to content
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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions OpenAI_API/Chat/ChatEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
4 changes: 2 additions & 2 deletions OpenAI_API/Chat/Conversation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions OpenAI_API/Completions/CompletionEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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);
Expand All @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions OpenAI_API/Completions/ICompletionEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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>
Expand Down
30 changes: 15 additions & 15 deletions OpenAI_API/EndpointBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand All @@ -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);
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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);
}


Expand All @@ -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);
}


Expand Down Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions OpenAI_API/Files/FilesEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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}");
}


Expand All @@ -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>
Expand All @@ -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}");
}


Expand All @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion OpenAI_API/Images/ImageGenerationEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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;
}
Expand All @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion OpenAI_API/Moderation/ModerationEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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());
}

}
Expand Down
4 changes: 2 additions & 2 deletions OpenAI_Tests/ChatEndpointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down