procedure TForm1.BtnLoadClick(Sender: TObject);
var
SelectedIndex, SessionID, i, j: Integer;
QA: TQandA;
Role, Content: string;
begin
try
SelectedIndex := Subjects.ItemIndex;
// Retrieve the session ID from the selected item
SessionID := PtrInt(Subjects.Items.Objects[SelectedIndex]);
// Query the database for the session details
SQLQuery.Close; // Ensure SQLQuery is closed before setting new SQL text
SQLQuery.SQL.Text := 'SELECT * FROM sessions WHERE id = :id';
SQLQuery.Params.ParamByName('id').AsInteger := SessionID;
SQLQuery.Open;
if SQLQuery.RecordCount = 0 then
begin
ShowMessage('Session not found.');
SQLQuery.Close;
Exit;
end;
// Assign the session ID to CurrentSessionID
CurrentSessionID := SessionID;
// Load session details into variables
temperature := SQLQuery.FieldByName('temperature').AsFloat;
presence_penalty := SQLQuery.FieldByName('presence_penalty').AsFloat;
frequency_penalty := SQLQuery.FieldByName('frequency_penalty').AsFloat;
max_tokens := SQLQuery.FieldByName('max_tokens').AsInteger;
max_Query := SQLQuery.FieldByName('max_tokens_query').AsInteger;
top_p := SQLQuery.FieldByName('top_p').AsFloat;
SYSTEM_MSG := SQLQuery.FieldByName('system_msg').AsString;
image_mode := SQLQuery.FieldByName('is_image_mode').AsBoolean;
assistant_id := SQLQuery.FieldByName('tool_choice').AsString;
// Load the messages for the session
SQLQuery.Close;
SQLQuery.SQL.Text := 'SELECT * FROM messages WHERE session_id = :session_id ORDER BY id';
SQLQuery.Params.ParamByName('session_id').AsInteger := SessionID;
SQLQuery.Open;
QA := TQandA.Create;
try
while not SQLQuery.EOF and (SQLQuery.FieldByName('session_id').AsInteger = SessionID) do
begin
Role := SQLQuery.FieldByName('role').AsString;
Content := SQLQuery.FieldByName('content').AsString;
if Role = 'user' then
QA.Question.Add(Content)
else if Role = 'assistant' then
QA.Response.Add(Content);
SQLQuery.Next; // <-- This statement jumps back to line 8 for some reason.
end;
except
on E: Exception do
begin
ShowMessage('Error processing messages: ' + E.Message);
QA.Free;
Exit;
end;
end;
// Add the QA object to the Conversation
Conversation.Add(QA); // Variable of type TConversationList = specialize TFPGObjectList<TQandA>;
SQLQuery.Close;
except
on E: Exception do
ShowMessage('Error loading session: ' + E.Message);
end;
end;