Once we've converted all our data to vectors, we can start to build our own model, the idea behind is very simple, every time we ask the bot, first we convert the question to a vector, then we search into our database which vector is more similar to the question, and finally we use the most similar data to the question and add it as a context.
procedure AskToChatGPT(const aQuestion: string);
var
oChatBot: TsgcAIOpenAIChatBot;
oEmbeddings: TsgcAIOpenAIEmbeddings;
oFile: TsgcAIDatabaseVectorFile;
vContext: string;
begin
oChatBot := TsgcAIOpenAIChatBot.Create(nil);
Try
oChatBot.OpenAIOptions.ApiKey := '<your api key>';
oEmbeddings := TsgcAIOpenAIEmbeddings.Create(nil);
Try
oChatBot.Embeddings := oEmbeddings;
oFile := TsgcAIDatabaseVectorFile.Create(nil);
Try
oEmbeddings.Database := oFile;
vContext := oChatBot.GetEmbedding(aQuestion);
oChatBot.ChatAsUser('Answer the question based on the context below.\n\nContext:\n' +
vContext + '\nQuestion:' + aQuestion + '\nAnswer:');
Finally
oFile.Free;
End;
Finally
oEmbeddings.Free;
End;
Finally
FreeAndNil(oDialog);
End;
end;