langflow 连接字符串解析问题

9vw9lbht  于 2个月前  发布在  其他
关注(0)|答案(3)|浏览(36)

Bug描述

如果用户密码以@结尾,解析连接字符串将失败。
例如,对于字符串 postgres://user:12345@@postgreshost.dev/dbname
密码将解析为:12345 而不是 12345@
主机将是: @postgreshost.dev
应该用正则表达式匹配最后一个@而不是第一个。
仅在pgvector组件上进行测试。

复现步骤

  1. 使用pgvector组件
  2. 设置连接字符串,密码以@结尾
  3. 组件构建失败。

预期行为

应该用最后一个@而不是第一个分隔密码和域名。

谁可以帮忙?

  • 无响应*

操作系统

Ubuntu 22.04

Langflow版本

1.0.2

Python版本

3.12

截图

  • 无响应*

流程文件

  • 无响应*
yquaqz18

yquaqz181#

你好@CannonFodderr!我在这里帮助你解决关于仓库的任何错误、问题或贡献。请随时寻求帮助!

要解决pgvector组件中连接字符串解析的问题,其中以'@'字符结尾的密码导致解析错误,你可以修改build方法,以确保连接字符串被正确地URL编码。可以使用Python的urllib.parse模块对连接字符串中的密码部分进行编码。

以下是如何更新build方法:

from urllib.parse import quote

def build(
    self,
    embedding: Embeddings,
    pg_server_url: str,
    collection_name: str,
    documents: Optional[Document] = None,
) -> Union[VectorStore, BaseRetriever]:
    """
Builds the Vector Store or BaseRetriever object.

Args:
- embedding (Embeddings): The embeddings to use for the Vector Store.
- documents (Optional[Document]): The documents to use for the Vector Store.
- collection_name (str): The name of the PG table.
- pg_server_url (str): The URL for the PG server.

Returns:
- VectorStore: The Vector Store object.
"""
    try:
        # Parse the connection string to handle special characters in the password
        parsed_url = pg_server_url.split('@')
        if len(parsed_url) == 2:
            user_info, host_info = parsed_url
            user_info = user_info.split(':')
            if len(user_info) == 2:
                user, password = user_info
                password = quote(password, safe='')
                pg_server_url = f"{user}:{password}@{host_info}"

        if documents is None:
            vector_store = PGVector.from_existing_index(
                embedding=embedding,
                collection_name=collection_name,
                connection_string=pg_server_url,
            )
        else:
            vector_store = PGVector.from_documents(
                embedding=embedding,
                documents=documents,  # type: ignore
                collection_name=collection_name,
                connection_string=pg_server_url,
            )
    except Exception as e:
        raise RuntimeError(f"Failed to build PGVector: {e}")
    return vector_store

这段代码确保密码中的所有特殊字符(包括'@')都被正确地URL编码,防止连接字符串被错误解析。

  • 要继续与1(https://github.com/logspace-ai/langflow/blob/dev/src/backend/langflow/components/vectorstores/pgvector.py)交流,请提及@dosu。*
nuypyhwy

nuypyhwy3#

嘿,@CannonFodderr,

请确保在连接字符串中包含"SQL",应该是:
postgresql://user:12345@@postgreshost.dev/dbname
而不是:
postgres://user:12345@@postgreshost.dev/dbname

非常感谢!

相关问题