最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python使用django調(diào)用deepseek api搭建ai網(wǎng)站

 更新時間:2025年02月26日 10:23:50   作者:陳王卜  
DeepSeek是一家人工智能公司,致力于通過創(chuàng)新的技術(shù)和算法,推動人工智能領(lǐng)域的發(fā)展,本文給大家介紹了python使用django調(diào)用deepseek api搭建ai網(wǎng)站,文中有相關(guān)的代碼示例供大家參考,感興趣的小伙伴跟著小編一起來看看吧

一、deepseek簡介

DeepSeek是一家人工智能公司,專注于開發(fā)先進(jìn)的人工智能模型和技術(shù)。以下是關(guān)于DeepSeek的一些詳細(xì)介紹:

1.公司背景

DeepSeek由杭州深度求索人工智能基礎(chǔ)技術(shù)研究有限公司開發(fā),致力于通過創(chuàng)新的技術(shù)和算法,推動人工智能領(lǐng)域的發(fā)展。

2.技術(shù)與模型

  • DeepSeek-V3:這是DeepSeek開發(fā)的一個大型語言模型,具有超過600B的參數(shù),在多項(xiàng)性能指標(biāo)上與國際頂尖模型相當(dāng)。
  • DeepSeek-R1:這是DeepSeek的第一代推理模型,通過大規(guī)模強(qiáng)化學(xué)習(xí)(RL)進(jìn)行訓(xùn)練,展示出了在推理任務(wù)上的優(yōu)異性能。
  • DeepSeek-R1-Distill:這些是從DeepSeek-R1中蒸餾出的小模型,具有更好的性能和效率,適合在資源受限的環(huán)境中使用。

3.應(yīng)用領(lǐng)域

  • 自然語言處理:DeepSeek的模型在文本生成、知識問答、推理等任務(wù)中表現(xiàn)出色,能夠?yàn)橛脩籼峁└哔|(zhì)量的語言交互服務(wù)。
  • 智能助手:DeepSeek開發(fā)了AI智能助手,可用于搜索、寫作、閱讀、解題、翻譯等多種任務(wù),幫助用戶提高效率。

4.優(yōu)勢與特點(diǎn)

  • 成本優(yōu)勢:DeepSeek的模型訓(xùn)練成本低,調(diào)用接口成本也較低,具有較高的性價比。
  • 中文處理能力強(qiáng):對中文語法、成語、文化背景理解更深入,在中文文本生成、摘要、情感分析等任務(wù)中表現(xiàn)自然。
  • 開源優(yōu)勢:DeepSeek-R1模型權(quán)重和技術(shù)報(bào)告開源,便于開發(fā)者二次開發(fā)和創(chuàng)新。

5.產(chǎn)品與服務(wù)

  • DeepSeek API:提供與OpenAI兼容的API,方便開發(fā)者將DeepSeek的模型集成到自己的應(yīng)用中。
  • DeepSeek App:提供AI智能助手應(yīng)用,可在App Store上下載,支持多種功能,如智能對話、搜索、寫作等。

二、獲取apikey

deepseek官方的api暫時無法充值,我使用的是阿里云的百煉平臺的deepseek v1模型,阿里云百煉平臺注冊送百萬token,可以白嫖。打開百煉控制臺,開通服務(wù),隨便選擇一個模型,點(diǎn)擊右上角的“查看我的apikey”,獲取apikey。

三、創(chuàng)建django項(xiàng)目,并startapp

使用django創(chuàng)建一個新的項(xiàng)目,python manage.py startapp chat新建app作為主要代碼文件夾。

四、編寫代碼

chat\views.py

from django.shortcuts import render
from openai import OpenAI
import os
from django.conf import settings
 
 
def get_ai_response(messages):
client = OpenAI(
api_key="xxx",//填寫apikey
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
 
 
try:
    completion = client.chat.completions.create(
        model="qwen2.5-14b-instruct-1m",
        messages=messages
    )
    return {
        'content': completion.choices[0].message.content,
        'reasoning': getattr(completion.choices[0].message, 'reasoning_content', '')
    }
except Exception as e:
    return {
        'content': f"發(fā)生錯誤:{str(e)}",
        'reasoning': ''
    }
 
def chat_view(request):
if 'messages' not in request.session:
request.session['messages'] = []
 
 
if request.method == 'POST':
    user_message = request.POST.get('message', '')
    if user_message:
        request.session['messages'].append({'role': 'user', 'content': user_message})
 
        response = get_ai_response(request.session['messages'])
 
        request.session['messages'].append({
            'role': 'assistant',
            'content': response['content'],
            'reasoning': response['reasoning']
        })
 
        request.session.modified = True
 
return render(request, 'chat.html', {
    'messages': request.session['messages']
})

要將api_key="xxx"中xxx替換為自己的apikey。

urls.py

from django.contrib import admin
from django.urls import path
from chat import views
 
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.chat_view, name='chat'),
]

