当前位置: 首页 > news >正文

Foundry IQ + Foundry Agent:从知识流到企业级 Agent

背景

今天这篇文章,我给大家介绍 Microsoft Foundry 平台上的两个重要组件:Foundry IQFoundry Agent Service,以及如何把它们串接起来,让Foundry IQ为 Agent 提供知识层。如果你对下面的问题有疑问:

  • 什么是 Foundry IQ?它有什么价值?
  • 如何把 Foundry IQ 和 Foundry Agent 连接起来?

那么这篇文章一定能解答你的疑惑!

创建 Foundry IQ

首先要声明,Foundry IQ 本身并不是一个服务,而是由 Azure 上各种服务围绕着 Azure AI Search 实现的一个数据流。它的核心包括下面几个组件:

  • Knowledge source:它告诉 Foundry IQ 去哪里能找到数据源。它可以是一个 Search Index,也可以是一个 SharePoint 文档,还可以是一个 OneLake 数据湖,或者是外部的网络搜索引擎。
  • Knowledge base:它可以封装多个下游的Knowledge source,而且配置了 Foundry 上的 LLM 模型。
  • Agentic Retrieval Engine:基于Knowledge base中配置的大模型,就可以实现智能化的检索,也就是 Agentic Retrieval。它可以针对检索语句做一个全局的计划,比如把一个复杂的检索语句拆解成多个下层的小语句,然后并行执行,再把结果进行合成!

这样,Agent 就只需要跟 Knowledge base 通信,而不需要单独跟每个下游的Knowledge source通信了。所以从这个角度看,你可以把 Foundry IQ 视为 Agent 的知识模块。

下面我们就来一步一步地创建一个 Foundry IQ 数据流。

创建 Azure AI Search Index

在这篇文章的案例里,我们还是会使用熟悉的 Azure AI Search Index 当作数据源。

from azure.search.documents.indexes.models import SearchIndex, SearchField, VectorSearch, VectorSearchProfile, HnswAlgorithmConfiguration, AzureOpenAIVectorizer, AzureOpenAIVectorizerParameters, SemanticSearch, SemanticConfiguration, SemanticPrioritizedFields, SemanticFieldfrom azure.search.documents.indexes import SearchIndexClientindex = SearchIndex( name=index_name, fields=[ SearchField(name="id",type="Edm.String", key=True, filterable=True, sortable=True, facetable=True), SearchField(name="page_chunk",type="Edm.String", filterable=False, sortable=False, facetable=False), SearchField(name="page_embedding_text_3_large",type="Collection(Edm.Single)", stored=False, vector_search_dimensions=3072, vector_search_profile_name="hnsw_text_3_large"), SearchField(name="page_number",type="Edm.Int32", filterable=True, sortable=True, facetable=True)], vector_search=VectorSearch( profiles=[VectorSearchProfile(name="hnsw_text_3_large", algorithm_configuration_name="alg", vectorizer_name="azure_openai_text_3_large")], algorithms=[HnswAlgorithmConfiguration(name="alg")], vectorizers=[ AzureOpenAIVectorizer( vectorizer_name="azure_openai_text_3_large", parameters=AzureOpenAIVectorizerParameters( resource_url=aoai_endpoint, deployment_name=aoai_embedding_deployment, model_name=aoai_embedding_model))]), semantic_search=SemanticSearch( default_configuration_name="semantic_config", configurations=[ SemanticConfiguration( name="semantic_config", prioritized_fields=SemanticPrioritizedFields( content_fields=[ SemanticField(field_name="page_chunk")]))]))index_client = SearchIndexClient(endpoint=search_endpoint, credential=azure_credential)index_client.create_or_update_index(index)print(f"Index '{index_name}' created or updated successfully.")
上传文档

下面,我来向上面那个 Search Index 里添加一些数据。这里我们用一本来自 NASA 的关于地球的电子书,这个 JSON 文件已经是切片和矢量化之后的内容了,大家可以把它下载下来看看内容,我这里就不展示了!

import requestsfrom azure.search.documents import SearchIndexingBufferedSenderurl ="https://raw.githubusercontent.com/Azure-Samples/azure-search-sample-data/refs/heads/main/nasa-e-book/earth-at-night-json/documents.json"documents = requests.get(url).json()with SearchIndexingBufferedSender(endpoint=search_endpoint, index_name=index_name, credential=azure_credential)as client: client.upload_documents(documents=documents)print(f"Documents uploaded to index '{index_name}' successfully.")

下面就是我们创建的 Index:earth-at-night

创建 Knowledge source

下面我们基于上面的 Search Index 创建一个 Knowledge source。注意,Knowledge source 本身不存储数据,它更像一个指针,指向真正的数据存储层!

