VUE對接deepseekAPI調用方式
更新時間:2025年01月24日 09:04:49 作者:小嘟嚷ovo
文章介紹了如何在Vue項目中對接DeepSeek API,包括在開放平臺注冊賬號申請APIKey、安裝axios庫、創(chuàng)建API調用函數以及在Vue組件中調用該函數
VUE對接deepseekAPI調用
1.先去開放平臺注冊賬號申請api key
開放平臺:https://platform.deepseek.com/api_keys
2.你的項目需要有發(fā)送請求的axios或者自己寫
npm install axios # 或 yarn add axios
3.創(chuàng)建 API 調用函數
在 Vue 項目中,通常會將 API 調用的邏輯封裝到一個單獨的文件中,例如 src/api/deepseek.js
。
關于其中 /your-endpoint-path 是需要你自己去api文檔中查看的,文檔具體地方在最后面。
import axios from 'axios'; const DEEPSEEK_API_URL = 'https://api.deepseek.com'; // 實際的 DeepSeek API 地址 const DEEPSEEK_API_KEY = 'your-api-key'; // 替換為你的 DeepSeek API Key // 創(chuàng)建 axios 實例 const deepseekClient = axios.create({ baseURL: DEEPSEEK_API_URL, headers: { 'Authorization': `Bearer ${DEEPSEEK_API_KEY}`, 'Content-Type': 'application/json', }, }); /** * 調用 DeepSeek API 示例 * @param {Object} data 請求參數 * @returns {Promise} API 響應 */ export const callDeepSeekAPI = async (data) => { try { const response = await deepseekClient.post('/your-endpoint-path', data); // 替換為實際的 API 路徑 return response.data; } catch (error) { console.error('DeepSeek API 調用失敗:', error); throw error; } };
在你的 Vue 組件中,可以調用上面封裝的 callDeepSeekAPI
函數。
<template> <div> <h1>DeepSeek API 調用示例</h1> <button @click="fetchData">調用 DeepSeek API</button> <div v-if="loading">加載中...</div> <div v-else> <pre>{ { responseData }}</pre> </div> </div> </template> <script> import { callDeepSeekAPI } from '@/api/deepseek'; // 導入封裝的 API 函數 export default { data() { return { loading: false, responseData: null, }; }, methods: { async fetchData() { this.loading = true; try { const data = { // 替換為實際的請求參數 prompt: '你好,DeepSeek!', max_tokens: 50, }; this.responseData = await callDeepSeekAPI(data); } catch (error) { console.error('API 調用失敗:', error); } finally { this.loading = false; } }, }, }; </script> <style scoped> pre { background: #f4f4f4; padding: 10px; border-radius: 5px; } </style>
這個文檔的url是
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
詳解Vue + Vuex 如何使用 vm.$nextTick
這篇文章主要介紹了詳解Vue + Vuex 如何使用 vm.$nextTick,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11