前端代碼

<!DOCTYPE html>
<html>
<head>
    <title>AI對話助手(Markdown支持版)</title>
    <!-- Markdown 渲染依賴 -->
    <link rel="stylesheet"  rel="external nofollow" >
    <link rel="stylesheet"  rel="external nofollow" >
    <style>
        :root {
            --user-color: #1a73e8;
            --assistant-color: #0b8043;
        }
        body {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
            background-color: #f8f9fa;
        }
        .chat-container {
            background: white;
            border-radius: 12px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            padding: 20px;
            margin-bottom: 20px;
        }
        .message {
            margin: 15px 0;
            padding: 12px 16px;
            border-radius: 8px;
        }
        .user-message {
            background-color: #e8f0fe;
            border: 1px solid var(--user-color);
            margin-left: 30px;
        }
        .assistant-message {
            background-color: #e6f4ea;
            border: 1px solid var(--assistant-color);
            margin-right: 30px;
        }
        .role-label {
            font-weight: 500;
            margin-bottom: 8px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .role-label::before {
            content: '';
            display: inline-block;
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        .user-message .role-label::before {
            background: var(--user-color);
        }
        .assistant-message .role-label::before {
            background: var(--assistant-color);
        }
        form {
            display: flex;
            gap: 10px;
            margin-top: 20px;
        }
        input[type="text"] {
            flex: 1;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
        }
        button {
            padding: 12px 24px;
            background-color: var(--user-color);
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            transition: background 0.2s;
        }
        button:hover {
            background-color: #1557b0;
        }
        .markdown-body pre {
            padding: 16px;
            border-radius: 8px;
            overflow-x: auto;
        }
        .reasoning-box {
            margin-top: 12px;
            padding: 12px;
            background: #fff8e5;
            border-left: 4px solid #ffd700;
            border-radius: 4px;
        }
    </style>
</head>
<body>
    <div class="chat-container">
        <h1 style="color: var(--user-color); text-align: center;">AI對話助手</h1>
 
        <div class="messages">
            {% for message in messages %}
                <div class="message {% if message.role == 'user' %}user-message{% else %}assistant-message{% endif %}">
                    <div class="role-label">
                        {% if message.role == 'user' %}
                            ?? 用戶
                        {% else %}
                            ?? 助手
                        {% endif %}
                    </div>
                    <!-- Markdown 內(nèi)容容器 -->
                    <div class="markdown-body"
                         data-markdown="{{ message.content|escape }}"
                         data-raw="{{ message.content|escape }}">
                        {{ message.content|safe }}
                    </div>
 
                    {% if message.reasoning %}
                    <div class="reasoning-box">
                        <div class="reasoning-label">?? 思考過程</div>
                        <div class="markdown-body"
                             data-markdown="{{ message.reasoning|escape }}"
                             data-raw="{{ message.reasoning|escape }}">
                            {{ message.reasoning|safe }}
                        </div>
                    </div>
                    {% endif %}
                </div>
            {% endfor %}
        </div>
 
        <form method="post">
            {% csrf_token %}
            <input type="text" name="message" placeholder="請輸入您的問題..." required autofocus>
            <button type="submit">發(fā)送</button>
        </form>
    </div>
 
    <!-- 依賴庫 -->
    <script src="https://cdn.jsdelivr.net/npm/marked@12.0.0/lib/marked.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/dompurify@3.0.5/dist/purify.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
 
    <script>
        // 初始化配置
        marked.setOptions({
            breaks: true,
            highlight: (code, lang) => {
                const language = hljs.getLanguage(lang) ? lang : 'plaintext';
                return hljs.highlight(code, { language }).value;
            }
        });
 
        // Markdown 渲染函數(shù)
        const renderMarkdown = () => {
            document.querySelectorAll('.markdown-body').forEach(container => {
                try {
                    // 優(yōu)先使用 data-markdown 屬性
                    const raw = container.dataset.markdown || container.dataset.raw;
                    const clean = DOMPurify.sanitize(raw, {
                        ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'code', 'pre', 'blockquote',
                                     'ul', 'ol', 'li', 'h1', 'h2', 'h3', 'a', 'p', 'br', 'hr'],
                        ALLOWED_ATTR: ['href', 'target']
                    });
                    container.innerHTML = marked.parse(clean);
                } catch (e) {
                    console.error('Markdown渲染失敗:', e);
                    container.innerHTML = container.dataset.raw;
                }
            });
 
            // 觸發(fā)代碼高亮
            hljs.highlightAll();
        };
 
        // 確保依賴加載完成后執(zhí)行
        const checkDependencies = () => {
            if (window.marked && window.DOMPurify && window.hljs) {
                renderMarkdown();
            } else {
                setTimeout(checkDependencies, 100);
            }
        };
 
        // 啟動渲染流程
        document.addEventListener('DOMContentLoaded', checkDependencies);
    </script>
</body>
</html>

遷移數(shù)據(jù)庫

python manage.py makemigrations
python manage.py migrate

五、效果展示

六、將模型切換為deepseek

切換模型只要修改model="qwen2.5-14b-instruct-1m"為deepseek.

到此這篇關(guān)于python使用django調(diào)用deepseek api搭建ai網(wǎng)站的文章就介紹到這了,更多相關(guān)django deepseek api搭建ai網(wǎng)站內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python線程池ThreadPoolExecutor使用方式

    Python線程池ThreadPoolExecutor使用方式

    這篇文章主要介紹了Python線程池ThreadPoolExecutor使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Django csrf校驗(yàn)的實(shí)現(xiàn)

    Django csrf校驗(yàn)的實(shí)現(xiàn)

    這篇文章主要介紹了Django csrf校驗(yàn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • Python 常用數(shù)據(jù)類型相同點(diǎn)、差異與使用指南

    Python 常用數(shù)據(jù)類型相同點(diǎn)、差異與使用指南

    在Python編程中,合理選擇數(shù)據(jù)類型是編寫高效、可維護(hù)代碼的關(guān)鍵,本文將對Python中常用的幾種數(shù)據(jù)類型進(jìn)行全面對比分析,幫助讀者更好地理解它們的特性,并做出明智的選擇
    2025-09-09
  • Python中Random和Math模塊學(xué)習(xí)筆記

    Python中Random和Math模塊學(xué)習(xí)筆記

    這篇文章主要介紹了Python中Random和Math模塊學(xué)習(xí)筆記,本文講解了math模塊的數(shù)學(xué)常量、常用簡單函數(shù)、三角函數(shù)等,講解了random模塊的常用函數(shù)、隨機(jī)挑選和排序等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • 升級anaconda中python到3.10版本的簡單步驟

    升級anaconda中python到3.10版本的簡單步驟

    anaconda是一個非常好用的python發(fā)行版本,其中包含了大部分常用的庫,下面這篇文章主要給大家介紹了關(guān)于升級anaconda中python到3.10版本的簡單步驟,需要的朋友可以參考下
    2024-03-03
  • python檢測文件夾變化,并拷貝有更新的文件到對應(yīng)目錄的方法

    python檢測文件夾變化,并拷貝有更新的文件到對應(yīng)目錄的方法

    今天小編就為大家分享一篇python檢測文件夾變化,并拷貝有更新的文件到對應(yīng)目錄的方法。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • python Tcp協(xié)議發(fā)送和接收信息的例子

    python Tcp協(xié)議發(fā)送和接收信息的例子

    今天小編就為大家分享一篇python Tcp協(xié)議發(fā)送和接收信息的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python怎么判斷模塊安裝完成

    python怎么判斷模塊安裝完成

    在本篇內(nèi)容里小編給大家分享的是關(guān)于python如何判斷模塊是否安裝的技術(shù)文章,有興趣的朋友們可以參考下。
    2020-06-06
  • python多維數(shù)組切片方法

    python多維數(shù)組切片方法

    下面小編就為大家分享一篇python多維數(shù)組切片方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Pycharm中切換pytorch的環(huán)境和配置的教程詳解

    Pycharm中切換pytorch的環(huán)境和配置的教程詳解

    這篇文章主要介紹了Pycharm中切換pytorch的環(huán)境和配置,本文給大家介紹的非常詳細(xì),對大家的工作或?qū)W習(xí)具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03

最新評論

桃园市| 柏乡县| 卫辉市| 平江县| 平定县| 德州市| 竹北市| 葵青区| 河东区| 开封县| 济宁市| 马尔康县| 象山县| 延吉市| 襄城县| 柘荣县| 舟山市| 安仁县| 嘉义县| 云龙县| 西吉县| 黄龙县| 麻城市| 渑池县| 广灵县| 黄陵县| 湖北省| 大同县| 赫章县| 青冈县| 勃利县| 社会| 南投市| 石首市| 武安市| 临夏县| 仁化县| 万州区| 南皮县| 襄樊市| 琼结县|