from azure.search.documents.indexes.models import SearchIndexKnowledgeSource, SearchIndexKnowledgeSourceParameters, SearchIndexFieldReferencefrom azure.search.documents.indexes import SearchIndexClientks = SearchIndexKnowledgeSource( name=knowledge_source_name, description="Knowledge source for Earth at night data", search_index_parameters=SearchIndexKnowledgeSourceParameters( search_index_name=index_name, source_data_fields=[SearchIndexFieldReference(name="id"), SearchIndexFieldReference(name="page_number")]),)index_client = SearchIndexClient(endpoint=search_endpoint, credential=azure_credential)index_client.create_or_update_knowledge_source(knowledge_source=ks)print(f"Knowledge source '{knowledge_source_name}' created or updated successfully.")

下面就是我们创建出来的 Knowledge source:earth-knowledge-source

创建 Knowledge base

Knowledge base 一方面封装了下游的一个或者多个 source,另一方面它配置了 Azure OpenAI 的大模型能力!具体情况见下面的代码:

from azure.search.documents.indexes.models import KnowledgeBase, KnowledgeBaseAzureOpenAIModel, KnowledgeSourceReference, AzureOpenAIVectorizerParameters, KnowledgeRetrievalOutputMode, KnowledgeRetrievalLowReasoningEffortfrom azure.search.documents.indexes import SearchIndexClientaoai_params = AzureOpenAIVectorizerParameters( resource_url=aoai_endpoint, deployment_name=aoai_gpt_deployment, model_name=aoai_gpt_model)knowledge_base = KnowledgeBase( name=knowledge_base_name, models=[KnowledgeBaseAzureOpenAIModel(azure_open_ai_parameters=aoai_params)], knowledge_sources=[ KnowledgeSourceReference( name=knowledge_source_name)], output_mode=KnowledgeRetrievalOutputMode.ANSWER_SYNTHESIS, answer_instructions="Provide a 2 sentence concise and informative answer based on the retrieved documents.")index_client = SearchIndexClient(endpoint=search_endpoint, credential=azure_credential)index_client.create_or_update_knowledge_base(knowledge_base)print(f"Knowledge base '{knowledge_base_name}' created or updated successfully.")

下面就是我们创建的 Knowledge base。

Agentic Retrieval Demo

有了 Knowledge base,下一步让我们看看 Agentic Retrieval 的智能检索效果。

from azure.search.documents.knowledgebases import KnowledgeBaseRetrievalClientfrom azure.search.documents.knowledgebases.models import KnowledgeBaseRetrievalRequest, KnowledgeBaseMessage, KnowledgeBaseMessageTextContent, SearchIndexKnowledgeSourceParamsfrom azure.search.documents.indexes.models import KnowledgeRetrievalLowReasoningEffortinstructions ="""A Q&A agent that can answer questions about the Earth at night.If you don't have the answer, respond with "I don't know"."""messages =[{"role":"system","content": instructions}]agent_client = KnowledgeBaseRetrievalClient(endpoint=search_endpoint, knowledge_base_name=knowledge_base_name, credential=azure_credential)query_1 ="What causes city lights to appear brighter from space during the holidays?"messages.append({"role":"user","content": query_1})req = KnowledgeBaseRetrievalRequest( messages=[ KnowledgeBaseMessage( role=m["role"], content=[KnowledgeBaseMessageTextContent(text=m["content"])])for m in messages if m["role"]!="system"], knowledge_source_params=[ SearchIndexKnowledgeSourceParams( knowledge_source_name=knowledge_source_name, include_references=True, include_reference_source_data=True, always_query_source=True)], include_activity=True, retrieval_reasoning_effort=KnowledgeRetrievalLowReasoningEffort)result = agent_client.retrieve(retrieval_request=req)print(f"Retrieved content from '{knowledge_base_name}' successfully.")

返回的结果包括三个内容:
response:针对用户的问题,大模型基于检索结果生成的答案
activity:整个智能检索过程拆解出的一个个步骤
references:从数据源中检索出的相关数据文档

那么我们具体来看一下,上面的示例中用户的问题得到了怎样的结果。我最关心的是activity,可以看到整个 query 被拆分为两个小 query,各自检索,然后合并结果。

[{"id":0,"type":"modelQueryPlanning","elapsed_ms":1251,"input_tokens":1682,"output_tokens":69},{"id":1,"type":"searchIndex","elapsed_ms":753,"knowledge_source_name":"earth-knowledge-source","query_time":"2026-04-21T02:18:29.251Z","count":36,"search_index_arguments":{"search":"Causes of increased city light brightness from space during holidays","source_data_fields":[{"name":"page_chunk"},{"name":"id"},{"name":"page_number"}],"search_fields":[],"semantic_configuration_name":"semantic_config"}},{"id":2,"type":"searchIndex","elapsed_ms":235,"knowledge_source_name":"earth-knowledge-source","query_time":"2026-04-21T02:18:29.497Z","count":38,"search_index_arguments":{"search":"How holiday lighting affects satellite images of city lights","source_data_fields":[{"name":"page_chunk"},{"name":"id"},{"name":"page_number"}],"search_fields":[],"semantic_configuration_name":"semantic_config"}},{"id":3,"type":"agenticReasoning","reasoning_tokens":46353,"retrieval_reasoning_effort":{"kind":"low"}},{"id":4,"type":"modelAnswerSynthesis","elapsed_ms":2324,"input_tokens":7705,"output_tokens":98}]

连接 Foundry Agent

下面我们就尝试把这个 Foundry IQ 串接到 Foundry Agent 上。

创建 MCP 的安全连接

Foundry Agent 和 Knowledge base 是通过 MCP 协议通信的,Knowledge base 基于下面这样的 URL,对外暴露了一个 MCP 服务:

from dotenv import load_dotenvfrom azure.identity import DefaultAzureCredential, get_bearer_token_providerimport osload_dotenv(override=True)search_endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")knowledge_base_name = os.getenv("KNOWLEDGE_BASE_NAME")mcp_endpoint =f"{search_endpoint}/knowledgebases/{knowledge_base_name}/mcp?api-version=2025-11-01-Preview"print(f"✅ MCP endpoint: {mcp_endpoint}")

下面的问题是如何安全地访问这个 MCP 服务呢?Foundry Agent 作为企业级平台,对于安全性的要求是非常严格的。我们必须建立一个安全的连接方式。

具体做法如下:在 Foundry 项目里创建一个 Remote Tool 连接,这个连接要求使用 Foundry 项目的 managed identity 和 MCP 服务进行权限认证,这样 Agent 就可以通过 MCP 服务安全地读取 Knowledge base 里的数据。

import requestsfrom azure.identity import DefaultAzureCredential, get_bearer_token_providercredential = DefaultAzureCredential()bearer_token_provider = get_bearer_token_provider( credential,"https://management.azure.com/.default")headers ={"Authorization":f"Bearer {bearer_token_provider()}",}FOUNDRY_ENDPOINT = os.environ["FOUNDRY_PROJECT_ENDPOINT"]MODEL_DEPLOYMENT = os.environ.get("FOUNDRY_MODEL_DEPLOYMENT_NAME")PROJECT_RESOURCE_ID = os.environ["FOUNDRY_PROJECT_RESOURCE_ID"]PROJECT_CONNECTION_NAME ="earth-kb-mcp-connection"conn_response = requests.put(f"https://management.azure.com{PROJECT_RESOURCE_ID}/connections/{PROJECT_CONNECTION_NAME}?api-version=2025-10-01-preview", headers=headers, json={"name": PROJECT_CONNECTION_NAME,"type":"Microsoft.MachineLearningServices/workspaces/connections","properties":{"authType":"ProjectManagedIdentity","category":"RemoteTool","target": mcp_endpoint,"isSharedToAll":True,"audience":"https://search.azure.com/","metadata":{"ApiType":"Azure"},},},)conn_response.raise_for_status()print(f"✅ Project connection '{PROJECT_CONNECTION_NAME}' created")

创建出的 Remote Tool 连接如下:

创建 Agent

创建一个 Foundry Agent:earth-at-night-agent,并且为其配置上述 Knowledge base 的 MCP 服务作为工具:

from azure.ai.projects import AIProjectClientfrom azure.ai.projects.models import PromptAgentDefinition, MCPToolproject_client = AIProjectClient( endpoint=FOUNDRY_ENDPOINT, credential=credential)instructions ="""You are a helpful assistant that must use the knowledge base to answer all the questions from user.You must never answer from your own knowledge under any circumstances.Every answer must always provide annotations for using the MCP knowledge base tooland render them as: `【message_idx:search_idx†source_name】`If you cannot find the answer in the provided knowledge base you must respond with "I don't know"."""mcp_kb_tool = MCPTool( server_label="knowledge-base", server_url=mcp_endpoint, require_approval="never", allowed_tools=["knowledge_base_retrieve"], project_connection_id=PROJECT_CONNECTION_NAME,)agent = project_client.agents.create_version( agent_name="earth-at-night-agent", definition=PromptAgentDefinition( model=MODEL_DEPLOYMENT, instructions=instructions, tools=[mcp_kb_tool],),)print(f"✅ Agent '{agent.name}' created (version={agent.version})")

系统提示词要求 Agent 必须基于 Knowledge base 里的信息做回答。下面我们就来试一试。

解决访问 MCP 的权限问题

在 Foundry 的界面上,找到这个 Agent,并且向它问一个问题,很明显我们遇到了 MCP 服务的权限认证问题,如下:

让我们来思考一下这个问题的原因:上面创建的 Remote Tool 连接要求必须用 Foundry 项目的 managed identity 和 MCP 服务进行权限认证,但是很显然我们还没有给 Foundry 项目的 managed identity 添加权限!这是根本原因。

