# 11. EnsembleRetriever with Convex Combination (CC)

## Ensemble Retriever `Convex Combination(CC)` Add <a href="#ensemble-retriever-convex-combinationcc" id="ensemble-retriever-convex-combinationcc"></a>

* Note: [Explaining the differences between the algorithmic methods published by AutoRAG ](https://velog.io/@autorag/%EB%9E%AD%EC%B2%B4%EC%9D%B8%EC%9D%98-Ensemble-Retriever-%EC%9D%B4%EA%B2%8C-%EB%8C%80%EC%B2%B4-%EB%AD%90%EC%A7%80)Uncomment below and update the package to proceed.

```python
# proceed after update
# !pip install -qU langchain-teddynote
```

```python
from dotenv import load_dotenv

load_dotenv()
```

```
 True 
```

### Pre-setting for experiment <a href="#id-1" id="id-1"></a>

```python
from langchain.retrievers import EnsembleRetriever as OriginalEnsembleRetriever
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import PDFPlumberLoader
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_teddynote.retrievers import KiwiBM25Retriever

# load document(Load Documents)
loader = PDFPlumberLoader("data/Digital Government Innovation Promotion Plan.pdf")

# Split Documents: Set to a small Chunk Size for testing
text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=0)
split_documents = loader.load_and_split(text_splitter)

# Creating an embedding
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")

# FaissRetriever generation
faiss = FAISS.from_documents(
    documents=split_documents, embedding=embeddings
).as_retriever(search_kwargs={"k": 5})

# KiwiBM25Retriever Generation (Korean morphological analyzer + BM25 algorithm)
bm25 = KiwiBM25Retriever.from_documents(documents=split_documents, embedding=embeddings)
bm25.k = 5

# LangChain version of EnsembleRetriever
original_ensemble_retriever = OriginalEnsembleRetriever(retrievers=[faiss, bm25])
```

EnsembleRetriever creation in CC and RRF

```python
from langchain_teddynote.retrievers import (
    EnsembleRetriever,
    EnsembleMethod,
)

# EnsembleRetriever in RRF mode (RRF is set by default)
rrf_ensemble_retriever = EnsembleRetriever(
    retrievers=[faiss, bm25], method=EnsembleMethod.RRF
)

# CC of the way EnsembleRetriever
cc_ensemble_retriever = EnsembleRetriever(
    retrievers=[faiss, bm25], method=EnsembleMethod.CC  # method designation: CC
)
```

### Search result comparison <a href="#id-2" id="id-2"></a>

```python
def pretty_print(query):
    for i, (original_doc, cc_doc, rrf_doc) in enumerate(
        zip(
            original_ensemble_retriever.invoke(query),
            cc_ensemble_retriever.invoke(query),
            rrf_ensemble_retriever.invoke(query),
        )
    ):
        print(f"[{i}] [Original] Q: {query}", end="\n\n")
        print(original_doc.page_content)
        print("-" * 100)
        print(f"[{i}] [RRF] Q: {query}", end="\n\n")
        print(rrf_doc.page_content)
        print("-" * 100)
        print(f"[{i}] [CC] Q: {query}", end="\n\n")
        print(cc_doc.page_content)
        print("=" * 100, end="\n\n")
```

* Search results `"Original"` and `"RRF"` There should be no difference. (LangChain implements as it is)
* Search results `"CC"` has `"RRF"` There may be a difference from.

`RRF` Wow `CC` Compare the search results of the method and borrow the right way for the document.

```python
# compare search results
pretty_print("What is digital transformation?")
```

```
 [0] [Original] Q: What is digital transformation? 

Note 1 Digital government innovation strategy 
A good world that opens digitally 
□ Vision 
※ Subtitle: Korea goes first. 
□ Promotion Principles △ From the end user's perspective 
△ aims to improve public service levels 
---------------------------------------------------------------------------------------------------- 
[0] [RRF] Q: What is digital transformation? 

Note 1 Digital government innovation strategy 
A good world that opens digitally 
□ Vision 
※ Subtitle: Korea goes first. 
□ Promotion Principles △ From the end user's perspective 
△ aims to improve public service levels 
---------------------------------------------------------------------------------------------------- 
[0] [CC] Q: What is digital transformation? 

○ (System) Difficulty introducing and utilizing digital new technologies 
-The existing complex way of developing services is unsuitable for digital transformation with a short innovation cycle. 
==================================================================================================== 

[1] [Original] Q: What is digital transformation? 

○ (System) Difficulty introducing and utilizing digital new technologies 
-The existing complex way of developing services is unsuitable for digital transformation with a short innovation cycle. 
---------------------------------------------------------------------------------------------------- 
[1] [RRF] Q: What is digital transformation? 

○ (System) Difficulty introducing and utilizing digital new technologies 
-The existing complex way of developing services is unsuitable for digital transformation with a short innovation cycle. 
---------------------------------------------------------------------------------------------------- 
[1] [CC] Q: What is digital transformation? 

○ (Digital Highlands ‧Retract) Various notices · Inner Doors*, etc. online (Public) 
Activate digital high-payment so that you can receive it and pay it easily 
==================================================================================================== 

[2] [Original] Q: What is digital transformation? 

Ⅰ. Dog yo 
□ propulsion background 
○ Korea invests in national high-speed information networks and actively communicates 
Build the world's highest level of e-government by driving business 
---------------------------------------------------------------------------------------------------- 
[2] [RRF] Q: What is digital transformation? 

Ⅰ. Dog yo 
□ propulsion background 
○ Korea invests in national high-speed information networks and actively communicates 
Build the world's highest level of e-government by driving business 
---------------------------------------------------------------------------------------------------- 
[2] [CC] Q: What is digital transformation? 

Note 1 Digital government innovation strategy 
A good world that opens digitally 
□ Vision 
※ Subtitle: Korea goes first. 
□ Promotion Principles △ From the end user's perspective 
△ aims to improve public service levels 
==================================================================================================== 

[3] [Original] Q: What is digital transformation? 

○ (Digital Highlands ‧Retract) Various notices · Inner Doors*, etc. online (Public) 
Activate digital high-payment so that you can receive it and pay it easily 
---------------------------------------------------------------------------------------------------- 
[3] [RRF] Q: What is digital transformation? 

○ (Digital Highlands ‧Retract) Various notices · Inner Doors*, etc. online (Public) 
Activate digital high-payment so that you can receive it and pay it easily 
---------------------------------------------------------------------------------------------------- 
[3] [CC] Q: What is digital transformation? 

○ Based on open source-focused digital government ecosystems and public market demand 
Accelerate innovation in the high-tech digital industry and raise electricity for global leap forward 
==================================================================================================== 

[4] [Original] Q: What is digital transformation? 

Introducing a more reliable smartphone-based digital identification card 
* Step-by-step expansion after safety check from areas with clear targets and objectives, such as student ID and civil service 
---------------------------------------------------------------------------------------------------- 
[4] [RRF] Q: What is digital transformation? 

Introducing a more reliable smartphone-based digital identification card 
* Step-by-step expansion after safety check from areas with clear targets and objectives, such as student ID and civil service 
---------------------------------------------------------------------------------------------------- 
[4] [CC] Q: What is digital transformation? 

Digital government innovation promotion plan 
2019. 10. 29. 
Joint Relations 
==================================================================================================== 

[5] [Original] Q: What is digital transformation? 

○ Based on open source-focused digital government ecosystems and public market demand 
Accelerate innovation in the high-tech digital industry and raise electricity for global leap forward 
---------------------------------------------------------------------------------------------------- 
[5] [RRF] Q: What is digital transformation? 

○ Based on open source-focused digital government ecosystems and public market demand 
Accelerate innovation in the high-tech digital industry and raise electricity for global leap forward 
---------------------------------------------------------------------------------------------------- 
[5] [CC] Q: What is digital transformation? 

Ⅰ. Dog yo 
□ propulsion background 
○ Korea invests in national high-speed information networks and actively communicates 
Build the world's highest level of e-government by driving business 
==================================================================================================== 

[6] [Original] Q: What is digital transformation? 

We will assist those who are difficult to use digital devices to receive services without discrimination. 
Kwon Yi, 
□1 People's Voice Listening · Analysis System Improvement (22 years) 
Angryer 
---------------------------------------------------------------------------------------------------- 
[6] [RRF] Q: What is digital transformation? 

We will assist those who are difficult to use digital devices to receive services without discrimination. 
Kwon Yi, 
□1 People's Voice Listening · Analysis System Improvement (22 years) 
Angryer 
---------------------------------------------------------------------------------------------------- 
[6] [CC] Q: What is digital transformation? 

Introducing a more reliable smartphone-based digital identification card 
* Step-by-step expansion after safety check from areas with clear targets and objectives, such as student ID and civil service 
==================================================================================================== 

[7] [Original] Q: What is digital transformation? 

Digital government innovation promotion plan 
2019. 10. 29. 
Joint Relations 
---------------------------------------------------------------------------------------------------- 
[7] [RRF] Q: What is digital transformation? 

Digital government innovation promotion plan 
2019. 10. 29. 
Joint Relations 
---------------------------------------------------------------------------------------------------- 
[7] [CC] Q: What is digital transformation? 

We will assist those who are difficult to use digital devices to receive services without discrimination. 
Kwon Yi, 
□1 People's Voice Listening · Analysis System Improvement (22 years) 
Angryer 
==================================================================================================== 

[8] [Original] Q: What is digital transformation? 

Note 3 Expectations for digital government innovation 
◈ The world's best in government digital services when successfully promoting East Coast 
---------------------------------------------------------------------------------------------------- 
[8] [RRF] Q: What is digital transformation? 

Note 3 Expectations for digital government innovation 
◈ The world's best in government digital services when successfully promoting East Coast 
---------------------------------------------------------------------------------------------------- 
[8] [CC] Q: What is digital transformation? 

Note 3 Expectations for digital government innovation 
◈ The world's best in government digital services when successfully promoting East Coast 
==================================================================================================== 

```

<br>
