Amazon Bedrock에서 Cohere 모델 사용하기
Amazon Bedrock에서 Cohere 모델 사용하기
Amazon Bedrock에서 Cohere의 모델을 사용하는 방법에 대한 가이드를 알아볼 거예요.
이 가이드에서는 Amazon Bedrock을 사용해 AWS 클라우드 컴퓨팅 플랫폼에 Cohere의 Command, Embed, Rerank 모델을 배포하고 활용하는 방법을 배울 수 있어요. Bedrock에서 사용할 수 있는 모델은 다음과 같아요.
- Command R
- Command R+
- Command Light
- Command
- Embed - English
- Embed - Multilingual
- Rerank v3.5
아래 코드 스니펫은 Python이지만, 다른 언어(지원된다면)에 대한 동등한 코드도 찾을 수 있어요.
출처: 문서
사전 요구 사항 (Prerequisites)
Amazon Bedrock에서 Cohere 모델을 실행하기 전에 미리 준비해야 할 단계는 다음과 같아요.
- Amazon Bedrock에서 Cohere의 모델을 구독하세요. 자세한 내용은 Amazon 문서를 참조하세요.
- AWS용 인증 자격 증명도 구성해야 해요. 자세한 내용은 Amazon 문서를 참조하세요.
임베딩 (Embeddings)
이 코드를 사용해 Amazon Bedrock에서 Cohere의 Embed English v3 모델(cohere.embed-english-v3) 또는 Embed Multilingual v3 모델(cohere.embed-multilingual-v3)을 호출할 수 있어요.
PYTHON
import cohere
co = cohere.BedrockClient(
aws_region="us-east-1",
aws_access_key="...",
aws_secret_key="...",
aws_session_token="...",
)
# Input parameters for embed. In this example we are embedding hacker news post titles.
texts = [
"Interesting (Non software) books?",
"Non-tech books that have helped you grow professionally?",
"I sold my company last month for $5m. What do I do with the money?",
"How are you getting through (and back from) burning out?",
"I made $24k over the last month. Now what?",
"What kind of personal financial investment do you do?",
"Should I quit the field of software development?",
]
input_type = "clustering"
truncate = "NONE" # optional
model_id = (
"cohere.embed-english-v3" # or "cohere.embed-multilingual-v3"
)
# Invoke the model and print the response
result = co.embed(
model=model_id,
input_type=input_type,
texts=texts,
truncate=truncate,
) # aws_client.invoke_model(**params)
print(result)
텍스트 외에 이미지도 처리할 수 있는 멀티모달 임베딩 모델을 출시했다는 점에 유의해 주세요.
텍스트 생성 (Text Generation)
이 코드를 사용해 Amazon Bedrock에서 Command R (cohere.command-r-v1:0) 또는 Command R+ (cohere.command-r-plus-v1:0)를 호출할 수 있어요.
PYTHON
import cohere
co = cohere.BedrockClient(
aws_region="us-east-1",
aws_access_key="...",
aws_secret_key="...",
aws_session_token="...",
)
result = co.chat(
message="Write a LinkedIn post about starting a career in tech:",
model="cohere.command-r-plus-v1:0", # or 'cohere.command-r-v1:0'
)
print(result)
Rerank
이 코드를 사용해 Bedrock에서 최신 Rerank 모델을 호출할 수 있어요.
PYTHON
import cohere
co = cohere.BedrockClientV2(
aws_region="us-west-2", # pick a region where the model is available
aws_access_key="...",
aws_secret_key="...",
aws_session_token="...",
)
docs = [
"Carson City is the capital city of the American state of Nevada.",
"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.",
"Capitalization or capitalisation in English grammar is the use of a capital letter at the start of a word. English usage varies from capitalization in other languages.",
"Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.",
"Capital punishment has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states.",
]
response = co.rerank(
model="cohere.rerank-v3-5:0",
query="What is the capital of the United States?",
documents=docs,
top_n=3,
)
print(response)