解决方案也非常直接:在 Knowledge base 所在的 Azure AI Search 服务里,给 Agent 所在的 Foundry 项目的 managed identity 添加Search Index Data Reader权限!

如果你对如何在 Azure 上通过 RBAC 机制进行权限管理有困惑,可以找我之前的文章读一读!

在解决了上面的问题之后,我们再试一试。这次 Agent 就可以通过 Knowledge base 里的知识跟用户进行问答了!

学AI大模型的正确顺序,千万不要搞错了

🤔2026年AI风口已来!各行各业的AI渗透肉眼可见,超多公司要么转型做AI相关产品,要么高薪挖AI技术人才,机遇直接摆在眼前!

有往AI方向发展,或者本身有后端编程基础的朋友,直接冲AI大模型应用开发转岗超合适!

就算暂时不打算转岗,了解大模型、RAG、Prompt、Agent这些热门概念,能上手做简单项目,也绝对是求职加分王🔋

📝给大家整理了超全最新的AI大模型应用开发学习清单和资料,手把手帮你快速入门!👇👇

学习路线:

✅大模型基础认知—大模型核心原理、发展历程、主流模型(GPT、文心一言等)特点解析
✅核心技术模块—RAG检索增强生成、Prompt工程实战、Agent智能体开发逻辑
✅开发基础能力—Python进阶、API接口调用、大模型开发框架(LangChain等)实操
✅应用场景开发—智能问答系统、企业知识库、AIGC内容生成工具、行业定制化大模型应用
✅项目落地流程—需求拆解、技术选型、模型调优、测试上线、运维迭代
✅面试求职冲刺—岗位JD解析、简历AI项目包装、高频面试题汇总、模拟面经

以上6大模块,看似清晰好上手,实则每个部分都有扎实的核心内容需要吃透!

我把大模型的学习全流程已经整理📚好了!抓住AI时代风口,轻松解锁职业新可能,希望大家都能把握机遇,实现薪资/职业跃迁~

这份完整版的大模型 AI 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

http://www.cnnetsun.cn/news/2039579.html

相关文章:

  • 联合概率、边缘概率与条件概率:机器学习基础解析
  • 第7篇:抽象基类(ABC)与接口设计
  • 如何用CoolProp在7天内掌握免费热力学物性计算?
  • ESP32多伺服控制器HAT:树莓派与无线控制实战
  • 基于全域数学的宇宙螺旋场统一结构研究【乖乖数学】
  • 如何永久保存微信聊天记录?免费工具WeChatMsg完整使用指南
  • 1×1卷积:深度学习中的通道操作利器
  • 自然语言处理四大核心技术路径解析与实践
  • 2026年移动应用动态发布,不发版怎么做?一站式方案解析
  • 别再只会用K-Means了!用Python的Scikit-learn实战DBSCAN和谱聚类,搞定非球形数据
  • 【Blazor 2026终极配置指南】:零基础30分钟完成生产级WebAssembly+Auto-Render混合部署
  • 树莓派4B串口通信实战:从蓝牙占用解除到PC双向通信(附完整代码)
  • LSTM时间序列预测中的特征工程实践与优化
  • 云从科技第一季营收2323万:同比降37% 净亏1301万
  • 006、PCIE物理层基础:通道、速率与编码
  • Genesis IoT Discovery Lab模块化开发平台解析与应用
  • 缓存基础知识:缓存策略、过期、击穿与雪崩
  • 考研线代救命指南:行最简形矩阵和标准形到底怎么化?看完这篇就够了
  • Vue 转 React:揭秘样式语言是如何被 VuReact 编译的?
  • nli-MiniLM2-L6-H768实战教程:构建私有化客服对话意图识别轻量系统
  • Docker边缘配置效率提升300%:基于K3s+EdgeX的7步极简部署法(附生产环境压测数据)
  • mTLS(双向TLS)介绍(Mutual Transport Layer Security)(客户端和服务端相互验证身份)X.509、Service Mesh、Istio、Linkerd、东西流量
  • 终极指南:如何快速配置英雄联盟云顶之弈自动挂机脚本
  • Redis Cluster 分片与主从详解
  • 【Blazor 2026安全白皮书】:OWASP Top 10在Razor组件中的新型攻击面及零信任加固方案(含CVE-2026-XXXX PoC)
  • 时间序列季节性分析与调整方法详解
  • Phi-3.5-mini-instruct助力C++项目:大型工程代码理解与重构建议
  • 从《愤怒的小鸟》到你的游戏:拆解Unity抛物线运动脚本的优化思路
  • K-Means聚类实战:从原理到可视化调优全解析
  • SOLAI推出Solode Neo个人AI终端:即插即用、保障隐私,399美元开启个人AI新时代