vLLM - 基本使用


一、安装

conda create -n e39 python=3.9

conda activate e39

pip install vllm

二、服务

1、vllm serve

vllm serve Qwen/Qwen2.5-1.5B-Instruct

用了 20G


2、Python 服务

from vllm import LLM, SamplingParams  

# llm = LLM(model="facebook/opt-125m")
llm = LLM(model="Qwen/Qwen2.5-1.5B-Instruct")

prompts = [
    "Hello, my name is",
    "The president of the United States is",
    "The capital of France is",
    "The future of AI is",
]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)  
outputs = llm.generate(prompts, sampling_params)

for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

三、访问

curl - 查看所有模型

curl http://10.0.1.23:8000/v1/models

{
	"object": "list",
	"data": [{
		"id": "Qwen/Qwen2.5-1.5B-Instruct",
		"object": "model",
		"created": 1741263998,
		"owned_by": "vllm",
		"root": "Qwen/Qwen2.5-1.5B-Instruct",
		"parent": null,
		"max_model_len": 32768,
		"permission": [{
			"id": "modelperm-79dc42186c2f46d085c3c98615f71e47",
			"object": "model_permission",
			"created": 1741263998,
			"allow_create_engine": false,
			"allow_sampling": true,
			"allow_logprobs": true,
			"allow_search_indices": false,
			"allow_view": true,
			"allow_fine_tuning": false,
			"organization": "*",
			"group": null,
			"is_blocking": false
		}]
	}]
}

curl - completions

curl http://10.0.1.23:8000/v1/completions \
    -H "Content-Type: application/json" \
    -d '{
        "model": "Qwen/Qwen2.5-1.5B-Instruct",
        "prompt": "San Francisco is a",
        "max_tokens": 7,
        "temperature": 0
    }'

{
	"id": "cmpl-3fba4fc307e04e3f8d656049437be215",
	"object": "text_completion",
	"created": 1741264121,
	"model": "Qwen/Qwen2.5-1.5B-Instruct",
	"choices": [{
		"index": 0,
		"text": " city in the state of California,",
		"logprobs": null,
		"finish_reason": "length",
		"stop_reason": null,
		"prompt_logprobs": null
	}],
	"usage": {
		"prompt_tokens": 4,
		"total_tokens": 11,
		"completion_tokens": 7,
		"prompt_tokens_details": null
	}
}

curl - chat/completions

curl http://10.0.1.23:8000/v1/chat/completions \
    -H "Content-Type: application/json" \
    -d '{
        "model": "Qwen/Qwen2.5-1.5B-Instruct",
        "messages": [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Who won the world series in 2020?"}
        ]
    }'

{
	"id": "chatcmpl-eaa9d73518ae4900ac3ca03445b94856",
	"object": "chat.completion",
	"created": 1741264227,
	"model": "Qwen/Qwen2.5-1.5B-Instruct",
	"choices": [{
		"index": 0,
		"message": {
			"role": "assistant",
			"reasoning_content": null,
			"content": "The World Series in 2020 was played between the New York Yankees and the Boston Red Sox. The Yankees won in seven games, defeating the Red Sox across the regular and季后赛 (playoffs) seasons.",
			"tool_calls": []
		},
		"logprobs": null,
		"finish_reason": "stop",
		"stop_reason": null
	}],
	"usage": {
		"prompt_tokens": 31,
		"total_tokens": 76,
		"completion_tokens": 45,
		"prompt_tokens_details": null
	},
	"prompt_logprobs": null
}

Python - completions

from openai import OpenAI

# Modify OpenAI's API key and API base to use vLLM's API server.
openai_api_key = "EMPTY"
openai_api_base = "http://10.0.1.23:8000/v1"
client = OpenAI(
    api_key=openai_api_key,
    base_url=openai_api_base,
)
completion = client.completions.create(model="Qwen/Qwen2.5-1.5B-Instruct",
                                      prompt="San Francisco is a")
print("Completion result:", completion)

