From 370cff683f8c6d4d9ec927467c9eaed21371efca Mon Sep 17 00:00:00 2001 From: Ernie Date: Sun, 25 Feb 2024 09:53:28 -0800 Subject: [PATCH] fix reporting somewhat, organize stats. Not really any new capabilities, though. --- OpenAI_API/EndpointBase.cs | 32 +++++++++----------------------- README.md | 5 ----- 2 files changed, 9 insertions(+), 28 deletions(-) diff --git a/OpenAI_API/EndpointBase.cs b/OpenAI_API/EndpointBase.cs index b2e4c93..8899413 100644 --- a/OpenAI_API/EndpointBase.cs +++ b/OpenAI_API/EndpointBase.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; + using System; using System.Collections.Generic; using System.Diagnostics; @@ -154,33 +155,18 @@ private async Task HttpRequestRaw(string url = null, HttpMe { throw new HttpRequestException("OpenAI had an internal server error, which can happen occasionally. Please retry your request. " + GetErrorMessage(resultAsString, response, Endpoint, url)); } + else if (resultAsString.IndexOf("504 Gateway Time-out") !=-1) + { + //resultAsString == "\r\n504 Gateway Time-out\r\n\r\n

504 Gateway Time-out

\r\n
openresty/1.21.4.2
\r\n\r\n\r\n + throw new HttpRequestException("OpenAI had a 504 Gateway Time-out, which can happen occasionally. Please retry your request. " + GetErrorMessage(resultAsString, response, Endpoint, url)); + } else { var errorToThrow = new HttpRequestException(GetErrorMessage(resultAsString, response, Endpoint, url)); ApiErrorResponse? parsedError; - try - { - parsedError = JsonConvert.DeserializeObject(resultAsString); - } - catch (Exception ex) - { - //typically gateway timeout 504 - //"\r\n504 Gateway Time-out\r\n\r\n

504 Gateway Time-out

\r\n
openresty/1.21.4.2
\r\n\r\n\r\n" - if (resultAsString == "\r\n504 Gateway Time-out\r\n\r\n

504 Gateway Time-out

\r\n
openresty/1.21.4.2
\r\n\r\n\r\n") - { - parsedError = new ApiErrorResponse(); - parsedError.Error = new ApiErrorResponseError(); - parsedError.Error.Message = "504 Gateway Time-out"; - parsedError.Error.ErrorType = "Gateway Time-out"; - parsedError.Error.Parameter = "N/A"; - parsedError.Error.ErrorCode = "504"; - } - else - { - var a = 3; - throw ex; - } - } + + parsedError = JsonConvert.DeserializeObject(resultAsString); + try { errorToThrow.Data.Add("message", parsedError.Error.Message); diff --git a/README.md b/README.md index ad77597..f5dfc7b 100644 --- a/README.md +++ b/README.md @@ -179,7 +179,6 @@ messageWithImage.images.Add(ImageInput.FromImageUrl("https://rogerpincombe.com/t chat.AppendUserInput("What colors do these logos have in common?", ImageInput.FromFile("path/to/logo.png"), ImageInput.FromImageUrl("https://rogerpincombe.com/templates/rp/center-aligned-no-shadow-small.png")); ``` - #### Conversation History Context Length Management If the chat conversation history gets too long, it may not fit into the context length of the model. By default, the earliest non-system message(s) will be removed from the chat history and the API call will be retried. You may disable this by setting `chat.AutoTruncateOnContextLengthExceeded = false`, or you can override the truncation algorithm like this: @@ -262,8 +261,6 @@ Console.WriteLine(results); */ ``` - - ### Completions API Completions are considered legacy by OpenAI. The Completion API is accessed via `OpenAIAPI.Completions`: @@ -407,7 +404,6 @@ The embedding result contains a lot of metadata, the actual vector of floats is For simplicity, you can directly ask for the vector of floats and disgard the extra metadata with `api.Embeddings.GetEmbeddingsAsync("test text here")` - ### Moderation The Moderation API is accessed via `OpenAIAPI.Moderation`: @@ -424,7 +420,6 @@ Console.WriteLine(result.results[0].MainContentFlag); The results are in `.results[0]` and have nice helper methods like `FlaggedCategories` and `MainContentFlag`. - ### Files (for fine-tuning) The Files API endpoint is accessed via `OpenAIAPI.Files`: