Repository: langchain
Issue: unable to use milvus for cosine similarity
Labels: Ɑ: vector store,investigate

### Checked other resources

- [X] I added a very descriptive title to this issue.
- [X] I searched the LangChain documentation with the integrated search.
- [X] I used the GitHub search to find a similar question and didn't find it.
- [X] I am sure that this is a bug in LangChain rather than my code.
- [X] The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

### Example Code

The following code:
URI = "./milvus_example.db"
index_params = {
    'metric_type': 'COSINE',
    'index_type': "FLAT",
    # 'params': {"nlist": 128}
}
search_params = {
    "metric_type": "COSINE",
    # "params": {"nprobe": 12},
}

vector_store = Milvus(
    embedding_function=embeddings,
    connection_args={"uri": URI},
    auto_id=True,
    index_params=index_params,
    search_params=search_params
)
all_splits = text_splitter.split_documents(documents)
ids = vector_store.add_documents(documents=all_splits)
results = vector_store.similarity_search_with_score(prompt,param=search_params)


### Error Message and Stack Trace (if applicable)

RPC error: [search], <MilvusException: (code=1100, message=fail to search: metric type not match: invalid [expected=L2][actual=COSINE]: invalid parameter)>, <Time:{'RPC start': '2024-12-25 16:15:58.358832', 'RPC error': '2024-12-25 16:15:58.359751'}>
  0%|                                                                                                                                                   | 0/1174 [00:01<?, ?it/s]
Traceback (most recent call last):
  File "/home/wupeiyang/work/RTL-Repo/src/generate_data_for_rag_test_dynamic_langchain.py", line 405, in <module>
    main()
  File "/home/wupeiyang/work/RTL-Repo/src/generate_data_for_rag_test_dynamic_langchain.py", line 306, in main
    results = vector_store.similarity_search_with_score(prompt,param=search_params)
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/langchain_milvus/vectorstores/milvus.py", line 1182, in similarity_search_with_score
    res = self.similarity_search_with_score_by_vector(
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/langchain_milvus/vectorstores/milvus.py", line 1221, in similarity_search_with_score_by_vector
    col_search_res = self._collection_search(
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/langchain_milvus/vectorstores/milvus.py", line 1047, in _collection_search
    res = self.col.search(
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/pymilvus/orm/collection.py", line 801, in search
    resp = conn.search(
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/pymilvus/decorators.py", line 141, in handler
    raise e from e
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/pymilvus/decorators.py", line 137, in handler
    return func(*args, **kwargs)
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/pymilvus/decorators.py", line 176, in handler
    return func(self, *args, **kwargs)
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/pymilvus/decorators.py", line 116, in handler
    raise e from e
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/pymilvus/decorators.py", line 86, in handler
    return func(*args, **kwargs)
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/pymilvus/client/grpc_handler.py", line 805, in search
    return self._execute_search(request, timeout, round_decimal=round_decimal, **kwargs)
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/pymilvus/client/grpc_handler.py", line 746, in _execute_search
    raise e from e
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/pymilvus/client/grpc_handler.py", line 735, in _execute_search
    check_status(response.status)
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/pymilvus/client/utils.py", line 63, in check_status
    raise MilvusException(status.code, status.reason, status.error_code)
pymilvus.exceptions.MilvusException: <MilvusException: (code=1100, message=fail to search: metric type not match: invalid [expected=L2][actual=COSINE]: invalid parameter)>
[2024-12-25 16:16:03,525] torch.distributed.elastic.multiprocessing.api: [ERROR] failed (exitcode: 1) local_rank: 0 (pid: 29719) of binary: /home-g2/wupeiyang/anaconda3/envs/llama3/bin/python
Traceback (most recent call last):
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/torch/distributed/launch.py", line 196, in <module>
    main()
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/torch/distributed/launch.py", line 192, in main
    launch(args)
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/torch/distributed/launch.py", line 177, in launch
    run(args)
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/torch/distributed/run.py", line 797, in run
    elastic_launch(
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/torch/distributed/launcher/api.py", line 134, in __call__
    return launch_agent(self._config, self._entrypoint, list(args))
  File "/home-g2/wupeiyang/anaconda3/envs/llama3/lib/python3.9/site-packages/torch/distributed/launcher/api.py", line 264, in launch_agent
    raise ChildFailedError(
torch.distributed.elastic.multiprocessing.errors.ChildFailedError: 
============================================================
/home/wupeiyang/work/RTL-Repo/src/generate_data_for_rag_test_dynamic_langchain.py FAILED
------------------------------------------------------------
Failures:
  <NO_OTHER_FAILURES>
------------------------------------------------------------
Root Cause (first observed failure):
[0]:
  time      : 2024-12-25_16:16:03
  host      : localhost
  rank      : 0 (local_rank: 0)
  exitcode  : 1 (pid: 29719)
  error_file: <N/A>
  traceback : To enable traceback see: https://pytorch.org/docs/stable/elastic/errors.html
============================================================

### Description

I'm trying to use langchain_milvus to search by cosine similarity which is supported according to Milvus Doc

### System Info

System Information
------------------
> OS:  Linux
> OS Version:  #224-Ubuntu SMP Thu Dec 5 13:38:28 UTC 2024
> Python Version:  3.9.18 (main, Sep 11 2023, 13:41:44) 
[GCC 11.2.0]

Package Information
-------------------
> langchain_core: 0.3.21
> langchain: 0.3.9
> langchain_community: 0.3.9
> langsmith: 0.1.147
> langchain_huggingface: 0.1.2
> langchain_milvus: 0.1.7
> langchain_ollama: 0.2.1
> langchain_text_splitters: 0.3.2

Optional packages not installed
-------------------------------
> langserve

Other Dependencies
------------------
> aiohttp: 3.9.1
> async-timeout: 4.0.3
> dataclasses-json: 0.6.7
> httpx: 0.27.0
> httpx-sse: 0.4.0
> huggingface-hub: 0.23.3
> jsonpatch: 1.33
> langsmith-pyo3: Installed. No version info available.
> numpy: 1.26.3
> ollama: 0.4.2
> orjson: 3.10.12
> packaging: 23.2
> pydantic: 2.10.2
> pydantic-settings: 2.6.1
> pymilvus: 2.5.0
> PyYAML: 6.0.1
> requests: 2.31.0
> requests-toolbelt: 1.0.0
> sentence-transformers: 3.3.1
> SQLAlchemy: 2.0.36
> tenacity: 9.0.0
> tokenizers: 0.19.1
> transformers: 4.41.2
> typing-extensions: 4.12.2