新闻详情

新闻详情

首页 / 资讯中心 / 详情

serpbase + Pinecone 向量数据库完整集成

发布时间:2026/11/1 16:57:50来源:尧图网络
serpbase + Pinecone 向量数据库完整集成
背景做 RAG 系统需要向量数据库,Pinecone 是最成熟的托管方案。serpbase 提供 SERP 数据做语义搜索,完整集成 5 步搭好。1. 整体流程[serpbase 拉 SERP 数据] → [OpenAI Embeddings 转成 1536 维向量] → [存到 Pinecone metadata] → [用户 query] → [query 转成向量] → [Pinecone 查 top 5] → [LLM 生成答案]2. 准备pipinstallopenai pinecone-client requestsimportosimportopenaifrompineconeimportPineconeimportrequests OPENAI_KEYos.environ[OPENAI_API_KEY]SERPBASE_KEYos.environ[SERPBASE_KEY]PINECONE_KEYos.environ[PINECONE_KEY]pcPinecone(api_keyPINECONE_KEY)openai.api_keyOPENAI_KEY index_nameserpbase-rag# 创建 index(只需 1 次)ifindex_namenotinpc.list_indexes().names():pc.create_index(nameindex_name,dimension1536,metriccosine,spec{serverless:{cloud:aws,region:us-east-1}})indexpc.Index(index_name)3. Index SERP 数据defcall_serpbase(query,glus,num5):rrequests.post(https://api.serpbase.dev/google/search,headers{X-API-Key:SERPBASE_KEY},json{q:query,gl:gl,hl:en,num:num},timeout10,)r.raise_for_status()returnr.json()defget_embedding(text):responseopenai.embeddings.create(inputtext,modeltext-embedding-3-small,)returnresponse.data[0].embeddingdefindex_query_results(query):查 SERP 索引到 Pineconedatacall_serpbase(query)vectors[]fori,iteminenumerate(data.get(organic,[]),1):textf{item[title]}.{item.get(snippet,)}embeddingget_embedding(text)vectors.append((f{query}-{i},# idembedding,{query:query,rank:i,title:item[title],link:item[link],snippet:item.get(snippet,),ts:2026-06-19T09:00:00Z,}))index.upsert(vectorsvectors)returnlen(vectors)# 索引countindex_query_results(SERP API 选型)print(fIndexed{count}SERP results)4. 语义搜索defsemantic_search(query,top_k5,filter_dictNone):语义搜索 SERP 数据q_embeddingget_embedding(query)resultsindex.query(vectorq_embedding,top_ktop_k,include_metadataTrue,filterfilter_dict,)return[{rank:r.metadata[rank],title:r.metadata[title],link:r.metadata[link],snippet:r.metadata[snippet],query:r.metadata[query],score:r.score,}forrinresults.matches]# 用法 1:语义搜索resultssemantic_search(how to monitor SERP API)forrinresults:print(f{r[title]}({r[score]:.2f}))# 用法 2:filter 检索resultssemantic_search(SERP API,filter_dict{query:SERP API 选型}# 只查特定 query)5. 集成 LLM 生成答案defanswer_with_rag(query):RAG:检索 生成# 1. 语义搜索sourcessemantic_search(query,top_k3)ifnotsources:returnNo relevant data found.# 2. 拼 promptcontext\n\n.join(f[{i1}]{s[title]}\n{s[link]}\n{s[snippet]}fori,sinenumerate(sources))# 3. LLM 生成responseopenai.chat.completions.create(modelgpt-4o,messages[{role:system,content:fUse these SERP results to answer:\n{context}},{role:user,content:query},],)returnresponse.choices[0].message.content# 用法print(answer_with_rag(SERP API 选型要注意什么?))6. 定时同步(更新 Pinecone)importschedule QUERIES[SERP API 选型,SERP API 教程,SERP API 评测,SERP API 价格,]defdaily_sync():每天同步 SERP 数据到 PineconeforqinQUERIES:countindex_query_results(q)print(f{q}: indexed{count})# 删除 30 天前的数据statsindex.describe_index_stats()fornamespace,statinstats.namespaces.items():ifstat[vector_count]100000:index.delete(delete_allTrue,filter{ts:{$lt:2026-05-01}},namespacenamespace,)schedule.every().day.at(03:00).do(daily_sync)7. 5 个工程细节1. Namespace 区分不同主题# 不同主题不同 namespaceindex.upsert(vectorsvectors,namespaceseo)index.upsert(vectorsvectors,namespaceads)# 查询时只查相关 namespaceresultsindex.query(vectorq_embedding,namespaceseo,top_k5)2. Metadata 索引(只查特定 query)# 索引时 metadata 字段会被索引index.upsert(vectorsvectors)# 自动索引所有 metadata# 查询时 filterresultsindex.query(vectorq_embedding,filter{rank:{$lt:5}},# 只查 top 5top_k10,)3. 批量 upsert(性能)defbatch_index(queries,batch_size100):vectors[]forqinqueries:datacall_serpbase(q)fori,iteminenumerate(data.get(organic,[]),1):textf{item[title]}.{item.get(snippet,)}vectors.append((f{q}-{i},get_embedding(text),{query:q,rank:i,title:item[title],link:item[link],snippet:item.get(snippet,)}))# 批量 upsert(100 条/批)foriinrange(0,len(vectors),batch_size):batchvectors[i:ibatch_size]index.upsert(vectorsbatch)batch_index(QUERIES)# 100 条 1 次 upsert4. 删除旧数据(节省成本)# 7 天前数据清理fromdatetimeimportdatetime,timedelta cutoff(datetime.now()-timedelta(days7)).isoformat()index.delete(filter{ts:{$lt:cutoff}},namespaceseo,)5. 多 region(不同 index)# 不同 region 不同 indexforregion,glin[(us,us),(cn,cn),(uk,uk)]:pc.create_index(fserpbase-{region},dimension1536,metriccosine)8. 实战数据(我项目 30 天)指标数值索引 query 数50索引 total 结果数250Pinecone 月成本$0(免费层)Embedding 月成本$0.50(50 queries × 5 results)SerpBase 月成本$0.45语义搜索 P99 延迟200ms检索精度85%(人工评估)9. 集成 N8N / Make / Slack# 通过 webhook 让 Slack / Make 触发# /webhook/answer?queryxxxapp.route(/webhook/answer)defanswer():queryrequest.args.get(query)answeranswer_with_rag(query)return{answer:answer}Slack 机器人用 /answer 触发,RAG 答案直接发回 Slack。10. 与纯 RAG 方案对比维度serpbase Pinecone自建向量库集成时间30 分钟1-2 天月成本$1$30数据时效5 分钟(实时索引)数小时检索精度85%90%维护0需更新嵌入serpbase Pinecone 比自建快很多,精度稍低但够用。小结serpbase Pinecone 5 步搭 RAG 系统:创建 Pinecone index(1 次)查 serpbase 转 embedding 存(每 query)语义搜索(用户 query)拼 prompt LLM 生成定时同步更新(每天)月成本 $1(serpbase $0.45 embedding $0.50 Pinecone 免费层),比自建向量库便宜 95%。
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

【外设】之大彩串口显示屏 2026/11/1 1:42:41

【外设】之大彩串口显示屏

大彩串口屏初步使用 1 .官网下载 STM32 屏幕 GUI 设计资料 http://www.gz-dc.com/category/typeid/4112 找到 STM32 Keil 工程,移植相关代码因项目而异进行移植,由于项目简单,本人只对用到的指令接口进行修改。 比如:注意事项&…

阅读更多 →
无法下载Windows系统iso文件 2026/11/1 2:34:07

无法下载Windows系统iso文件

当我遇到这个问题的时候,我打开了一个网站: 登录 然后我打算下载的时候: 突然那个官方的连接就可以下载了:

阅读更多 →
【清华代码熊】DeepSeek V4.1 Flash 后训练详解 2026/11/1 2:01:57

【清华代码熊】DeepSeek V4.1 Flash 后训练详解

📌 上期解析了 DeepSeek V4.1 Flash 模型架构改进,本期解析 DeepSeek V4.1 Flash 预训练/后训练技术: 🌟 预训练:45T 文本 多模态混合语料、直接训练 sparse attention(取消 DeepSeek V4 的 dense 冷启动&…

阅读更多 →
Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ... 2026/11/1 3:13:10

Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ...

文章主要内容和创新点 主要内容 本文聚焦于多模态大语言模型(MLLM)强化学习(RL)训练中的效率问题,提出了一个名为Shuffle-R1的框架。研究发现,当前RL训练存在两个关键缺陷: 优势值坍缩(Advantage Collapsing):批次中大多数优势值集中在零附近,导致有效梯度信号被淹…

阅读更多 →
PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction 2026/11/1 2:11:52

PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction

一、文章主要内容总结 本文聚焦于利用大型语言模型(LLMs)实现个人身份信息(PII)脱敏的研究,旨在解决传统脱敏方法(如基于规则的系统、领域特定命名实体识别(NER)模型)泛化能力差、跨格式/跨语境适应性弱的问题。 研究通过全面评估多种LLM架构(包括密集型LLM(D-LLM…

阅读更多 →
LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model 2026/11/1 3:23:38

LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model

文章主要内容和创新点 主要内容 本文聚焦于二进制图像-文本相关性评估任务(判断图像与文本“相关”或“不相关”),针对该任务中文本格式多样、相关性定义随场景变化等挑战,提出了基于多模态大语言模型(MLLM)的解决方案LLaVA-RE。 模型设计:LLaVA-RE基于LLaVA 1.5架构,…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