Skip to content

Instantly share code, notes, and snippets.

Last active June 14, 2023 00:40
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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
'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