百度大模型开源,俩条命令、本地启动

百度大模型开源

本地启动手册

安装依赖:

bash 复制代码
python -m pip install paddlepaddle-gpu==3.1.0 -i https://www.paddlepaddle.org.cn/packages/stable/cu126/

python -m pip install fastdeploy-gpu -i https://www.paddlepaddle.org.cn/packages/stable/fastdeploy-gpu-80_90/ --extra-index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple

apt install libgomp1

启动 API

bash 复制代码
python -m fastdeploy.entrypoints.openai.api_server \
       --model baidu/ERNIE-4.5-0.3B-Paddle \
       --port 8180 \
       --metrics-port 8181 \
       --engine-worker-queue-port 8182 \
       --max-model-len 32768 \
       --max-num-seqs 32

大概等待10分钟左右

测试 Python 代码

py 复制代码
import requests
import json

url = "http://127.0.0.1:8180/v1/chat/completions"
headers = {"Content-Type": "application/json"}

data = {
    "model": "baidu/ERNIE-4.5-0.3B-PT",
    "messages": [
        {"role": "user", "content": "我是仕宇2050,你帮我写一个Python函数判断字符串是否为回文串"}
    ],
    "temperature": 0.7
}

response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.json()["choices"][0]["message"]["content"])

模型开源地址:https://ai.gitcode.com/theme/1939325484087291906

视频演示: 全网同名 #JavaPub #仕宇2050

循环对话的python代码

py 复制代码
import requests
import json

def chat():
    url = "http://127.0.0.1:8180/v1/chat/completions"
    headers = {
        "Content-Type": "application/json"
    }
    model = "baidu/ERNIE-4.5-0.3B-PT"
    
    # 保存对话历史
    messages = []

    print("开始对话吧!(输入 'exit' 或 'quit' 退出)")
    while True:
        user_input = input("\n你:")
        if user_input.lower() in {"exit", "quit"}:
            print("结束对话。")
            break

        # 添加用户消息到对话历史
        messages.append({"role": "user", "content": user_input})

        # 构建请求体
        data = {
            "model": model,
            "messages": messages
        }

        try:
            response = requests.post(url, headers=headers, data=json.dumps(data))
            response.raise_for_status()
            result = response.json()

            if "choices" in result and len(result["choices"]) > 0:
                ai_message = result["choices"][0]["message"]["content"]
                print("AI:", ai_message)

                # 将AI的回复加入对话历史
                messages.append({"role": "assistant", "content": ai_message})
            else:
                print("AI 没有返回内容。")

        except requests.exceptions.RequestException as e:
            print(f"请求错误: {e}")
        except json.JSONDecodeError:
            print(f"JSON解析错误,响应内容: {response.text}")
        except Exception as e:
            print(f"发生错误: {e}")

if __name__ == "__main__":
    chat()
相关推荐
下页、再停留1 天前
【PHP】接入百度AI开放平台人脸识别API,实现人脸对比
人工智能·百度·php
京东零售技术1 天前
京东零售在智能供应链领域的前沿探索与技术实践
人工智能·百度·零售
高hongyuan5 天前
搜索引擎高级搜索指令大全(Google、百度等浏览器通用)
搜索引擎·百度·google
趣味科技v6 天前
WAIC直击:百度慧播星发布新一代数字人技术NOVA
百度
愿你天黑有灯下雨有伞6 天前
告别复杂配置!Spring Boot优雅集成百度OCR的终极方案
spring boot·百度·ocr
SEO_juper7 天前
企业级 AI 工具选型报告:9 个技术平台的 ROI 对比与部署策略
人工智能·搜索引擎·百度·llm·工具·geo·数字营销
市象7 天前
独家|百度副总裁尚国斌即将离职,此前统筹百度地图;行业搜索及智能体业务总经理谢天转岗IDG
百度
百度SEO专员7 天前
百度快排技术分析的核心要素
百度
DO_Community8 天前
DigitalOcean 一键模型部署,新增支持百度开源大模型ERNIE 4.5 21B
人工智能·深度学习·百度·自然语言处理·开源