Skip to content

OpenAiService.ChatCompletion.CreateCompletion Exception: The input does not contain any JSON tokens (Root cause: HttpClient.Timeout) #207

@chaojian-zhang

Description

@chaojian-zhang

Starting Words

Again, thank you very much for this handy library and sorry to have to bother you again🥲

Describe the bug
Occasionally when using the API, the service call itself will error out and cause an exception.
Unfortunately I don't know the shortest possible input to replicate this issue so please use the long version below.

Your code piece

var system = "In this setup, you are going to help the user to generate text-based contents according to their specifications.";
var user = """
In this setup, you are going to help me populate a database that represents personel information of a company. The database is going to be described using Prolog, specifically, the SWI implementation of Prolog.

See below snippet for schema and some information:

```prolog
human('Charles Wang', 55, male, 'Professional Engineer').
human('Alice Yu', 27, female, 'Software Developer').
human('Bob Jones', 30, male, 'Doctor').
human('Cynthia Lee', 25, female, 'Nurse').
human('David Smith', 5, male, 'Student').
father('Charles Wang', 'Alice Yu').
father('Charles Wang', 'Bob Jones').
mother('Cynthia Lee', 'David Smith').

father(X, Y) :- human(X, _, male, _), father(X, Y).
mother(X, Y) :- human(X, _, female, _), father(X, Y).
parent(X, Y) :- father(X, Y).
parent(X, Y) :- mother(X, Y).
son(X, Y) :- human(X, _, male, _), parent(Y, X).
daughter(X, Y) :- human(X, _, female, _), parent(Y, X).
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
grandfather(X, Y) :- grandparent(X, Y), human(X, _, male, _).
grandmother(X, Y) :- grandparent(X, Y), human(X, _, female, _).

company('Top Tech', 'Technology Consultation', 'Toronto', 150).
employee('Charles Wang', 'Top Tech', 2007, 125000).
employee('Thomas Ho', 'Top Tech', 2002, 140000).
employee('Sarah Adams', 'Top Tech', 2010, 100000).
employee('Jenny Smith', 'Top Tech', 2015, 90000).
teacher('William Brown', 'Top Tech', 2006, 110000).
manager('Thomas Ho', 'Charles Wang').
manager('Thomas Ho', 'Sarah Adams').
supervisor('Charles Wang', 'Jenny Smith').
supervisor('Sarah Adams', 'William Brown').
directly_reports_to(Subordinate, Manager) :- manager(Manager, Subordinate).
directly_reports_to(Subordinate, Supervisor) :- supervisor(Supervisor, Subordinate).
indirectly_reports_to(Subordinate, Manager) :- directly_reports_to(Subordinate, Supervisor), directly_reports_to(Supervisor, Manager).
team_member_of(EmployeeA, EmployeeB) :-
    directly_reports_to(EmployeeA, Manager),
    directly_reports_to(EmployeeB, Manager),
    EmployeeA \== EmployeeB.
```

Where:

1. `human` describes character name, age, gender and current profession/title.
2. `company` describes company name, company type, location, and number of employees.
3. `employee` describes employee name, employer name, start year, and current salary.

You may fix any syntax/grammar errors in existing code if necessary.
Now, help me populate a database with 25 employee information for a fictional VR hardware (technology) company named Dreamscape, where a majority of employee have an Asian background but most have adopted western first names; Other employees are local Canadians. The database should also populate all relative or related person information, i.e. people with direct relations to those 25 people, like parents, and children.
""";

Result

Run with:

OpenAIService OpenAiService = new OpenAIService(new OpenAiOptions()
{
    ApiKey = "Key......................................"
});
var CurrentModel = Models.Gpt_4;
var CurrentMaxTokenLimit = 8192;
var messages = new ChatMessage[]
{
    ChatMessage.FromSystem(system),
    ChatMessage.FromUser(user),
}
var completionResult = await OpenAiService.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest()
{
    Messages = messages,
    Model = CurrentModel,
    MaxTokens = CurrentMaxTokenLimit - messages.Sum(m => m.Content.Length) - 21, /*MAX is 4096 including inputs and outputs; The additional 21 (sometimes 20) seems like the default input token size.*/
    Temperature = 1
});

And you will encounter a System.Text.Json.JsonException: 'The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.'

Expected behavior

Even if the service cannot handle it, it should not throw an exception - I thought that's what the completionResult.Successful should be checking.

