在查询结果时从 RedisVL 获取正确的向量嵌入

问题描述 投票:0回答:1

我在我的项目中使用RedisVL来存储一些数据。用于在 RedisVL 中创建索引的 schema.yaml 文件如下所示:

version: '0.1.0'
index:
  name: event-index
  prefix: event

fields:
  - name: lp
    type: tag
  - name: customer_token
    type: tag
  - name: event_type
    type: tag
  - name: corrected_lp
    type: tag
  - name: embedding
    type: vector
    attrs:
      algorithm: flat
      dims: 59
      distance_metric: L2
      datatype: float32

现在,当我将一些数据加载到该索引中时,如下所示:

# event is a pydantic model having fields lp, customer_token, event_type, corrected_lp
# embed the image using an embedding model
vec = to_embedding(image)

index = await search_index()
info = {
        'lp': event.lp,
        'embedding': vec.tobytes(),
        'customer_token': event.customer_token,
        'event_type': event.event_type
    }
if event.corrected_lp:
    info['corrected_lp'] = event.corrected_lp
await index.load([info],
      keys=[f'event:{identifier}'],
      ttl=config['index']['ttl'].get(int))

它将数据正确加载到索引中。

当我想在使用 FilterQuery 后从索引中获取相同的数据时,它会将所有值作为字典列表返回。

index = await search_index()
        
filter_exp = ((Tag("customer_token") == filters.customer_token) &
              (Tag("event_type") == filters.event_type)) 
        
filter_query = FilterQuery(
            return_fields=["event_type", "lp", "corrected_lp",
                           "customer_token", "embedding"],
            filter_expression=filter_exp)
        
res = await index.query(filter_query)

if not res:
    return None
# Converting the list of dictionaries to a list of Event objects
events = [
         Event(event_type=event['event_type'],
               lp=event['lp'],
               corrected_lp=event.get('corrected_lp'),
               customer_token=event['customer_token'],
               vector=list(np.frombuffer(event['embedding'],dtype=np.float32)),
               ) for event in res
         ]

FilterQuery 之后从索引获得的结果 (res) 显示嵌入值是普通字符串,而不是加载到索引时转换的预期字节串。如何从字符串中获取原始编码的字节串。

如果我使用 string.encode() 之类的东西,那么它无法解释正确的格式。

对此的任何帮助都会非常有帮助。

filter redis embedding vector-search
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.