Search Embeddings#
Let's consider an example of a robot that is the second part of the project "AI Chat With Own Document" (in the first part, the robot computed embeddings from fragments of the specified text file and recorded them in a CSV file). This robot uses previously obtained embeddings and the ChatGPT neural network to answer any questions about the content of the document. That is, when using this robot, any natural language queries can be entered, and the neural network will provide an answer based on the content of the document.
The project of the robot consists of a single diagram. Thus, to describe step by step, this robot works as follows:
- Loads the CSV file created in the first part of the project with text fragments and their corresponding embeddings.
- Opens a window for inputting a query.
- Executes the user's query.
- If the user entered a string (word), it retrieves embeddings for that string (word).
- Sends a request to the GPT model.
- Opens a window with the response to the request to the GPT model.
- After that, it opens a window for inputting a query and executes the user's request.
The project diagram looks like this (for convenience, the blocks of the diagram are numbered):
- Start Block (any diagram begins with this block).
- The "Load from CSV" block allows loading a Data Table from a CSV document. The following properties are specified for this block:
- File path (the path to the file from which the Data Table needs to be loaded);
- Separator (the separator character);
- Encoding (the file encoding).
- Text Input Window Block shows a modal window designed to receive information from the user. The following properties are specified for this block:
- Title (the text that will be reflected in the dialog window title);
- Message (the text that will be reflected inside the dialog window).
- Condition Block checks the specified condition for truth, after which the execution of the scenario continues towards the "Yes" exit (if the condition is met) or towards the "No" exit (if the condition is not met).
The condition is recorded in the format: "variable" equals (==)/ greater than (>)/ less than (<) "value".
For example: $a == "Hello", that is, if the value of variable $a is equal to "Hello", then the exit is "Yes", otherwise – the exit is "No".
$Result > 5, that is, if the value of variable $Result is less than 5, then the exit is "Yes", otherwise – the exit is "No".
In this case, the condition is set as: $TextQuery.Length == 0. That is, if the length of the string is zero, then the exit is "Yes".
- Get Embeddings Block allows obtaining embeddings for a string or list of strings using the Open AI service. The following properties are specified for this block:
- Text (the input text for which embeddings will be calculated);
- Model (the neural network model for generating the response);
- Timeout (the maximum waiting time for a response in seconds).
- Log Block allows outputting arbitrary messages and/or variable values to the log during the robot's scenario execution. The property "Value" is specified for this block. A text constant is indicated in quotes, and the variable name starts with the symbol $. That is, this block logs the embeddings obtained from the user's string in the previous block.
- Find Embeddings Block searches for the most similar strings in the knowledge base, which is a table of objects and their embeddings, based on the given embedding query and returns the indices of these strings as a list sorted by descending similarity. The embeddings for both the query and the embeddings table being searched can be obtained using the Get Embeddings block. When the block is used for the first time, a vector database is built, which may take a long time. In subsequent calls to the block with the same "DB ID", the already created vector database will be used, and the speed of the block's operation increases significantly. The following properties are specified for this block:
- Query embedding (the embedding for which similar strings need to be found in the table);
- Knowledge base (the table in which the search will be conducted, one of the columns of which must contain embeddings, while other columns can contain any user data);
- Embeddings column (the index of the column from the table (default value is set to 1), specified in the "Knowledge base" property, where the embeddings are located);
- Number of results (the maximum number of result rows returned by the block from the knowledge base (default value is set to 5).
- Assign Value to Variable Block sets new values for one or more variables. In this case, two values are specified in the properties that need to be assigned to the variables: the variable $AIQuery should be assigned the value "Answer the question using only knowledge base listed below.`nQuestion: {query}`nKnowledge base: `n{strresult}", and the variable $strresult – an empty value (for subsequent writing of the response to the user's query).
- For Each (List) Loop Block iterates over all elements in the specified list.
- Assign Value to Variable Block sets new values for one or more variables. In this case, one value is specified in the properties that needs to be assigned to the variable.
- Assign Value to Variable Block sets new values for one or more variables. In this case, two values are specified in the properties that need to be assigned to the specified variables.
- Log Block allows outputting arbitrary messages and/or variable values to the log during the robot's scenario execution. The property "Value" is specified for this block. A text constant is indicated in quotes, and the variable name starts with the symbol $. That is, this block logs the value of the variable $AIQuery (the resulting query to GPT). In turn, the variable $AIQuery includes the user's text and the Knowledge Base.
- Request to GPT Model Block allows sending requests to classic generative models from Open AI prior to ChatGPT and receiving a response. The following properties are specified for this block:
- Request (a request in natural language);
- Model (the model for generating text);
- Temperature (a decimal number from 0 to 1, indicating the degree of "randomness" or "creativity" of the result, where 0 is the least creative result and 1 is the most random);
- Timeout (the maximum waiting time for a response in seconds).
- Message Window Block shows the user a modal dialog window with a specified title, text, and set of buttons. The following properties are specified for this block:
- Title (the text that will be displayed in the dialog window title);
- Message (the variable that will be displayed inside the dialog window, in this case, it is the variable $Response, which is the response of the generative model to the request);
- Buttons (the buttons that will be available for the user to click).
15. End Block (this block concludes the execution of the scenario or returns the subprocess diagram to the main process).