TypeScript v1의 언어 모델 바인딩
TSv1은 Palantir가 제공하는 언어 모델의 생성 바인딩을 가져와 호출할 수 있습니다. 플랫폼 인스턴스에 AIP가 활성화되어 있고 AIP 제작 기능을 사용할 권한이 있어야 합니다. 모델 제공 범위와 처리량 한도는 플랫폼 인스턴스마다 다를 수 있습니다.
TSv2와 Python은 프록시 엔드포인트 및 모델 별칭을 사용합니다.
모델 가져오기
- Model Imports 사이드바에서 이미 가져온 모델을 확인합니다.
- Resource Imports의 Add > Models를 선택하여 접근 가능한 Palantir 제공 모델을 엽니다.
사용자 모델 탭에는 Modeling Objective 또는 직접 배포에서 만든 모델이 표시됩니다. 이 모델은 모델 함수의 게시와 가져오기 계약을 따릅니다.
- 모델을 선택하고 Confirm selection을 누릅니다. Task Runner가 localDev를 실행하여 코드 바인딩을 생성합니다.
- 사이드바에서 모델을 선택하면 사용 가능한 메서드, 입력과 출력 및 코드 예제를 볼 수 있습니다.
GPT-4.1 텍스트와 이미지
GPT-4.1을 가져오면 @foundry/languagemodelservice/models의 Gpt41을 사용합니다. createChatCompletion, createChatVisionCompletion과 createChatCompletionStreamed는 서로 다른 메서드입니다. 사용할 수 있는 추가 기능은 가져온 모델 버전의 상세 패널을 확인합니다.
게시 함수는 호출하는 모델 메서드를 @Uses의 queries에 선언합니다. 정적 분석도 호출을 찾지만 일부 호출을 놓치면 실행 시 @Uses를 추가하라는 오류가 발생합니다.
아래 클래스의 텍스트 함수는 Good, Bad 또는 Uncertain을 요청하는 감정 분석 예제이고, 이미지 함수는 사용자가 입력한 지시와 PNG를 함께 전달합니다. 프롬프트의 출력 요구는 모델에 대한 지시이며 함수 타입 자체가 세 문자열만 허용하는 열거형은 아닙니다.
import { Function, Uses, UserFacingError } from "@foundry/functions-api";
import { Gpt41 } from "@foundry/languagemodelservice/models";
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 LanguageModelFunctions {
@Function()
@Uses({ queries: [Gpt41.createChatCompletion] })
public async llmFunction_createChatCompletion(userPrompt: string): Promise<string> {
const response = await Gpt41.createChatCompletion({
messages: [
{ role: "SYSTEM", content: SYSTEM_PROMPT },
{ role: "USER", content: userPrompt },
],
params: { temperature: 0 },
});
if (response.type !== "ok") throw new UserFacingError("텍스트 모델 호출에 실패했습니다.");
if (typeof response.value.completion !== "string") {
throw new UserFacingError("모델 응답에 텍스트가 없습니다.");
}
return response.value.completion;
}
@Function()
@Uses({ queries: [Gpt41.createChatVisionCompletion] })
public async llmFunction_createChatVisionCompletion(userPrompt: string, pngBase64String: string): Promise<string> {
const response = await Gpt41.createChatVisionCompletion({
messages: [{
role: "USER",
content: [
{ type: "text", text: userPrompt },
{ type: "genericMedia", genericMedia: { mimeType: "IMAGE_PNG", content: pngBase64String } },
],
}],
params: { temperature: 0 },
});
if (response.type !== "ok") throw new UserFacingError("이미지 모델 호출에 실패했습니다.");
if (typeof response.value.completion !== "string") {
throw new UserFacingError("모델 응답에 텍스트가 없습니다.");
}
return response.value.completion;
}
}
pngBase64String은 PNG 바이너리 내용을 Base64로 인코딩한 문자열입니다. 파일 경로나 URL을 넣는 매개변수가 아닙니다. 이 바인딩의 SYSTEM, USER 및 IMAGE_PNG는 정확한 wire 값이며 OpenAI 호환 프록시의 소문자 role과 구분합니다.
임베딩
Textembedding3large를 가져오면 입력 문자열 목록에 대응하는 벡터를 생성할 수 있습니다.
import { Double, Function, Uses, UserFacingError } from "@foundry/functions-api";
import { Textembedding3large } from "@foundry/languagemodelservice/models";
export class EmbeddingFunctions {
@Function()
@Uses({ queries: [Textembedding3large.createEmbeddings] })
public async llmFunction_embeddings(inputs: string[]): Promise<Double[][]> {
const response = await Textembedding3large.createEmbeddings({ inputs });
if (response.type !== "ok") throw new UserFacingError("임베딩 생성에 실패했습니다.");
return response.value.embeddings;
}
}
기본 text-embedding-3-large 출력은 3,072차원입니다. TSv1 Objects API의 KNN 검색은 최대 2,048차원이므로 기본 출력을 그대로 넣을 수 없습니다. 의미 기반 검색 예제는 문서와 질의에 같은 1,536차원 ada-002 경로를 사용합니다. 임베딩 생성 자체와 검색 API의 지원 차원을 구분해야 합니다.
기존 바인딩에서 이전
레거시 모델 바인딩을 쓰는 저장소에는 모델 이전 경고가 표시됩니다. 새 저장소에 레거시 모델이 없다면 이 절차는 필요하지 않습니다.
- 사이드바의 경고에서 Select imports를 선택합니다.
- 가져오기 창에서 Fix를 선택해 이전 모델 가져오기를 제거합니다.
- 모델을 다시 선택하여 새 버전으로 가져옵니다. 중앙 상세 패널에서 추가 기능을 확인합니다.
- 새 import와 호출 및 응답 형식으로 코드를 바꿉니다. 이전은 컴파일 호환을 깨므로 모델만 다시 가져오고 기존 코드를 그대로 게시할 수 없습니다.
모델의 토큰 처리량 제한과 함수 실행 한도는 함께 적용합니다. 스트리밍 반환을 사용하는 함수는 스트리밍 계약을 따릅니다.