昇腾社区首页
中文
注册

query

功能描述

根据文本检索出相关图片结合提示词发送给大模型生成图片。

函数原型

def query(text, llm_config, *args, **kwargs)

参数说明

参数名

数据类型

可选/必选

说明

text

str

必选

检索图片的描述文字,长度取值:(0, 1* 1000 * 1000]

llm_config

LLMParameterConfig

可选

继承父类方法,此处未使用。

args

list

可选

当前chain未使用。

kwargs["prompt"]

str

必选

图片生成提示词,由入参kwargs传递,长度取值:(0, 1* 1024 * 1024]

kwargs["size"]

str

可选

图片生成尺寸,表示为"height*width",由入参kwargs传递,具体支持的尺寸由对应的大模型决定,正则匹配格式为: "^\d{1,5}\*\d{1,5}$",默认512*512。

返回值说明

数据类型

说明

Dict,

{"prompt": prompt, "result": data}

其中data为图片base64编码后的数据。

调用示例

from mx_rag.chain import Img2ImgChain
from mx_rag.llm import Img2ImgMultiModel
from mx_rag.retrievers import Retriever
from mx_rag.storage.vectorstore import MindFAISS
from mx_rag.storage.document_store import SQLiteDocstore
from mx_rag.embedding.local import ImageEmbedding
from mx_rag.utils import ClientParam
dev = 0
img_emb = ImageEmbedding(model_name="ViT-B-16", model_path="/path/to/chinese-clip-vit-base-patch16", dev_id=dev)
img_vector_store = MindFAISS(x_dim=512,
                             devs=[dev],
                             load_local_index="/path/to/image_faiss.index",
                             auto_save=True)
chunk_store = SQLiteDocstore(db_path="/path/to/sql.db")
img_retriever = Retriever(vector_store=img_vector_store, document_store=chunk_store,
                          embed_func=img_emb.embed_documents, k=1, score_threshold=0.5)
multi_model = Img2ImgMultiModel(model_name="sd",
                                url="img to image url",
                                client_param=ClientParam(ca_file="/path/to/ca.crt"))
img2img_chain = Img2ImgChain(multi_model=multi_model, retriever=img_retriever)
llm_data = img2img_chain.query("查找小男孩图片",
                               prompt="he is a knight, wearing armor, big sword in right hand. Blur the background, focus on the knight")
print(llm_data)
# 该用例基于知识库中已上传的图片检索出相关图片,结合提示词发送给大模型生成图片