토큰 생성 클라이언트
토큰 생성 클라이언트 (Token Generation Client)
--enable-scale-out으로 시작한 토큰 인/토큰 아웃(token-in/token-out) 서버를 호출하는 예제입니다. 토크나이저로 채팅 템플릿을 토큰 ID로 변환한 뒤 /inference/v1/generate로 보내고, 반환된 토큰 ID를 다시 디코드해 텍스트를 얻는 흐름을 보여줍니다.
출처: 문서
원본: https://github.com/vllm-project/vllm/blob/main/examples/scale_out/token_generation_client.py
본문
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Call a token-in/token-out server started with:
vllm serve Qwen/Qwen3-0.6B --enable-scale-out
"""
import httpx
from transformers import AutoTokenizer
GEN_ENDPOINT = "http://localhost:8000/inference/v1/generate"
DUMMY_API_KEY = "empty"
MODEL_NAME = "Qwen/Qwen3-0.6B"
transport = httpx.HTTPTransport()
headers = {"Authorization": f"Bearer {DUMMY_API_KEY}"}
client = httpx.Client(
transport=transport,
base_url=GEN_ENDPOINT,
timeout=600,
headers=headers,
)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "How many countries are in the EU?"},
]
def main(client):
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
token_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
enable_thinking=False,
return_dict=True,
).input_ids
payload = {
"model": MODEL_NAME,
"token_ids": token_ids,
"sampling_params": {"max_tokens": 24, "temperature": 0.2, "detokenize": False},
"stream": False,
}
resp = client.post(GEN_ENDPOINT, json=payload)
resp.raise_for_status()
data = resp.json()
print(data)
print("-" * 50)
print("Token generation results:")
res = tokenizer.decode(data["choices"][0]["token_ids"])
print(res)
print("-" * 50)
if __name__ == "__main__":
main(client)
핵심 포인트는 서버가 토큰 단위로 주고받는다는 것입니다. 클라이언트가 apply_chat_template으로 토큰 ID를 만들어 보내고, 서버가 반환한 token_ids를 클라이언트가 다시 디코드해 텍스트로 보여줍니다. 실행 전에 vllm serve Qwen/Qwen3-0.6B --enable-scale-out으로 서버를 시작하세요.
더 알아보기 (Learn more)
- Example MM Serve — 멀티모달 렌더 → 생성 왕복 예제
- 원본 예제 파일