Completion result: Completion(
  id='cmpl-accdc1011da548a08ac8f297146e7791', 
  choices=[
    CompletionChoice(
      finish_reason='length', index=0, logprobs=None, 
      text=' great place with a lot of rich history. But in recent years, the people', 
      stop_reason=None, prompt_logprobs=None)
  ], 
  created=1741264345, model='Qwen/Qwen2.5-1.5B-Instruct', 
  object='text_completion', system_fingerprint=None, 
  usage=CompletionUsage(
    completion_tokens=16, prompt_tokens=4, 
    total_tokens=20, completion_tokens_details=None, 
    prompt_tokens_details=None))

Python - chat.completions

from openai import OpenAI
# Set OpenAI's API key and API base to use vLLM's API server.
openai_api_key = "EMPTY"
openai_api_base = "http://10.0.1.23:8000/v1"

client = OpenAI(
    api_key=openai_api_key,
    base_url=openai_api_base,
)

chat_response = client.chat.completions.create(
    model="Qwen/Qwen2.5-1.5B-Instruct",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Tell me a joke."},
    ]
)

print("Chat response:", chat_response)

Chat response: ChatCompletion(
  id='chatcmpl-ae641c01fc54450b9a02bba0260c445b', 
  choices=[Choice(
    finish_reason='stop', index=0, logprobs=None, 
    message=ChatCompletionMessage(
      content='Why could the statue of liberty sleep 8 hours a day?\n\nBecause she had a full moon each month.', 
      refusal=None, role='assistant', audio=None, function_call=None, tool_calls=[], reasoning_content=None), stop_reason=None)], 
  created=1741264394, model='Qwen/Qwen2.5-1.5B-Instruct', 
  object='chat.completion', service_tier=None, system_fingerprint=None, 
  usage=CompletionUsage(
    completion_tokens=23, prompt_tokens=24, total_tokens=47, 
    completion_tokens_details=None, prompt_tokens_details=None), prompt_logprobs=None)

2025-03-06(四)

### 关于DVWA靶场搭建过程中出现404错误的解决方案 在DVWA靶场搭建的过程中,如果遇到404错误,通常是因为服务器无法找到目标页面或资源。以下是可能的原因及其解决方法: #### 1. **检查文件路径** 确保DVWA解压后的文件夹已正确放置到PHPStudy的网页根目录下。对于PHPStudy,默认的网页根目录通常是`C:\phpstudy_pro\WWW`[^2]。确认DVWA文件夹名称是否与访问URL一致。例如,如果文件夹名为`DVWA-master`,则应通过`http://127.0.0.1/DVWA-master`访问。 #### 2. **验证Apache服务状态** 确保Apache服务已经成功启动。可以通过PHPStudy控制面板查看并启动Apache服务。如果服务未正常运行,则可能导致请求的目标页面不可达[^4]。 #### 3. **配置Apache虚拟主机** 有时需要手动调整Apache的配置文件来支持自定义路径。打开`httpd.conf`文件(位于`C:\phpstudy_pro\Extensions\Apache\conf`),查找以下两行内容并取消注释: ```apache LoadModule rewrite_module modules/mod_rewrite.so <Directory "C:/phpstudy_pro/WWW"> Options Indexes FollowSymLinks Includes ExecCGI AllowOverride All </Directory> ``` 保存更改后重启Apache服务[^4]。 #### 4. **检查权限设置** 确保DVWA所在的目录具有足够的读取和执行权限。可以在命令提示符中运行以下命令赋予相应权限: ```bash icacls C:\phpstudy_pro\WWW\DVWA-master /grant Everyone:F /T ``` #### 5. **浏览器缓存清理** 有时候浏览器会因为缓存问题而返回旧的结果。尝试清除浏览器缓存或者更换不同的浏览器重新访问网站[^3]。 #### 6. **数据库初始化** 即使能够加载初始界面,但如果某些功能模块依赖的数据表尚未创建也可能引发类似的错误表现形式之一即表现为部分链接指向不存在的内容从而触发HTTP Status Code 404响应消息;因此建议按照官方文档指引完成必要的SQL脚本导入操作以便建立起完整的后台支撑结构体系[^1]。 --- ### 示例代码片段 当修改配置文件时,请注意以下示例中的语法准确性: ```php <?php $_DVWA['db_server'] = '127.0.0.1'; $_DVWA['db_user'] = 'root'; $_DVWA['db_password'] = ''; // 如果设置了密码,请填写实际值 ?> ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值