Comments (11)
Thank you so much. This is incredible. The reason that I am wanting streaming is that for complex answers, ChatGPT can take over 60 seconds. Within my app, the user would submit a request, and see nothing happen for 60 seconds. If I stream the answer, like the ChatGPT website does, the end result is that the user can start reading within a few seconds, when ChatGPT starts generating the answer.
from delphiopenai.
Because it responds in this case not with a json object, but in its own special format.
Example
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": "\r", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": "\n", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": "1", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": ",", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": " 2", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": ",", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": " 3", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": ",", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": " 4", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": ",", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": " 5", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": ",", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": " 6", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": ",", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": " 7", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": ",", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": " 8", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": ",", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": " 9", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": ",", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": " 10", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
data: [DONE]
from delphiopenai.
You can choose not to use this mode at all and simply output data by manually separating it.
from delphiopenai.
Thank you for your response. In my sample code above, when I turn on streaming, (by uncommenting the line) the OpenAI.Chat.Create call fails after a few moments. It is getting sent to the Chat website, but it apparently is not in the proper format. I never get to the code within the TRY block in order to parse the response.
from delphiopenai.
OpenAI.Completion.CreateStream(
procedure(Params: TCompletionParams)
begin
Params.Prompt(Buf.Text);
Params.MaxTokens(1024);
Params.Stream;
end,
procedure(Response: TStringStream)
begin
Writeln(Response.DataString);
Writeln('-------');
Sleep(100);
end);
I experimentally did this
from delphiopenai.
The event is fired multiple times as data arrives in the stream. Each line (--------) is a new event in the stream response
from delphiopenai.
When I try to use the OpenAI.Completion.CreateStream, I am getting an error that CreateStream is an undeclared identifier. For my Uses clause, I am using:
System.SysUtils,
System.Classes,
OpenAI.Completions,
OpenAI.Chat,
OpenAI;
Do I need to add something else? I am using Delphi 11.1
from delphiopenai.
I haven't released it yet
from delphiopenai.
Pushed a new method for working in stream mode
from delphiopenai.
Example
OpenAI.Chat.CreateStream(
procedure(Params: TChatParams)
begin
Params.Messages([TchatMessageBuild.User(Buf.Text)]);
Params.MaxTokens(1024);
Params.Stream;
end,
procedure(Chat: TChat; IsDone: Boolean; var Cancel: Boolean)
begin
if (not IsDone) and Assigned(Chat) then
Writeln(Chat.Choices[0].Delta.Content)
else if IsDone then
Writeln('DONE!');
Writeln('-------');
Sleep(100);
end);
from delphiopenai.
But, for the same effect of writing a response sequentially, I would still use a regular query with a full answer and draw a conclusion by words manually. This is much easier to do than streaming.
from delphiopenai.
Related Issues (20)
- Can the chat remember the chat history? HOT 6
- Possible bug in TFineTuningCreateParams HOT 3
- Does the "Regenerate" function in ChatGPT is simply resend the original prompt to ChatGPT server again? HOT 2
- Is the first Choice has the highest probablity? HOT 4
- Timeout error? HOT 5
- How to deal with the OpenAI exceptions? HOT 4
- Can OpenAI.Chat.Create works asynchronously? HOT 2
- Add-on to use Vision in this version HOT 4
- OpenAI.Audio.CreateTranscription with SRT format causes OpenAIExceptionInvalidResponse HOT 1
- TAudioTranscription size limit HOT 1
- OpenAI.assistants modifications HOT 3
- Can I set a model in TImageCreateParams? HOT 3
- DallE3 does not support generate multiple images? HOT 1
- Stream mode HOT 2
- Support to text to speech model? HOT 2
- Frequenly get error "The input prompt cannot be handled by the engine" when calling Dalle-3 model HOT 1
- Problems in Delphi 11 HOT 2
- Completion return 'The model `text-davinci-003` has been deprecated, HOT 1
- TFinishReasonHelper.Create procedure give incorrect TFinishReason? HOT 2
- Function calling working, but not Tools HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from delphiopenai.