链载Ai

标题: Milvus 最强大模型Gemini 3 Pro怎么搭RAG?(含免费教程) [打印本页]

作者: 链载Ai    时间: 昨天 22:39
标题: Milvus 最强大模型Gemini 3 Pro怎么搭RAG?(含免费教程)
图片

最大的亮点或许是Google Antigravity

图片

就在昨天晚上,Gemini 3 Pro 正式发布了。

作为迄今为止最强推理,最强多模态理解,以及最强智能体和vibe coding的模型,Gemini 3 Pro只发了一篇博客,就掀翻了Hacker News, Reddit等平台讨论区,就连OpenAI CEO奥特曼,也亲自发推祝贺

是的,用了这么多最字,但并不违反广告法。

因为,这是Gemini 3 Pro 的综合表现:

除 在解决真实 GitHub 问题(SWE-Bench Verified)榜单表现略逊于 GPT-5.1和CLaude Sonnet 4.5之外,Gemini 3 Pro 在其余核心评测中全面领先 :

那么实际体验如何?如何将其落地企业知识库?本文将一一解读。

01

实测

Gemini 3 Pro 此次重点更新的能力主要有四方面:

基于以上能力创新,以下是我们对Gemini 3 Pro 实测的几个案例

案例一:多模态理解能力不论用户上传的是文字,视频,还是代码都能够清晰理解

我们上传一个Zilliz在Youtube的视频,它能够在40秒左右完成阅读和理解,速度惊人

在官方测试中,上传各种不同语言手写的食谱,都其能够成功翻译成一本可共享的家庭食谱


案例二:在零样本(zero-shot)生成

Gemini 3 Pro 能够处理各种复杂的提示词和命令,从而渲染出更加丰富和具有交互性的的Web UI.

我们用Gemini 3 Pro 编写了一个完美的80年代复古未来主义美学的3D飞行射击游戏,可以看到霓虹紫色网格地面配合赛博朋克风格的飞船和光效,视觉效果相当惊艳。

案例三:复杂任务规划

Gemini 3 Pro的负责任务规划能力也优于其他模型,可以看到,它就像一位AI行政秘书,能自动将杂乱的邮件按项目归类并预先起草好处理方案(如回复、建任务、归档),我们只需点击全部确认即可一键清空收件箱


02

RAG 教程

(1)依赖和环境准备

安装或升级pymilvusgoogle-generativeairequeststqdm这 4 个库到最新版本

