Skip to content

Commit 2b95fab

Browse files
committed
2 parents 9c10263 + 9d95a65 commit 2b95fab

4 files changed

Lines changed: 74 additions & 2 deletions

File tree

OpenAI-DotNet/Chat/ChatError.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Text.Json.Serialization;
6+
using System.Threading.Tasks;
7+
8+
namespace OpenAI.Chat
9+
{
10+
public class ChatError
11+
{
12+
[JsonConstructor]
13+
public ChatError(string message, string type, string param, string code)
14+
{
15+
Message = message;
16+
Type = type;
17+
Param = param;
18+
Code = code;
19+
}
20+
21+
[JsonInclude]
22+
[JsonPropertyName("message")]
23+
public string Message { get; set; }
24+
25+
[JsonInclude]
26+
[JsonPropertyName("type")]
27+
public string Type { get; set; }
28+
29+
[JsonInclude]
30+
[JsonPropertyName("param")]
31+
public string Param { get; set; }
32+
33+
[JsonInclude]
34+
[JsonPropertyName("code")]
35+
public string Code { get; set; }
36+
}
37+
}

OpenAI-DotNet/Chat/ChatResponse.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public ChatResponse(string id, string @object, int created, string model, Usage
4242
[JsonPropertyName("choices")]
4343
public IReadOnlyList<Choice> Choices { get; private set; }
4444

45+
[JsonInclude]
46+
[JsonPropertyName("error")]
47+
public ChatError? Error { get; set; }
48+
49+
4550
[JsonIgnore]
4651
public Choice? FirstChoice => Choices.FirstOrDefault();
4752
}

OpenAI-DotNet/ResponseExtensions.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using OpenAI.Chat;
2+
using System;
23
using System.Linq;
34
using System.Net.Http;
45
using System.Net.Http.Headers;
@@ -43,7 +44,16 @@ internal static async Task CheckResponseAsync(this HttpResponseMessage response,
4344
if (!response.IsSuccessStatusCode)
4445
{
4546
var responseAsString = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
46-
throw new HttpRequestException(message: $"{methodName} Failed! HTTP status code: {response.StatusCode} | Response body: {responseAsString}", null, statusCode: response.StatusCode);
47+
var responseJson = JsonSerializer.Deserialize<ChatResponse>(responseAsString);
48+
49+
var e = new HttpRequestException(message: $"{methodName} Failed! HTTP status code: {response.StatusCode} | Response body: {responseAsString}", null, statusCode: response.StatusCode);
50+
51+
if (responseJson?.Error is { } error)
52+
{
53+
e.Data.Add("Error", error);
54+
}
55+
56+
throw e;
4757
}
4858
}
4959

OpenGptChat/Views/Pages/ChatPage.xaml.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
 using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
25
using System.Threading.Tasks;
36
using System.Windows;
47
using System.Windows.Controls;
58
using System.Windows.Input;
69
using CommunityToolkit.Mvvm.Input;
10+
using OpenAI.Chat;
711
using OpenGptChat.Models;
812
using OpenGptChat.Services;
913
using OpenGptChat.Utilities;
@@ -118,6 +122,22 @@ await ChatService.ChatAsync(SessionId, input, content =>
118122
{
119123
_ = NoteService.ShowAndWaitAsync($"{ex.GetType().Name}: {ex.Message}", 3000);
120124

125+
var chatError = ex.Data.Cast<DictionaryEntry>()
126+
.Where(v => v.Key as string is "Error")
127+
.Select(v => v.Value as ChatError)
128+
.FirstOrDefault();
129+
130+
if (chatError != null)
131+
{
132+
if (chatError.Code == "context_length_exceeded")
133+
{
134+
if (ViewModel.Messages.Count > 0)
135+
{
136+
Console.WriteLine("token reaches the upper limit");
137+
}
138+
}
139+
}
140+
121141
Rollback(requestMessageModel, responseMessageModel, input);
122142
}
123143

0 commit comments

Comments
 (0)