From e616d0c6c23038a80cb7b642425e48a8003c17e8 Mon Sep 17 00:00:00 2001 From: Arjan Spieard | Asgard Sings! Date: Fri, 10 Mar 2023 14:17:14 +0100 Subject: [PATCH] Async suffix added so the intent is clear and there's no ambiguity. --- OpenAI_API/APIAuthentication.cs | 2 +- OpenAI_API/Chat/ChatEndpoint.cs | 4 +-- OpenAI_API/Chat/Conversation.cs | 4 +-- OpenAI_API/Completions/CompletionEndpoint.cs | 8 ++--- OpenAI_API/Completions/ICompletionEndpoint.cs | 4 +-- OpenAI_API/Embedding/EmbeddingEndpoint.cs | 2 +- OpenAI_API/EndpointBase.cs | 30 +++++++++---------- OpenAI_API/Files/FilesEndpoint.cs | 10 +++---- OpenAI_API/Images/ImageGenerationEndpoint.cs | 2 +- OpenAI_API/Model/ModelsEndpoint.cs | 4 +-- OpenAI_API/Moderation/ModerationEndpoint.cs | 2 +- OpenAI_Tests/AuthTests.cs | 6 ++-- OpenAI_Tests/ChatEndpointTests.cs | 4 +-- 13 files changed, 41 insertions(+), 41 deletions(-) diff --git a/OpenAI_API/APIAuthentication.cs b/OpenAI_API/APIAuthentication.cs index 4346281..26e6458 100644 --- a/OpenAI_API/APIAuthentication.cs +++ b/OpenAI_API/APIAuthentication.cs @@ -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. /// /// if the api key is valid, or if empty or not accepted by the OpenAI API. - public async Task ValidateAPIKey() + public async Task ValidateAPIKeyAsync() { if (string.IsNullOrEmpty(ApiKey)) return false; diff --git a/OpenAI_API/Chat/ChatEndpoint.cs b/OpenAI_API/Chat/ChatEndpoint.cs index 1dee60b..286a8a3 100644 --- a/OpenAI_API/Chat/ChatEndpoint.cs +++ b/OpenAI_API/Chat/ChatEndpoint.cs @@ -47,7 +47,7 @@ public Conversation CreateConversation() /// Asynchronously returns the completion result. Look in its property for the results. public async Task CreateChatCompletionAsync(ChatRequest request) { - return await HttpPost(postData: request); + return await HttpPostAsync(postData: request); } /// @@ -167,7 +167,7 @@ public async Task StreamChatAsync(ChatRequest request, Action result public IAsyncEnumerable StreamChatEnumerableAsync(ChatRequest request) { request = new ChatRequest(request) { Stream = true }; - return HttpStreamingRequest(Url, HttpMethod.Post, request); + return HttpStreamingRequestAsync(Url, HttpMethod.Post, request); } /// diff --git a/OpenAI_API/Chat/Conversation.cs b/OpenAI_API/Chat/Conversation.cs index 6bd1ac7..11e9d31 100644 --- a/OpenAI_API/Chat/Conversation.cs +++ b/OpenAI_API/Chat/Conversation.cs @@ -40,7 +40,7 @@ public OpenAI_API.Models.Model Model } /// - /// After calling , this contains the full response object which can contain useful metadata like token usages, , etc. This is overwritten with every call to and only contains the most recent result. + /// After calling , this contains the full response object which can contain useful metadata like token usages, , etc. This is overwritten with every call to and only contains the most recent result. /// 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 as an . /// /// The string of the response from the chatbot API - public async Task GetResponseFromChatbot() + public async Task GetResponseFromChatbotAsync() { ChatRequest req = new ChatRequest(RequestParameters); req.Messages = _Messages.ToList(); diff --git a/OpenAI_API/Completions/CompletionEndpoint.cs b/OpenAI_API/Completions/CompletionEndpoint.cs index 519d70a..04743f6 100644 --- a/OpenAI_API/Completions/CompletionEndpoint.cs +++ b/OpenAI_API/Completions/CompletionEndpoint.cs @@ -36,7 +36,7 @@ internal CompletionEndpoint(OpenAIAPI api) : base(api) { } /// Asynchronously returns the completion result. Look in its property for the completions. public async Task CreateCompletionAsync(CompletionRequest request) { - return await HttpPost(postData: request); + return await HttpPostAsync(postData: request); } /// @@ -153,7 +153,7 @@ public async Task StreamCompletionAsync(CompletionRequest request, Action StreamCompletionEnumerableAsync(CompletionRequest request) { request = new CompletionRequest(request) { Stream = true }; - return HttpStreamingRequest(Url, HttpMethod.Post, request); + return HttpStreamingRequestAsync(Url, HttpMethod.Post, request); } /// @@ -211,7 +211,7 @@ public IAsyncEnumerable StreamCompletionEnumerableAsync(string /// /// The request to send to the API. This does not fall back to default values specified in . /// A string of the prompt followed by the best completion - public async Task CreateAndFormatCompletion(CompletionRequest request) + public async Task CreateAndFormatCompletionAsync(CompletionRequest request) { string prompt = request.Prompt; var result = await CreateCompletionAsync(request); @@ -223,7 +223,7 @@ public async Task CreateAndFormatCompletion(CompletionRequest request) /// /// The prompt to complete /// The best completion - public async Task GetCompletion(string prompt) + public async Task GetCompletionAsync(string prompt) { CompletionRequest request = new CompletionRequest(DefaultCompletionRequestArgs) { diff --git a/OpenAI_API/Completions/ICompletionEndpoint.cs b/OpenAI_API/Completions/ICompletionEndpoint.cs index 1f53010..d80caec 100644 --- a/OpenAI_API/Completions/ICompletionEndpoint.cs +++ b/OpenAI_API/Completions/ICompletionEndpoint.cs @@ -123,13 +123,13 @@ IAsyncEnumerable StreamCompletionEnumerableAsync(string prompt /// /// The request to send to the API. This does not fall back to default values specified in . /// A string of the prompt followed by the best completion - Task CreateAndFormatCompletion(CompletionRequest request); + Task CreateAndFormatCompletionAsync(CompletionRequest request); /// /// Simply returns the best completion /// /// The prompt to complete /// The best completion - Task GetCompletion(string prompt); + Task GetCompletionAsync(string prompt); } } \ No newline at end of file diff --git a/OpenAI_API/Embedding/EmbeddingEndpoint.cs b/OpenAI_API/Embedding/EmbeddingEndpoint.cs index a324de9..80bf614 100644 --- a/OpenAI_API/Embedding/EmbeddingEndpoint.cs +++ b/OpenAI_API/Embedding/EmbeddingEndpoint.cs @@ -42,7 +42,7 @@ public async Task CreateEmbeddingAsync(string input) /// Asynchronously returns the embedding result. Look in its property of to find the vector of floating point numbers public async Task CreateEmbeddingAsync(EmbeddingRequest request) { - return await HttpPost(postData: request); + return await HttpPostAsync(postData: request); } /// diff --git a/OpenAI_API/EndpointBase.cs b/OpenAI_API/EndpointBase.cs index 66a4ee0..89f4a3c 100644 --- a/OpenAI_API/EndpointBase.cs +++ b/OpenAI_API/EndpointBase.cs @@ -101,7 +101,7 @@ protected string GetErrorMessage(string resultAsString, HttpResponseMessage resp /// (optional) If true, streams the response. Otherwise waits for the entire response before returning. /// The HttpResponseMessage of the response, which is confirmed to be successful. /// Throws an exception if a non-success HTTP response was returned - private async Task HttpRequestRaw(string url = null, HttpMethod verb = null, object postData = null, bool streaming = false) + private async Task 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 HttpRequestRaw(string url = null, HttpMe /// (optional) If provided, overrides the url endpoint for this request. If omitted, then will be used. /// The text string of the response, which is confirmed to be successful. /// Throws an exception if a non-success HTTP response was returned - internal async Task HttpGetContent(string url = null) + internal async Task HttpGetContentAsync(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 HttpGetContent(string url = null) /// (optional) A json-serializable object to include in the request body. /// An awaitable Task with the parsed result of type /// Throws an exception if a non-success HTTP response was returned or if the result couldn't be parsed. - private async Task HttpRequest(string url = null, HttpMethod verb = null, object postData = null) where T : ApiResultBase + private async Task HttpRequestAsync(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(resultAsString); @@ -246,9 +246,9 @@ private async Task StreamingHttpRequest(string url = null, HttpMethod verb /// (optional) If provided, overrides the url endpoint for this request. If omitted, then will be used. /// An awaitable Task with the parsed result of type /// Throws an exception if a non-success HTTP response was returned or if the result couldn't be parsed. - internal async Task HttpGet(string url = null) where T : ApiResultBase + internal async Task HttpGetAsync(string url = null) where T : ApiResultBase { - return await HttpRequest(url, HttpMethod.Get); + return await HttpRequestAsync(url, HttpMethod.Get); } /// @@ -259,9 +259,9 @@ internal async Task HttpGet(string url = null) where T : ApiResultBase /// (optional) A json-serializable object to include in the request body. /// An awaitable Task with the parsed result of type /// Throws an exception if a non-success HTTP response was returned or if the result couldn't be parsed. - internal async Task HttpPost(string url = null, object postData = null) where T : ApiResultBase + internal async Task HttpPostAsync(string url = null, object postData = null) where T : ApiResultBase { - return await HttpRequest(url, HttpMethod.Post, postData); + return await HttpRequestAsync(url, HttpMethod.Post, postData); } /// @@ -272,9 +272,9 @@ internal async Task HttpPost(string url = null, object postData = null) wh /// (optional) A json-serializable object to include in the request body. /// An awaitable Task with the parsed result of type /// Throws an exception if a non-success HTTP response was returned or if the result couldn't be parsed. - internal async Task HttpDelete(string url = null, object postData = null) where T : ApiResultBase + internal async Task HttpDeleteAsync(string url = null, object postData = null) where T : ApiResultBase { - return await HttpRequest(url, HttpMethod.Delete, postData); + return await HttpRequestAsync(url, HttpMethod.Delete, postData); } @@ -286,9 +286,9 @@ internal async Task HttpDelete(string url = null, object postData = null) /// (optional) A json-serializable object to include in the request body. /// An awaitable Task with the parsed result of type /// Throws an exception if a non-success HTTP response was returned or if the result couldn't be parsed. - internal async Task HttpPut(string url = null, object postData = null) where T : ApiResultBase + internal async Task HttpPutAsync(string url = null, object postData = null) where T : ApiResultBase { - return await HttpRequest(url, HttpMethod.Put, postData); + return await HttpRequestAsync(url, HttpMethod.Put, postData); } @@ -336,9 +336,9 @@ private async IAsyncEnumerable HttpStreamingRequestRaw(string url = null /// (optional) A json-serializable object to include in the request body. /// The HttpResponseMessage of the response, which is confirmed to be successful. /// Throws an exception if a non-success HTTP response was returned - protected async IAsyncEnumerable HttpStreamingRequest(string url = null, HttpMethod verb = null, object postData = null) where T : ApiResultBase + protected async IAsyncEnumerable HttpStreamingRequestAsync(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; diff --git a/OpenAI_API/Files/FilesEndpoint.cs b/OpenAI_API/Files/FilesEndpoint.cs index 0435a9c..3c61afa 100644 --- a/OpenAI_API/Files/FilesEndpoint.cs +++ b/OpenAI_API/Files/FilesEndpoint.cs @@ -29,7 +29,7 @@ internal FilesEndpoint(OpenAIAPI api) : base(api) { } /// public async Task> GetFilesAsync() { - return (await HttpGet()).Data; + return (await HttpGetAsync()).Data; } /// @@ -39,7 +39,7 @@ public async Task> GetFilesAsync() /// public async Task GetFileAsync(string fileId) { - return await HttpGet($"{Url}/{fileId}"); + return await HttpGetAsync($"{Url}/{fileId}"); } @@ -50,7 +50,7 @@ public async Task GetFileAsync(string fileId) /// public async Task GetFileContentAsStringAsync(string fileId) { - return await HttpGetContent($"{Url}/{fileId}/content"); + return await HttpGetContentAsync($"{Url}/{fileId}/content"); } /// @@ -60,7 +60,7 @@ public async Task GetFileContentAsStringAsync(string fileId) /// public async Task DeleteFileAsync(string fileId) { - return await HttpDelete($"{Url}/{fileId}"); + return await HttpDeleteAsync($"{Url}/{fileId}"); } @@ -77,7 +77,7 @@ public async Task UploadFileAsync(string filePath, string purpose = "fine- { new ByteArrayContent(System.IO.File.ReadAllBytes(filePath)), "file", Path.GetFileName(filePath) } }; - return await HttpPost(Url, content); + return await HttpPostAsync(Url, content); } /// diff --git a/OpenAI_API/Images/ImageGenerationEndpoint.cs b/OpenAI_API/Images/ImageGenerationEndpoint.cs index 06d55a2..1934b87 100644 --- a/OpenAI_API/Images/ImageGenerationEndpoint.cs +++ b/OpenAI_API/Images/ImageGenerationEndpoint.cs @@ -40,7 +40,7 @@ public async Task CreateImageAsync(string input) /// Asynchronously returns the image result. Look in its public async Task CreateImageAsync(ImageGenerationRequest request) { - return await HttpPost(postData: request); + return await HttpPostAsync(postData: request); } } } diff --git a/OpenAI_API/Model/ModelsEndpoint.cs b/OpenAI_API/Model/ModelsEndpoint.cs index da13e78..f16be0b 100644 --- a/OpenAI_API/Model/ModelsEndpoint.cs +++ b/OpenAI_API/Model/ModelsEndpoint.cs @@ -28,7 +28,7 @@ internal ModelsEndpoint(OpenAIAPI api) : base(api) { } /// Asynchronously returns the with all available properties public async Task RetrieveModelDetailsAsync(string id) { - string resultAsString = await HttpGetContent($"{Url}/{id}"); + string resultAsString = await HttpGetContentAsync($"{Url}/{id}"); var model = JsonConvert.DeserializeObject(resultAsString); return model; } @@ -39,7 +39,7 @@ public async Task RetrieveModelDetailsAsync(string id) /// Asynchronously returns the list of all s public async Task> GetModelsAsync() { - return (await HttpGet()).data; + return (await HttpGetAsync()).data; } /// diff --git a/OpenAI_API/Moderation/ModerationEndpoint.cs b/OpenAI_API/Moderation/ModerationEndpoint.cs index 1fe6069..545a99f 100644 --- a/OpenAI_API/Moderation/ModerationEndpoint.cs +++ b/OpenAI_API/Moderation/ModerationEndpoint.cs @@ -45,7 +45,7 @@ public async Task CallModerationAsync(string input) /// Asynchronously returns the classification result public async Task CallModerationAsync(ModerationRequest request) { - return await HttpPost(postData: request); + return await HttpPostAsync(postData: request); } } } diff --git a/OpenAI_Tests/AuthTests.cs b/OpenAI_Tests/AuthTests.cs index e791c81..d5f9c85 100644 --- a/OpenAI_Tests/AuthTests.cs +++ b/OpenAI_Tests/AuthTests.cs @@ -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()); } } diff --git a/OpenAI_Tests/ChatEndpointTests.cs b/OpenAI_Tests/ChatEndpointTests.cs index 174a7f9..689873f 100644 --- a/OpenAI_Tests/ChatEndpointTests.cs +++ b/OpenAI_Tests/ChatEndpointTests.cs @@ -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());