The length of the embedding contents

This is one way:

Using this code to construct the returned vectors into context docs:

        // Iterate over the results array to extract the relevant elements
        foreach ($results as $index => $result) {
            // Extract the 'content', 'date', 'groups', and 'taxonomy' elements for each row
            $contextDocument = $result['content'];
            $documentTitle = isset($result['title']) ? $result['title'] : '';
            $documentSummary = isset($result['summary']) ? $result['summary'] : '';
            $documentDate = isset($result['date']) ? $result['date'] : '';
            $documentGroups = isset($result['groups']) ? implode(', ', $result['groups']) : '';
            $documentTaxonomy = isset($result['taxonomy']) ? implode(', ', $result['taxonomy']) : '';
            $documentURL = isset($result['url']) ? $result['url'] : '';
            $documentQuestions = isset($result['questions']) ? $result['questions'] : '';

            // Construct the context document string with labeled elements
            $documentString = "Document Title: '{$documentTitle}'\n";
            $documentString .= "Content: {$contextDocument}\n";
            if ($this->includeSummary === true ) {
                $documentString .= "Source document summary: {$documentSummary}\n";
            }
            $documentString .= "Event Date: {$documentDate}\n";
            $documentString .= "Document Groups: {$documentGroups}\n";
            $documentString .= "Document Taxonomy/Tags: {$documentTaxonomy}\n";
            $documentString .= "URL: {$documentURL}\n";
            if ($this->includeQuestions === true) {
                $documentString .= "Questions that this document answers: {$documentQuestions}\n";
            }
            
            # Debug
            # $this->newOutput .= "Document Title: {$documentTitle}. " . "<br>";        

            // Append the context document string to the prompt content
            $promptContent .= "Context document " . ($index + 1) . ": {$documentString}\n";
            $promptContent .= "-----\n"; // Delimiter to separate context documents
        }

Then,


			// Build the prompt containing question and context documents
			$prompt = $this->solrai_createPromptContent($question, $context);
			# Debug
			# print_r($context) . "<br>";
			# print_r($prompt) . "<br>";
			
			// Initialize the $messages array with the system message
			$messages = array(
				array("role" => "system", "content" => $systemMessage)
			);

			// Define the new user message (question + context docs)
			$newUserMessage = array("role" => "user", "content" => $prompt);
			
			// Append the new user message to the end of the $messages array
			$messages[] = $newUserMessage;
			
			// Get the chat completion with history from the LLM
			$result = $this->solrai_getChatCompletion($messages, $apiOpenAI);

This is php. If you are using python or something else, just ask ChatGPT to translate.

Another thing you may want to consider, depending upon your use case and your users, is to give the end-user options to make choices at the prompt:

1 Like