Chat 엔드포인트로 텍스트 요약하기

Chat 엔드포인트로 텍스트 요약하기

길이 제어와 RAG 같은 기능을 사용해 Cohere의 Chat 엔드포인트로 텍스트 요약을 수행하는 방법을 배우는 문서예요.

출처: 문서

본문

텍스트 요약은 필수 정보를 추려내고 밀도 있는 문서에서 간결한 스니펫을 생성해요. Cohere를 사용하면 Chat 엔드포인트를 통해 텍스트 요약을 수행할 수 있습니다.

Command R 모델 패밀리(R 및 R+)는 128k 컨텍스트 길이를 지원하므로, 요약할 긴 문서를 전달할 수 있어요.

기본 요약(Basic summarization)

모델에게 텍스트 조각을 요약하라고 요청하는 간단한 프롬프트로 텍스트 요약을 수행할 수 있어요.

PYTHON

import cohere

co = cohere.ClientV2(api_key="<YOUR API KEY>")

document = """Equipment rental in North America is predicted to "normalize" going into 2024,
according to Josh Nickell, vice president of equipment rental for the American Rental
Association (ARA).

"Rental is going back to 'normal,' but normal means that strategy matters again -
geography matters, fleet mix matters, customer type matters," Nickell said. "In
late 2020 to 2022, you just showed up with equipment and you made money.

"Everybody was breaking records, from the national rental chains to the smallest
rental companies; everybody was having record years, and everybody was raising
prices. The conversation was, 'How much are you up?' And now, the conversation
is changing to 'What's my market like?'"
Nickell stressed this shouldn't be taken as a pessimistic viewpoint. It's simply
coming back down to Earth from unprecedented circumstances during the time of Covid.
Rental companies are still seeing growth, but at a more moderate level."""

message = f"Generate a concise summary of this text\n{document}"

response = co.chat(
    model="command-a-plus-05-2026",
    messages=[{"role": "user", "content": message}],
)


print(response.message.content[0].text)

cURL

curl --request POST \
  --url https://api.cohere.ai/v2/chat \
  --header 'accept: application/json' \
  --header 'content-type: application/json' \
  --header "Authorization: bearer ***" \
  --data '{
  "model": "command-a-plus-05-2026",
  "messages": [
    {
      "role": "user",
      "content": "Generate a concise summary of this text\n\nEquipment rental in North America is predicted to \"normalize\" going into 2024, according to Josh Nickell, vice president of equipment rental for the American Rental Association (ARA).\n\"Rental is going back to '\''normal,'\'' but normal means that strategy matters again - geography matters, fleet mix matters, customer type matters,\" Nickell said. \"In late 2020 to 2022, you just showed up with equipment and you made money.\n\"Everybody was breaking records, from the national rental chains to the smallest rental companies; everybody was having record years, and everybody was raising prices. The conversation was, '\''How much are you up?'\'' And now, the conversation is changing to '\''What'\''s my market like?'\''\"\nNickell stressed this shouldn'\''t be taken as a pessimistic viewpoint. It'\''s simply coming back down to Earth from unprecedented circumstances during the time of Covid. Rental companies are still seeing growth, but at a more moderate level."
    }
  ]
}'

(주의: 여기서는 문서를 변수로 전달하고 있지만, 문서를 메시지에 직접 복사하고 Chat에 요약을 요청할 수도 있어요.)

다음은 샘플 출력입니다:

The equipment rental market in North America is expected to normalize by 2024,
according to Josh Nickell of the American Rental Association. This means a shift
from the unprecedented growth of 2020-2022, where demand and prices were high,
to a more strategic approach focusing on geography, fleet mix, and customer type.
Rental companies are still experiencing growth, but at a more moderate and sustainable level.

길이 제어(Length control)

프롬프트에서 요약의 길이를 정의해 출력을 더 제어할 수 있어요. 예를 들어 생성할 문장 수를 지정할 수 있습니다.

PYTHON

message = f"Summarize this text in one sentence\n{document}"

response = co.chat(
    model="command-a-plus-05-2026",
    messages=[{"role": "user", "content": message}],
)

print(response.message.content[0].text)

그리고 출력의 샘플은 이런 모습일 수 있어요:

The equipment rental market in North America is expected to stabilize in 2024,
with a focus on strategic considerations such as geography, fleet mix, and
customer type, according to Josh Nickell of the American Rental Association (ARA).

단어 수 기준으로 길이를 지정할 수도 있어요.

PYTHON

message = f"Summarize this text in less than 10 words\n{document}"

