{"id":416,"date":"2026-03-20T11:55:58","date_gmt":"2026-03-20T03:55:58","guid":{"rendered":"https:\/\/ziyuan.work\/?p=416"},"modified":"2026-03-20T11:55:58","modified_gmt":"2026-03-20T03:55:58","slug":"%e5%ad%98%e5%8f%96qdrant%e5%90%91%e9%87%8f%ef%bc%88golang%ef%bc%89","status":"publish","type":"post","link":"https:\/\/ziyuan.work\/?p=416","title":{"rendered":"\u5b58\u53d6qDrant\u5411\u91cf\uff08Golang\uff09"},"content":{"rendered":"\n<p class=\"has-text-align-left is-style-text-annotation is-style-text-annotation--1\">\u68c0\u7d22\u589e\u5f3a\u751f\u6210\uff08Retrieval-Augmented Generation, RAG\uff09\u6210\u4e3a\u89e3\u51b3\u6a21\u578b\u77e5\u8bc6\u5c40\u9650\u6027\u548c\u5e7b\u89c9\u95ee\u9898\u7684\u6709\u6548\u624b\u6bb5\u3002\u5728RAG\u67b6\u6784\u4e2d\uff0c\u5411\u91cf\u6570\u636e\u5e93\u8d1f\u8d23\u5b58\u50a8\u548c\u68c0\u7d22\u6587\u672c\u7684\u5411\u91cf\u8868\u793a\uff0c\u800cqDrant\u4f5c\u4e3a\u9ad8\u6027\u80fd\u7684\u5411\u91cf\u6570\u636e\u5e93\uff0c\u5e38\u88ab\u9009\u4e3a\u5e95\u5c42\u5b58\u50a8\u3002\u672c\u6587\u5c06\u7ed3\u5408Go\u8bed\u8a00\u4ee3\u7801\u793a\u4f8b\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7langchain-go\u548cqdrant-go\u5e93\u5b58\u53d6qDrant\u5411\u91cf\uff0c\u5e76\u5b9e\u73b0\u4e00\u4e2a\u5b8c\u6574\u7684RAG\u6d41\u7a0b\u3002<\/p>\n\n\n\n<p>\u73af\u5883\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/qdrant.tech\/\" target=\"_blank\" rel=\"noreferrer noopener\">qDrant<\/a>&nbsp;\u670d\u52a1\uff08\u672c\u5730\u6216\u8fdc\u7a0b\u8fd0\u884c\uff09<\/li>\n\n\n\n<li><a href=\"https:\/\/ollama.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Ollama<\/a>&nbsp;\u670d\u52a1\uff08\u7528\u4e8eEmbedding\u548c\u6587\u672c\u751f\u6210\uff09\n<ul class=\"wp-block-list\">\n<li>\u51c6\u5907nomic-embed-text\u6a21\u578b<\/li>\n\n\n\n<li>\u51c6\u5907\u4efb\u610f\u652f\u6301text\u7684\u5927\u6a21\u578b<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Go\u8bed\u8a00\u73af\u5883\u53ca\u4ee5\u4e0b\u5e93\uff1a\n<ul class=\"wp-block-list\">\n<li><code>github.com\/tmc\/langchaingo<\/code>\uff08\u5305\u542b\u5411\u91cf\u5b58\u50a8\u3001\u6587\u6863\u52a0\u8f7d\u5668\u3001\u6587\u672c\u5206\u5272\u5668\u7b49\uff09<\/li>\n\n\n\n<li><code>github.com\/qdrant\/go-client<\/code>\uff08qDrant\u5b98\u65b9Go\u5ba2\u6237\u7aef\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u65b0\u5efa\u6587\u4ef6\u5939config\/<br><code>config<\/code>\u6587\u4ef6\u542bconfig.go\uff0c\u7ba1\u7406\u914d\u7f6e\u4fe1\u606f\u3002\u5305\u542bqDrant\u5730\u5740\u3001\u96c6\u5408\u540d\u79f0\u3001Ollama URL\u3001Embedding\u6a21\u578b\u540d\u548c\u751f\u6210\u6a21\u578b\u540d\u7b49\u3002<\/p>\n\n\n\n<p>1. \u96c6\u5408\u7ba1\u7406\uff1a<code>EnsureCollection<\/code>&nbsp;\u548c&nbsp;<code>resetCollection<\/code><\/p>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--2\">\u786e\u4fdd\u6307\u5b9a\u7684\u96c6\u5408\u5b58\u5728\uff0c\u82e5\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u3002\u5411\u91cf\u7ef4\u5ea6\uff08768\uff09\u4e0eEmbedding\u6a21\u578b\uff08\u5982nomic-embed-text\uff09\u5339\u914d\uff0c\u8ddd\u79bb\u5ea6\u91cf\u91c7\u7528\u4f59\u5f26\u76f8\u4f3c\u5ea6\u3002\u521b\u5efa\u96c6\u5408\u65f6\u6307\u5b9a\u7684\u5411\u91cf\u7ef4\u5ea6\uff08768\uff09\u5fc5\u987b\u4e0eEmbedding\u6a21\u578b\u8f93\u51fa\u7684\u7ef4\u5ea6\u5b8c\u5168\u4e00\u81f4\u3002\u672c\u4f8b\u4f7f\u7528nomic-embed-text\uff08\u7ef4\u5ea6768\uff09\uff0c\u82e5\u6362\u7528\u5176\u4ed6\u6a21\u578b\uff08\u5982all-MiniLM-L6-v2\u7ef4\u5ea6384\uff09\uff0c\u9700\u76f8\u5e94\u8c03\u6574\u3002<br>resetCollection\uff1a\u5148\u5220\u9664\u6307\u5b9a\u96c6\u5408\uff0c\u518d\u91cd\u65b0\u521b\u5efa\uff0c\u7528\u4e8e\u91cd\u7f6e\u6574\u4e2a\u77e5\u8bc6\u5e93\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func EnsureCollection(urlStr string, collectionName string) {\n\tmyclient, err := qdrant.NewClient(&amp;qdrant.Config{\n\t\tHost:   config.QdrantIP,\n\t\tPort:   6334,\n\t\tUseTLS: false,\n\t})\n\tif err != nil {\n\t\tlog.Fatal(\"\u8fde\u63a5Qdrant\u5931\u8d25:\", err)\n\t}\n\tdefer myclient.Close()\n\tctx := context.Background()\n\texists, err := myclient.CollectionExists(ctx, collectionName)\n\tif err != nil {\n\t\tlog.Fatal(\"\u67e5\u8be2\u96c6\u5408\u72b6\u6001\u5931\u8d25:\", err)\n\t}\n\tif !exists {\n\t\tfmt.Printf(\"\u6b63\u5728\u521b\u5efa\u96c6\u5408: %s...\\n\", collectionName)\n\t\terr = myclient.CreateCollection(ctx, &amp;qdrant.CreateCollection{\n\t\t\tCollectionName: collectionName,\n\t\t\tVectorsConfig: qdrant.NewVectorsConfig(&amp;qdrant.VectorParams{\n\t\t\t\tSize:     768, \/\/ nomic-embed-text \u7ef4\u5ea6\u662f 768\uff1a\u5fc5\u987b\u4e0e Embedding \u6a21\u578b\u4e00\u81f4\n\t\t\t\tDistance: qdrant.Distance_Cosine,\n\t\t\t}),\n\t\t})\n\t\tif err != nil {\n\t\t\tlog.Fatal(\"\u521b\u5efa\u96c6\u5408\u5931\u8d25:\", err)\n\t\t}\n\t\tfmt.Println(\"\u96c6\u5408\u521b\u5efa\u6210\u529f\")\n\t}\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>func resetCollection(name string) {\n\tmyclient, _ := qdrant.NewClient(&amp;qdrant.Config{\n\t\tHost: config.QdrantIP, Port: 6334, UseTLS: false,\n\t})\n\tdefer myclient.Close()\n\tctx := context.Background()\n\t_ = myclient.DeleteCollection(ctx, name)\n\tEnsureCollection(config.QdrantIP, name)\n\tfmt.Println(\"\u96c6\u5408\u5df2\u91cd\u7f6e\uff0c\u51c6\u5907\u91cd\u65b0\u5bfc\u5165...\")\n}<\/code><\/pre>\n\n\n\n<p>2. \u6570\u636e\u5bfc\u5165\uff1a<code>IngestKnowledge<\/code><\/p>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--3\">\u5206\u5757\u7b56\u7565\u76f4\u63a5\u5f71\u54cd\u68c0\u7d22\u6548\u679c\uff0c\u9700\u6839\u636e\u6587\u6863\u7c7b\u578b\u548c\u4efb\u52a1\u8c03\u6574\u5757\u5927\u5c0f\u548c\u91cd\u53e0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func IngestKnowledge(filePath string, shouldReset bool) {\n    \/\/ 1. \u91cd\u7f6e\u6216\u786e\u4fdd\u96c6\u5408\u5b58\u5728\n    if shouldReset {\n        resetCollection(config.Collection)\n    } else {\n        EnsureCollection(config.QdrantIP, config.Collection)\n    }\n\n    \/\/ 2. \u6839\u636e\u6587\u4ef6\u7c7b\u578b\u9009\u62e9\u52a0\u8f7d\u5668\uff08PDF\u6216\u6587\u672c\uff09\n    f, _ := os.Open(filePath)\n    defer f.Close()\n    var loader documentloaders.Loader\n    if strings.HasSuffix(filePath, \".pdf\") {\n        loader = documentloaders.NewPDF(f, fileSize)\n    } else {\n        loader = documentloaders.NewText(f)\n    }\n\n    \/\/ 3. \u52a0\u8f7d\u5e76\u5206\u5272\u6587\u6863\n    docs, _ := loader.LoadAndSplit(ctx, textsplitter.NewRecursiveCharacter(\n        textsplitter.WithChunkSize(300),\n        textsplitter.WithChunkOverlap(100),\n    ))\n\n    \/\/ 4. \u521d\u59cb\u5316Embedder\u548c\u5411\u91cf\u5b58\u50a8\n    embedLLM, _ := ollama.New(ollama.WithModel(config.EmbedModel), ollama.WithServerURL(config.OllamaURL))\n    embedder, _ := embeddings.NewEmbedder(embedLLM)\n    store, _ := qdrantl.New(\n        qdrantl.WithURL(url.URL{Scheme: \"http\", Host: config.QdrantURL}),\n        qdrantl.WithCollectionName(config.Collection),\n        qdrantl.WithEmbedder(embedder),\n    )\n\n    _, err = store.AddDocuments(ctx, docs) \/\/ 5. \u6dfb\u52a0\u6587\u6863\u5230qDrant\n}<\/code><\/pre>\n\n\n\n<p>3. \u67e5\u8be2\u4e0e\u751f\u6210\uff1a<code>UpdateRAG<\/code><\/p>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--4\">\u786e\u4fdd\u96c6\u5408\u5b58\u5728\uff0c\u521d\u59cb\u5316Embedder\u548c\u5411\u91cf\u5b58\u50a8\uff08\u4e0e\u5bfc\u5165\u65f6\u76f8\u540c\uff09\u3002\u5411\u96c6\u5408\u4e2d\u6dfb\u52a0\u51e0\u4e2a\u793a\u4f8b\u6587\u6863\u5757\uff08\u6f14\u793a\u7528\uff09\u3002\u6267\u884c\u76f8\u4f3c\u6027\u641c\u7d22\uff1a\u5c06\u67e5\u8be2\u6587\u672c\u5411\u91cf\u5316\uff0c\u5728qDrant\u4e2d\u68c0\u7d22\u6700\u76f8\u4f3c\u76843\u4e2a\u6587\u6863\u5757\u3002\u5c06\u68c0\u7d22\u7ed3\u679c\u62fc\u63a5\u4e3a\u4e0a\u4e0b\u6587\uff0c\u6784\u9020\u63d0\u793a\u8bcd\uff0c\u8c03\u7528\u751f\u6210\u6a21\u578b\uff08Ollama\uff09\u5f97\u5230\u6700\u7ec8\u56de\u7b54\u3002<\/p>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--5\">\u6ce8\u610f\uff1a\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6587\u6863\u5bfc\u5165\u548c\u67e5\u8be2\u751f\u6210\u901a\u5e38\u662f\u5206\u5f00\u7684\u6b65\u9aa4\uff0c\u6b64\u5904\u5408\u5e76\u4ec5\u4e3a\u5c55\u793a\u5b8c\u6574\u6d41\u7a0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func UpdateRAG() {\n    \/\/ 1. \u786e\u4fdd\u96c6\u5408\u5b58\u5728\uff0c\u521d\u59cb\u5316Embedder\u548c\u5411\u91cf\u5b58\u50a8\n    EnsureCollection(config.QdrantIP, config.Collection)\n    embedLLM, _ := ollama.New(ollama.WithModel(config.EmbedModel), ollama.WithServerURL(config.OllamaURL))\n    embedder, _ := embeddings.NewEmbedder(embedLLM)\n    store, _ := qdrantl.New(\n        qdrantl.WithURL(url.URL{Scheme: \"http\", Host: config.QdrantURL}),\n        qdrantl.WithCollectionName(config.Collection),\n        qdrantl.WithEmbedder(embedder),\n    )\n\n    \/\/ 2. \u51c6\u5907\u793a\u4f8b\u6587\u6863\u5e76\u5206\u5272\uff08\u6b64\u5904\u4ec5\u4e3a\u6f14\u793a\uff0c\u5b9e\u9645\u53ef\u4ece\u6587\u4ef6\u52a0\u8f7d\uff09\n    docs := &#91;]schema.Document{ ... }\n    splitter := textsplitter.NewRecursiveCharacter(textsplitter.WithChunkSize(200), textsplitter.WithChunkOverlap(20))\n    var chunks &#91;]schema.Document\n    for _, doc := range docs {\n        texts, _ := splitter.SplitText(doc.PageContent)\n        for _, t := range texts {\n            chunks = append(chunks, schema.Document{PageContent: t, Metadata: doc.Metadata})\n        }\n    }\n    store.AddDocuments(ctx, chunks)\n\n    \/\/ 3. \u6267\u884c\u76f8\u4f3c\u6027\u641c\u7d22\n    query := \"Qdrant \u9002\u5408\u7528\u5728\u4ec0\u4e48\u573a\u666f\uff1f\"\n    results, _ := store.SimilaritySearch(ctx, query, 3)\n\n    \/\/ 4. \u6784\u5efa\u63d0\u793a\u8bcd\u5e76\u8c03\u7528LLM\u751f\u6210\u56de\u7b54\n    contextText := \"\"\n    for _, doc := range results {\n        contextText += doc.PageContent + \"\\n\"\n    }\n    llm, _ := ollama.New(ollama.WithModel(config.GenerateModel), ollama.WithServerURL(config.OllamaURL))\n    prompt := fmt.Sprintf(`\n\u4f60\u662f\u4e00\u4e2a\u57fa\u4e8e\u77e5\u8bc6\u5e93\u56de\u7b54\u95ee\u9898\u7684\u52a9\u624b\u3002\n\u53ea\u80fd\u6839\u636e\u3010\u77e5\u8bc6\u5e93\u3011\u5185\u5bb9\u4f5c\u7b54\uff0c\u5982\u679c\u65e0\u6cd5\u5f97\u5230\u7b54\u6848\uff0c\u8bf7\u56de\u7b54\u201c\u4e0d\u77e5\u9053\u201d\u3002\n\u3010\u77e5\u8bc6\u5e93\u3011\n%s\n\u3010\u95ee\u9898\u3011\n%s\n`, contextText, query)\n    answer, _ := llms.GenerateFromSinglePrompt(ctx, llm, prompt)\n    fmt.Println(answer)\n}<\/code><\/pre>\n\n\n\n<p>4. \u8fd0\u884c\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func main() {\n\tIngestKnowledge(\".\/xxix.pdf\", true) \/\/\u6307\u5411\u9700\u8981\u7d22\u5f15\u7684\u77e5\u8bc6\u6587\u4ef6\n        UpdateRAG()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8f93\u51fa\uff1a\n\u6587\u6863\u5df2\u6210\u529f\u5199\u5165 Qdrant\n\nModel\u56de\u7b54\uff1a\nQdrant \u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u5411\u91cf\u6570\u636e\u5e93\uff0c\u5e38\u7528\u4e8e RAG\uff08\u68c0\u7d22\u589e\u5f3a\u751f\u6210\uff09\u7cfb\u7edf\uff0c\u56e0\u6b64\u9002\u5408\u7528\u4e8e\u6784\u5efa\u57fa\u4e8e\u8bed\u4e49\u641c\u7d22\u3001\u63a8\u8350\u7cfb\u7edf\u3001\u5f02\u5e38\u68c0\u6d4b\u7b49\u9700\u8981\u5411\u91cf\u76f8\u4f3c\u6027\u68c0\u7d22\u7684\u573a\u666f\u3002<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u68c0\u7d22\u589e\u5f3a\u751f\u6210\uff08Retrieval-Augmented Generation, RAG\uff09\u6210\u4e3a\u89e3\u51b3\u6a21\u578b\u77e5\u8bc6\u5c40\u9650\u6027\u548c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-416","post","type-post","status-publish","format-standard","hentry","category-solution"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ziyuan.work\/index.php?rest_route=\/wp\/v2\/posts\/416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ziyuan.work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ziyuan.work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ziyuan.work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ziyuan.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=416"}],"version-history":[{"count":11,"href":"https:\/\/ziyuan.work\/index.php?rest_route=\/wp\/v2\/posts\/416\/revisions"}],"predecessor-version":[{"id":476,"href":"https:\/\/ziyuan.work\/index.php?rest_route=\/wp\/v2\/posts\/416\/revisions\/476"}],"wp:attachment":[{"href":"https:\/\/ziyuan.work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ziyuan.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ziyuan.work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}