Git Product home page Git Product logo

Comments (6)

HemulGM avatar HemulGM commented on September 22, 2024

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.

HemulGM avatar HemulGM commented on September 22, 2024

OpenAI.Utils.ChatHistory.pas

from delphiopenai.

HemulGM avatar HemulGM commented on September 22, 2024
History: TChatHistory;
...
History.New(your message)
History.New(answer)
History.New(your message)
History.New(answer)
...
Params.Messages(History.ToArray);

from delphiopenai.

HemulGM avatar HemulGM commented on September 22, 2024

The OpenAI API cannot and does not allow storing history somewhere on its servers.

from delphiopenai.

alanccw avatar alanccw commented on September 22, 2024

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:

  1. 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

  2. What is the usage of "Tag"?

  3. Which role should be used for ChatGPT's reply? Currently I try to use TMessageRole.Assistant.

  4. Also what to do if the total token of ChatHistory.ToArray() reaches the limit(4096)?

from delphiopenai.

HemulGM avatar HemulGM commented on September 22, 2024
  1. TMessageRole.System - type of messages to which the neural network does not respond, but takes it into account (as a context)
  2. Tag is simply a value that can be used to identify a message within a program. This value is not sent to the server.
  3. That's right
  4. 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.