跳转至

Language models in TypeScript v1 functions(TypeScript v1 函数中的语言模型)

:::callout{theme="warning"} The following documentation is specific to TypeScript v1 functions. For more robust capabilities, including support for Ontology SDK and configurable resource requests, we recommend migrating to TypeScript v2. :::

:::callout{title="Prerequisites" theme="neutral"} To use Palantir-provided language models, AIP must first be enabled on your enrollment. You must also have permissions to use AIP builder capabilities. :::

Palantir provides a set of language models which can be used within functions. Read more about Palantir-provided LLMs.

Import a language model

To begin using a language model, you must import the specific model into the code repository where you are writing your functions by following the steps below:

  1. Navigate and open the Model Imports side panel to see all existing imported models.

Model import sidebar.

  1. To import a new language model, select Add in the upper right corner of the Resource Imports panel, then select Models. This will open a new window where you will be able to see Palantir-provided models that are available to you.

Model import dialog showing a few Palantir-provided LLMs.

  1. You will also see a tab where you can view custom models created through the Modeling Objectives application or direct model deployments previously. More information on using those models can be found in the functions on models documentation.

  2. Choose the models you would like to import, then select Confirm selection to import these models into your repository. Task runner will execute the localDev task, generating code bindings to interact with these models.

  3. After importing the language models, select the model in the sidebar to view the detailed capabilities offered by this model. You can also copy code snippets to help you import and author functions with the model.

Model details in sidebar.

Write a function that uses a language model

At this stage, you can now write a function that uses the language model you imported. For this example, assume that you imported GPT-4.1.

Begin by adding the following import statement to your file:

import { Function } from "@foundry/functions-api";
import { Gpt41 } from "@foundry/languagemodelservice/models";

Each language model will have generated methods available with strongly typed inputs and outputs. For example, the GPT-4.1 model provides createChatCompletion, createChatVisionCompletion, and createChatCompletionStreamed as different APIs to interact with the model. The list of capabilities could expand in later versions of the imported model.

In the following illustrative example, the provided GPT_4o model is used to run a simple sentiment analysis on a piece of text or image provided by a user. The function will classify the text as "Good", "Bad", or "Uncertain".

const SYSTEM_PROMPT =
    "Provide an estimation of the sentiment the text the user has provided. \
You may respond with either Good, Bad, or Uncertain. Only choose Good or Bad if you are overwhelmingly \
sure that the text is either good or bad. If the text is neutral, or you are unable to determine, choose Uncertain.";

export class MyFunctions {
    @Function()
    public async llmFunction_createChatCompletion(userPrompt: string): Promise<string | undefined> {
        const response = await Gpt41.createChatCompletion({
            messages: [
                {
                    role: "SYSTEM",
                    content: SYSTEM_PROMPT,
                },
                {
                    role: "USER",
                    content: userPrompt,
                },
            ],
            params: {
                temperature: 0,
            },
        });
        return response.type === "ok" ? response.value.completion : "error";
    }

    @Function()
    public async llmFunction_createChatVisionCompletion(
        userPrompt: string,
        pngBase64String: string,
    ): Promise<string | undefined> {
        const response = await Gpt41.createChatVisionCompletion({
            messages: [
                {
                    role: "USER",
                    content: [
                        { type: "text", text: userPrompt },
                        {
                            type: "genericMedia",
                            genericMedia: {
                                mimeType: "IMAGE_PNG",
                                // Base64 encoded PNG String
                                content: pngBase64String,
                            },
                        },
                    ],
                },
            ],
            params: {
                temperature: 0,
            },
        });
        return response.type === "ok" ? response.value.completion : "error";
    }
}

This function can then be used throughout the platform.

Embeddings

Along with generative language models, Palantir also provides models that can be used to generate embeddings. A simple example is as follows:

@Function()
public async llmFunction_embeddings(inputs: string[]): Promise<Double[][]> {
    const response = await Textembedding3large.createEmbeddings({ inputs });
    return response.type === "ok" ? response.value.embeddings : [[]];
}

This is most commonly used to perform semantic search workflows.

Upgrade from legacy language models within functions

:::callout{theme="neutral"} Skip this step if you are starting from a new repository with no legacy language models imported. :::

:::callout{theme="warning"} The following process will result in a compile-time break in your code repository, as the code syntax will be updated. Refer to the code snippets in the sidebar for each updated model to update your code. :::

The updated language models in functions offer more advanced capabilities, such as better support for vision and streaming. We highly recommend upgrading your repository to take advantage of the latest AIP offerings.

  1. If you have existing legacy language models imported, a warning icon to upgrade will appear in the sidebar. Choose Select imports to open the model import dialog.

Model imports warning in sidebar.

  1. In the model import dialog, select Fix to remove any deprecated legacy language models.

Model import dialog with warning.

  1. Reselect the models to migrate to the updated language model versions. You can view additional capabilities supported by this version in the details panel in the center of the dialog.

Model import dialog shows removed models.

  1. Now, you can view the updated model imports in the sidebar. Selecting a model will show you a details panel with code snippets to help you update your code to take advantage of the additional capabilities.

Model details and code snippet in sidebar.

Performance considerations

Certain models may have rate limits applied, limiting the number of tokens that may be passed over a certain time period. This will be enforced along with any standard limits that apply to functions.


Note: AIP feature availability is subject to change and may differ between customers.


中文翻译


TypeScript v1 函数中的语言模型

:::callout{theme="warning"} 以下文档仅适用于 TypeScript v1 函数。如需更强大的功能,包括对 Ontology SDK 和可配置资源请求的支持,我们建议迁移至 TypeScript v2。 :::

:::callout{title="前提条件" theme="neutral"} 要使用 Palantir 提供的语言模型,必须首先在您的注册中启用 AIP。您还需要拥有使用 AIP 构建者功能的权限。 :::

Palantir 提供了一组可在函数中使用的语言模型。了解更多关于 Palantir 提供的 LLM

导入语言模型

要开始使用语言模型,您必须按照以下步骤将特定模型导入到编写函数的代码仓库中:

  1. 导航并打开 模型导入 侧面板,查看所有已导入的模型。

模型导入侧边栏。

  1. 要导入新的语言模型,请在 资源导入 面板的右上角选择 添加,然后选择 模型。这将打开一个新窗口,您可以在其中看到可供您使用的 Palantir 提供的模型。

模型导入对话框,显示几个 Palantir 提供的 LLM。

  1. 您还会看到一个选项卡,可以查看之前通过建模目标应用程序或直接模型部署创建的自定义模型。有关使用这些模型的更多信息,请参阅函数与模型文档。

  2. 选择您要导入的模型,然后选择 确认选择 将这些模型导入到您的仓库中。任务运行器将执行 localDev 任务,生成用于与这些模型交互的代码绑定。

  3. 导入语言模型后,在侧边栏中选择该模型以查看该模型提供的详细功能。您还可以复制代码片段,帮助您导入模型并使用它编写函数。

侧边栏中的模型详情。

编写使用语言模型的函数

现在,您可以编写一个使用您导入的语言模型的函数。在此示例中,假设您导入了 GPT-4.1。

首先,在文件中添加以下导入语句:

import { Function } from "@foundry/functions-api";
import { Gpt41 } from "@foundry/languagemodelservice/models";

每个语言模型都会生成具有强类型输入和输出的可用方法。例如,GPT-4.1 模型提供了 createChatCompletioncreateChatVisionCompletioncreateChatCompletionStreamed 作为与模型交互的不同 API。功能列表可能会在导入模型的后续版本中扩展。

在以下示例中,使用提供的 GPT_4o 模型对用户提供的文本或图像进行简单的情感分析。该函数会将文本分类为"好"、"坏"或"不确定"。

const SYSTEM_PROMPT =
    "请评估用户提供的文本的情感倾向。\
您只能回复“好”、“坏”或“不确定”。只有在您非常确定文本是正面或负面时,才选择“好”或“坏”。\
如果文本是中性,或者您无法判断,请选择“不确定”。";

export class MyFunctions {
    @Function()
    public async llmFunction_createChatCompletion(userPrompt: string): Promise<string | undefined> {
        const response = await Gpt41.createChatCompletion({
            messages: [
                {
                    role: "SYSTEM",
                    content: SYSTEM_PROMPT,
                },
                {
                    role: "USER",
                    content: userPrompt,
                },
            ],
            params: {
                temperature: 0,
            },
        });
        return response.type === "ok" ? response.value.completion : "error";
    }

    @Function()
    public async llmFunction_createChatVisionCompletion(
        userPrompt: string,
        pngBase64String: string,
    ): Promise<string | undefined> {
        const response = await Gpt41.createChatVisionCompletion({
            messages: [
                {
                    role: "USER",
                    content: [
                        { type: "text", text: userPrompt },
                        {
                            type: "genericMedia",
                            genericMedia: {
                                mimeType: "IMAGE_PNG",
                                // Base64 编码的 PNG 字符串
                                content: pngBase64String,
                            },
                        },
                    ],
                },
            ],
            params: {
                temperature: 0,
            },
        });
        return response.type === "ok" ? response.value.completion : "error";
    }
}

然后,该函数可以在整个平台中使用。

嵌入

除了生成式语言模型,Palantir 还提供可用于生成嵌入的模型。一个简单的示例如下:

@Function()
public async llmFunction_embeddings(inputs: string[]): Promise<Double[][]> {
    const response = await Textembedding3large.createEmbeddings({ inputs });
    return response.type === "ok" ? response.value.embeddings : [[]];
}

这最常用于执行语义搜索工作流。

从函数中的旧版语言模型升级

:::callout{theme="neutral"} 如果您是从一个没有导入旧版语言模型的新仓库开始,请跳过此步骤。 :::

:::callout{theme="warning"} 以下过程将导致您的代码仓库出现编译时中断,因为代码语法将会更新。请参考侧边栏中每个更新模型的代码片段来更新您的代码。 :::

函数中更新的语言模型提供了更高级的功能,例如对视觉和流式传输的更好支持。我们强烈建议您升级仓库以利用最新的 AIP 产品。

  1. 如果您已导入现有的旧版语言模型,侧边栏中将出现一个升级警告图标。选择 选择导入 以打开模型导入对话框。

侧边栏中的模型导入警告。

  1. 在模型导入对话框中,选择 修复 以移除任何已弃用的旧版语言模型。

带有警告的模型导入对话框。

  1. 重新选择要迁移到更新语言模型版本的模型。您可以在对话框中央的详细信息面板中查看此版本支持的额外功能。

模型导入对话框显示已移除的模型。

  1. 现在,您可以在侧边栏中查看更新后的模型导入。选择一个模型将显示一个详细信息面板,其中包含代码片段,帮助您更新代码以利用额外功能。

侧边栏中的模型详情和代码片段。

性能考量

某些模型可能应用了速率限制,限制了在特定时间段内可以传递的令牌数量。这将与适用于函数的任何标准限制一起强制执行。


注意:AIP 功能的可用性可能会发生变化,并且可能因客户而异。