Skip to main content

vLLM

vLLM 是一个本地运行的代理和推理服务器,提供了一个与 OpenAI 兼容的 API。由于它同时执行代理和推理的功能,您无需安装额外的推理服务器。

注意:vLLM 不支持 OpenAI 的函数调用(可与 AutoGen 一起使用)。然而,它正在开发中,可能在您阅读本文时已经可用。

运行此堆栈需要安装以下内容:

  1. AutoGen(安装说明
  2. vLLM

注意:我们建议为您的堆栈使用虚拟环境,参考这篇文章进行操作。

安装 vLLM

在您的终端中执行以下命令:

pip install vllm

选择模型

当您运行服务器时,vLLM 将下载新的模型。

这些模型来自于Hugging Face,一个经过筛选的文本生成模型列表在这里,vLLM 还有一个常用模型列表。请使用完整的模型名称,例如 mistralai/Mistral-7B-Instruct-v0.2

聊天模板

vLLM 使用预定义的聊天模板,除非模型在 Hugging Face 的配置文件中定义了聊天模板。如果聊天模板不允许使用 'role' : 'system' 的消息(在 AutoGen 中使用),这可能会导致问题。

因此,我们将为我们正在使用的 Mistral.AI Mistral 7B 模型创建一个聊天模板,允许 'user'、'assistant' 和 'system' 这些角色。

创建一个名为 autogenmistraltemplate.jinja 的文件,内容如下:

{{ bos_token }}
{% for message in messages %}
{% if ((message['role'] == 'user' or message['role'] == 'system') != (loop.index0 % 2 == 0)) %}
{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}
{% endif %}

{% if (message['role'] == 'user' or message['role'] == 'system') %}
{{ '[INST] ' + message['content'] + ' [/INST]' }}
{% elif message['role'] == 'assistant' %}
{{ message['content'] + eos_token}}
{% else %}
{{ raise_exception('Only system, user and assistant roles are supported!') }}
{% endif %}
{% endfor %}
warning

聊天模板是特定于模型/模型系列的。这里显示的示例适用于基于 Mistral 的模型,如 Mistral 7B 和 Mixtral 8x7B。

vLLM 有许多示例模板,适用于可以作为聊天模板起点的模型。只需记住,模板可能需要进行调整以支持 'system' 角色的消息。

运行 vLLM 代理服务器

要使用选择的模型和我们的聊天模板运行 vLLM,请在终端中执行以下命令:

python -m vllm.entrypoints.openai.api_server --model mistralai/Mistral-7B-Instruct-v0.2 --chat-template autogenmistraltemplate.jinja

这是一个使用 Python 命令行运行的命令。它调用了一个名为 vllm.entrypoints.openai.api_server 的模块,并传入了一些参数。其中 --model 参数指定了使用的模型为 mistralai/Mistral-7B-Instruct-v0.2--chat-template 参数指定了使用的聊天模板为 autogenmistraltemplate.jinja。 默认情况下,vLLM将在 'http://0.0.0.0:8000' 上运行。

在 AutoGen 中使用 vLLM

现在我们有了 vLLM 代理服务器的 URL,您可以像使用 OpenAI 或基于云的代理服务器一样在 AutoGen 中使用它。

由于您正在本地运行此代理服务器,因此不需要 API 密钥。由于 api_key 是 AutoGen 配置中的必填字段,我们在其中放入了一个虚拟值,如下面的示例所示。

尽管在运行 vLLM 命令时我们已经指定了模型,但我们仍然必须将其放入 vLLM 的 model 值中。

from autogen import UserProxyAgent, ConversableAgent

local_llm_config={
"config_list": [
{
"model": "mistralai/Mistral-7B-Instruct-v0.2", # 与 vLLM 命令中的相同
"api_key": "NotRequired", # 不需要
"base_url": "http://0.0.0.0:8000/v1" # 您的 vLLM URL,加上 '/v1'
}
],
"cache_seed": None # 关闭缓存,用于测试不同的模型
}

# 创建使用 LLM 的代理
assistant = ConversableAgent("agent", llm_config=local_llm_config,system_message="")

# 创建代表对话中用户的代理
user_proxy = UserProxyAgent("user", code_execution_config=False,system_message="")

# 让助手开始对话。当用户输入 exit 时对话将结束。
assistant.initiate_chat(user_proxy, message="How can I help you today?")

输出:

agent (to user):

How can I help you today?

--------------------------------------------------------------------------------
Provide feedback to agent. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: Why is the sky blue?
user (to agent):

Why is the sky blue?

--------------------------------------------------------------------------------

>>>>>>>> USING AUTO REPLY...
agent (to user):


The sky appears blue due to a phenomenon called Rayleigh scattering. As sunlight reaches Earth's atmosphere, it interacts with molecules and particles in the air, causing the scattering of light. Blue light has a shorter wavelength and gets scattered more easily than other colors, which is why the sky appears blue during a clear day.

However, during sunrise and sunset, the sky can appear red, orange, or purple due to a different type of scattering called scattering by dust, pollutants, and water droplets, which scatter longer wavelengths of light more effectively.

--------------------------------------------------------------------------------
Provide feedback to agent. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: and why does it turn red?
user (to agent):

and why does it turn red?

--------------------------------------------------------------------------------

>>>>>>>> USING AUTO REPLY...
agent (to user):
日出和日落时,太阳光线在天空中的角度较低,它们必须穿过更多的地球大气层才能到达观察者。这种额外的距离导致阳光更多地发生散射,而散射更偏好散射较长波长(红色、橙色和黄色)而不是较短波长(蓝色和绿色)。

地球大气层对阳光的散射导致日出和日落时红色、橙色和黄色在天空中更为常见,从而呈现出美丽的色彩,通常被称为日出或日落。

随着太阳继续下山,天空可以过渡到各种紫色、粉红色,最终变为深蓝色或黑色,因为可用的阳光继续减少,较长波长逐渐散射效果较差。