Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text Vectorisation #121

Closed
ClicShopping opened this issue May 12, 2024 · 2 comments
Closed

text Vectorisation #121

ClicShopping opened this issue May 12, 2024 · 2 comments
Labels
question Further information is requested

Comments

@ClicShopping
Copy link

Hello,

Just a little help. Is it possible to do that ? I tried different way to resolve this point: I do not understand why with a text file it works but with this process it does not work. At the end the embedding is the same ?

Thank you for your help.

$documents = 'I love food when I am in restaurant';
$embeddingGenerator = new OpenAI3LargeEmbeddingGenerator();
$documents = $embeddingGenerator->embedText($documents);

// Créer une instance de MemoryVectorStore et ajouter les documents
$memoryVectorStore = new MemoryVectorStore();
$memoryVectorStore->addDocuments($documents);

// Créer une instance de QuestionAnswering
$qa = new QuestionAnswering(
  $memoryVectorStore,
  $embeddingGenerator,
  new OpenAIChat()
);

// Répondre à la question
$answer = $qa->answerQuestion('Where do you love food?');
echo $answer;

error

HP Warning:  Attempt to read property "embedding" on float in /home/www/......./llphant/src/Embeddings/VectorStores/Memory/MemoryVectorStore.php on line 50
[12-May-2024 18:20:10 UTC] PHP Stack trace:
[12-May-2024 18:20:10 UTC] PHP   1. {main}() /home/www/toto_db.php:0
[12-May-2024 18:20:10 UTC] PHP   2. LLPhant\Query\SemanticSearch\QuestionAnswering->answerQuestion($question = 'Where do you love food?', $k = *uninitialized*, $additionalArguments = *uninitialized*) /home/www/toto_db.php:49

or

$documents = 'I love food when I am in restaurant';
$embeddingGenerator = new OpenAI3LargeEmbeddingGenerator();
$embeddedDocuments = $embeddingGenerator->embedText($documents); 

$documents = [];
foreach ($embeddedDocuments as $embedding) {
  $document = new Document();
  $document->embedding = [$embedding]; 
  $documents[] = $document;
}

$memoryVectorStore = new MemoryVectorStore();
$memoryVectorStore->addDocuments($documents);

$qa = new QuestionAnswering(
  $memoryVectorStore,
  $embeddingGenerator,
  new OpenAIChat()
);

$answer = $qa->answerQuestion('Where you love food?');
echo $answer;

error

PHP Fatal error:  Uncaught InvalidArgumentException: Arrays must have the same length. in /home/www/.......theodo-group/llphant/src/Embeddings/VectorStores/DistanceL2Utils.php:18
Stack trace:
#0 /home/www/...../theodo-group/llphant/src/Embeddings/VectorStores/Memory/MemoryVectorStore.php(53): LLPhant\Embeddings\VectorStores\DistanceL2Utils::euclideanDistanceL2()
#1 /home/www/......r/theodo-group/llphant/src/Query/SemanticSearch/QuestionAnswering.php(64): LLPhant\Embeddings\VectorStores\Memory\MemoryVectorStore->similaritySearch()
#2 /home/www......../theodo-group/llphant/src/Query/SemanticSearch/QuestionAnswering.php(25): LLPhant\Query\SemanticSearch\QuestionAnswering->searchDocumentAndCreateSystemMessage()
@ezimuel
Copy link
Collaborator

ezimuel commented May 13, 2024

@ClicShopping the MemoryVectorStore::addDocuments() needs an array of Document objects as parameter. You can also use the addDocument that require only a single Document object.

The correct solution is the second but you need to adjust the code as follows (the embedText returns the array of embeddings and not an array of docs):

$text = 'I love food when I am in restaurant';
$embeddingGenerator = new OpenAI3LargeEmbeddingGenerator();
$embedding = $embeddingGenerator->embedText($text); 

$document = new Document();
$document->content = $text;
$document->embedding = $embedding; 

$memoryVectorStore = new MemoryVectorStore();
$memoryVectorStore->addDocument($document);

$qa = new QuestionAnswering(
  $memoryVectorStore,
  $embeddingGenerator,
  new OpenAIChat()
);

$answer = $qa->answerQuestion('Where you love food?');
echo $answer;

@ezimuel ezimuel added the question Further information is requested label May 13, 2024
@ClicShopping
Copy link
Author

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants