[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-golang-the-series-ep152-intro-to-vector-embeddings-go-all--*":3,"academy-blog-translations-j8rehtbpflwmsyv":87},{"data":4,"page":73,"perPage":73,"totalItems":73,"totalPages":73},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":81,"keywords":82,"locale":53,"published_at":83,"scheduled_at":69,"school_blog":77,"short_description":84,"status":75,"title":85,"updated":86,"updated_by":13,"slug":78,"views":80},"Go code implementation using go-openai library to convert text into Vector Embeddings array via OpenAI API","sclblg987654321","school_blog_translations","\u003Cp>Welcome to EP.152! In our previous episode, we discussed how RAG allows an AI to take an \"open-book exam.\" However, as backend developers, the critical question we must answer next is: \u003Cstrong>\"How do we search through that book to find the exact sentences that match our user's intent?\"\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>If we rely on traditional SQL queries like \u003Ccode>WHERE content LIKE '%payment%'\u003C\u002Fcode>, and an indie user asks, \u003Cem>\"How do I fork over the cash?\"\u003C\u002Fem>, our system will fail to retrieve the document. Even though the intent is identical, the actual words share zero characters in common.\u003C\u002Fp>\u003Cp>To solve this, the AI world uses Embeddings. It is the process of converting human language into a structured array of numbers (Vectors), enabling computers to mathematically calculate and understand word definitions. Today, we’re going to build this pipeline using Go!\u003C\u002Fp>\u003Ch2>What are Vector Embeddings?\u003C\u002Fh2>\u003Cp>An \u003Cstrong>Embedding\u003C\u002Fstrong> takes a piece of text (a word, a sentence, or a whole article) and processes it through a specialized AI model (like OpenAI's \u003Ccode>text-embedding-3-small\u003C\u002Fcode>). The model outputs a fixed-length array of floating-point numbers (\u003Ccode>[]float32\u003C\u002Fcode>), usually spanning 1,536 dimensions.\u003C\u002Fp>\u003Cp>The brilliance behind this lies in two aspects:\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp>\u003Cstrong>Semantic Closeness:\u003C\u002Fstrong> Words with similar meanings, such as \"cat\" and \"kitten,\" or \"payment\" and \"cash out,\" will yield coordinate numbers that are positioned very close to one another in the semantic space.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Math Over Language:\u003C\u002Fstrong> Computers do not understand words; they understand mathematics. By turning text into numbers, we can use simple geometric formulas (like Cosine Similarity) to calculate whether two distinct sentences are talking about the same topic.\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Ch2>Implementing OpenAI Embeddings in Go\u003C\u002Fh2>\u003Cp>We will use our trusted \u003Ccode>go-openai\u003C\u002Fcode> library to pass our strings to the API and retrieve the vectors.\u003C\u002Fp>\u003Cp>Go\u003C\u002Fp>\u003Cpre>\u003Ccode>package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n\n\t\"github.com\u002Fsashabaranov\u002Fgo-openai\"\n)\n\nfunc main() {\n\t\u002F\u002F Retrieve API Key from Environment Variables\n\tapiKey := os.Getenv(\"OPENAI_API_KEY\")\n\tif apiKey == \"\" {\n\t\tlog.Fatal(\"Embedding API Key is required\")\n\t}\n\n\tclient := openai.NewClient(apiKey)\n\n\t\u002F\u002F The text we want to process semantic meaning for\n\tinputText := \"I want to pay with my credit card\"\n\n\t\u002F\u002F 1. Construct the Embedding Request\n\treq := openai.EmbeddingRequest{\n\t\tInput: []string{inputText},\n\t\tModel: openai.SmallEmbedding3Small, \u002F\u002F Popular 1,536-dimension model: fast and cost-effective\n\t}\n\n\t\u002F\u002F 2. Send request to OpenAI API\n\tresp, err := client.CreateEmbeddings(context.Background(), req)\n\tif err != nil {\n\t\tlog.Fatalf(\"Embedding failed: %v\", err)\n\t}\n\n\t\u002F\u002F 3. Extract the Vector result ([]float32)\n\tvector := resp.Data[0].Embedding\n\n\tfmt.Printf(\"Text: '%s'\\n\", inputText)\n\tfmt.Printf(\"Vector Dimensions: %d\\n\", len(vector))\n\tfmt.Printf(\"First 5 Dimensions Sample: %v\\n\", vector[:5])\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch2>Production Data Structure\u003C\u002Fh2>\u003Cp>In a production RAG application, once you generate these vectors, you typically store them alongside the raw source text inside a struct. This sets them up for distance calculations later:\u003C\u002Fp>\u003Cp>Go\u003C\u002Fp>\u003Cpre>\u003Ccode>type DocumentChunk struct {\n\tID        string    `json:\"id\"`\n\tContent   string    `json:\"content\"`   \u002F\u002F Raw text, e.g., \"7-Day Return Policy Manual\"\n\tEmbedding []float32 `json:\"embedding\"` \u002F\u002F The 1,536-dimension array from the API\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch2>Why Golang Excels at Embedding Pipelines\u003C\u002Fh2>\u003Cp>When scaling an enterprise RAG application, you may need to convert hundreds of thousands of document pages into vectors. This workload is heavily I\u002FO-bound (API requests) and compute-bound (data processing) simultaneously. Go dominates this playing field:\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp>\u003Cstrong>Goroutine Worker Pools:\u003C\u002Fstrong> You can easily read documents, chunk them, and dispatch hundreds of concurrent embedding API requests via Go's concurrency model, shrinking processing times from hours to minutes.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Memory Efficiency:\u003C\u002Fstrong> Managing millions of \u003Ccode>[]float32\u003C\u002Fcode> arrays can cause massive memory spikes if handled poorly. Go’s low runtime overhead and predictable memory management ensure your infrastructure remains light and stable.\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Ch2>🎯 Daily Mission: Challenge Yourself\u003C\u002Fh2>\u003Cp>Initialize 3 sample sentences inside your Go codebase:\u003C\u002Fp>\u003Col>\u003Cli>\u003Cp>\u003Cem>\"How do I change my account password?\"\u003C\u002Fem>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cem>\"Steps to reset my credentials.\"\u003C\u002Fem>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cem>\"It is raining heavily in Bangkok today.\"\u003C\u002Fem>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Fol>\u003Cp>\u003Cstrong>Your Task:\u003C\u002Fstrong> Modify your program to generate embeddings for all 3 sentences simultaneously. Loop through the output to inspect the values. For an extra engineering challenge, write a basic function to calculate the distance (like a Dot Product) between [1 and 2] versus [1 and 3]. Which pair yields a closer mathematical score?\u003C\u002Fp>\u003Ch2>❓ FAQ: Frequently Asked Questions about Embeddings\u003C\u002Fh2>\u003Ch3>What does a 1,536-dimensional array actually represent?\u003C\u002Fh3>\u003Cp>Think of it as a physical map. While a standard chart uses 2D coordinates (X, Y), an AI model maps concepts onto a 1,536-dimensional matrix. Each dimension tracks a specific semantic feature—such as formality, financial context, or animal relations. More dimensions mean deeper semantic comprehension.\u003C\u002Fp>\u003Ch3>If I embed a Thai sentence and an English sentence with the same meaning, will their vectors match?\u003C\u002Fh3>\u003Cp>They won't match identically, but they will be remarkably close! Modern models like \u003Ccode>text-embedding-3-small\u003C\u002Fcode> are multilingual. They align meanings across languages, so words like \"แมว\" and \"Cat\" map out to near-identical coordinates in the vector space.\u003C\u002Fp>\u003Cdiv data-type=\"horizontalRule\">\u003Chr>\u003C\u002Fdiv>\u003Ch2>📌 Conclusion\u003C\u002Fh2>\u003Cp>\u003Cstrong>Vector Embeddings\u003C\u002Fstrong> act as the ultimate bridge translating human conversations into mathematical structures that computers can reason with. They elevate your search capabilities from strict word matching to full semantic understanding. When backed by Go's concurrent architecture, handling high-throughput pipeline transformations becomes an effortlessly scalable task.\u003C\u002Fp>\u003Cp>\u003Cstrong>In the Next Episode (EP.153):\u003C\u002Fstrong> Once you have millions of \u003Ccode>[]float32\u003C\u002Fcode> vectors floating around your system, where do you store them to search and compare them in milliseconds? Next time, we enter the world of \u003Cstrong>\"Vector Databases 101: Meet Pinecone, Weaviate, and Milvus\"\u003C\u002Fstrong>. Get ready to upgrade your stack!\u003C\u002Fp>\u003Cp>\u003Cstrong>Follow Superdev Academy on all platforms:\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp>\u003Cstrong>🔵 Facebook: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\">\u003Cstrong>Superdev Academy Thailand\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>🎬 YouTube: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.youtube.com\u002F@SuperdevAcademy\">\u003Cstrong>Superdev Academy Channel\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>📸 Instagram: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.instagram.com\u002Fsuperdevacademy\u002F\">\u003Cstrong>@superdevacademy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>🎬 TikTok: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\">\u003Cstrong>@superdevacademy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>🌐 Website: \u003C\u002Fstrong>\u003Ca rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fsuperdevacademy.com\">\u003Cstrong>superdevacademy.com\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003C\u002Fp>","24bqh1y9fnyb_56ybxb9aur.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Flbptt3t44ujg8iq\u002F24bqh1y9fnyb_56ybxb9aur.png","2026-06-11 16:34:44.871Z","76qprkevbgfdps8",{"keywords":15,"locale":47,"school_blog":57},[16,22,26,31,35,39,43],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":19,"updated_by":13},"sclkey987654321","school_keywords","2026-06-11 16:25:16.987Z","n7490lhschwfwmx","Vector Embeddings",{"collectionId":17,"collectionName":18,"created":23,"created_by":13,"id":24,"name":25,"updated":23,"updated_by":13},"2026-06-11 16:25:22.867Z","0umxsfrwhw7w81k","Go OpenAI",{"collectionId":17,"collectionName":18,"created":27,"created_by":13,"id":28,"name":29,"updated":30,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-06-07 06:45:07.798Z",{"collectionId":17,"collectionName":18,"created":32,"created_by":13,"id":33,"name":34,"updated":32,"updated_by":13},"2026-06-11 16:25:34.988Z","fslp7pvjf16upiv","Text Embedding",{"collectionId":17,"collectionName":18,"created":36,"created_by":13,"id":37,"name":38,"updated":36,"updated_by":13},"2026-06-11 16:25:43.052Z","0e3t6eerwk8xl3u","Go Concurrency",{"collectionId":17,"collectionName":18,"created":40,"created_by":13,"id":41,"name":42,"updated":40,"updated_by":13},"2026-06-11 16:25:49.118Z","8gdef2395f0aey9","Data Pipeline",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":44,"updated_by":13},"2026-06-11 16:25:53.881Z","jzb7gzyrenusyyy","RAG Backend",{"code":48,"collectionId":49,"collectionName":50,"created":51,"flag":52,"id":53,"is_default":54,"label":55,"updated":56},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":58,"collectionId":59,"collectionName":60,"created":61,"expand":62,"id":77,"slug":78,"updated":79,"views":80},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs","2026-06-11 16:31:45.024Z",{"category":63},{"blogIds":64,"collectionId":65,"collectionName":66,"created":67,"created_by":13,"id":58,"image":68,"image_alt":69,"image_path":70,"label":71,"name":72,"priority":73,"publish_at":74,"scheduled_at":69,"status":75,"updated":76,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:33:53.210Z","59ty92ns80w_15oc1implw.png","","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fwqxt7ag2gn7xcmk\u002F59ty92ns80w_15oc1implw.png",{"en":72,"th":72},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-06-07 06:45:03.856Z","j8rehtbpflwmsyv","golang-the-series-ep152-intro-to-vector-embeddings-go","2026-06-16 06:29:02.666Z",109,"lbptt3t44ujg8iq",[20,24,28,33,37,41,45],"2026-06-16 04:00:00.000Z","Say goodbye to keyword matching! Understand Vector Embeddings and how they translate human language into numbers for semantic search, complete with a step-by-step Go and OpenAI API guide.","Golang The Series EP.152: Intro to Embeddings — Converting Text into Vectors with Go","2026-06-16 04:00:00.077Z",{"th":78,"en":78}]