langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
95.59k stars 15.52k forks source link

Syntax error, incorrect syntax near '{'."} for Azure Cosmos DB No SQL tutorial #25468

Open hepengfe opened 3 months ago

hepengfe commented 3 months ago

Checked other resources

Example Code

Following tutorial here https://python.langchain.com/v0.2/docs/integrations/vectorstores/azure_cosmos_db_no_sql/

After fixing the code as instructed here by adding

cosmos_database_properties={
        "id" : Config.COSMOS_DB_NAME
    }

Another issue occur and the message indicates some syntax error.

Error Message and Stack Trace (if applicable)

page_content='Direct Preference Optimization: Your Language Model is Secretly a Reward Model Rafael Rafailov∗†Archit Sharma∗†Eric Mitchell∗† Stefano Ermon†‡Christopher D. Manning†Chelsea Finn† †Stanford University‡CZ Biohub {rafailov,architsh,eric.mitchell}@cs.stanford.edu Abstract While large-scale unsupervised language models (LMs) learn broad world knowl- edge and some reasoning skills, achieving precise control of their behavior is difficult due to the completely unsupervised nature of their training. Existing methods for gaining such steerability collect human labels of the relative quality of model generations and fine-tune the unsupervised LM to align with these prefer- ences, often with reinforcement learning from human feedback (RLHF). However, RLHF is a complex and often unstable procedure, first fitting a reward model that reflects the human preferences, and then fine-tuning the large unsupervised LM using reinforcement learning to maximize this estimated reward without drifting' metadata={'source': 'https://arxiv.org/pdf/2305.18290', 'page': 0} Traceback (most recent call last): File "/home/pii/adabit/proj/proj/src/db/cosmos_db_langchain.py", line 114, in results = vectorstore.similarity_search(query) File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/langchain_community/vectorstores/azure_cosmos_db_no_sql.py", line 338, in similarity_search docs_and_scores = self.similarity_search_with_score( File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/langchain_community/vectorstores/azure_cosmos_db_no_sql.py", line 322, in similarity_search_with_score docs_and_scores = self._similarity_search_with_score( File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/langchain_community/vectorstores/azure_cosmos_db_no_sql.py", line 298, in _similarity_search_with_score items = list( File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/core/paging.py", line 124, in next return next(self._page_iterator) File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/core/paging.py", line 76, in next self._response = self._get_next(self.continuation_token) File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_query_iterable.py", line 99, in _fetch_next block = self._ex_context.fetch_next_block() File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_execution_context/execution_dispatcher.py", line 110, in fetch_next_block raise e File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_execution_context/execution_dispatcher.py", line 102, in fetch_next_block return self._execution_context.fetch_next_block() File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_execution_context/base_execution_context.py", line 79, in fetch_next_block self._ensure() File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_execution_context/base_execution_context.py", line 64, in _ensure results = self._fetch_next_block() File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_execution_context/base_execution_context.py", line 175, in _fetch_next_block return self._fetch_items_helper_with_retries(self._fetch_function) File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_execution_context/base_execution_context.py", line 147, in _fetch_items_helper_with_retries return _retry_utility.Execute(self._client, self._client._global_endpoint_manager, callback) File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_retry_utility.py", line 87, in Execute result = ExecuteFunction(function, *args, kwargs) File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_retry_utility.py", line 149, in ExecuteFunction return function(args, kwargs) File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_execution_context/base_execution_context.py", line 145, in callback return self._fetch_items_helper_no_retries(fetch_function) File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_execution_context/base_execution_context.py", line 126, in _fetch_items_helper_no_retries (fetched_items, response_headers) = fetch_function(new_options) File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_cosmos_client_connection.py", line 1065, in fetch_fn return self.__QueryFeed( File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_cosmos_client_connection.py", line 3092, in QueryFeed result, last_response_headers = self.Post(path, request_params, query, req_headers, kwargs) File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_cosmos_client_connection.py", line 2811, in __Post return synchronized_request.SynchronizedRequest( File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_synchronized_request.py", line 204, in SynchronizedRequest return _retry_utility.Execute( File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_retry_utility.py", line 85, in Execute result = ExecuteFunction(function, global_endpoint_manager, args, kwargs) File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_retry_utility.py", line 149, in ExecuteFunction return function(*args, **kwargs) File "/home/pii/miniconda3/envs/proj/lib/python3.9/site-packages/azure/cosmos/_synchronized_request.py", line 155, in _Request raise exceptions.CosmosHttpResponseError(message=data, response=response) azure.cosmos.exceptions.CosmosHttpResponseError: (BadRequest) Message: {"errors":[{"severity":"Error","location":{"start":26,"end":27},"code":"SC1001","message":"Syntax error, incorrect syntax near '{'."}]} ActivityId: 6f7cd23d-76b5-48c0-b125-5359559ef992, Microsoft.Azure.Documents.Common/2.14.0 Code: BadRequest Message: Message: {"errors":[{"severity":"Error","location":{"start":26,"end":27},"code":"SC1001","message":"Syntax error, incorrect syntax near '{'."}]} ActivityId: 6f7cd23d-76b5-48c0-b125-5359559ef992, Microsoft.Azure.Documents.Common/2.14.0

Description

System Info

System Information

OS: Linux OS Version: #93~20.04.1-Ubuntu SMP Wed Sep 6 16:15:40 UTC 2023 Python Version: 3.9.19 | packaged by conda-forge | (main, Mar 20 2024, 12:50:21) [GCC 12.3.0]

Package Information

langchain_core: 0.2.25 langchain: 0.2.12 langchain_community: 0.2.11 langsmith: 0.1.96 langchain_nomic: 0.1.2 langchain_openai: 0.1.20 langchain_text_splitters: 0.2.2 langchainhub: 0.1.20 langgraph: 0.2.3

hepengfe commented 3 months ago

I change the document sources to a list of simple Document instance, and the issue persists. Therefore, I suspect there is something wrong with the cosmos db end.

hepengfe commented 3 months ago

After rolling back to langchain_community==0.2.6, the problem is solved. I checked the latest source code, and the reason is at the query formatting.

fnurglewitz commented 2 months ago

After rolling back to langchain_community==0.2.6, the problem is solved. I checked the latest source code, and the reason is at the query formatting.

yup, there's a leftover python placeholder, if you check the last commit for that file it used to be a formatted string instead of a parameterized query, that field should be c.@embeddingKey

Also, even after locally fixing that I I'm still getting an error:

Code: BadRequest 
Message: One of the input values is invalid.

I managed to make it work by writing my own cosmos query but I'm not sure what's going on

marcellorego commented 2 months ago

Quick fix should be by calling format method at the end of the string and change VectorDistance parameter to same column reference. e.g.

query += (
            "c.id, c.{}, c.text, c.metadata, "
            "VectorDistance(c.{}, @embeddings) AS SimilarityScore FROM c"
        ).format(self._embedding_key, self._embedding_key)

Same fix should be done for the ORDER BY statement.

query += " ORDER BY VectorDistance(c.{}, @embeddings)".format(self._embedding_key)
khoangothe commented 1 month ago

Quick fix should be by calling format method at the end of the string and change VectorDistance parameter to same column reference. e.g.

query += (
            "c.id, c.{}, c.text, c.metadata, "
            "VectorDistance(c.{}, @embeddings) AS SimilarityScore FROM c"
        ).format(self._embedding_key, self._embedding_key)

Same fix should be done for the ORDER BY statement.

query += " ORDER BY VectorDistance(c.{}, @embeddings)".format(self._embedding_key)

Thanks! This works for me

codenamics commented 1 month ago

when fix will be deployed?

ks6088ts commented 1 week ago

@codenamics I've just confirmed this issue was fixed by updating langchain-community to 0.3.5+ ref. langchain-community==0.3.5

community: fix @embeddingKey in azure cosmos db no sql (https://github.com/langchain-ai/langchain/pull/27377)