首页 > 其他分享 >Docker下使用llama.cpp部署带Function calling和Json Mode功能的Mistral 7B模型

Docker下使用llama.cpp部署带Function calling和Json Mode功能的Mistral 7B模型

时间:2024-08-27 19:37:07浏览次数:5  
标签:Function https 7B -- calling llama nvidia cpp com

Docker下使用llama.cpp部署带Function calling和Json Mode功能的Mistral 7B模型

说明:

下载GGUF模型

使用HuggingFace的镜像 https://hf-mirror.com/

方式一:

pip install -U huggingface_hub
export HF_ENDPOINT=https://hf-mirror.com

huggingface-cli download --resume-download MaziyarPanahi/Mistral-7B-Instruct-v0.3-GGUF --include *Q4_K_M.gguf

方式二(推荐):

sudo apt update
sudo apt install aria2 git-lfs

wget https://hf-mirror.com/hfd/hfd.sh

chmod a+x hfd.sh

./hfd.sh MaziyarPanahi/Mistral-7B-Instruct-v0.3-GGUF --include *Q4_K_M.gguf --tool aria2c -x 16 --local-dir MaziyarPanahi--Mistral-7B-Instruct-v0.3-GGUF

使用Docker部署服务

构建之前需要先安装NVIDIA Container Toolkit

安装NVIDIA Container Toolkit

准备:

curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
  && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
    sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
    sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list

安装:

sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit

配置docker

sudo nvidia-ctk runtime configure --runtime=docker

NVIDIA Container Toolkit 安装的更多信息请参考官方文档: https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html

构建镜像

使用官方的Dockerfile: https://github.com/abetlen/llama-cpp-python/blob/main/docker/cuda_simple/Dockerfile

ARG CUDA_IMAGE="12.2.0-devel-ubuntu22.04"
FROM nvidia/cuda:${CUDA_IMAGE}

# We need to set the host to 0.0.0.0 to allow outside access
ENV HOST 0.0.0.0

RUN apt-get update && apt-get upgrade -y \
    && apt-get install -y git build-essential \
    python3 python3-pip gcc wget \
    ocl-icd-opencl-dev opencl-headers clinfo \
    libclblast-dev libopenblas-dev \
    && mkdir -p /etc/OpenCL/vendors && echo "libnvidia-opencl.so.1" > /etc/OpenCL/vendors/nvidia.icd

COPY . .

# setting build related env vars
ENV CUDA_DOCKER_ARCH=all
ENV GGML_CUDA=1

# Install depencencies
RUN python3 -m pip install --upgrade pip pytest cmake scikit-build setuptools fastapi uvicorn sse-starlette pydantic-settings starlette-context

# Install llama-cpp-python (build with cuda)
RUN CMAKE_ARGS="-DGGML_CUDA=on" pip install llama-cpp-python

# Run the server
CMD python3 -m llama_cpp.server

因为我本地安装的CUDA版本为12.2,所以将base镜像改为nvidia/cuda:12.2.0-devel-ubuntu22.04

docker build -t llama_cpp_cuda_simple .

启动服务

docker run --gpus=all --cap-add SYS_RESOURCE -e USE_MLOCK=0 -e model=/models/downloaded/MaziyarPanahi--Mistral-7B-Instruct-v0.3-GGUF/Mistral-7B-Instruct-v0.3.Q4_K_M.gguf -e n_gpu_layers=-1 -e chat_format=chatml-function-calling -v /mnt/d/16-LLM-Cache/llama_cpp_gnuf:/models -p 8000:8000 -t llama_cpp_cuda_simple

其中:

启动完成后,在浏览器打开 http://localhost:8000/docs 查看API文档

调用测试

Function Calling

curl --location 'http://localhost:8000/v1/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-xxxxxxxxxxxxxxxxxxxxxx' \
--data '{
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant.\nYou can call functions with appropriate input when necessary"
    },
    {
      "role": "user",
      "content": "What'\''s the weather like in Mauritius?"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given latitude and longitude",
        "parameters": {
          "type": "object",
          "properties": {
            "latitude": {
              "type": "number",
              "description": "The latitude of a place"
            },
            "longitude": {
              "type": "number",
              "description": "The longitude of a place"
            }
          },
          "required": ["latitude", "longitude"]
        }
      }
    }
  ],
  "tool_choice": "auto"
}'

输出:

{
    "id": "chatcmpl-50c8e261-2b1a-4285-a6ee-e18a07ce92d9",
    "object": "chat.completion",
    "created": 1724757544,
    "model": "gpt-3.5-turbo",
    "choices": [
        {
            "index": 0,
            "message": {
                "content": null,
                "tool_calls": [
                    {
                        "id": "call__0_get_current_weather_cmpl-97515c72-d214-4ed9-b183-7736199e5be1",
                        "type": "function",
                        "function": {
                            "name": "get_current_weather",
                            "arguments": "{\"latitude\": -20.375, \"longitude\": 57.568} "
                        }
                    }
                ],
                "role": "assistant",
                "function_call": {
                    "name": "",
                    "arguments": "{\"latitude\": -20.375, \"longitude\": 57.568} "
                }
            },
            "logprobs": null,
            "finish_reason": "tool_calls"
        }
    ],
    "usage": {
        "prompt_tokens": 299,
        "completion_tokens": 25,
        "total_tokens": 324
    }
}

JSON Mode

