Comments (6)
Official GPT chat sends the entire chat history every time. Those. you need to remember each time which message you generated and which message was the response and, with the subsequent request, supplement the history with your request and send the entire array of data.
The library even has a special class for this, which helps organize the history and send it, while cutting off the excess (from above) if we do not fall within the token limits.
from delphiopenai.
OpenAI.Utils.ChatHistory.pas
from delphiopenai.
History: TChatHistory;
...
History.New(your message)
History.New(answer)
History.New(your message)
History.New(answer)
...
Params.Messages(History.ToArray);
from delphiopenai.
The OpenAI API cannot and does not allow storing history somewhere on its servers.
from delphiopenai.
Hi,
Thank you very much. I try to write the codes as below:
function TMainForm.SendPrompt(Role: TMessageRole; Prompt: string; ChatHistory: TChatHistory): string;
begin
ChatHistory.New(Role, Prompt, '');
var Chat := FOpenAI.Chat.Create(
procedure(Params: TChatParams)
begin
Params.Messages(ChatHistory.ToArray());
Params.Model('gpt-4');
//Params.MaxTokens(1024);
end);
try
for var Choice in Chat.Choices do
begin
ChatHistory.New(TMessageRole.Assistant, Choice.Message.Content, '');
txtChatReply.Lines.Add(Choice.Message.Content);
txtChatReply.Lines.Add('---------------');
end;
finally
Chat.Free;
end;
end;
procedure TMainForm.btnSendPromptClick(Sender: TObject);
var
ChatHistory: TChatHistory;
begin
ChatHistory := TChatHistory.Create;
try
SendPrompt(TMessageRole.System, PromptTemplate4, ChatHistory);
SendPrompt(TMessageRole.User, 'Translate button text: &Repair', ChatHistory);
SendPrompt(TMessageRole.User, 'Translate button text: &Batch Repair', ChatHistory);
finally
ChatHistory.Free;
end;
end;
Is that correct?
Some more questions:
-
What is the difference between using TMessageRole.System and TMessageRole.User. I try to follow the reply at https://community.openai.com/t/prompt-to-handle-shortcut-key-in-translation/365090/3?u=chcw
-
What is the usage of "Tag"?
-
Which role should be used for ChatGPT's reply? Currently I try to use TMessageRole.Assistant.
-
Also what to do if the total token of ChatHistory.ToArray() reaches the limit(4096)?
from delphiopenai.
- TMessageRole.System - type of messages to which the neural network does not respond, but takes it into account (as a context)
- Tag is simply a value that can be used to identify a message within a program. This value is not sent to the server.
- That's right
- This class allows you to configure limits and when adding a new message it will delete old ones (in whole or in part)
This class is not required, it is just an example and a quick solution. You can implement your class for history
from delphiopenai.
Related Issues (20)
- 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
- ollama support? HOT 1
- Example upload file HOT 4
- Support for models deployed via Azure OpenAI 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.