response = co.chat(
    model="command-a-plus-05-2026",
    messages=[{"role": "user", "content": message}],
)

print(response.message.content[0].text)
Rental equipment supply and demand to balance.

(참고: 모델은 일반적으로 길이 지시를 잘 따르지만, LLM의 특성상 정확한 단어, 문장, 문단 수가 생성될 것이라고 보장하지는 않아요.)

형식 제어(Format control)

요약을 문단으로 생성하는 대신, 불릿 포인트로 요약을 생성하도록 프롬프트할 수도 있어요.

PYTHON

message = f"Generate a concise summary of this text as bullet points\n{document}"

response = co.chat(
    model="command-a-plus-05-2026",
    messages=[{"role": "user", "content": message}],
)

print(response.message.content[0].text)
- Equipment rental in North America is expected to "normalize" by 2024, according to Josh Nickell
  of the American Rental Association (ARA).
- This "normalization" means a return to strategic focus on factors like geography, fleet mix,
  and customer type.
- In the past two years, rental companies easily made money and saw record growth due to the
  unique circumstances of the Covid pandemic.
- Now, the focus is shifting from universal success to varying market conditions and performance.
- Nickell's outlook is not pessimistic; rental companies are still growing, but at a more 
  sustainable and moderate pace.

근거 기반 요약(Grounded summarization)

요약의 또 다른 접근 방식은 검색 증강 생성(retrieval-augmented generation)(RAG)을 사용하는 것이에요. 여기서는 문서를 청크(chunk)로 나눠 Chat 엔드포인트 호출에 전달할 수 있습니다.

이 접근 방식을 사용하면 엔드포인트가 생성하는 인용을 활용할 수 있어서, 문서에 근거한(grounded) 요약을 얻을 수 있어요. 각 근거 기반 요약은 원본 문서를 연결하는 세밀한 인용을 포함해, 응답을 쉽게 검증할 수 있게 하고 사용자와의 신뢰를 쌓아줍니다.

다음은 문서의 청크 버전이에요. (여기서 청킹(chunking) 과정은 다루지 않지만, 더 알고 싶다면 청킹 전략(chunking strategies)에 대한 이 cookbook을 확인해 보세요.)

PYTHON

document_chunked = [
    {
        "data": {
            "text": "Equipment rental in North America is predicted to “normalize” going into 2024, according to Josh Nickell, vice president of equipment rental for the American Rental Association (ARA)."
        }
    },
    {
        "data": {
            "text": "“Rental is going back to ‘normal,’ but normal means that strategy matters again - geography matters, fleet mix matters, customer type matters,” Nickell said. “In late 2020 to 2022, you just showed up with equipment and you made money."
        }
    },
    {
        "data": {
            "text": "“Everybody was breaking records, from the national rental chains to the smallest rental companies; everybody was having record years, and everybody was raising prices. The conversation was, ‘How much are you up?’ And now, the conversation is changing to ‘What’s my market like?’”"
        }
    },
]

또한 사용자 지정 시스템 메시지를 만들어 모델에게 작업(문서의 시간순으로 제시된 일련의 텍스트 조각을 받게 될 것임)을 미리 알려주는 것도 도움이 됩니다.

PYTHON

system_message = """## Task and Context
You will receive a series of text fragments from a document that are presented in chronological order. As the assistant, you must generate responses to user's requests based on the information given in the fragments. Ensure that your responses are accurate and truthful, and that you reference your sources where appropriate to answer the queries, regardless of their complexity."""

사용자 지정 시스템 메시지 외에, Chat 엔드포인트 호출의 유일한 변경점은 문서 청크 목록을 담은 document 파라미터를 전달하는 것이에요.

실제 요약을 표시하는 것 외에도 인용도 함께 표시할 수 있어요. 인용은 모델이 받은 문서에서 인용하는 응답의 특정 구절 목록입니다.

PYTHON

message = f"Summarize this text in one sentence."

response = co.chat(
    model="command-a-plus-05-2026",
    documents=document_chunked,
    messages=[
        {"role": "system", "content": system_message},
        {"role": "user", "content": message},
    ],
)

print(response.message.content[0].text)

if response.message.citations:
    print("\nCITATIONS:")
    for citation in response.message.citations:
        print(
            f"Start: {citation.start} | End: {citation.end} | Text: '{citation.text}'",
            end="",
        )
        if citation.sources:
            for source in citation.sources:
                print(f"| {source.id}")

cURL

