Cohere Rerank:
- Cohere 회사에서 제공하는 강력한 텍스트 재순위화(reranking) 서비스임.
- 초기 검색 결과나 추천 목록의 순위를 재조정하여 가장 관련성 높은 항목을 상위로 배치 시키는 기능을 함.
- LLM 을 이용해서 쿼리와 각 문서 간의 의미론적 관련성을 평가한 다음 가장 상위에 관련성이 있는 문서를 배치시키는 것.
- 단순한 키워드 매칭을 넘어 깊이 있는 문맥 이해를 제공한다.
Cohere Rerank 간단한 예시:
import cohere
# Get your cohere API key on: www.cohere.com
co = cohere.Client("{apiKey}")
# Example query and passages
query = "What is the capital of the United States?"
documents = [
"Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.",
"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.",
"Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.",
"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 (the death penalty) has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states.",
"North Dakota is a state in the United States. 672,591 people lived in North Dakota in the year 2010. The capital and seat of government is Bismarck."
]
results = co.rerank(query=query, documents=documents, top_n=3, model="rerank-multilingual-v2.0")
Cohere Rerank + Elasticserach 조합 예시:
- 현재 ES 에서는 검색 문서를 가져올 때 Cohere rerank 를 자동으로 적용해서 반환하지 않음. 개발자가 따로 요청해줘야한다.
- 다음과 같은 ES built-in API 를 사용하거나, Cohere API 를 따로 호출하거나 그래야 할 것.
- 이후에는 이 과정을 모두 거쳐서 검색 결과를 제공해주는 Retriever 를 ES 에 만든다고 하니까 참고하면 될 듯
- https://www.elastic.co/search-labs/blog/semantic-reranking-with-retrievers
from elasticsearch import Elasticsearch, helpers
import cohere
from datasets import load_dataset
# Get your cohere API key on: www.cohere.com
co = cohere.Client("{apiKey}")
# Connect to elastic
es = Elasticsearch("http://localhost:9200")
# If the ES index does not exist yet, load simple English Wikipedia dataset and index it
index = "wikipedia"
if not es.indices.exists(index=index):
print("Load dataset")
data = load_dataset(f"Cohere/wikipedia-22-12", "simple", split='train', streaming=True)
all_docs = map(lambda row : {"_index": index, "_id": row['id'], "_source": {"text": row['text']}}, data)
print("Start index docs. This might take few minutes.")
helpers.bulk(es, all_docs)
# Traditional lexical search with ES
query = "Cats lifespan"
# Retrieve top-100 documents from ES lexical search
resp = es.search(index=index, size=100, query={'query_string': {'query': query}})
docs = [hit['_source']['text'] for hit in resp['hits']['hits']]
print("Elasticsearch Lexical Search results:")
for doc in docs[0:3]:
print(doc)
print("-----")
# Re-Rank them with cohere
rerank_hits = co.rerank(query=query, documents=docs, top_n=3, model='rerank-multilingual-v2.0')
print("\n===========")
print("ReRank results:")
for hit in rerank_hits:
print(docs[hit.index])
print("-----")
POST _inference/rerank/cohere-rerank-v3-model
{
"query": "What is the capital of the USA?",
"input": [
"Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.",
"Capital punishment (the death penalty) has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states.",
"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.",
"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.",
"Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.",
"North Dakota is a state in the United States. 672,591 people lived in North Dakota in the year 2010. The capital and seat of government is Bismarck."
]
}
References:
'Generative AI > RAG' 카테고리의 다른 글
Elasticsearch: Advanced RAG Techniques Part 2: Querying (0) | 2024.08.16 |
---|---|
Elasticsearch: Advanced RAG Techniques Part 1: Data Processing (0) | 2024.08.16 |
Prompt Compression and Query Optimization (0) | 2024.08.10 |
Choosing the right embedding model for your RAG application (0) | 2024.08.06 |
The GraphRAG Manifesto: Adding Knowledge to GenAI (0) | 2024.07.14 |