本文使用「署名 4.0 国际 (CC BY 4.0)」许可协议,欢迎转载、或重新修改使用,但需要注明来源。 [署名 4.0 国际 (CC BY 4.0)](https://creativecommons.org/licenses/by/4.0/deed.zh) 本文作者: 苏洋 创建时间: 2024年09月30日 统计字数: 16369字 阅读时间: 33分钟阅读 本文链接: https://soulteary.com/2024/09/30/etl-using-small-size-llm-and-dify-qwen-2-5-7b.html ----- # 使用小尺寸大模型和 Dify 清洗数据:Qwen 2.5 7B 本篇文章,我们聊聊如何使用最近发布的 Qwen 2.5 7B 模型来做日常低成本的数据清理工作。 ## 写在前面 这个月好像比上个月还忙,去了很多地方,见了很多朋友。 之前云栖大会上说要写几篇 Qwen 相关的实践,一直没有时间,趁着今天出行前的空档,分享一篇之前使用小模型的经验。 ![简单好用的 AI 流水线](https://attachment.soulteary.com/2024/09/30/dify-pipeline.jpg) 本篇文章使用的模型是千问 2.5 版本的 7B 模型的官方量化版:Qwen2.5-7B-Instruct-GPTQ-Int4,因为我们要处理的数据任务非常简单,追求效率第一,所以即使使用较小参数量的模型,搭配它的量化版本,也问题不大,在不优化显存占用的情况下大概 17G vRAM(可优化)。 **如果你对纯 CPU 推理或者端侧硬件推理感兴趣,可以翻阅之前的文章,更换推理方式。** 完整的流水线配置,在文末配置处,有需要自取。 ## 准备工作 本文的准备工作很简单,如果你是我的老读者,已经有顺手抄起来就能使用的 Dify 和 Docker,那么只需要跟着步骤下载必要的 Docker 镜像和你想使用的模型,最后选择一个想要清理的数据源即可。 ### Docker 环境的准备 如果你已经安装了 Docker ,那么可以跳过这个小节。 如果你还没有安装 Docker,不论你使用的是 Windows、Linux、macOS,都可以相对快速简单的完成 Docker 的安装和简单配置。 你可以参考之前的一些文章:《[Docker 环境下使用 Traefik 3 的最佳实践:快速上手](https://soulteary.com/2024/08/04/best-practices-for-traefik-3-in-docker-getting-started-quickly.html#docker-%E7%8E%AF%E5%A2%83)》中的“Docker 环境”、《[基于 Docker 的深度学习环境:Windows 篇](https://soulteary.com/2023/07/29/docker-based-deep-learning-environment-under-windows.html)》中的“准备 Docker 虚拟化运行环境” 或者《[在笔记本上搭建高性价比的 Linux 学习环境:基础篇](https://soulteary.com/2022/06/21/building-a-cost-effective-linux-learning-environment-on-a-laptop-the-basics.html)》中的“更简单的 Docker 安装”,来根据不同的操作系统,完成相关的配置,这里就不多做赘述了。 ### Dify 的安装使用 关于 Dify 的安装和使用,在[之前 Dify 相关的文章](https://soulteary.com/tags/dify.html)中有提到过,所以就不再赘述。 完成安装之后,在 Dify 主界面中创建一个新的应用,后文中使用。 ![在 Dify 中创建一个应用](https://attachment.soulteary.com/2024/09/30/create-dify-app.jpg) 上篇文章提到过,考虑到开箱即用,我在写一个小工具,来更简单的完成 Dify 的安装和组件选配,目前完成了除前端界面之外的部分,或许后面的文章里,这块会更加简单。 ### 模型下载 你可以从你喜欢的社区,来快速下载本文中使用的模型,或者替换为你觉得不错的其他模型: - 魔搭:[Qwen/Qwen2.5-7B-Instruct-GPTQ-Int4](https://modelscope.cn/models/Qwen/Qwen2.5-7B-Instruct-GPTQ-Int4) - HuggingFace: [Qwen/Qwen2.5-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-7B-Instruct) 如果你在国内,我建议你使用魔搭来进行下载,具体可参考《[节省时间:AI 模型靠谱下载方案汇总](https://soulteary.com/2024/01/09/summary-of-reliable-download-solutions-for-ai-models.html)》这篇文章中的方法。 ### PyTorch Docker 镜像准备 我使用的模型是下面的 PyTorch 社区镜像,因为基于这个镜像,我们将能够极大简化 VLLM 组件的安装。 ```bash docker pull pytorch/pytorch:2.4.0-cuda12.1-cudnn9-runtime ``` 当我们完成模型和 Docker 镜像下载之后,在模型所在目录,执行下面的命令,可以得到一个交互式的终端: ```bash docker run --gpus=all -it -v `pwd`/Qwen:/models/Qwen -p 8000:8000 pytorch/pytorch:2.4.0-cuda12.1-cudnn9-runtime bash ``` 执行下面的命令,来到工作目录: ```bash cd /models ``` ### VLLM 因为我们要处理大量数据,所以数据的处理效率非常关键,除了选择小模型之外,使用合理的缓存机制,能够大幅提升模型吞吐,VLLM 能够提供单卡 500 左右的吞吐,对于我们处理数据非常友好。 在进入交互式终端后,我们可以执行下面的命令,先将 Python PyPi 软件源换为国内更快的清华源: ```bash python -m pip install --upgrade pip pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple ``` 然后,执行下面的命令,快速的完成 `vllm` 的安装: ```bash pip install vllm ``` 等到 vllm 安装完毕之后,我们执行下面的命令,启动一个监听 8000 端口的兼容 OpenAI API 的 Web 服务: ```bash vllm serve Qwen/Qwen2.5-7B-Instruct-GPTQ-Int4 ``` 执行命令后,我们将看到一大堆日志,包括服务启动,模型加载等等: ```bash # vllm serve Qwen/Qwen2.5-7B-Instruct-GPTQ-Int4 INFO 09-30 06:47:30 api_server.py:526] vLLM API server version 0.6.1.dev238+ge2c6e0a82 INFO 09-30 06:47:30 api_server.py:527] args: Namespace(model_tag='Qwen/Qwen2.5-7B-Instruct-GPTQ-Int4', config='', host=None, port=8000, uvicorn_log_level='info', allow_credentials=False, allowed_origins=['*'], allowed_methods=['*'], allowed_headers=['*'], api_key=None, lora_modules=None, prompt_adapters=None, chat_template=None, response_role='assistant', ssl_keyfile=None, ssl_certfile=None, ssl_ca_certs=None, ssl_cert_reqs=0, root_path=None, middleware=[], return_tokens_as_token_ids=False, disable_frontend_multiprocessing=False, enable_auto_tool_choice=False, tool_call_parser=None, model='Qwen/Qwen2.5-7B-Instruct-GPTQ-Int4', tokenizer=None, skip_tokenizer_init=False, revision=None, code_revision=None, tokenizer_revision=None, tokenizer_mode='auto', trust_remote_code=False, download_dir=None, load_format='auto', config_format='auto', dtype='auto', kv_cache_dtype='auto', quantization_param_path=None, max_model_len=None, guided_decoding_backend='outlines', distributed_executor_backend=None, worker_use_ray=False, pipeline_parallel_size=1, tensor_parallel_size=1, max_parallel_loading_workers=None, ray_workers_use_nsight=False, block_size=16, enable_prefix_caching=False, disable_sliding_window=False, use_v2_block_manager=False, num_lookahead_slots=0, seed=0, swap_space=4, cpu_offload_gb=0, gpu_memory_utilization=0.9, num_gpu_blocks_override=None, max_num_batched_tokens=None, max_num_seqs=256, max_logprobs=20, disable_log_stats=False, quantization=None, rope_scaling=None, rope_theta=None, enforce_eager=False, max_context_len_to_capture=None, max_seq_len_to_capture=8192, disable_custom_all_reduce=False, tokenizer_pool_size=0, tokenizer_pool_type='ray', tokenizer_pool_extra_config=None, limit_mm_per_prompt=None, mm_processor_kwargs=None, enable_lora=False, max_loras=1, max_lora_rank=16, lora_extra_vocab_size=256, lora_dtype='auto', long_lora_scaling_factors=None, max_cpu_loras=None, fully_sharded_loras=False, enable_prompt_adapter=False, max_prompt_adapters=1, max_prompt_adapter_token=0, device='auto', num_scheduler_steps=1, multi_step_stream_outputs=False, scheduler_delay_factor=0.0, enable_chunked_prefill=None, speculative_model=None, speculative_model_quantization=None, num_speculative_tokens=None, speculative_draft_tensor_parallel_size=None, speculative_max_model_len=None, speculative_disable_by_batch_size=None, ngram_prompt_lookup_max=None, ngram_prompt_lookup_min=None, spec_decoding_acceptance_method='rejection_sampler', typical_acceptance_sampler_posterior_threshold=None, typical_acceptance_sampler_posterior_alpha=None, disable_logprobs_during_spec_decoding=None, model_loader_extra_config=None, ignore_patterns=[], preemption_mode=None, served_model_name=None, qlora_adapter_name_or_path=None, otlp_traces_endpoint=None, collect_detailed_traces=None, disable_async_output_proc=False, override_neuron_config=None, disable_log_requests=False, max_log_len=None, disable_fastapi_docs=False, dispatch_function=) INFO 09-30 06:47:30 api_server.py:164] Multiprocessing frontend to use ipc:///tmp/7c3171d1-993e-4f8f-a466-95f0877a42a5 for IPC Path. INFO 09-30 06:47:30 api_server.py:177] Started engine process with PID 45 INFO 09-30 06:47:30 gptq_marlin.py:107] The model is convertible to gptq_marlin during runtime. Using gptq_marlin kernel. INFO 09-30 06:47:32 gptq_marlin.py:107] The model is convertible to gptq_marlin during runtime. Using gptq_marlin kernel. INFO 09-30 06:47:32 llm_engine.py:226] Initializing an LLM engine (v0.6.1.dev238+ge2c6e0a82) with config: model='Qwen/Qwen2.5-7B-Instruct-GPTQ-Int4', speculative_config=None, tokenizer='Qwen/Qwen2.5-7B-Instruct-GPTQ-Int4', skip_tokenizer_init=False, tokenizer_mode=auto, revision=None, override_neuron_config=None, rope_scaling=None, rope_theta=None, tokenizer_revision=None, trust_remote_code=False, dtype=torch.float16, max_seq_len=32768, download_dir=None, load_format=LoadFormat.AUTO, tensor_parallel_size=1, pipeline_parallel_size=1, disable_custom_all_reduce=False, quantization=gptq_marlin, enforce_eager=False, kv_cache_dtype=auto, quantization_param_path=None, device_config=cuda, decoding_config=DecodingConfig(guided_decoding_backend='outlines'), observability_config=ObservabilityConfig(otlp_traces_endpoint=None, collect_model_forward_time=False, collect_model_execute_time=False), seed=0, served_model_name=Qwen/Qwen2.5-7B-Instruct-GPTQ-Int4, use_v2_block_manager=False, num_scheduler_steps=1, multi_step_stream_outputs=False, enable_prefix_caching=False, use_async_output_proc=True, use_cached_outputs=True, mm_processor_kwargs=None) INFO 09-30 06:47:33 model_runner.py:1014] Starting to load model Qwen/Qwen2.5-7B-Instruct-GPTQ-Int4... INFO 09-30 06:47:33 gptq_marlin.py:198] Using MarlinLinearKernel for GPTQMarlinLinearMethod Loading safetensors checkpoint shards: 0% Completed | 0/2 [00:00{ return !line.includes("热点精选") }).join("\n"); return { result: input } } ``` 然后,更新内容改写节点中的数据源变量为代码执行节点的执行结果。 ![在内容改写节点中应用新数据源](https://attachment.soulteary.com/2024/09/30/use-code-result.jpg) 最后,我们就能够得到符合预期的执行结果啦。 ![符合预期的处理结果](https://attachment.soulteary.com/2024/09/30/test-new-result.jpg) ### 其他:使用 API 构建新应用 在上文中,我们已经构建了完整的数据清洗流水线,并在 Dify 界面中进行了调试。实际生产过程中,我们会处理非常多的数据,所以需要使用 API 编程使用。 在页面的右上角,我们找到发布按钮,点击之后,选择 “访问 API”,能够打开 API 文档页面。 ![获取应用 API](https://attachment.soulteary.com/2024/09/30/get-api.jpg) 如何使用 Dify API 进行编程交互,可以参考之前的内容《[使用 Dify、Meilisearch、零一万物模型实现最简单的 RAG 应用(三):AI 电影推荐](https://soulteary.com/2024/05/20/use-dify-with-meilisearch-and-01-ai-model-services-to-create-the-simplest-rag-application-ai-movie-recommendation.html)》、《[使用字节豆包大模型在 Dify 上实现最简单的 Agent 应用(四):AI 信息检索](https://soulteary.com/2024/05/22/use-bytedance-doubao-llm-to-implement-the-simplest-ai-agent-app-on-dify-ai-information-retrieval.html)》中相关的章节。 ![Dify 文档页面](https://attachment.soulteary.com/2024/09/30/api-docs.jpg) ## 完整流水线配置 本文中搭建的 AI 流水线应用的完整配置如下,你可以通过“导入”功能,快速复现这个应用: ```yaml app: description: '' icon: 🤖 icon_background: '#FFEAD5' mode: advanced-chat name: 快来帮我洗数据!(copy) use_icon_as_answer_icon: false kind: app version: 0.1.2 workflow: conversation_variables: [] environment_variables: [] features: file_upload: image: enabled: false number_limits: 3 transfer_methods: - remote_url - local_file opening_statement: '' retriever_resource: enabled: true sensitive_word_avoidance: configs: [] enabled: false type: '' speech_to_text: enabled: false suggested_questions: [] suggested_questions_after_answer: enabled: false text_to_speech: enabled: false language: '' voice: '' graph: edges: - data: isInIteration: false sourceType: llm targetType: llm id: llm-source-1727679645155-target source: llm sourceHandle: source target: '1727679645155' targetHandle: target type: custom zIndex: 0 - data: isInIteration: false sourceType: llm targetType: answer id: 1727679645155-source-answer-target source: '1727679645155' sourceHandle: source target: answer targetHandle: target type: custom zIndex: 0 - data: isInIteration: false sourceType: start targetType: code id: start-source-1727679927101-target source: start sourceHandle: source target: '1727679927101' targetHandle: target type: custom zIndex: 0 - data: isInIteration: false sourceType: code targetType: llm id: 1727679927101-source-llm-target source: '1727679927101' sourceHandle: source target: llm targetHandle: target type: custom zIndex: 0 nodes: - data: selected: false title: 开始处理 type: start variables: - default: '' description: null hint: null label: content max_length: null options: null required: true type: paragraph variable: content - label: 内容类型 max_length: 48 options: [] required: true type: text-input variable: type height: 116 id: start position: x: 30 y: 258 positionAbsolute: x: 30 y: 258 selected: false type: custom width: 244 - data: context: enabled: false variable_selector: null memory: role_prefix: assistant: '' user: '' window: enabled: false model: completion_params: stop: [] temperature: 0.1 mode: chat name: Qwen/Qwen2.5-7B-Instruct-GPTQ-Int4 provider: openai_api_compatible prompt_template: - id: 8591e02f-6d72-480d-9b61-b39e3802bf43 role: user text: '你是资深的记者,擅长严谨客观的表达内容,对下面的内容进行内容标题改写,使用陈述句表达。 # 示例 改写前 `text 半导体需求火爆!韩国芯片库存以2009年以来最快速度减少 财联社 1小时前 ` 改写后 `text [财联社] 韩国芯片库存以2009年以来最快速度减少,半导体需求火爆 ` ## 待处理内容 `text {{#1727679927101.result#}} ` ## 处理结果 ' selected: false title: 内容改写 type: llm vision: configs: null enabled: false variable_selector: null height: 98 id: llm position: x: 638 y: 258 positionAbsolute: x: 638 y: 258 selected: false type: custom width: 244 - data: answer: '{{#1727679645155.text#}}' selected: false title: 输出结果 type: answer height: 107 id: answer position: x: 1246 y: 258 positionAbsolute: x: 1246 y: 258 selected: false type: custom width: 244 - data: context: enabled: false variable_selector: [] desc: '' model: completion_params: temperature: 0.1 mode: chat name: Qwen/Qwen2.5-7B-Instruct-GPTQ-Int4 provider: openai_api_compatible prompt_template: - id: 3411a24a-f57d-4f26-9078-67a01915df57 role: system text: '你是资深的记者,擅长严谨客观的表达内容,只输出待处理内容中和 “{{#start.type#}}“ 相关的内容。 ## 待处理内容 `text {{#llm.text#}} ` ## 处理结果 ' selected: false title: 筛选内容 type: llm variables: [] vision: enabled: false height: 98 id: '1727679645155' position: x: 942 y: 258 positionAbsolute: x: 942 y: 258 selected: false sourcePosition: right targetPosition: left type: custom width: 244 - data: code: "\nfunction main({arg1}) {\n let input = arg1;\n input = input.split(\"\ \\n\").filter((line)=>{\n return !line.includes(\"热点精选\")\n }).join(\"\ \\n\");\n return { \n result: input\n }\n}\n" code_language: javascript desc: '' outputs: result: children: null type: string selected: false title: 处理输入内容 type: code variables: - value_selector: - start - content variable: arg1 height: 54 id: '1727679927101' position: x: 334 y: 258 positionAbsolute: x: 334 y: 258 selected: true sourcePosition: right targetPosition: left type: custom width: 244 viewport: x: -158.82992408947268 y: 90.16096026215655 zoom: 0.961545363400739 ``` ## 最后 这篇文章就先写到这里啦,祝大家国庆假期愉快。 --EOF