Source code for agentscope.service.retrieval.retrieval_from_list
# -*- coding: utf-8 -*-"""Retrieve service working with memory specially."""fromtypingimportCallable,Optional,Any,Sequencefromloguruimportloggerfromagentscope.service.service_responseimportServiceResponsefromagentscope.service.service_statusimportServiceExecStatusfromagentscope.modelsimportModelWrapperBase
[docs]defretrieve_from_list(query:Any,knowledge:Sequence,# TODO: renamescore_func:Callable[[Any,Any],float],top_k:int=None,embedding_model:Optional[ModelWrapperBase]=None,preserve_order:bool=True,)->ServiceResponse:""" Retrieve data in a list. Memory retrieval with user-defined score function. The score function is expected to take the `query` and one of the element in 'knowledge' (a list). This function retrieves top-k elements in 'knowledge' with HIGHEST scores. If the 'query' is a dict but has no embedding, we use the embedding model to embed the query. Args: query (`Any`): A message to be retrieved. knowledge (`Sequence`): Data/knowledge to be retrieved from. score_func (`Callable[[Any, Any], float]`): User-defined function for comparing two messages. top_k (`int`, defaults to `None`): Maximum number of messages returned. embedding_model (`Optional[ModelWrapperBase]`, defaults to `None`): A model to embed the query/message. preserve_order (`bool`, defaults to `True`): Whether to preserve the original order of the retrieved data. Defaults to True. Returns: `ServiceResponse`: The top-k retrieved messages with HIGHEST scores. """ifisinstance(query,dict):ifembedding_modelisnotNoneand"embedding"notinquery:query["embedding"]=embedding_model([query],return_embedding_only=True,)elifembedding_modelisNoneand"embedding"notinquery:logger.warning("Since the input query has no embedding, embedding model is ""is not provided either.",)# (score, index, object)scores=[(score_func(query,msg),i,msg)fori,msginenumerate(knowledge)]# ordered by score, and extract the top-k items with highest scorestop_k=len(scores)iftop_kisNoneelsetop_kordered_top_k_scores=sorted(scores,key=lambdax:x[0],reverse=True)[:top_k]# if keep the original orderifpreserve_order:# ordered by indexcontent=sorted(ordered_top_k_scores,key=lambdax:x[1])else:content=ordered_top_k_scores# The returned content includes a list of triples of (score, index, object)returnServiceResponse(status=ServiceExecStatus.SUCCESS,content=content,)