python使用django調(diào)用deepseek api搭建ai網(wǎng)站
一、deepseek簡介
DeepSeek是一家人工智能公司,專注于開發(fā)先進的人工智能模型和技術(shù)。以下是關(guān)于DeepSeek的一些詳細介紹:
1.公司背景
DeepSeek由杭州深度求索人工智能基礎(chǔ)技術(shù)研究有限公司開發(fā),致力于通過創(chuàng)新的技術(shù)和算法,推動人工智能領(lǐng)域的發(fā)展。
2.技術(shù)與模型
- DeepSeek-V3:這是DeepSeek開發(fā)的一個大型語言模型,具有超過600B的參數(shù),在多項性能指標上與國際頂尖模型相當。
- DeepSeek-R1:這是DeepSeek的第一代推理模型,通過大規(guī)模強化學習(RL)進行訓練,展示出了在推理任務(wù)上的優(yōu)異性能。
- DeepSeek-R1-Distill:這些是從DeepSeek-R1中蒸餾出的小模型,具有更好的性能和效率,適合在資源受限的環(huán)境中使用。
3.應(yīng)用領(lǐng)域
- 自然語言處理:DeepSeek的模型在文本生成、知識問答、推理等任務(wù)中表現(xiàn)出色,能夠為用戶提供高質(zhì)量的語言交互服務(wù)。
- 智能助手:DeepSeek開發(fā)了AI智能助手,可用于搜索、寫作、閱讀、解題、翻譯等多種任務(wù),幫助用戶提高效率。
4.優(yōu)勢與特點
- 成本優(yōu)勢:DeepSeek的模型訓練成本低,調(diào)用接口成本也較低,具有較高的性價比。
- 中文處理能力強:對中文語法、成語、文化背景理解更深入,在中文文本生成、摘要、情感分析等任務(wù)中表現(xiàn)自然。
- 開源優(yōu)勢:DeepSeek-R1模型權(quán)重和技術(shù)報告開源,便于開發(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ù),隨便選擇一個模型,點擊右上角的“查看我的apikey”,獲取apikey。
三、創(chuàng)建django項目,并startapp
使用django創(chuà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)文章希望大家以后多多支持腳本之家!
- Python調(diào)用DeepSeek?API的完整操作指南
- 如何使用?Python?實現(xiàn)?DeepSeek?R1?本地化部署
- Python使用Flask結(jié)合DeepSeek開發(fā)(實現(xiàn)代碼)
- Python調(diào)用DeepSeek?API的案例詳細教程
- Python結(jié)合DeepSeek API實現(xiàn)PDF轉(zhuǎn)Word的方案
- Python與DeepSeek的深度融合實戰(zhàn)
- Python使用DeepSeek進行聯(lián)網(wǎng)搜索功能詳解
- python+ollama自己寫代碼調(diào)用本地deepseek模型
相關(guān)文章
基于logstash實現(xiàn)日志文件同步elasticsearch
這篇文章主要介紹了基于logstash實現(xiàn)日志文件同步elasticsearch,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08Python使用Redis實現(xiàn)作業(yè)調(diào)度系統(tǒng)(超簡單)
Redis作為內(nèi)存數(shù)據(jù)庫的一個典型代表,已經(jīng)在很多應(yīng)用場景中被使用,這里僅就Redis的pub/sub功能來說說怎樣通過此功能來實現(xiàn)一個簡單的作業(yè)調(diào)度系統(tǒng)。這里只是想展現(xiàn)一個簡單的想法,所以還是有很多需要考慮的東西沒有包括在這個例子中,比如錯誤處理,持久化等2016-03-03Python Flask實現(xiàn)圖片驗證碼與郵箱驗證碼流程詳細講解
這篇文章主要介紹了如何利用Python生成隨機的圖片驗證碼與郵箱驗證碼,驗證碼是一種區(qū)分用戶是計算機還是人的公共全自動程序,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起試試2022-10-10Python中使用pymysql連接MySQL數(shù)據(jù)庫進行數(shù)據(jù)查詢
在當今數(shù)字化時代,數(shù)據(jù)的重要性不言而喻,而數(shù)據(jù)庫作為數(shù)據(jù)存儲與管理的核心工具,在各類應(yīng)用系統(tǒng)中扮演著關(guān)鍵角色,Python 作為一種廣泛使用的編程語言,提供了多種與數(shù)據(jù)庫交互的方式,其中 pymysql 庫是連接 MySQL 數(shù)據(jù)庫的常用選擇之一,需要的朋友可以參考下2025-01-01對pytorch網(wǎng)絡(luò)層結(jié)構(gòu)的數(shù)組化詳解
今天小編就為大家分享一篇對pytorch網(wǎng)絡(luò)層結(jié)構(gòu)的數(shù)組化詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12