[Claude AI agent] Working with the API

DH.J·2025년 1월 28일

Claude로 ai agent만들기

API key 발급받기

pip install anthropic

api key는 여기서 발급을 받을 수 있습니다.
(https://console.anthropic.com/settings/billing)

api_key="<api_key>"에 copy & paste.

import anthropic

client = anthropic.Anthropic(
    api_key="<api_key>",
)
response = client.messages.create(
    model=MODEL_NAME,
    max_tokens=1000,
    messages=[
        {"role": "user", "content": "Hello, Claude"}
    ]
)
print(response.content[0].text) # Hi! How can I help you today?

Token이란?

AI 모델이 처리할 수 있는 단위입니다.
1개의 token은 일반적으로 2-4개 문자로 구성됩니다.

  • hello -> 1개의 토큰
  • understanding -> 2-3개의 토큰

max_tokens은 모델이 생성할 수 있는 최대 토큰 수를 지정합니다.
max_tokens=1000 -> 한 번에 1000개의 토큰 응답 생성

만약 이 제한을 초과한다면 자동으로 중단됩니다.

그런데 왜 제한할까요?
유저들이 이용할 수 있는 API call을 제한하지 않으면 서버 비용을 도저히 감당할 수 없을 것입니다.

response 객체

anthropic api로 받은 응답을 담는다.
content[0].text로 접근한다.

Message(id='msg_01RMJewE8WagpMTALNjbUDhi', 
content=[TextBlock(citations=None, 
text='Hi! How can I help you today?', type='text')], 
model='claude-3-5-sonnet-20241022', # 사용된 모델 이름
role='assistant', 
stop_reason='end_turn', # 응답 생성 중단 이유
stop_sequence=None, # 응답 생성 중단에 사용된 시퀀스
type='message', 
usage=Usage(cache_creation_input_tokens=0, cache_read_input_tokens=0, input_tokens=10, output_tokens=12))

만약 max_token을 초과한다면 stop_reason은 end_turn 대신 max_token으로 바뀝니다.

Assistant 만들기

user가 말한 내용을 assistant가 스페인어로 대답하는 챗봇을 만들어 봅시다.
이제 messages=[] 리스트의 내용을 바꿔봅시다.

    messages=[
        {"role": "user", "content": "Hello, only speak to me in Spanish"},
        {"role": "assistant", "content": "Hola!"},
        {"role": "user", "content": "How are you?"},
    ]

실행해보면?

¡Estoy muy bien, gracias! ¿Y tú? ¿Cómo estás?

(이 문장을 영어로 해석해보면 I'm very well, thanks! And you? How are you? 이라는 뜻)

이제 대화를 해볼 수 있는 챗봇을 만들어 봅시다.

print("Simple Chatbot (type 'quit' to exit)")

messages = []

while True:
  # Get user input
  user_input = input("You: ")

  # Exit command
  if user_input.lower() == "quit":
    print("Bye")
    break

  # Add user message to history
  messages.append({"role": "user", "content": user_input})
  try:
    # Get response from Claude
    response = client.messages.create(
      model=MODEL_NAME,
      max_tokens=100,
      messages=messages
    )

    assistant_response = response.content[0].text
    print(f"Assistant: {assistant_response}")

    # Add assistant message to history
    messages.append({"role": "assistant", "content": assistant_response})

  except Exception as e:
    print(f"An error occurred: {e}")

Prompt 입력하기

user가 입력한 모든 말을 스페인어로 바꿔주는 프롬프트를 message 리스트 안에 추가해봅시다.

prompt = """
Translate my all inputs into Spanish.
"""

messages = [
    {"role": "user", "content": prompt}
]

이제 실행해보면

You: hello
Assistant: ¡Hola!
You: how are you?
Assistant: ¿Cómo estás?

성공적으로 응답을 받아볼 수 있습니다.

    response = client.messages.create(
      model=MODEL_NAME,
      max_tokens=300,
      stop_sequences=["3."], # 3. 으로 시작하는 값 생성을 중단함.
      messages=messages
    )

stop_sequences=[]를 설정하면 생성하는 것을 중단합니다.
response를 보면 stop_reason을 통해 중단된 이유를 알 수 있습니다.
stop_reason='stop_sequence', stop_sequence='3.'

Temperature

AI 모델의 출력값의 특징을 결정하는 hyperparameter로, 다음 토큰 예측에 대한 확률 분포(probability distribution)입니다.
Temperature setting은 모델의 예측이 얼마나 confident 한지, risky한지 영향을 줍니다.
창의성과 정확성 정도를 0과 1 사이의 숫자로 설정할 수 있다.

  • Temperature = 0 : 일관성있는 응답, 수학, 코딩 문제에 적합함.
  • Temperature = 0.5 ~ 0.7 : 적절한 균형
  • Temperature = 1 : 창의적이고 예측 불가능한 응답, 브레인 스토밍에 적합

EX) 아이스크림 가게에서 "초콜릿 아이스크림 주세요" 라고 주문하면,

  • Temperature = 0: "벨기에 다크 초콜릿 아이스크림 드립니다" (매번 동일한 메뉴)
  • Temperature = 1: "오늘은 초코와 바나나를 섞은 아이스크림을 콘에 담아 토핑과 함께 드립니다!" (창의적이고 모험적인 메뉴)

자 이제 모델을 구현해봅시다.

def demonstrate_temperature():
  temperatures = [0, 0.5, 1] # Setting temperatures

  for temperature in temperatures:
    print(f"Prompting Claude three times with temperature {temperature}")
    print("=================")

    for i in range(3):
      response = client.messages.create(
        model=MODEL_NAME,
        max_tokens=300,
        messages=[{"role": "user", "content": f"Prompt {i + 1}: Come up with a name for chocolate cream cake! Respond with a single word."}],
        temperature=temperature
      )

      print(f"Response {i+1}: {response.content[0].text}")

Reference

building toward computer use with anthropic
Understanding temperature in ai

profile
평생 질문하며 살고 싶습니다.

0개의 댓글