前言

你以为 Byzer-LLM 只能部署大语言模型?Naive!今天我们来看看,如何使用 Byzer-LLM 部署多模特模型。今天闪亮登场的案例是:

  1. QWen-VL-Chat

  2. StableDiffusion

Byzer-LLM 基本可以像完全和大语言模型一样和这些模型进行交互。

QWen-VL-Chat

该模型能够阅读图片,给出文字解释。首先看看如何使用Python API 部署:

import ray
from byzerllm.utils.client import ByzerLLM,InferBackend


ray.init(address="auto",namespace="default")   


llm = ByzerLLM()
chat_model_name = "qwen_vl_chat"
model_location = "/home/byzerllm/models/Qwen-VL-Chat"


llm.setup_gpus_per_worker(1).setup_num_workers(1).setup_infer_backend(InferBackend.Transformers)
llm.deploy(
    model_path=model_location,
    pretrained_model_type="custom/qwen_vl_chat",
    udf_name=chat_model_name,
    infer_params={}
)

这里只要指定名字,指定模型路径,以及指定资源即可。和大语言模型的部署是一模一样的。如果你忘记了,不妨看看下面这篇文章:

Byzer-LLM 支持同时开源和SaaS版通义千问

部署完成后,你就可以像使用大语言模型一样和该多模态模型聊天:

import base64
image_path = "/home/byzerllm/projects/jupyter-workspace/1.jpg"
with open(image_path, "rb") as f:
    image_content = base64.b64encode(f.read()).decode("utf-8")


t = llm.chat_oai(conversations=[{
    "role": "user",
    "content": "这是什么"
}],model=chat_model_name,llm_config={"image":image_content})


t[0].output


# '{"response": "图中是一名女子在沙滩上和狗玩耍,旁边的狗是一只拉布拉多犬,它坐在沙滩上,面对着一名身穿格子衬

这里我们可以在 llm_config 参数配置一个 image 参数,然后把图片按base64编码后传递即可。

你也可以进行多轮对话:

import json
history = json.loads(t[0].output)["history"]


llm.chat_oai(conversations=history+[{
    "role": "user",
    "content": "能圈出狗么?"
}],model=chat_model_name)


# [LLMResponse(output='{"response": "<ref>狗</ref><box>(221,425),(511,889)</box>", "history":

比如我们让他把狗圈出来,他会返回一个坐标。这里比较特殊的是,我们需要从上一次对话中拿到对话历史,然后和当前对话合并下,再发送过去。其实很方便。

整个使用体验是不是非常简单易用?

StableDiffusion

其实 StableDiffusion 我们很早就支持了,以前主要给的例子都是 Byzer-SQL 语法的。这次介绍下 Python API的使用。

部署:

import ray
from byzerllm.utils.client import ByzerLLM,InferBackend


ray.init(address="auto",namespace="default")   


llm = ByzerLLM()
chat_model_name = "sd_chat"
model_location = "/home/byzerllm/models/stable-diffusion-v1-5"


llm.setup_gpus_per_worker(2).setup_num_workers(1).setup_infer_backend(InferBackend.Transformers)
llm.deploy(
    model_path=model_location,
    pretrained_model_type="custom/stable_diffusion",
    udf_name=chat_model_name,
    infer_params={}
)


def show_image(content):
    from IPython.display import display, Image
    import base64             
    img = Image(base64.b64decode(content))
    display(img)

部署方式基本完全没啥变化,指定路径,名字,资源。这里我们新增了一个 show_image 方法,方便后续显示 StableDiffusion 生成的图片。

现在,你可以和 StableDiffusion进行对话了:

import json
t = llm.chat_oai(
    conversations=[
        {
            "role":"user",
            "content":"画一只猫"
        }
    ],model=chat_model_name,llm_config={"gen.batch_size":3}
)


cats = json.loads(t[0].output)
for res in cats:
    show_image(res["img64"])

这里我们可以通过 llm_config 传递一些额外参数,比如一次生成多少图片。详细的参数列表可以打我们 Byzer-LLM 项目README里找。

下面是生成结果:

9ceca4c679499130e338bbdf8981ecfd.png

总结

可以看到,Byzer-LLM 对多模态模型的支持也是很不错的,而且使用体验和大语言模型非常一致。实际上,除了Python API 我们也支持SQL API,比如我们可以从数据库拉取文本数据,然后使用这些文本数据批量生成图片,一条SQL就可以搞定:

select explode(from_json(llm_response_predict(sd_chat(llm_param(map(
"instruction", 'cat',
"generation.width", "512",
"generation.height", "512",
"generation.batch_count", "2",
"generation.batch_size", "2"
)))),'Array<struct<prompt: string, img64: string>>')) as nr from finalResult as jsonTable;


select nr.prompt, nr.img64 from jsonTable as result;
save overwrite result as image.`/tmp/images` ....;

最后,再来一张水墨猫咪:

5ace1328ae7b334b0263d18f934c60b5.png


Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