Last active
June 14, 2023 00:40
-
-
Save whobutsb/b0b2247739dc2cbb323617aa2c17f7e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Pgvector\Laravel\Vector; | |
use Illuminate\Console\Command; | |
use App\Models\Document; | |
use App\Models\Post; | |
use OpenAI\Laravel\Facades\OpenAI; | |
class ProcessPosts extends Command { | |
protected $signature = 'app:process-posts'; | |
public function handle() | |
{ | |
// select all the posts from the wordpress database | |
$posts = Post::all(); | |
foreach($posts as $post) { | |
try { | |
// query the openai vector embeddings with the post content | |
$response = OpenAI::embeddings()->create([ | |
'model' => 'text-embedding-ada-002', | |
'input' => strip_tags($post->post_content), | |
]); | |
} catch (\Exception $e) { | |
throw new \Exception($e); | |
} | |
// get the embeddings and create it as a vector object | |
$embeddings = new Vector($response->embeddings[0]->embedding); | |
// create the document in the pgsql database | |
Document::create([ | |
'id' => $post->ID, | |
'title' => $post->post_title, | |
'post_name' => $post->post_name, | |
'text' => strip_tags($post->post_content), | |
'embeddings' => $embeddings, | |
'tokens' => $response->usage->totalTokens, | |
'created_at' => $post->post_date, | |
]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment