텍스트 생성 - 퀵스타트

텍스트 생성 - 퀵스타트

Cohere의 Command 모델(v2 API)로 텍스트 생성을 수행하는 퀵스타트 가이드예요.

출처: 문서

본문

텍스트 생성이란

Cohere의 Command 계열 LLM은 Chat 엔드포인트를 통해 사용할 수 있어요. 이 엔드포인트를 사용하면 생성형 AI 애플리케이션을 만들 수 있고, 챗봇을 구축하기 위한 대화형 인터페이스를 제공해 줘요.

이 퀵스타트 가이드는 Chat 엔드포인트로 텍스트 생성을 수행하는 방법을 보여드려요.

설정

먼저 다음 명령으로 Cohere Python SDK를 설치해요.

pip install -U cohere

다음으로 라이브러리를 import하고 클라이언트를 만들어요.

Cohere Platform

PYTHON

import cohere

co = cohere.ClientV2(
    "COHERE_API_KEY"
)  # Get your free API key here: https://dashboard.cohere.com/api-keys

Private Deployment

PYTHON

import cohere

co = cohere.ClientV2(
    api_key="",  # Leave this blank
    base_url="<YOUR_DEPLOYMENT_URL>",
)

Bedrock

PYTHON

import cohere

co = cohere.BedrockClientV2(
    aws_region="AWS_REGION",
    aws_access_key="AWS_ACCESS_KEY_ID",
    aws_secret_key="AWS_SECRET_ACCESS_KEY",
    aws_session_token="AWS_SESSION_TOKEN",
)

# Get the model name: https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html

SageMaker

PYTHON

import cohere

co = cohere.SagemakerClientV2(
    aws_region="AWS_REGION",
    aws_access_key="AWS_ACCESS_KEY_ID",
    aws_secret_key="AWS_SECRET_ACCESS_KEY",
    aws_session_token="AWS_SESSION_TOKEN",
)

Azure AI

PYTHON

import cohere

co = cohere.ClientV2(
    api_key="AZURE_API_KEY",
    base_url="AZURE_ENDPOINT",  # example: "https://cohere-command-r-plus-08-2024-xyz.eastus.models.ai.azure.com/"
)

기본 텍스트 생성

기본 텍스트 생성을 수행하려면, user 메시지를 담은 messages 파라미터를 전달해서 Chat 엔드포인트를 호출해요.

Command A+ 같은 reasoning 모델에서는 응답 content 목록에 최종 text 블록 앞에 thinking 블록이 포함될 수 있어요. content[0]이 텍스트라고 가정하지 말고, content 항목들을 순회하면서 각 항목의 type을 확인하는 게 좋아요. 자세한 내용은 Reasoning 페이지를 참고하세요.

Info

프라이빗 배포에서의 model 파라미터 정의는 아래처럼 Cohere 플랫폼과 동일해요. 프라이빗 배포 사용에 대한 자세한 내용은 여기에서 확인할 수 있어요.

Cohere Platform

PYTHON

response = co.chat(
    model="command-a-plus-05-2026",
    messages=[
        {
            "role": "user",
            "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
        }
    ],
)

for content_item in response.message.content:
    if content_item.type == "thinking":
        print("thinking:", content_item.thinking)
    if content_item.type == "text":
        print("text:", content_item.text)

Private Deployment

PYTHON

response = co.chat(
    model="command-a-plus-05-2026",
    messages=[
        {
            "role": "user",
            "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
        }
    ],
)

for content_item in response.message.content:
    if content_item.type == "thinking":
        print("thinking:", content_item.thinking)
    if content_item.type == "text":
        print("text:", content_item.text)

Bedrock

PYTHON

response = co.chat(
    model="YOUR_MODEL_NAME",
    messages=[
        {
            "role": "user",
            "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
        }
    ],
)

for content_item in response.message.content:
    if content_item.type == "thinking":
        print("thinking:", content_item.thinking)
    if content_item.type == "text":
        print("text:", content_item.text)

SageMaker

PYTHON

response = co.chat(
    model="YOUR_ENDPOINT_NAME",
    messages=[
        {
            "role": "user",
            "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
        }
    ],
)

for content_item in response.message.content:
    if content_item.type == "thinking":
        print("thinking:", content_item.thinking)
    if content_item.type == "text":
        print("text:", content_item.text)

Azure AI

PYTHON

response = co.chat(
    model="model",  # Pass a dummy string
    messages=[
        {
            "role": "user",
            "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
        }
    ],
)

for content_item in response.message.content:
    if content_item.type == "thinking":
        print("thinking:", content_item.thinking)
    if content_item.type == "text":
        print("text:", content_item.text)

"Excited to be part of the Co1t team, I'm [Your Name], a [Your Role], passionate about [Your Area of Expertise] and looking forward to contributing to the company's success."

상태 관리(State Management)

챗봇을 만들 때처럼 대화의 상태를 유지하려면, user와 assistant 메시지의 연속을 messages 목록에 추가하면 돼요. 또한 목록의 시작 부분에 system 메시지를 넣어 대화의 컨텍스트를 설정할 수도 있어요.

Cohere Platform

PYTHON

messages = [
    {
        "role": "system",
        "content": "You respond in concise sentences.",
    },
    {"role": "user", "content": "Hello"},
]

# User sends a message

response = co.chat(
    model="command-a-plus-05-2026",
    messages=messages,
)

# The model responds

for content_item in response.message.content:
    if content_item.type == "thinking":
        print("thinking:", content_item.thinking)
    if content_item.type == "text":
        print(
            "text:", content_item.text
        )  # Hi, how can I help you today?

# Append the model's response to the messages

messages.append(response.message)

# append another user message to the messages

messages.append(
    {
        "role": "user",
        "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
    }
)

# get the model's second response

response = co.chat(
    model="command-a-plus-05-2026",
    messages=messages,
)

for content_item in response.message.content:
    if content_item.type == "thinking":
        print("thinking:", content_item.thinking)
    if content_item.type == "text":
        print("text:", content_item.text)

Private Deployment

PYTHON

messages = [
    {
        "role": "system",
        "content": "You respond in concise sentences.",
    },
    {"role": "user", "content": "Hello"},
]

# User sends a message
response = co.chat(
    model="command-a-plus-05-2026",
    messages=messages,
)

# The model responds
for content_item in response.message.content:
    if content_item.type == "thinking":
        print("thinking:", content_item.thinking)
    if content_item.type == "text":
        print(
            "text:", content_item.text
        )  # Hi, how can I help you today?

# Append the model's response to the messages
messages.append(response.message)

# append another user message to the messages
messages.append(
    {
        "role": "user",
        "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
    }
)

# get the model's second response
response = co.chat(
    model="command-a-plus-05-2026",
    messages=messages,
)

for content_item in response.message.content:
    if content_item.type == "thinking":
        print("thinking:", content_item.thinking)
    if content_item.type == "text":
        print("text:", content_item.text)

Bedrock

PYTHON

messages = [
    {
        "role": "system",
        "content": "You respond in concise sentences.",
    },
    {"role": "user", "content": "Hello"},
]

# User sends a message

response = co.chat(
    model="YOUR_MODEL_NAME",
    messages=messages,
)

# The model responds

for content_item in response.message.content:
    if content_item.type == "thinking":
        print("thinking:", content_item.thinking)
    if content_item.type == "text":
        print(
            "text:", content_item.text
        )  # Hi, how can I help you today?

# Append the model's response to the messages

messages.append(response.message)

# append another user message to the messages

messages.append(
    {
        "role": "user",
        "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
    }
)

# get the model's second response

response = co.chat(
    model="YOUR_MODEL_NAME",
    messages=messages,
)

for content_item in response.message.content:
    if content_item.type == "thinking":
        print("thinking:", content_item.thinking)
    if content_item.type == "text":
        print("text:", content_item.text)

SageMaker

PYTHON

messages = [
    {
        "role": "system",
        "content": "You respond in concise sentences.",
    },
    {"role": "user", "content": "Hello"},
]

# User sends a message
response = co.chat(
    model="YOUR_ENDPOINT_NAME",
    messages=messages,
)

# The model responds
for content_item in response.message.content:
    if content_item.type == "thinking":
        print("thinking:", content_item.thinking)
    if content_item.type == "text":
        print(
            "text:", content_item.text
        )  # Hi, how can I help you today?

# Append the model's response to the messages
messages.append(response.message)

# append another user message to the messages
messages.append(
    {
        "role": "user",
        "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
    }
)

# get the model's second response
response = co.chat(
    model="YOUR_ENDPOINT_NAME",
    messages=messages,
)

for content_item in response.message.content:
    if content_item.type == "thinking":
        print("thinking:", content_item.thinking)
    if content_item.type == "text":
        print("text:", content_item.text)

Azure AI

PYTHON

messages = [
    {
        "role": "system",
        "content": "You respond in concise sentences.",
    },
    {"role": "user", "content": "Hello"},
]

# User sends a message

response = co.chat(
    model="model",  # Pass a dummy string
    messages=messages,
)

# The model responds

for content_item in response.message.content:
    if content_item.type == "thinking":
        print("thinking:", content_item.thinking)
    if content_item.type == "text":
        print(
            "text:", content_item.text
        )  # Hi, how can I help you today?

# Append the model's response to the messages

messages.append(response.message)

# append another user message to the messages

messages.append(
    {
        "role": "user",
        "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
    }
)

# get the model's second response

response = co.chat(
    model="model",  # Pass a dummy string
    messages=messages,
)

for content_item in response.message.content:
    if content_item.type == "thinking":
        print("thinking:", content_item.thinking)
    if content_item.type == "text":
        print("text:", content_item.text)

"Excited to join the team at Co1t, looking forward to contributing my skills and collaborating with everyone!"

스트리밍(Streaming)

텍스트 생성을 스트리밍하려면 chat 대신 chat_stream으로 Chat 엔드포인트를 호출해요. 그러면 chunk 객체를 생성하는 제너레이터를 반환하는데, 여기서 생성된 텍스트에 접근할 수 있어요.

Cohere Platform

PYTHON

res = co.chat_stream(
    model="command-a-plus-05-2026",
    messages=[
        {
            "role": "user",
            "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
        }
    ],
)

for chunk in res:
    if chunk.type == "content-delta":
        if chunk.delta.message.content.thinking:
            print(chunk.delta.message.content.thinking, end="")
        if chunk.delta.message.content.text:
            print(chunk.delta.message.content.text, end="")

Private Deployment

PYTHON

res = co.chat_stream(
    model="command-a-plus-05-2026",
    messages=[
        {
            "role": "user",
            "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
        }
    ],
)

for chunk in res:
    if chunk.type == "content-delta":
        if chunk.delta.message.content.thinking:
            print(chunk.delta.message.content.thinking, end="")
        if chunk.delta.message.content.text:
            print(chunk.delta.message.content.text, end="")

Bedrock

PYTHON

res = co.chat_stream(
    model="YOUR_MODEL_NAME",
    messages=[
        {
            "role": "user",
            "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
        }
    ],
)

for chunk in res:
    if chunk.type == "content-delta":
        if chunk.delta.message.content.thinking:
            print(chunk.delta.message.content.thinking, end="")
        if chunk.delta.message.content.text:
            print(chunk.delta.message.content.text, end="")

SageMaker

PYTHON

res = co.chat_stream(
    model="YOUR_ENDPOINT_NAME",
    messages=[
        {
            "role": "user",
            "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
        }
    ],
)

for chunk in res:
    if chunk.type == "content-delta":
        if chunk.delta.message.content.thinking:
            print(chunk.delta.message.content.thinking, end="")
        if chunk.delta.message.content.text:
            print(chunk.delta.message.content.text, end="")

Azure AI

PYTHON

res = co.chat_stream(
    model="model",  # Pass a dummy string
    messages=[
        {
            "role": "user",
            "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
        }
    ],
)

for chunk in res:
    if chunk.type == "content-delta":
        if chunk.delta.message.content.thinking:
            print(chunk.delta.message.content.thinking, end="")
        if chunk.delta.message.content.text:
            print(chunk.delta.message.content.text, end="")

"Excited to be part of the Co1t team, I'm [Your Name], a [Your Role/Position], looking forward to contributing my skills and collaborating with this talented group to drive innovation and success."

더 보기(Further Resources)

더 알아보기 (Learn more)