curl --request POST \
  --url https://api.cohere.ai/v2/chat \
  --header 'accept: application/json' \
  --header 'content-type: application/json' \
  --header "Authorization: bearer ***" \
  --data '{
  "model": "command-a-plus-05-2026",
  "messages": [
    {
      "role": "system",
      "content": "## Task and Context\nYou will receive a series of text fragments from a document that are presented in chronological order. As the assistant, you must generate responses to user'\''s requests based on the information given in the fragments. Ensure that your responses are accurate and truthful, and that you reference your sources where appropriate to answer the queries, regardless of their complexity."
    },
    {
      "role": "user",
      "content": "Summarize this text in one sentence."
    }
  ],
  "documents": [
    {
      "data": {
        "text": "Equipment rental in North America is predicted to \"normalize\" going into 2024, according to Josh Nickell, vice president of equipment rental for the American Rental Association (ARA)."
      }
    },
    {
      "data": {
        "text": "\"Rental is going back to '\''normal,'\'' but normal means that strategy matters again - geography matters, fleet mix matters, customer type matters,\" Nickell said. \"In late 2020 to 2022, you just showed up with equipment and you made money.\""
      }
    },
    {
      "data": {
        "text": "\"Everybody was breaking records, from the national rental chains to the smallest rental companies; everybody was having record years, and everybody was raising prices. The conversation was, '\''How much are you up?'\'' And now, the conversation is changing to '\''What'\''s my market like?'\''\""
      }
    }
  ]
}'
Josh Nickell, vice president of the American Rental Association, predicts that equipment rental in North America will "normalize" in 2024, requiring companies to focus on strategy, geography, fleet mix, and customer type.

CITATIONS:
Start: 0 | End: 12 | Text: 'Josh Nickell'| doc:1:0
Start: 14 | End: 63 | Text: 'vice president of the American Rental Association'| doc:1:0
Start: 79 | End: 112 | Text: 'equipment rental in North America'| doc:1:0
Start: 118 | End: 129 | Text: '"normalize"'| doc:1:0
| doc:1:1
Start: 133 | End: 137 | Text: '2024'| doc:1:0
Start: 162 | End: 221 | Text: 'focus on strategy, geography, fleet mix, and customer type.'| doc:1:1
| doc:1:2

Summarize 엔드포인트에서 Chat 엔드포인트로 마이그레이션

요약에 Command R/R+ 모델을 사용하려면 Chat 엔드포인트를 사용하는 것을 권장해요. 이 가이드는 Summarize 엔드포인트에서 Chat 엔드포인트로 마이그레이션하는 방법을 설명합니다.

PYTHON

# Before

co.summarize(
    format="bullets",
    length="short",
    extractiveness="low",
    text="""Equipment rental in North America is predicted to “normalize” going into 2024, according
  to Josh Nickell, vice president of equipment rental for the American Rental Association (ARA).
  “Rental is going back to ‘normal,’ but normal means that strategy matters again - geography
  matters, fleet mix matters, customer type matters,” Nickell said. “In late 2020 to 2022, you
  just showed up with equipment and you made money.
  “Everybody was breaking records, from the national rental chains to the smallest rental companies;
  everybody was having record years, and everybody was raising prices. The conversation was, ‘How
  much are you up?’ And now, the conversation is changing to ‘What’s my market like?’”
  Nickell stressed this shouldn’t be taken as a pessimistic viewpoint. It’s simply coming back
  down to Earth from unprecedented circumstances during the time of Covid. Rental companies are
  still seeing growth, but at a more moderate level.
  """,
)

# After
message = """Write a short summary from the following text in bullet point format, in different words.
  
  Equipment rental in North America is predicted to “normalize” going into 2024, according to Josh
  Nickell, vice president of equipment rental for the American Rental Association (ARA).
  “Rental is going back to ‘normal,’ but normal means that strategy matters again - geography
  matters, fleet mix matters, customer type matters,” Nickell said. “In late 2020 to 2022, you just
  showed up with equipment and you made money.
  “Everybody was breaking records, from the national rental chains to the smallest rental companies;
  everybody was having record years, and everybody was raising prices. The conversation was,
  ‘How much are you up?’ And now, the conversation is changing to ‘What’s my market like?’”
  Nickell stressed this shouldn’t be taken as a pessimistic viewpoint. It’s simply coming back
  down to Earth from unprecedented circumstances during the time of Covid. Rental companies are
  still seeing growth, but at a more moderate level.

"""

co.chat(
    messages=[{"role": "user", "content": message}],
    model="command-a-plus-05-2026",
)

더 알아보기 (Learn more)