Docs Menu
Docs Home
/

토큰화

입력이 주어지면 임베딩 및 순위 변경 프로세스 의 첫 번째 단계는 입력을 토큰 목록으로 분할 하는 것입니다. API 호출하면 당사 서버가 이 토큰화 단계를 자동으로 수행합니다.Python 클라이언트 API 호출하기 전에 토크나이저 사용해 볼 수 있는 메서드가 포함되어 있습니다.

tokenize 메서드를 사용하여 특정 모델의 텍스트 목록을 토큰화합니다.

예시

import voyageai
# Initialize client (uses VOYAGE_API_KEY environment variable)
vo = voyageai.Client()
texts = [
"The Mediterranean diet emphasizes fish, olive oil, and vegetables, believed to reduce chronic diseases.",
"Photosynthesis in plants converts light energy into glucose and produces essential oxygen."
]
# Tokenize the texts
tokenized = vo.tokenize(texts, model="voyage-4-large")
for i in range(len(texts)):
print(tokenized[i].tokens)
['The', 'ĠMediterranean', 'Ġdiet', 'Ġemphasizes', 'Ġfish', ',', 'Ġolive', 'Ġoil', ',', 'Ġand', 'Ġvegetables', ',', 'Ġbelieved', 'Ġto', 'Ġreduce', 'Ġchronic', 'Ġdiseases', '.']
['Photos', 'ynthesis', 'Ġin', 'Ġplants', 'Ġconverts', 'Ġlight', 'Ġenergy', 'Ġinto', 'Ġglucose', 'Ġand', 'Ġproduces', 'Ġessential', 'Ġoxygen', '.']

count_tokens 메서드를 사용하여 특정 모델의 텍스트 목록에 있는 토큰 수를 계산합니다.

예시

import voyageai
# Initialize client (uses VOYAGE_API_KEY environment variable)
vo = voyageai.Client()
texts = [
"The Mediterranean diet emphasizes fish, olive oil, and vegetables, believed to reduce chronic diseases.",
"Photosynthesis in plants converts light energy into glucose and produces essential oxygen."
]
# Count total tokens
total_tokens = vo.count_tokens(texts, model="voyage-4-large")
print(total_tokens)
32

count_usage 메서드를 사용하여 특정 모델에 대한 입력 목록에 있는 토큰 및 픽셀 수를 계산합니다.

참고

Voyage 임베딩 모델에는 컨텍스트 길이 제한이 있습니다. 텍스트가 제한을 초과하는 경우 API 호출하기 전에 텍스트를 잘라내거나 인수를 truncationTrue 지정합니다.

예시

import voyageai
import PIL
# Initialize client (uses VOYAGE_API_KEY environment variable)
vo = voyageai.Client()
# Create input with text and image
inputs = [
["This is a banana.", PIL.Image.open('banana.jpg')]
]
# Count tokens and pixels
usage = vo.count_usage(inputs, model="voyage-multimodal-3.5")
print(usage)
{'text_tokens': 5, 'image_pixels': 2000000, 'total_tokens': 3576}

토크나이저 를 사용할 때는 다음 사항을 고려하세요.

  • 최신 NLP 모델은 일반적으로 텍스트 문자열을 토큰 목록으로 변환합니다. "you" 및 "apple"과 같이 자주 사용되는 단어는 그 자체로 토큰입니다. 예시, 드물거나 긴 단어는 여러 토큰으로 나뉩니다. 한 단어는 도메인의 복잡성에 따라 평균적으로 1.2 ~ 1.5 토큰에 대략적으로 해당합니다.

    토크나이저 에서 생성된 토큰의 평균은 5 자이므로 텍스트 문자열의 문자 수를 5로 나누어 토큰 수를 대략적으로 추정할 수 있습니다. 정확한 토큰 수를 확인하려면 count_tokens() 메서드를 사용합니다.

  • Voyage의 토크나이저는 Hugging Face에서도 사용할 수 있습니다. 다음 코드를 사용하여 특정 모델과 연결된 토크나이저 액세스 할 수 있습니다.

    from transformers import AutoTokenizer
    tokenizer = AutoTokenizer.from_pretrained('voyageai/voyage-4-large')
  • tiktoken 는 인기 있는 오픈 소스 토크나이저 입니다. Voyage 모델은 다양한 토크나이저를 사용합니다. 따라서 토크나이저 저는 tiktoken와 비교하여 특정 텍스트에 대해 다른 토큰 목록을 생성합니다. 통계적으로 토크나이저 에서 생성된 토큰 수는 tiktoken의 평균 1.1 ~ 1.2 배입니다. 정확한 토큰 수를 확인하려면 count_tokens() 메서드를 사용합니다.

돌아가기

RAG

이 페이지의 내용