[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"$flbcgl8v1czan":3,"$fanuq43nlrv5g":48},{"slug":4,"title":5,"body":6,"summary":7,"tags":8,"author":13,"cover_url":14,"published_at":15,"seo_title":5,"seo_description":7,"reading_minutes":16,"related":17},"openai-embeddings-python-mongodb","Transforming Text into Vectors: OpenAI Embeddings in Python","\u003Cp>\u003Cimg alt=\"tailwind-nextjs-banner\" src=\"https:\u002F\u002Fmedia.stufio.com\u002Fmedia\u002Fifcodes\u002Fblog\u002Fge\u002Fgetty-2-69fd9cb7.jpg\" \u002F>\u003C\u002Fp>\n\u003Ch3>Introduction\u003C\u002Fh3>\n\u003Cp>In the world of machine learning and artificial intelligence, working with unstructured data often involves converting textual information into vector representations, or embeddings. These embeddings power search, recommendation engines, and natural language understanding tasks. By transforming textual data into numerical representations, we can leverage vector databases such as Pinecone, Weaviate, or Redis for fast similarity search and other advanced analytics.\u003C\u002Fp>\n\u003Cp>One of the easiest ways to generate high-quality text embeddings is by using the OpenAI API. In this post, we’ll walk through how to generate embeddings for product or document data and store the results in a MongoDB collection for further use.\u003C\u002Fp>\n\u003Ch3>Why Use Text Embeddings?\u003C\u002Fh3>\n\u003Cp>Text embeddings are dense vector representations of text that capture its semantic meaning. Instead of dealing with raw text, embeddings allow algorithms to process the contextual meaning of words and phrases efficiently. This is especially useful for applications such as:\u003C\u002Fp>\n\u003Cul>\n\u003Cli>\u003Cstrong>Semantic search:\u003C\u002Fstrong> Finding similar documents or products.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Recommendation systems:\u003C\u002Fstrong> Personalizing user experiences.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Clustering and classification:\u003C\u002Fstrong> Organizing data into meaningful groups.\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Chr \u002F>\n\u003Ch3>Getting Started with OpenAI Embeddings\u003C\u002Fh3>\n\u003Cp>To generate embeddings with OpenAI, you'll need to use the \u003Ccode>text-embedding-3-small\u003C\u002Fcode> (or other relevant) model provided by the API. Below is an example implementation in Python.\u003C\u002Fp>\n\u003Ch4>Setting Up the OpenAI API Client\u003C\u002Fh4>\n\u003Cpre>\u003Ccode class=\"language-python\">from openai import OpenAI\n\ndef get_openai_client():\n    &quot;&quot;&quot;Returns an OpenAI client instance.&quot;&quot;&quot;\n    global client\n    if not client:\n        client = OpenAI(api_key=config(&quot;OPENAI_API_KEY&quot;))\n    return client\n\ndef get_embedding(text, model=&quot;text-embedding-3-small&quot;):\n    try: \n        client = get_openai_client()\n        ret = client.embeddings.create(input=[text], model=model)\n        embedding = ret.data[0].embedding\n    except Exception as e:\n        # LOG ERROR\n        return None\n\n    return embedding\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>The get_embedding function sends a text input to OpenAI’s embedding model and returns a vector representation. This embedding can then be used in downstream applications.\u003C\u002Fp>\n\u003Ch4>Example OpenAI API Response\u003C\u002Fh4>\n\u003Cp>The OpenAI API provides a structured JSON response. Here’s an example:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-JSON\">{\n  &quot;object&quot;: &quot;list&quot;,\n  &quot;data&quot;: [\n    {\n      &quot;object&quot;: &quot;embedding&quot;,\n      &quot;index&quot;: 0,\n      &quot;embedding&quot;: [\n        -0.006929283495992422,\n        -0.005336422007530928,\n        ... (omitted for spacing)\n        -4.547132266452536e-05,\n        -0.024047505110502243\n      ]\n    }\n  ],\n  &quot;model&quot;: &quot;text-embedding-3-small&quot;,\n  &quot;usage&quot;: {\n    &quot;prompt_tokens&quot;: 5,\n    &quot;total_tokens&quot;: 5\n  }\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch3>Generating Embeddings for MongoDB Documents\u003C\u002Fh3>\n\u003Cp>A practical use case is to generate embeddings for product attributes stored in a MongoDB collection. The first step is to convert product objects into a text prompt format.\u003C\u002Fp>\n\u003Ch4>Converting Product Data to Text Prompts\u003C\u002Fh4>\n\u003Cpre>\u003Ccode class=\"language-python\">def generate_embeddings_text(product, fields) -&gt; str:\n    data = &quot;&quot;\n    for field in fields:\n        if field in product and product.get(field, ''):\n            data += f&quot;{field.upper()}: {product[field]}\\n&quot;\n    return data\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>This function formats the selected product fields into a structured text prompt that can be passed to the embedding model.\u003C\u002Fp>\n\u003Ch4>Processing MongoDB Documents\u003C\u002Fh4>\n\u003Cp>The following example demonstrates how to fetch product data from a MongoDB collection, generate embeddings, and store the results back into the database:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-python\">fields = ['name', 'description', ...]\nfor doc in pymongo_db['products'].find():\n    embedding_data = generate_embeddings_text(doc, fields)\n    embedding_openai = get_embedding(embedding_data)\n    pymongo_db['embeddings'].update_one(\n        {'sku': doc['sku']},\n        {&quot;$set&quot;: {\n            'sku': doc['sku'],\n            'embedding': embedding_openai,\n            'updated_at': datetime.now()\n        }},\n        upsert=True\n    )\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>This pipeline transforms product attributes into embeddings and updates the results in a MongoDB collection. The collection embeddings will contain each product’s SKU and its corresponding embedding vector.\u003C\u002Fp>\n\u003Cp>\u003Cimg alt=\"mongodb embeddings\" src=\"https:\u002F\u002Fmedia.stufio.com\u002Fmedia\u002Fifcodes\u002Fblog\u002Fmo\u002Fmongodb-embeddings-6a27cb37.png\" \u002F>\u003C\u002Fp>\n\u003Ch4>Pricing of OpenAI Embeddings\u003C\u002Fh4>\n\u003Cp>As of the time of writing, the cost of generating embeddings using OpenAI’s text-embedding-3-small model is $0.020 per 1M tokens. To estimate the cost of processing 1 million products, consider the average size of the product data:\u003C\u002Fp>\n\u003Cp>Example Calculation:\n- If each product's attributes average 1 KB of text, then 1M products ≈ 1 billion tokens.\n- Total cost = 0.020 x (1 billion tokens \u002F 1M tokens) ≈ $20.\u003C\u002Fp>\n\u003Cp>For larger datasets, the cost remains competitive, given the high quality and versatility of OpenAI’s embeddings.\u003C\u002Fp>\n\u003Ch3>Conclusion\u003C\u002Fh3>\n\u003Cp>Generating embeddings with OpenAI’s API is a simple yet powerful way to work with textual data in modern machine learning applications. By converting structured or unstructured data into embeddings, you unlock capabilities like semantic search, personalized recommendations, and clustering.\u003C\u002Fp>\n\u003Cp>In this post, we’ve demonstrated how to integrate OpenAI’s embedding API into a Python pipeline, process MongoDB documents, and manage costs effectively. This workflow can be adapted to various use cases, from e-commerce to document management systems.\u003C\u002Fp>\n\u003Cp>Start experimenting with OpenAI embeddings today and transform the way you work with textual data!\u003C\u002Fp>","Learn how to generate text embeddings with the OpenAI API in Python to power semantic search, recommendations, and more. Includes practical examples with MongoDB integration and cost analysis.",[9,10,11,12],"openai","ai","python","mongodb","if.codes","https:\u002F\u002Fmedia.stufio.com\u002Fmedia\u002Fifcodes\u002Fblog\u002Fge\u002Fgetty-2-69fd9cb7.jpg","2024-11-23T00:00:00Z",3,[18,29,37],{"slug":19,"title":20,"type":21,"summary":22,"tags":23,"author":13,"cover_url":26,"published_at":27,"updated_at":28},"check-pricing-availability-ing-domains","Last Chance to Grab Short .ING Domains: The Extended List Part II","blog","Welcome back to the second part of our exciting exploration into the .ING domain zone! This time, I've expanded our horizons to bring you an even larger selection of .ING domain names. List of over 24,000 domain names inside.",[24,25],"domains","business","https:\u002F\u002Fmedia.stufio.com\u002Fmedia\u002Fifcodes\u002Fblog\u002Fdo\u002Fdosmthgreat-60b31838.png","2023-12-14T00:00:00Z","2026-09-26T22:29:53.739Z",{"slug":30,"title":31,"type":21,"summary":32,"tags":33,"author":13,"cover_url":34,"published_at":35,"updated_at":36},"impressive-ing-domains","Unveiling the Impressive .ING Domains","Discover the vast potential of the new .ING domain zone in my latest blog post! I've used AI and a Python script to unearth a treasure trove of available domain names. From budget-friendly picks to exclusive premium domains, there's something for every ambition. Plus, a special list of unique, lesser-known domains awaits.",[24,25],"https:\u002F\u002Fmedia.stufio.com\u002Fmedia\u002Fifcodes\u002Fblog\u002Fin\u002Fing-zone-092f42bc.jpg","2023-12-11T00:00:00Z","2026-09-26T22:29:53.788Z",{"slug":38,"title":39,"type":21,"summary":40,"tags":41,"author":13,"cover_url":45,"published_at":46,"updated_at":47},"secured-web-server-in-5-minutes","Fortify Web Server Security in 5 Minutes with Tailscale","Tailscale revolutionizes secure networking with its user-friendly approach, effortlessly connecting devices across diverse networks.",[42,43,44],"firewall","tailscale","webserver","https:\u002F\u002Fmedia.stufio.com\u002Fmedia\u002Fifcodes\u002Fblog\u002Fta\u002Ftailscale-logo-black-4bc15a01.png","2023-11-03T00:00:00Z","2026-09-26T22:29:53.977Z",[49,52,55,58,60],{"slug":4,"title":5,"type":21,"summary":7,"tags":50,"author":13,"cover_url":14,"published_at":15,"updated_at":51,"reading_minutes":16},[9,10,11,12],"2026-09-26T22:29:53.912Z",{"slug":19,"title":20,"type":21,"summary":22,"tags":53,"author":13,"cover_url":26,"published_at":27,"updated_at":28,"reading_minutes":54},[24,25],1,{"slug":30,"title":31,"type":21,"summary":32,"tags":56,"author":13,"cover_url":34,"published_at":35,"updated_at":36,"reading_minutes":57},[24,25],2,{"slug":38,"title":39,"type":21,"summary":40,"tags":59,"author":13,"cover_url":45,"published_at":46,"updated_at":47,"reading_minutes":16},[42,43,44],{"slug":61,"title":62,"type":21,"summary":63,"tags":64,"author":13,"cover_url":67,"published_at":68,"updated_at":69,"reading_minutes":70},"lets-encrypt-free-ssl","How to Secure Your Website with Free SSL Certificates for a Lifetime","Let’s Encrypt certificates have revolutionized internet security by providing free, automated, and widely trusted SSL\u002FTLS certificates. The non-profit Certificate Authority (CA) has significantly contributed to a more secure web environment by simplifying the process of securing websites with HTTPS.",[65,66,44],"ssl","https","https:\u002F\u002Fmedia.stufio.com\u002Fmedia\u002Fifcodes\u002Fblog\u002Fce\u002Fcertbot-logo-7d6d4311.png","2023-11-01T00:00:00Z","2026-09-26T22:29:53.839Z",6]