!pipinstall--upgradepymilvusgoogle-generativeairequeststqdm登录GoogleAIStudio平台获取APIKEY(地址:https://aistudio.google.com/api-keys)
importosos.environ["GEMINI_API_KEY"]="**********"(2)数据准备

以Milvus文档 2.4.x 中的常见问题解答页面作为我们 RAG 系统中的私有知识库.

下载该压缩文件,并将文档解压至“milvus_docs”文件夹中。

!wgethttps://github.com/milvus-io/milvus-docs/releases/download/v2.4.6-preview/milvus_docs_2.4.x_en.zip!unzip-qmilvus_docs_2.4.x_en.zip-dmilvus_docs从“milvus_docs/en/faq”文件夹中加载所有的Markdown文件。对于每个文档,我们只是简单地用“#”将文件中的内容隔开,这样就能大致将Markdown文件中每个主要部分的内容分离开来。
fromglobimportglobtext_lines=[]forfile_pathinglob("milvus_docs/en/faq/*.md",recursive=True):withopen(file_path,"r")asfile:file_text=file.read()text_lines+=file_text.split("#")(3)LLM和Embedding模型准备

我们将使用 gemini-3-pro-preview 作为LLM, text-embedding-004作为embedding模型

importgoogle.generativeaiasgenaigenai.configure(api_key=os.environ["GEMINI_API_KEY"])gemini_model=genai.GenerativeModel("gemini-3-pro-preview")response=gemini_model.generate_content("whoareyou")print(response.text)回复:

I am Gemini, a large language model built by Google.

生成一个测试向量,并打印其维度以及前几个元素。

test_embeddings=genai.embed_content(model="models/text-embedding-004",content=["Thisisatest1","Thisisatest2"])["embedding"]embedding_dim=len(test_embeddings[0])print(embedding_dim)print(test_embeddings[0][:10])768

[0.013588584, -0.004361838, -0.08481652, -0.039724775, 0.04723794, -0.0051557426, 0.026071774, 0.045514572, -0.016867816, 0.039378334]

(4)加载数据到Milvus

创建集合

frompymilvusimportMilvusClientmilvus_client=MilvusClient(uri="./milvus_demo.db")collection_name="my_rag_collection"关于MilvusClient的参数设置:

检查集合是否存在,如果存在的话就删除重建

ifmilvus_client.has_collection(collection_name):milvus_client.drop_collection(collection_name)创建一个具有指定参数的新集合。

如果未指定任何字段信息,Milvus将自动创建一个默认的ID字段作为主键,以及一个向量字段用于存储向量数据。一个预留的JSON字段用于存储未在schema中定义的字段及其值。

milvus_client.create_collection(collection_name=collection_name,dimension=embedding_dim,metric_type="COSINE",consistency_level="Strong",#Strongconsistencylevel)插入集合

逐行遍历文本,创建嵌入向量,然后将数据插入Milvus。

下面是一个新的字段text,它是集合中的一个未定义的字段。 它将自动创建一个对应的text字段(实际上它底层是由保留的JSON动态字段实现的 ,你不用关心其底层实现。)

fromtqdmimporttqdmdata=[]doc_embeddings=genai.embed_content(model="models/text-embedding-004",content=text_lines)["embedding"]fori,lineinenumerate(tqdm(text_lines,desc="Creatingembeddings")):data.append({"id":i,"vector":doc_embeddings[i],"text":line})milvus_client.insert(collection_name=collection_name,data=data)输出结果示例:
Creatingembeddings:100%|█████████████████████████|72/72[00:00<00:00,431414.13it/s]{'insert_count':72,'ids':[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71],'cost':0}

(5)创建RAG

检索数据

我们来问一个关于Milvus的常见问题。

question="Howisdatastoredinmilvus?"在集合中搜索该问题,并返回top3最相关的问题
question_embedding=genai.embed_content(model="models/text-embedding-004",content=question)["embedding"]search_res=milvus_client.search(collection_name=collection_name,data=[question_embedding],limit=3,#Returntop3resultssearch_params={"metric_type":"COSINE","params":{}},#Innerproductdistanceoutput_fields=["text"],#Returnthetextfield)importjsonretrieved_lines_with_distances=[(res["entity"]["text"],res["distance"])forresinsearch_res[0]]print(json.dumps(retrieved_lines_with_distances,indent=4))结果按照距离从近到远排序返回
[["WheredoesMilvusstoredata?\n\nMilvusdealswithtwotypesofdata,inserteddataandmetadata.\n\nInserteddata,includingvectordata,scalardata,andcollection-specificschema,arestoredinpersistentstorageasincrementallog.Milvussupportsmultipleobjectstoragebackends,including[MinIO](https://min.io/),[AWSS3](https://aws.amazon.com/s3/?nc1=h_ls),[GoogleCloudStorage](https://cloud.google.com/storage?hl=en#object-storage-for-companies-of-all-sizes)(GCS),[AzureBlobStorage](https://azure.microsoft.com/en-us/products/storage/blobs),[AlibabaCloudOSS](https://www.alibabacloud.com/product/object-storage-service),and[TencentCloudObjectStorage](https://www.tencentcloud.com/products/cos)(COS).\n\nMetadataaregeneratedwithinMilvus.EachMilvusmodulehasitsownmetadatathatarestoredinetcd.\n\n###",0.8048489093780518],["Doesthequeryperforminmemory?Whatareincrementaldataandhistoricaldata?\n\nYes.Whenaqueryrequestcomes,Milvussearchesbothincrementaldataandhistoricaldatabyloadingthemintomemory.Incrementaldataareinthegrowingsegments,whicharebufferedinmemorybeforetheyreachthethresholdtobepersistedinstorageengine,whilehistoricaldataarefromthesealedsegmentsthatarestoredintheobjectstorage.Incrementaldataandhistoricaldatatogetherconstitutethewholedatasettosearch.\n\n###",0.757495105266571],["WhatisthemaximumdatasetsizeMilvuscanhandle?\n\n\nTheoretically,themaximumdatasetsizeMilvuscanhandleisdeterminedbythehardwareitisrunon,specificallysystemmemoryandstorage:\n\n-Milvusloadsallspecifiedcollectionsandpartitionsintomemorybeforerunningqueries.Therefore,memorysizedeterminesthemaximumamountofdataMilvuscanquery.\n-Whennewentitiesandandcollection-relatedschema(currentlyonlyMinIOissupportedfordatapersistence)areaddedtoMilvus,systemstoragedeterminesthemaximumallowablesizeofinserteddata.\n\n###",0.7453694343566895]]使用大型语言模型(LLM)构建检索增强生成(RAG)响应

将检索到的文档转换为字符串格式。

context="\n".join([line_with_distance[0]forline_with_distanceinretrieved_lines_with_distances])为大语言模型提供系统提示(systemprompt)和用户提示(userprompt)。这个提示是通过从Milvus检索到的文档生成的。
SYSTEM_PROMPT="""Human:YouareanAIassistant.Youareabletofindanswerstothequestionsfromthecontextualpassagesnippetsprovided."""USER_PROMPT=f"""Usethefollowingpiecesofinformationenclosedin<context>tagstoprovideananswertothequestionenclosedin<question>tags.<context>{context}</context><question>{question}</question>"""使用gemini-3-pro-preview模型和提示词生成回复
gemini_model=genai.GenerativeModel("gemini-3-pro-preview",system_instruction=SYSTEM_PROMPT)response=gemini_model.generate_content(USER_PROMPT)print(response.text)从输出结果可以看到,gemini3Pro能够条理清晰地返回结果
Basedontheprovideddocuments,Milvusstoresdatainthefollowingways:***InsertedData:**Vectordata,scalardata,andcollection-specificschemaarestoredinpersistentstorageasanincrementallog.Milvussupportsmultipleobjectstoragebackendsforthispurpose,including:*MinIO*AWSS3*GoogleCloudStorage(GCS)*AzureBlobStorage*AlibabaCloudOSS*TencentCloudObjectStorage(COS)***Metadata:**MetadatageneratedwithinMilvusmodulesisstoredin**etcd**.***MemoryBuffering:**Incrementaldata(growingsegments)arebufferedinmemorybeforebeingpersisted,whilehistoricaldata(sealedsegments)residesinobjectstoragebutisloadedintomemoryforquerying.友情提示

目前Gemini 3 Pro不向免费用户提供(详情:https://ai.google.dev/gemini-api/docs/rate-limits#tier-1)

但是我们可以通过OpenRouter进行调用(https://openrouter.ai/google/gemini-3-pro-preview/api)

具体用法参考下面

fromopenaiimportOpenAIclient=OpenAI(base_url="https://openrouter.ai/api/v1",api_key="<OPENROUTER_API_KEY>",)response2=client.chat.completions.create(model="google/gemini-3-pro-preview",messages=[{"role":"system","content":SYSTEM_PROMPT},{"role":"user","content":USER_PROMPT}],extra_body={"reasoning":{"enabled":True}})response_message=response2.choices[0].messageprint(response_message.content)


03

one more thing

这次与旗舰模型Gemini 3 Pro同步推出的,还有Google Antigravity(反重力)平台,作为一个AI编程平台,它可以自主访问我们的编辑器、终端、甚至内置浏览器;能力边界上,也从过去的执行具体指令、单次调用升级为自主任务导向型开发。

更直白一点说,之前,我们只能让AI编程软件写一段代码,然后自己校验、合并;但是借助Google Antigravity,我们可以直接告诉AI,我要写一个宠物互动游戏,它就可以自动把这个任务进行一步步分解,执行,并且自动打开浏览器进行校验。整个过程,甚至还会学习你的个人风格,来进行持续的迭代优化。

当然,调度数据库,搭配MCP能力,读取你的Milvus数据库内容,自己搞一个知识库,长期来看,也不是不可能。

这个在我个人看来,其实比模型发布本身的意义要更重大,毕竟,它都能按照产品经理的抽象描述做任务拆解了,那开发者现在转产品,还来得及吗?






欢迎光临 链载Ai (https://www.lianzai.com/) Powered by Discuz! X3.5