使用项目连接
每个 Azure AI Foundry 项目都包含 连接的资源,这些资源在 父 级(Azure AI Foundry 资源或中心)级别以及 项目 级别定义。 每个资源都是与外部服务(例如 Azure 存储、Azure AI 搜索、Azure OpenAI 或其他 Azure AI Foundry 资源) 的连接 。
使用 Azure AI Foundry SDK,可以连接到项目并检索连接;然后,可以使用它来使用连接的服务。
例如,Python 中的 AIProjectClient 对象具有 连接 属性,可用于访问项目中的资源连接。 对象 连接的方法包括:
-
connections.list():返回连接对象的集合,每个对象表示项目中的连接。 可以通过指定具有有效枚举的可选 connection_type 参数(例如ConnectionType.AZURE_OPEN_AI)来筛选结果。 -
connections.get(connection_name, include_credentials):返回具有指定名称的连接的连接对象。 如果 include_credentials 参数 True(默认值),则返回连接到连接的凭据 -例如,以 Azure AI 服务资源的 API 密钥的形式返回。
这些方法返回的连接对象包括特定于连接的属性,包括可用于连接到关联资源的凭据。
下面的代码示例列出了已添加到项目的所有资源连接:
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
try:
# Get project client
project_endpoint = "https://....."
project_client = AIProjectClient(
credential=DefaultAzureCredential(),
endpoint=project_endpoint,
)
## List all connections in the project
connections = project_client.connections
print("List all connections:")
for connection in connections.list():
print(f"{connection.name} ({connection.type})")
except Exception as ex:
print(ex)