欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 用api的方式调用本地下载好的大模型(以llama为例,不是ollama!!!)

用api的方式调用本地下载好的大模型(以llama为例,不是ollama!!!)

2025/4/27 4:03:08 来源:https://blog.csdn.net/YiHanXii/article/details/147440931  浏览:    关键词:用api的方式调用本地下载好的大模型(以llama为例,不是ollama!!!)

目录

      • 1、创建虚拟环境
      • 2、激活虚拟环境
      • 3、安装相关库
      • 4、编写脚本(test.py)
      • 调用脚本
      • 5、bash中测试通信
      • 完美结果

1、创建虚拟环境

conda create -n myenv python=3.12 -y

2、激活虚拟环境

conda activate myenv

3、安装相关库

pip install vllm fastapi uvicorn

4、编写脚本(test.py)

from fastapi import FastAPI, Request
from vllm import LLM, SamplingParams
import uvicorn# Initialize FastAPI
app = FastAPI()# Load the model once at startup with adjusted parameters
model_path = "/home/zhengyihan/.cache/modelscope/hub/LLM-Research/Llama-3___2-3B-Instruct"
llm = LLM(model=model_path,max_model_len=8192,  # Reduced from defaultgpu_memory_utilization=0.95  # Increase memory allocation
)@app.post("/generate")
async def generate(request: Request):# Parse the request bodybody = await request.json()# Extract parameters from the requestprompt = body.get("prompt", "")temperature = body.get("temperature", 0.7)top_p = body.get("top_p", 0.95)max_tokens = body.get("max_tokens", 512)  # Reduced default# Set up sampling parameterssampling_params = SamplingParams(temperature=temperature,top_p=top_p,max_tokens=max_tokens)# Generate the responseoutputs = llm.generate(prompt, sampling_params)# Extract the generated textresults = []for output in outputs:results.append({"generated_text": output.outputs[0].text,"prompt": output.prompt})return {"results": results}if __name__ == "__main__":uvicorn.run(app, host="0.0.0.0", port=8000)

调用脚本

python test.py

5、bash中测试通信

curl -X POST http://localhost:8000/generate -H "Content-Type: application/json" -d '{"prompt": "Once upon a time"}'

完美结果

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词