-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-search-answer.php
93 lines (77 loc) · 2.95 KB
/
02-search-answer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
declare(strict_types=1);
/*
* This file is part of PHP LLM Documents.
*
* (c) Thomas Joußen <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
use Google\Client;
use Thojou\LLMDocuments\Crawler\Spatie\SpatieCrawlerConfig;
use Thojou\LLMDocuments\Crawler\Spatie\SpatieCrawlerFactory;
use Thojou\LLMDocuments\Document\Document;
use Thojou\LLMDocuments\Document\DocumentInterface;
use Thojou\LLMDocuments\Embedding\OpenAi\OpenAiEmbeddings;
use Thojou\LLMDocuments\Loader\WebSearchLoader;
use Thojou\LLMDocuments\Parser\Unstructured\Api\UnstructuredAPI;
use Thojou\LLMDocuments\Parser\Unstructured\UnstructuredParserFactory;
use Thojou\LLMDocuments\Retriever\SimilarityRetriever;
use Thojou\LLMDocuments\Search\Google\GoogleSearchEngineFactory;
use Thojou\LLMDocuments\Storage\VectorStore\LocalVectorStore;
use Thojou\LLMDocuments\Transformation\DocumentTransformationBuilder;
use Thojou\LLMDocuments\ValueObjects\DoctranConfig;
use Thojou\OpenAi\OpenAi;
use Thojou\SimpleApiClient\Adapter\GuzzleClientFactory;
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/credentials.php';
// INPUT
$query = $argv[1] ?? "Wer hat die Basketball WM 2023 gewonnen?";
$threshold = isset($argv[3]) ? (float)$argv[3] : 0.85;
// Define the OpenAI API Interface
$openAI = new OpenAi(
OPENAI_KEY,
);
// Define the SimilarityRetriever to store and find similar documents
$retriever = new SimilarityRetriever(
new LocalVectorStore(
'/tmp/test.json',
new OpenAiEmbeddings($openAI, 'text-embedding-ada-002'),
),
$threshold
);
/**
* @param array<DocumentInterface> $contextDocuments
*
* @return DocumentInterface
*/
function combineDocuments(array $contextDocuments): DocumentInterface
{
$text = "";
foreach($contextDocuments as $key => $document) {
$text .= "DOCUMENT $key\n";
$text .= $document->getPageContent() . "\n";
$text .= "METADATA\n";
$text .= json_encode($document->getMetadata(), JSON_PRETTY_PRINT) . "\n";
$text .= "\n";
}
return new Document($text);
}
echo "Start retrieving relevant documents\n";
$contextDocuments = $retriever->getRelevantDocuments($query);
echo "Start combining documents\n";
$document = combineDocuments($contextDocuments);
echo "Start chat with OpenAI\n";
$context = $document->getPageContent();
$messages = [
'model' => 'gpt-3.5-turbo',
'messages' => [
['role' => 'system', 'content' => "Respond to the user's query using only the information provided in the given context. If you lack the necessary information to answer the question, reply with 'I don't know'."],
['role' => 'user', 'content' => "CONTEXT INFORMATION:\n===\n$context\n===\nUSER QUERY: $query\n===\nRESPONSE:"],
]
];
$response = $openAI->chat()->completion($messages);
echo "User QUERY: $query\n";
echo "RESPONSE:\n";
echo $response['choices'][0]['message']['content'] . "\n";