Screenshots

image

Desktop (please complete the following information):

  • OS: Windows 11
  • Language: C# Net 7
  • Version: Betalgo.OpenAI.GPT3 6.8.3

Additional context

Will upload shorter inputs that may trigger the same issue when available.

Complete Code

Copy and paste below code for a fresh Console Application created with Visual Studio targeting Net 7:

using OpenAI.GPT3.ObjectModels.RequestModels;
using OpenAI.GPT3.ObjectModels;
using OpenAI.GPT3.Managers;
using OpenAI.GPT3;

namespace ConsoleApp
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            var system = "In this setup, you are going to help the user to generate text-based contents according to their specifications.";
            var user = """
In this setup, you are going to help me populate a database that represents personel information of a company. The database is going to be described using Prolog, specifically, the SWI implementation of Prolog.

See below snippet for schema and some information:

```prolog
human('Charles Wang', 55, male, 'Professional Engineer').
human('Alice Yu', 27, female, 'Software Developer').
human('Bob Jones', 30, male, 'Doctor').
human('Cynthia Lee', 25, female, 'Nurse').
human('David Smith', 5, male, 'Student').
father('Charles Wang', 'Alice Yu').
father('Charles Wang', 'Bob Jones').
mother('Cynthia Lee', 'David Smith').

father(X, Y) :- human(X, _, male, _), father(X, Y).
mother(X, Y) :- human(X, _, female, _), father(X, Y).
parent(X, Y) :- father(X, Y).
parent(X, Y) :- mother(X, Y).
son(X, Y) :- human(X, _, male, _), parent(Y, X).
daughter(X, Y) :- human(X, _, female, _), parent(Y, X).
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
grandfather(X, Y) :- grandparent(X, Y), human(X, _, male, _).
grandmother(X, Y) :- grandparent(X, Y), human(X, _, female, _).

company('Top Tech', 'Technology Consultation', 'Toronto', 150).
employee('Charles Wang', 'Top Tech', 2007, 125000).
employee('Thomas Ho', 'Top Tech', 2002, 140000).
employee('Sarah Adams', 'Top Tech', 2010, 100000).
employee('Jenny Smith', 'Top Tech', 2015, 90000).
teacher('William Brown', 'Top Tech', 2006, 110000).
manager('Thomas Ho', 'Charles Wang').
manager('Thomas Ho', 'Sarah Adams').
supervisor('Charles Wang', 'Jenny Smith').
supervisor('Sarah Adams', 'William Brown').
directly_reports_to(Subordinate, Manager) :- manager(Manager, Subordinate).
directly_reports_to(Subordinate, Supervisor) :- supervisor(Supervisor, Subordinate).
indirectly_reports_to(Subordinate, Manager) :- directly_reports_to(Subordinate, Supervisor), directly_reports_to(Supervisor, Manager).
team_member_of(EmployeeA, EmployeeB) :-
    directly_reports_to(EmployeeA, Manager),
    directly_reports_to(EmployeeB, Manager),
    EmployeeA \== EmployeeB.
```

Where:

1. `human` describes character name, age, gender and current profession/title.
2. `company` describes company name, company type, location, and number of employees.
3. `employee` describes employee name, employer name, start year, and current salary.

You may fix any syntax/grammar errors in existing code if necessary.
Now, help me populate a database with 25 employee information for a fictional VR hardware (technology) company named Dreamscape, where a majority of employee have an Asian background but most have adopted western first names; Other employees are local Canadians. The database should also populate all relative or related person information, i.e. people with direct relations to those 25 people, like parents, and children.
""";
            var CurrentModel = Models.Gpt_4;
            var CurrentMaxTokenLimit = 8192;
            var messages = new ChatMessage[]
            {
                ChatMessage.FromSystem(system),
                ChatMessage.FromUser(user),
            };
            OpenAIService OpenAiService = new OpenAIService(new OpenAiOptions()
            {
                ApiKey = "Key......................................"
            });
            var completionResult = await OpenAiService.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest()
            {
                Messages = messages,
                Model = CurrentModel,
                MaxTokens = CurrentMaxTokenLimit - messages.Sum(m => m.Content.Length) - 21, /*MAX is 4096 including inputs and outputs; The additional 21 (sometimes 20) seems like the default input token size.*/
                Temperature = 1
            });
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions