Skip to content

Instantly share code, notes, and snippets.

@whobutsb
Last active June 14, 2023 00:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whobutsb/b0b2247739dc2cbb323617aa2c17f7e4 to your computer and use it in GitHub Desktop.
Save whobutsb/b0b2247739dc2cbb323617aa2c17f7e4 to your computer and use it in GitHub Desktop.
<?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