一、实际背景介绍1、业务背景京东衰弱内容中台H2有一个指标就是须要替换两家CP内容(总体内容体量百万级),咱们当初的逻辑是想依照PV热度优先高热去新生产和替换。替换后能够极大的节俭cp内容引入的老本。
第一步:这么多内容,咱们的生产逻辑须要依照学科和索引归类和调配,进而批量生产,靠人工一篇篇补索引,效率会很低。心愿借助算法的能力,如果当初还不是十分精确,也能够算法+人工修改,
第二步:按索引归类好之后,咱们和库内非CP但主题类似内容进行比对,曾经有的就不做反复生产。最初剩下来的进行批量生产和替换。
2、技术背景M3E(M3E(Multimodal Multitask Meta-Embedding)是一个开源的中文嵌入模型
Vearch 是对大规模深度学习向量进行高性能类似搜寻的弹性分布式系统。也是京东自研开源的我的项目,具备弱小的类似搜寻的弹性分布式能力。
OpenAI的迅速倒退对算法老本产生了重大影响。随着技术的提高和钻研的一直推动,OpenAI曾经获得了许多冲破,使得算法的开发和部署老本大大降低。OpenAI的Chat模式和Embedding模式是OpenAI API中的两种不同的应用形式。
1、Chat模式: Chat模式是OpenAI API的一种应用形式,旨在反对对话式的人机交互。在Chat模式下,您能够通过向API发送一系列的用户音讯来与模型进行交互,模型将逐条回复每个音讯。这种交互式的形式使得您能够与模型进行对话,提出问题、申请解释、寻求倡议等。
import openairesponse = openai.Completion.create( engine="davinci", prompt="What is the capital of France?", max_tokens=100, n=1, stop=None, temperature=0.7)print(response.choices[0].text.strip())2、Embedding模式: Embedding模式是OpenAI API的另一种应用形式,旨在获取文本的嵌入示意。在Embedding模式下,您能够将一段文本传递给API,并获取该文本的高维向量示意,也称为嵌入向量。这些嵌入向量能够用于计算文本之间的类似度、聚类、分类等工作。
import openairesponse = openai.Embed.create( model="text-embedding-ada-002", documents=["Once upon a time", "In a land far, far away"],)embedding1 = response.embeddings[0]embedding2 = response.embeddings[1]# 进行嵌入向量的类似度计算等其它操作本次实际次要应用了Embedding,具体实际如下文。
二、实际流程1、总体流程(1)、总体流程图
(2)、OpenAi/M3E向量生成局部代码实际async def embed_and_store_with_limit_and_check( self, semaphore, id, vector_store, text_future_func = None, text: Union[str, list[str]] = "", **additional_properties ): async with semaphore: retry_count = ( 3 # Task failed with exception Response payload is not completed ) retry_count_doubled = False retry = 1 last_error = None while retry <= retry_count: # Retry up to 3 times. try: try: data = await vector_store.get(vector_id=id) id = data.id embedding = data.result.embedding.feature return (id, embedding) except VearchRouterGetNotFoundError: try: return await self.embed_and_store( text=text, id=id, vector_store=vector_store, text_future_func=text_future_func, **additional_properties, ) except asyncio.TimeoutError: logger.error( f"embed_and_store_with_limit_and_check - id {id} #[{vector_store.space_name} {vector_store.db_name}] - Timeout during embed_and_store()" ) raise except Exception as error: error_message = f"{error}" or f"{error.__class__} {error.__doc__}" logger.error( f"embed_and_store_with_limit_and_check - id {id} #[{vector_store.space_name} {vector_store.db_name}] - failed with exception {error_message}, retry {retry}" ) if isinstance(error, VearchRouterStatusError): if error.reason == "partition_not_leader": logger.info( f"embed_and_store_with_limit_and_check - id {id} #[{vector_store.space_name} {vector_store.db_name}] - {error_message}, retry {retry} asyncio.sleep(10) doubled" ) await asyncio.sleep(10) # Response payload is not completed if not retry_count_doubled: retry_count = retry_count * 2 retry_count_doubled = True if isinstance(error, aiohttp.client_exceptions.ClientPayloadError): await asyncio.sleep(5) # Response payload is not completed if not retry_count_doubled: retry_count = retry_count * 2 retry_count_doubled = True else: await asyncio.sleep(1) # Wait for 1 second before retrying retry = retry + 1 last_error = error raise VearchRouterClientRetryError( retry_count, f"embed_and_store_with_limit_and_check - id {id} #[{vector_store.space_name} {vector_store.db_name}] - completely failed with exception {last_error} - retried {retry_count} times", error=last_error, )(3)、vearch向量存储及类似度搜寻局部代码实async def score_similarity( self, vector_store, embedding=None, id=None, **search_properties ): """Find the most similar word and the similarity score for a given word in the document""" if not isinstance(embedding, list): try: results_with_scores = await vector_store.search_by_ids(ids=[id]) # embedding = response.result.embedding.feature return results_with_scores.results[0].hits.hits except VearchRouterStatusError as error: raise error # if error.found == False: # query_result = await embeddings.embed_query(word) results_with_scores = await vector_store.search( feature=embedding, **search_properties ) return results_with_scores.hits.hits2、OpenAi实现查重的局限性(1)、老本以目前100万数据量为例,如果应用目前OpenAi的凋谢接口实现,每篇内容因为token等限度进出一次须要0.007美元,100万篇内容须要7000美元才能够实现数据特征提取和向量生成,按照目前的内容体量和使用,这个老本还是高于预期,在老本方面没有比其余计划有劣势。
...