下载ollama
https://www.ollama.com/
全部下一步,下一步即可。
验证安装:
CMD输入 ollama --version
返回 ollama version is x.x.x
说明安装成功
ollama run gemma4:e2b
以上是脚本安装,其实ollama提供了完整的图形界面,一看就会。
只是在图形界面里面没有你需要的模型的时侯才需要用到脚本安装。
*在图形化界面中,在设置里面,可以看到模型的目录。
在这里可以修改模型目录,然后把这个目录下的blobs和manifests两个文件夹都剪切过去就可以了。
接下来安装web访问
$env:OLLAMA_ORIGINS="*"
$env:OLLAMA_HOST="0.0.0.0"
ollama serve
手搓一个网页,然后在局域网内就可以访问了
chat.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ollama 聊天</title>
<style>
body{max-width:800px;margin:20px auto;font-family:Arial;background:#f5f7fa;padding:0 10px}
#chat{height:550px;overflow-y:auto;background:white;padding:20px;border-radius:12px;margin-bottom:10px}
.msg{margin:10px 0;padding:12px 16px;border-radius:10px;max-width:80%;line-height:1.4;display:flex}
.user{background:#0077ff;color:white;margin-left:auto}
.bot{background:#e9ecef;color:#222;margin-right:auto}
#input{display:flex;gap:8px}
#text{flex:1;padding:12px;border-radius:8px;border:1px solid #ddd}
button{padding:12px 20px;background:#0077ff;color:white;border:none;border-radius:8px;cursor:pointer}
</style>
</head>
<body>
<div id="chat"></div>
<div id="input">
<textarea id="text" placeholder="输入消息..."></textarea>
<button onclick="send()">发送</button>
</div>
<script>
let messages = [];
const chatBox = document.getElementById("chat");
function addMessage(text, isUser) {
const div = document.createElement("div");
div.className = "msg " + (isUser ? "user" : "bot");
div.innerText = text;
chatBox.appendChild(div);
chatBox.scrollTop = chatBox.scrollHeight;
return div;
}
async function send() {
const text = document.getElementById("text").value.trim();
if (!text) return;
document.getElementById("text").value = "";
addMessage(text, true);
const loadingMsg = addMessage("思考中...", false);
try {
messages.push({ role: "user", content: text });
// 调用后端,不再直接访问 Ollama
const res = await fetch("/api/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ messages })
});
const data = await res.json();
const reply = data.message.content;
chatBox.removeChild(loadingMsg);
addMessage(reply, false);
messages.push({ role: "assistant", content: reply });
} catch (err) {
loadingMsg.innerText = "错误:" + err.message;
}
}
</script>
</body>
</html>server.py
#Python 3.12.4
#pip install flask requests
#python server.py
from flask import Flask, request, jsonify, send_file
import requests
app = Flask(__name__)
# 网页
@app.route('/')
def index():
return send_file("chat.html")
# 聊天接口(逻辑在这里)
@app.route("/api/chat", methods=["POST"])
def api_chat():
data = request.get_json()
messages = data.get("messages", [])
# 你要的这段逻辑,放在服务端了 qwen2.5:3b gemma4:e2b
resp = requests.post(
"http://127.0.0.1:11434/api/chat",
json={
"model": "qwen2.5:1.5b",
"messages": messages,
"stream": False
}
)
return jsonify(resp.json())
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000, debug=True)修改http://127.0.0.1:11434/api/chat指向自己的ollama所在地址
把chat.html和server.py放在同一个文件夹中,运行python server.py就能在同一局域网内通过手机访问了。
以上内容如果对你有用,请收藏。


评论列表: