토큰 한도 설정
한 전자상거래 플랫폼에서 OpenAI API로 구축한 고객 지원 봇의 성능 개선을 의뢰했습니다. 먼저 입력 메시지가 레이트 리밋 문제를 일으키지 않도록 토큰 한도를 100개로 설정하고, 샘플 입력 메시지로 테스트해 보려고 합니다.
tiktoken 라이브러리는 미리 로드되어 있습니다.
이 연습은 강의의 일부입니다
OpenAI API로 AI 시스템 개발하기
연습 안내
tiktoken라이브러리를 사용해gpt-4o-mini모델용 인코딩을 생성하세요.- 입력 메시지의 예상 토큰 수를 확인하세요.
- 두 검사를 모두 통과하면 응답을 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
client = OpenAI(api_key="")
input_message = {"role": "user", "content": "I'd like to buy a shirt and a jacket. Can you suggest two color pairings for these items?"}
# Use tiktoken to create the encoding for your model
encoding = tiktoken.____(____)
# Check for the number of tokens
num_tokens = ____
# Run the chat completions function and print the response
if num_tokens <= ____:
response = client.chat.completions.create(model="gpt-4o-mini", messages=[input_message])
print(____)
else:
print("Message exceeds token limit")