생성형 AI 모델은 처리를 위해 토큰이라는 단위로 텍스트 데이터를 분할합니다. 텍스트 데이터가 토큰으로 변환되는 방식은 사용된 토크나이저에 따라 달라집니다. 토큰은 문자, 단어, 어구일 수 있습니다. 각 모델에는 프롬프트 및 응답에서 처리할 수 있는 최대 토큰 수가 포함됩니다. 이 페이지에서는 프롬프트에 대해 토큰 수 및 청구 가능한 문자 수를 가져오는 방법을 보여줍니다.
지원되는 모델
다음 기반 모델은 프롬프트 토큰 수 가져오기를 지원합니다.
text-bison
chat-bison
code-bison
codechat-bison
code-gecko
textembedding-gecko
Gemini 모델의 프롬프트 토큰 수를 가져오는 방법은 Vertex AI Gemini API의 토큰 수 가져오기 안내를 참조하세요.
프롬프트의 토큰 수 가져오기
countTokens
API를 사용해서 프롬프트에 대해 토큰 수 및 청구 가능한 문자 수를 가져올 수 있습니다. countTokens
의 입력 형식은 사용하는 모델에 따라 달라집니다. 각 입력 형식은 predict
입력 형식과 동일합니다.
Vertex AI API를 사용해서 프롬프트에 대해 토큰 수 및 청구 가능한 문자 수를 가져오려면 게시자 모델 엔드포인트에 POST 요청을 전송합니다.
요청 데이터를 사용하기 전에 다음을 바꿉니다.
LOCATION : 지원되는 리전을 입력합니다. 지원되는 리전의 전체 목록은 사용 가능한 위치를 참조하세요.PROJECT_ID : 프로젝트 IDPROMPT : 토큰 수 및 청구 가능한 문자를 가져오는 프롬프트입니다. (여기에서 프롬프트 주위에 따옴표를 추가하지 마세요.)
HTTP 메서드 및 URL:
POST https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/text-bison:countTokens
JSON 요청 본문:
{ "instances": [ { "prompt": "PROMPT " } ] }
요청을 보내려면 다음 옵션 중 하나를 선택합니다.
요청 본문을 request.json
파일에 저장합니다.
터미널에서 다음 명령어를 실행하여 현재 디렉터리에 이 파일을 만들거나 덮어씁니다.
cat > request.json << 'EOF' { "instances": [ { "prompt": "PROMPT " } ] } EOF
그런 후 다음 명령어를 실행하여 REST 요청을 전송합니다.
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/text-bison:countTokens"
요청 본문을 request.json
파일에 저장합니다.
터미널에서 다음 명령어를 실행하여 현재 디렉터리에 이 파일을 만들거나 덮어씁니다.
@' { "instances": [ { "prompt": "PROMPT " } ] } '@ | Out-File -FilePath request.json -Encoding utf8
그런 후 다음 명령어를 실행하여 REST 요청을 전송합니다.
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/text-bison:countTokens" | Select-Object -Expand Content
다음과 비슷한 JSON 응답이 수신됩니다.
{ "totalTokens": 12, "totalBillableCharacters": 54 }
curl 명령어 예시
PROJECT_ID="PROJECT_ID " curl \ -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/text-bison:countTokens -d \ $'{ "instances": [ { "prompt": "Give me ten interview questions for the role of program manager." }, { "prompt": "List some good qualities for a program manager." } ] }'
Vertex AI API를 사용해서 프롬프트에 대해 토큰 수 및 청구 가능한 문자 수를 가져오려면 게시자 모델 엔드포인트에 POST 요청을 전송합니다.
요청 데이터를 사용하기 전에 다음을 바꿉니다.
LOCATION : 지원되는 리전을 입력합니다. 지원되는 리전의 전체 목록은 사용 가능한 위치를 참조하세요.PROJECT_ID : 프로젝트 IDCONTEXT : 선택사항. 컨텍스트는 모델이 응답하는 방법에 대해 모델에 제공하는 지침이거나 모델이 응답을 생성하는 데 사용하거나 참조하는 정보일 수 있습니다. 모델에 정보를 제공해야 할 때 프롬프트에 컨텍스트 정보를 추가하거나 컨텍스트 내에 있는 정보만으로 응답 경계를 제한합니다.EXAMPLE_AUTHOR_1 :EXAMPLE_INPUT
의 작성자(사용자)입니다.EXAMPLE_INPUT : 메시지 예시입니다.EXAMPLE_AUTHOR_2 :EXAMPLE_OUTPUT
의 작성자(봇)입니다.EXAMPLE_OUTPUT : 이상적인 응답의 예시입니다.AUTHOR_1 : 메시지의 작성자(사용자)입니다.CONTENT : 메시지 콘텐츠입니다.
HTTP 메서드 및 URL:
POST https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/chat-bison:countTokens
JSON 요청 본문:
{ "instances": [ { "context": "CONTEXT ", "examples": [ { "input": { "author": "EXAMPLE_AUTHOR_1 ", "content": "EXAMPLE_INPUT " }, "output": { "author": "EXAMPLE_AUTHOR_2 ", "content": "EXAMPLE_OUTPUT " } } ], "messages": [ { "author": "AUTHOR_1 ", "content": "CONTENT " } ] } ] }
요청을 보내려면 다음 옵션 중 하나를 선택합니다.
요청 본문을 request.json
파일에 저장합니다.
터미널에서 다음 명령어를 실행하여 현재 디렉터리에 이 파일을 만들거나 덮어씁니다.
cat > request.json << 'EOF' { "instances": [ { "context": "CONTEXT ", "examples": [ { "input": { "author": "EXAMPLE_AUTHOR_1 ", "content": "EXAMPLE_INPUT " }, "output": { "author": "EXAMPLE_AUTHOR_2 ", "content": "EXAMPLE_OUTPUT " } } ], "messages": [ { "author": "AUTHOR_1 ", "content": "CONTENT " } ] } ] } EOF
그런 후 다음 명령어를 실행하여 REST 요청을 전송합니다.
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/chat-bison:countTokens"
요청 본문을 request.json
파일에 저장합니다.
터미널에서 다음 명령어를 실행하여 현재 디렉터리에 이 파일을 만들거나 덮어씁니다.
@' { "instances": [ { "context": "CONTEXT ", "examples": [ { "input": { "author": "EXAMPLE_AUTHOR_1 ", "content": "EXAMPLE_INPUT " }, "output": { "author": "EXAMPLE_AUTHOR_2 ", "content": "EXAMPLE_OUTPUT " } } ], "messages": [ { "author": "AUTHOR_1 ", "content": "CONTENT " } ] } ] } '@ | Out-File -FilePath request.json -Encoding utf8
그런 후 다음 명령어를 실행하여 REST 요청을 전송합니다.
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/chat-bison:countTokens" | Select-Object -Expand Content
다음과 비슷한 JSON 응답이 수신됩니다.
{ "totalTokens": 43, "totalBillableCharacters": 182 }
curl 명령어 예시
PROJECT_ID="PROJECT_ID " curl \ -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/chat-bison:countTokens -d \ $'{ "instances": [ { "context": "You are Captain Bartholomew, the most feared pirate dog of the seven seas.", "examples": [ { "input": { "author": "User", "content": "Hello!" }, "output": { "author": "Captain Barktholomew", "content": "Argh! What brings ye to my ship?" } }, { "input": { "author": "User", "content": "Who are you?" }, "output": { "author": "Captain Barktholomew", "content": "I be Captain Barktholomew, the most feared pirate dog of the seven seas." } } ], "messages": [ { "author": "User", "content": "Hello!" }, { "author": "Captain Barktholomew", "content": "Ahoy there, landlubber! What brings ye to me ship?" }, { "author": "User", "content": "Can you tell me a tale of your most recent adventure?" }, { "author": "Captain Barktholomew", "content": "Aye, I\'ll spin ye a tale of me latest adventure....", }, { "author": "User", "content": "I\'m listening." } ] } ] }'
Vertex AI API를 사용해서 프롬프트에 대해 토큰 수 및 청구 가능한 문자 수를 가져오려면 게시자 모델 엔드포인트에 POST 요청을 전송합니다.
요청 데이터를 사용하기 전에 다음을 바꿉니다.
LOCATION : 지원되는 리전을 입력합니다. 지원되는 리전의 전체 목록은 사용 가능한 위치를 참조하세요.PROJECT_ID : 프로젝트 IDPREFIX : 코드 모델에서prefix
는 생성할 코드를 설명하는 의미 있는 프로그래밍 코드 조각 또는 자연어 프롬프트의 시작 부분을 나타냅니다.
HTTP 메서드 및 URL:
POST https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/code-bison:countTokens
JSON 요청 본문:
{ "instances": [ { "prefix": "PREFIX " } ] }
요청을 보내려면 다음 옵션 중 하나를 선택합니다.
요청 본문을 request.json
파일에 저장합니다.
터미널에서 다음 명령어를 실행하여 현재 디렉터리에 이 파일을 만들거나 덮어씁니다.
cat > request.json << 'EOF' { "instances": [ { "prefix": "PREFIX " } ] } EOF
그런 후 다음 명령어를 실행하여 REST 요청을 전송합니다.
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/code-bison:countTokens"
요청 본문을 request.json
파일에 저장합니다.
터미널에서 다음 명령어를 실행하여 현재 디렉터리에 이 파일을 만들거나 덮어씁니다.
@' { "instances": [ { "prefix": "PREFIX " } ] } '@ | Out-File -FilePath request.json -Encoding utf8
그런 후 다음 명령어를 실행하여 REST 요청을 전송합니다.
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/code-bison:countTokens" | Select-Object -Expand Content
다음과 비슷한 JSON 응답이 수신됩니다.
{ "totalTokens": 43, "totalBillableCharacters": 182 }
curl 명령어 예시
PROJECT_ID=PROJECT_ID curl \ -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/code-bison:countTokens -d \ $'{ "instances": [ { "prefix": "Write a function that checks if a year is a leap year." } ] }'
Vertex AI API를 사용해서 프롬프트에 대해 토큰 수 및 청구 가능한 문자 수를 가져오려면 게시자 모델 엔드포인트에 POST 요청을 전송합니다.
요청 데이터를 사용하기 전에 다음을 바꿉니다.
LOCATION : 지원되는 리전을 입력합니다. 지원되는 리전의 전체 목록은 사용 가능한 위치를 참조하세요.PROJECT_ID : 프로젝트 IDAUTHOR : 메시지의 작성자입니다.CONTENT : 메시지 콘텐츠입니다.
HTTP 메서드 및 URL:
POST https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/codechat-bison:countTokens
JSON 요청 본문:
{ "instances": { "messages": [ { "author": "AUTHOR ", "content": "CONTENT " } ] } }
요청을 보내려면 다음 옵션 중 하나를 선택합니다.
요청 본문을 request.json
파일에 저장합니다.
터미널에서 다음 명령어를 실행하여 현재 디렉터리에 이 파일을 만들거나 덮어씁니다.
cat > request.json << 'EOF' { "instances": { "messages": [ { "author": "AUTHOR ", "content": "CONTENT " } ] } } EOF
그런 후 다음 명령어를 실행하여 REST 요청을 전송합니다.
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/codechat-bison:countTokens"
요청 본문을 request.json
파일에 저장합니다.
터미널에서 다음 명령어를 실행하여 현재 디렉터리에 이 파일을 만들거나 덮어씁니다.
@' { "instances": { "messages": [ { "author": "AUTHOR ", "content": "CONTENT " } ] } } '@ | Out-File -FilePath request.json -Encoding utf8
그런 후 다음 명령어를 실행하여 REST 요청을 전송합니다.
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/codechat-bison:countTokens" | Select-Object -Expand Content
다음과 비슷한 JSON 응답이 수신됩니다.
{ "totalTokens": 43, "totalBillableCharacters": 182 }
curl 명령어 예시
PROJECT_ID=PROJECT_ID curl \ -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/codechat-bison:countTokens -d \ $'{ "instances": { "messages": [ { "author": "user", "content": "Hi, how are you?" }, { "author": "system", "content": "I am doing good. What Can I help you with in the coding world?" }, { "author": "user", "content": "Please help write a function to calculate the min of two numbers" } ] } }'
Vertex AI API를 사용해서 프롬프트에 대해 토큰 수 및 청구 가능한 문자 수를 가져오려면 게시자 모델 엔드포인트에 POST 요청을 전송합니다.
요청 데이터를 사용하기 전에 다음을 바꿉니다.
LOCATION : 지원되는 리전을 입력합니다. 지원되는 리전의 전체 목록은 사용 가능한 위치를 참조하세요.PROJECT_ID : 프로젝트 IDPREFIX : 코드 모델에서prefix
는 생성할 코드를 설명하는 의미 있는 프로그래밍 코드 조각 또는 자연어 프롬프트의 시작 부분을 나타냅니다. 모델이prefix
와suffix
사이의 코드를 채우려고 시도합니다.SUFFIX : 코드 완성에서suffix
는 의미 있는 프로그래밍 코드 조각의 끝 부분을 나타냅니다. 모델이prefix
와suffix
사이의 코드를 채우려고 시도합니다.
HTTP 메서드 및 URL:
POST https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/code-gecko:countTokens
JSON 요청 본문:
{ "instances": [ { "prefix": "PREFIX ", "suffix": "SUFFIX " } ] }
요청을 보내려면 다음 옵션 중 하나를 선택합니다.
요청 본문을 request.json
파일에 저장합니다.
터미널에서 다음 명령어를 실행하여 현재 디렉터리에 이 파일을 만들거나 덮어씁니다.
cat > request.json << 'EOF' { "instances": [ { "prefix": "PREFIX ", "suffix": "SUFFIX " } ] } EOF
그런 후 다음 명령어를 실행하여 REST 요청을 전송합니다.
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/code-gecko:countTokens"
요청 본문을 request.json
파일에 저장합니다.
터미널에서 다음 명령어를 실행하여 현재 디렉터리에 이 파일을 만들거나 덮어씁니다.
@' { "instances": [ { "prefix": "PREFIX ", "suffix": "SUFFIX " } ] } '@ | Out-File -FilePath request.json -Encoding utf8
그런 후 다음 명령어를 실행하여 REST 요청을 전송합니다.
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/code-gecko:countTokens" | Select-Object -Expand Content
다음과 비슷한 JSON 응답이 수신됩니다.
{ "totalTokens": 43, "totalBillableCharacters": 182 }
curl 명령어 예시
PROJECT_ID=PROJECT_ID curl \ -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/code-gecko:countTokens -d \ $'{ "instances": [ { "prefix": "def reverse_string(s):", "suffix": "" } ] }'
Vertex AI API를 사용해서 프롬프트에 대해 토큰 수 및 청구 가능한 문자 수를 가져오려면 게시자 모델 엔드포인트에 POST 요청을 전송합니다.
요청 데이터를 사용하기 전에 다음을 바꿉니다.
LOCATION : 지원되는 리전을 입력합니다. 지원되는 리전의 전체 목록은 사용 가능한 위치를 참조하세요.PROJECT_ID : 프로젝트 IDTEXT : 임베딩을 생성하려는 텍스트입니다.
HTTP 메서드 및 URL:
POST https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/textembedding-gecko:countTokens
JSON 요청 본문:
{ "instances": [ { "content": "TEXT " } ] }
요청을 보내려면 다음 옵션 중 하나를 선택합니다.
요청 본문을 request.json
파일에 저장합니다.
터미널에서 다음 명령어를 실행하여 현재 디렉터리에 이 파일을 만들거나 덮어씁니다.
cat > request.json << 'EOF' { "instances": [ { "content": "TEXT " } ] } EOF
그런 후 다음 명령어를 실행하여 REST 요청을 전송합니다.
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/textembedding-gecko:countTokens"
요청 본문을 request.json
파일에 저장합니다.
터미널에서 다음 명령어를 실행하여 현재 디렉터리에 이 파일을 만들거나 덮어씁니다.
@' { "instances": [ { "content": "TEXT " } ] } '@ | Out-File -FilePath request.json -Encoding utf8
그런 후 다음 명령어를 실행하여 REST 요청을 전송합니다.
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION -aiplatform.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /publishers/google/models/textembedding-gecko:countTokens" | Select-Object -Expand Content
다음과 비슷한 JSON 응답이 수신됩니다.
{ "totalTokens": 43, "totalBillableCharacters": 182 }
curl 명령어 예시
PROJECT_ID=PROJECT_ID curl \ -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/textembedding-gecko:countTokens -d \ $'{ "instances": [ { "content": "What is life?" } ] }'
가격 책정 및 할당량
CountTokens
API를 사용하는 경우 요금이 부과되지 않습니다. CountTokens
API 및 ComputeTokens
API의 최대 할당량은 분당 요청 3000개입니다.
다음 단계
- 토큰 계산 방법 알아보기
- 채팅 프롬프트 테스트 방법 알아보기
- 텍스트 프롬프트 테스트 방법 알아보기
- 텍스트 임베딩 가져오기 방법 알아보기