curl --location "http://localhost:8000/v1/chat/completions" \
     --header 'Content-Type: application/json' \
     --header 'Accept: application/json' \
     --header "Authorization: Bearer sk-xxxxxxxxxxxxxxxxxxxxxx" \
     --data '{
    "model": "gpt-3.5-turbo",
    "messages": [
     {
        "role": "user",
        "content": "What is the best French cheese? Return the product and produce location in JSON format"
      }
    ],
    "response_format": {"type": "json_object"}
  }'

输出:

{
    "id": "chatcmpl-bbfecfc5-2ea9-4052-93b2-08f1733e8219",
    "object": "chat.completion",
    "created": 1724757752,
    "model": "gpt-3.5-turbo",
    "choices": [
        {
            "index": 0,
            "message": {
                "content": "{\n  \"product\": \"Roquefort\",\n  \"produce_location\": \"France, South of France\"\n}\n  \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t",
                "role": "assistant"
            },
            "logprobs": null,
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 44,
        "completion_tokens": 50,
        "total_tokens": 94
    }
}

使用以下代码将content部分写入到文本:

text = "{\n  \"product\": \"Roquefort\",\n  \"location\": \"France, South of France\"\n}\n \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"

with open('resp.txt', 'w') as f:
    f.write(text)

可以看到内容:

{
  "product": "Roquefort",
  "location": "France, South of France"
}
 																			

标签:Function,https,7B,--,calling,llama,nvidia,cpp,com
From: https://www.cnblogs.com/shizidushu/p/18383368

相关文章

  • Azure Function host.json 设置不起作用
    解决方案:https://github.com/Azure/azure-functions-servicebus-extension/issues/81#issuecomment-1139175220publicclassStartup:FunctionsStartup{publicoverridevoidConfigure(IFunctionsHostBuilderbuilder){vartempSer......
  • 062 Finishing the Core Functionality
    示例index.html<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metaname="viewport"content="width=device-width,initial-scale=1.0"/><title>VueBa......
  • 题解:SP22382 ETFD - Euler Totient Function Depth
    题目链接:link,点击这里喵。前置知识:【模板】线性筛素数,欧拉函数,点击这里喵。题意简述:给定整数$l,r,k$,求出$[l,r]$中有多少个整数不断对自己取欧拉函数刚好$k$次结果为$1$。思路:看眼数据范围,$10^{10}$的量级显然不容我们每次暴力,故考虑预处理$\varphi(i),can(i,k),su......
  • [C++ Error] f0202.cpp(13): E2268 Call to undefined function 'system'
    system('pause');解决方法,修改代码:system("pause");[C++Error]f0202.cpp(13):E2268Calltoundefinedfunction'system'错误解释:这个错误表明您在C++代码中尝试调用了一个未定义的函数system。system函数是C标准库中的函数,用于执行一个字符串中给出的命令。在C++中,......
  • 微调Qwen2:7B模型,加入未知信息语料
    对于QWen2这样的模型,在微调的时候,语料的投喂格式满足ChatML这样的格式!!!OpenAI-ChatML下面是ChatML格式的介绍:https://github.com/openai/openai-python/blob/release-v0.28.1/chatml.md传统上,GPT模型使用非结构化文本。ChatGPT模型需要一种结构化格式,称为ChatMarkupL......
  • [AGC067B] Modifications
    MyBlogs[AGC067B]Modifications谔谔,做过类似的题还是不会啊啊啊。首先考虑给定一个\(a\)序列如何进行判定。倒着做这个覆盖的过程,每次可以看成是,如果\([l_i,r_i]\)剩下的点的颜色都相同,则可以把\([l_i,r_i]\)删掉。如果最后能删空就是合法的。区间DP判定这个过程:\(f......
  • apt update 报错:Could not handshake: Error in the pull function. [IP: 185.199.108
    sudoaptupdate报错:错误:12https://nvidia.github.io/nvidia-container-runtime/stable/ubuntu18.04/amd64ReleaseCouldnothandshake:Errorinthepullfunction.[IP:185.199.108.153443]错误:13https://nvidia.github.io/nvidia-docker/ubuntu18.04/amd64Re......
  • [CF1447B]Numbers Box
    [CF1447B]NumbersBox题目传送门一道思路十分好想的水题贪心题。题目大意:有\(t\)次提问,每次提问输入两个数\(m,n\)表示行和列,输入\(a_{ij}\)表示第\(i\)行\(j\)列中的数,每次可将两个相邻的数乘\({-1}\)使最终矩阵中所有数相加的和最大。思路:要使矩阵中所有......
  • 开源模型应用落地-qwen2-7b-instruct-LoRA微调-LLaMA-Factory-单机单卡-V100(八)
    一、前言  本篇文章将使用LLaMA-Factory去高效微调(命令和界面方式)QWen2系列模型,通过阅读本文,您将能够更好地掌握这些关键技术,理解其中的关键技术要点,并应用于自己的项目中。二、术语介绍2.1.LoRA微调  LoRA(Low-RankAdaptation)用于微调大型语言模型(LLM)。......
  • YC327B [ 20240821 CQYC NOIP 模拟赛 T2 ] 括号串(bracket)
    题意给定\(S\in\{(,),?\}\)。定义深度为括号嵌套的子序列的最大长度除以\(2\)。求出将\(?\)替换为括号的所有括号串的深度之和,对\(998244353\)取模。\(n\le10^6\)。Sol考虑如何把每次贡献只计算一次。不难想到在括号的中心点计算。可以发现,若当前左右括号......