跳转至

Use platform APIs with the Foundry platform SDK(使用 Foundry 平台 SDK 调用平台 API)

Foundry APIs expose a variety of functionality that you can leverage with the Foundry platform SDK library through functions. You can use the platform SDK to build functions for administrative or governance workflows, interact with schedules and builds, access media sets, and more.

:::callout{theme="warning"} First-class authentication is not supported for TypeScript v1 functions. We recommend using Python functions and TypeScript v2 functions for these workflows. :::

Install the SDK

To install the Foundry platform SDK, navigate to the Libraries side panel in your code repository and search for the SDK name: foundry-platform-sdk for Python, or @osdk/foundry for TypeScript.

The Libraries search panel, searching for the Python platform SDK.

The Libraries search panel, searching for the TypeScript platform SDK.

Initialize your client

Your function requires authentication to interact with Foundry APIs. This process involves instantiating an authenticated “client” through which you can make requests to the Foundry APIs through the SDK. In TypeScript v2 repositories, this requires the @osdk/client library, which should be pre-installed. You can verify this by looking for the green pin:

The authentication library for TypeScript.

Use platform APIs

Once your function is authenticated, you can start using Foundry APIs. The examples below show how to call a language model or query media sets in both Python and TypeScript:

```typescript tab="TypeScript v2" import { Client } from "@osdk/client"; import { Functions } from "@osdk/foundry";

export default async function useLlm( client: Client, // This parameter gets populated by Foundry at runtime prompt: string ): Promise {

const promptMessage = [
    {
        role: "USER",
        content: prompt
    }
];
const result = await Functions.Queries.execute(
    client,
    "com.foundry.languagemodelservice.models.gpt41.CreateChatCompletion",
    {
        parameters: {
            messages: promptMessage
        }
    },
    {
        preview: true, // Required only for unstable endpoints, see API reference
    }
);
return result.value["completion"] as string;

} python tab="Python" from foundry_sdk import FoundryClient @function def media_item_to_base64(media_item_rid: str, media_set_rid: str) -> str: foundry_client = FoundryClient() result = foundry_client.media_sets.MediaSet.read( media_set_rid=media_set_rid, media_item_rid=media_item_rid, preview=True # Required only for unstable endpoints, see API reference ) # Convert the binary stream to a base64 encoded string base64_encoded = base64.b64encode(result).decode('utf-8') return base64_encoded ```

Client permissions

The TypeScript v2 client (passed to the function by Foundry at runtime) and the Python client initialized in code have the following permissions scope:

  • api:admin-read
  • api:functions-read
  • api:ontologies-read
  • api:orchestration-read
  • api:usage:mediasets-read
  • api:usage:ontologies-write

Each platform API endpoint requires certain scopes to hit the endpoint. Documentation on these scopes can be found in the API reference


中文翻译


使用 Foundry 平台 SDK 调用平台 API

Foundry API 提供了多种功能,您可以通过 Foundry 平台 SDK 库中的函数来利用这些功能。您可以使用平台 SDK 构建用于管理或治理工作流的函数、与调度和构建交互、访问媒体集(media sets)等。

:::callout{theme="warning"} TypeScript v1 函数不支持一级身份验证(first-class authentication)。我们建议对这些工作流使用 Python 函数和 TypeScript v2 函数。 :::

安装 SDK

要安装 Foundry 平台 SDK,请导航到代码仓库中的 Libraries 侧面板,搜索 SDK 名称:Python 为 foundry-platform-sdk,TypeScript 为 @osdk/foundry

Libraries 搜索面板,搜索 Python 平台 SDK。

Libraries 搜索面板,搜索 TypeScript 平台 SDK。

初始化客户端

您的函数需要身份验证才能与 Foundry API 交互。此过程涉及实例化一个经过身份验证的“客户端”(client),通过该客户端您可以使用 SDK 向 Foundry API 发出请求。在 TypeScript v2 仓库中,这需要 @osdk/client 库,该库应已预装。您可以通过查找绿色图钉来验证:

TypeScript 的身份验证库。

使用平台 API

一旦您的函数通过身份验证,您就可以开始使用 Foundry API。以下示例展示了如何在 Python 和 TypeScript 中调用语言模型或查询媒体集:

```typescript tab="TypeScript v2" import { Client } from "@osdk/client"; import { Functions } from "@osdk/foundry";

export default async function useLlm( client: Client, // 此参数由 Foundry 在运行时填充 prompt: string ): Promise {

const promptMessage = [
    {
        role: "USER",
        content: prompt
    }
];
const result = await Functions.Queries.execute(
    client,
    "com.foundry.languagemodelservice.models.gpt41.CreateChatCompletion",
    {
        parameters: {
            messages: promptMessage
        }
    },
    {
        preview: true, // 仅对不稳定端点必需,请参阅 API 参考
    }
);
return result.value["completion"] as string;

} python tab="Python" from foundry_sdk import FoundryClient @function def media_item_to_base64(media_item_rid: str, media_set_rid: str) -> str: foundry_client = FoundryClient() result = foundry_client.media_sets.MediaSet.read( media_set_rid=media_set_rid, media_item_rid=media_item_rid, preview=True # 仅对不稳定端点必需,请参阅 API 参考 ) # 将二进制流转换为 base64 编码字符串 base64_encoded = base64.b64encode(result).decode('utf-8') return base64_encoded ```

客户端权限

TypeScript v2 客户端(由 Foundry 在运行时传递给函数)和代码中初始化的 Python 客户端具有以下权限范围:

  • api:admin-read
  • api:functions-read
  • api:ontologies-read
  • api:orchestration-read
  • api:usage:mediasets-read
  • api:usage:ontologies-write

每个平台 API 端点都需要特定的范围才能访问该端点。有关这些范围的文档,请参阅 API 参考