大模型

 

模型

安装

curl -fsSL https://ollama.com/install.sh | sh

运行

ollama run llama3:8b

交互

from flask import Flask, render_template, request, jsonify
import requests

app = Flask(__name__)

# Ollama API 地址
OLLAMA_API_URL = "http://localhost:11434/api/generate"

@app.route('/')
def index():
    return render_template('index.html')  # 显示主页面

@app.route('/ask', methods=['POST'])
def ask():
    user_input = request.form['user_input']

    # 发送到 Ollama API
    response = requests.post(OLLAMA_API_URL, json={
        "model": "llama3:8b",
        "prompt": user_input,
        "stream": False
    })

    # 返回模型的响应
    if response.status_code == 200:
        data = response.json()
        return jsonify({"response": data["response"]})
    else:
        return jsonify({"response": "Sorry, something went wrong!"}), 500

if __name__ == '__main__':
    app.run(debug=True)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ollama Web Chat</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
        }
        #chat-box {
            border: 1px solid #ccc;
            padding: 10px;
            height: 300px;
            overflow-y: scroll;
        }
        #user-input {
            width: 100%;
            padding: 10px;
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <h1>与 Ollama 对话</h1>
    <div id="chat-box"></div>
    <textarea id="user-input" rows="4" placeholder="请输入您的问题..."></textarea>
    <button id="send-btn">发送</button>

    <script>
        document.getElementById('send-btn').addEventListener('click', function() {
            var userInput = document.getElementById('user-input').value;
            if (userInput.trim() === "") {
                alert("请输入问题!");
                return;
            }

            // 显示用户输入
            document.getElementById('chat-box').innerHTML += `<div><strong>你: </strong>${userInput}</div>`;
            document.getElementById('user-input').value = ''; // 清空输入框

            // 发送请求到 Flask 后端
            fetch('/ask', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: 'user_input=' + encodeURIComponent(userInput)
            })
            .then(response => response.json())
            .then(data => {
                // 显示模型回复
                document.getElementById('chat-box').innerHTML += `<div><strong>Ollama: </strong>${data.response}</div>`;
                document.getElementById('chat-box').scrollTop = document.getElementById('chat-box').scrollHeight; // 滚动到底部
            })
            .catch(error => {
                document.getElementById('chat-box').innerHTML += `<div><strong>Ollama: </strong>出错了, 稍后再试!</div>`;
            });
        });
    </script>
</body>
</html>