대화 기록 만들기
Easy as Pi라는 온라인 수학 학습 플랫폼에서 AI 튜터 개발을 의뢰해 왔습니다. 여러분은 DeepSeek의 채팅 모드를 활용해 이 애플리케이션을 구축할 수 있다고 판단하고, 주요 이해관계자들이 검토할 간단한 개념 검증(POC)을 설계하기 시작합니다.
먼저, 학생 메시지에 대한 응답을 메시지 기록에 저장하는 방법을 시연합니다. 이를 통해 완전한 대화 흐름을 구현할 수 있습니다.
이 연습은 강의의 일부입니다
Python으로 DeepSeek 활용하기
연습 안내
messages를 채팅 요청으로 모델에 전송하세요.response에서 어시스턴트 메시지를 추출하고, 메시지 딕셔너리로 변환한 뒤messages에 추가하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
client = OpenAI(api_key="", base_url="https://api.together.xyz/v1")
messages = [
{"role": "system", "content": "You are a helpful math tutor that generates concise, one-sentence responses."},
{"role": "user", "content": "Explain what pi is."}
]
# Send the chat messages to the model
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V4-Pro",
messages=____,
max_tokens=500
)
# Extract the assistant message from the response
assistant_dict = {"role": "____", "content": ____}
# Add assistant_dict to the messages dictionary
messages.____(assistant_dict)
print(messages)