의미 기반 검색(Semantic search) - 퀵스타트
의미 기반 검색(Semantic search) - 퀵스타트
Cohere의 Embed 모델(v2 API)로 텍스트 의미 기반 검색을 수행하는 퀵스타트 가이드예요.
출처: 문서
본문
의미 기반 검색이란
Cohere의 임베딩 모델은 Embed 엔드포인트를 통해 사용할 수 있어요. 이 엔드포인트를 사용하면 텍스트 문서(다국어)와 이미지를 벡터 공간으로 임베딩할 수 있죠.
임베딩으로 구동되는 의미 기반 검색은 애플리케이션이 문서의 맥락이나 의미에 기반해서 정보 검색을 수행할 수 있게 해 줘요.
이 퀵스타트 가이드는 Embed 엔드포인트로 의미 기반 검색을 수행하는 방법을 보여드려요.
설정
먼저 다음 명령으로 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-embed-v3-multilingual-xyz.eastus.models.ai.azure.com/"
)
문서 임베딩(Document Embeddings)
먼저 Embed 엔드포인트에 input_type을 search_document로 지정해서 사용 가능한 문서 목록을 임베딩해요.
Cohere Platform
PYTHON
# Define the documents
documents = [
"Joining Slack Channels: Be sure to join relevant channels to stay informed and engaged.",
"Finding Coffee Spots: For your caffeine fix, cross the street to the café for artisan coffee.",
"Working Hours Flexibility: While our core hours are 9 AM to 5 PM, we offer flexibility to adjust as needed.",
]
# Embed the documents
doc_emb = co.embed(
model="embed-v4.0",
input_type="search_document",
texts=documents,
embedding_types=["float"],
).embeddings.float
Private Deployment
PYTHON
# Define the documents
documents = [
"Joining Slack Channels: Be sure to join relevant channels to stay informed and engaged.",
"Finding Coffee Spots: For your caffeine fix, cross the street to the café for artisan coffee.",
"Working Hours Flexibility: While our core hours are 9 AM to 5 PM, we offer flexibility to adjust as needed.",
]
# Embed the documents
doc_emb = co.embed(
model="embed-v4.0",
input_type="search_document",
texts=documents,
embedding_types=["float"],
).embeddings.float
Bedrock
PYTHON
# Define the documents
documents = [
"Joining Slack Channels: Be sure to join relevant channels to stay informed and engaged.",
"Finding Coffee Spots: For your caffeine fix, cross the street to the café for artisan coffee.",
"Working Hours Flexibility: While our core hours are 9 AM to 5 PM, we offer flexibility to adjust as needed.",
]
# Embed the documents
doc_emb = co.embed(
model="YOUR_MODEL_NAME",
input_type="search_document",
texts=documents,
embedding_types=["float"],
).embeddings.float
SageMaker
PYTHON
# Define the documents
documents = [
"Joining Slack Channels: Be sure to join relevant channels to stay informed and engaged.",
"Finding Coffee Spots: For your caffeine fix, cross the street to the café for artisan coffee.",
"Working Hours Flexibility: While our core hours are 9 AM to 5 PM, we offer flexibility to adjust as needed.",
]
# Embed the documents
doc_emb = co.embed(
model="YOUR_ENDPOINT_NAME",
input_type="search_document",
texts=documents,
embedding_types=["float"],
).embeddings.float
Azure AI
PYTHON
# Define the documents
documents = [
"Joining Slack Channels: Be sure to join relevant channels to stay informed and engaged.",
"Finding Coffee Spots: For your caffeine fix, cross the street to the café for artisan coffee.",
"Working Hours Flexibility: While our core hours are 9 AM to 5 PM, we offer flexibility to adjust as needed.",
]
# Embed the documents
doc_emb = co.embed(
input_type="search_document",
texts=documents,
embedding_types=["float"],
).embeddings.float
쿼리 임베딩(Query Embedding)
다음으로, Embed 엔드포인트에 input_type을 search_query로 지정해서 사용자 쿼리를 임베딩해요.
Cohere Platform
PYTHON
# Add the user query
query = "Ways to connect with my teammates"
# Embed the query
query_emb = co.embed(
model="embed-v4.0",
input_type="search_query",
texts=[query],
embedding_types=["float"],
).embeddings.float
Private Deployment
PYTHON
# Add the user query
query = "Ways to connect with my teammates"
# Embed the query
query_emb = co.embed(
model="embed-v4.0",
input_type="search_query",
texts=[query],
embedding_types=["float"],
).embeddings.float
Bedrock
PYTHON
# Add the user query
query = "Ways to connect with my teammates"
# Embed the query
query_emb = co.embed(
model="YOUR_MODEL_NAME",
input_type="search_query",
texts=[query],
embedding_types=["float"],
).embeddings.float
SageMaker
PYTHON
# Add the user query
query = "Ways to connect with my teammates"
query_emb = co.embed(
model="embed-v4.0",
input_type="search_query",
texts=[query],
embedding_types=["float"],
).embeddings.float
Azure AI
PYTHON
# Add the user query
query = "Ways to connect with my teammates"
query_emb = co.embed(
model="embed-v4.0",
input_type="search_query",
texts=[query],
embedding_types=["float"],
).embeddings.float
의미 기반 검색 수행
그런 다음, 쿼리 임베딩과 문서 임베딩 사이의 유사도를 계산하고 가장 유사한 문서를 반환해서 의미 기반 검색을 수행해요.
PYTHON
import numpy as np
# Compute dot product similarity and display results
def return_results(query_emb, doc_emb, documents):
n = 2 # customize your top N results
scores = np.dot(query_emb, np.transpose(doc_emb))[0]
max_idx = np.argsort(-scores)[:n]
for rank, idx in enumerate(max_idx):
print(f"Rank: {rank+1}")
print(f"Score: {scores[idx]}")
print(f"Document: {documents[idx]}\n")
return_results(query_emb, doc_emb, documents)
Rank: 1
Score: 0.262197161387274
Document: Joining Slack Channels: Be sure to join relevant channels to stay informed and engaged.
Rank: 2
Score: 0.1266074257723145
Document: Working Hours Flexibility: While our core hours are 9 AM to 5 PM, we offer flexibility to adjust as needed.