VUE对接deepseekAPI调用

1.先去开放平台注册账号申请api key。开放平台:https://platform.deepseek.com/api_keys

 

2.你的项目需要有发送请求的axios或者自己写。

npm install axios
# 或
yarn add axios

3.创建 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

// 创建 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>

文档:对话补全 | DeepSeek API Docs

这个文档的url是

 

### 如何在 Vue 项目中调用 DeepSeek API 为了在 Vue 项目中成功调用 DeepSeek API,可以遵循以下方法: #### 安装 Axios 库 Axios 是一个基于 Promise 的 HTTP 客户端库,适用于浏览器和 Node.js。它简化了向服务器发送请求的过程。 ```bash npm install axios ``` #### 创建 API 请求服务文件 创建一个新的 JavaScript 文件 `apiService.js` 来封装所有的 API 调用逻辑。这有助于保持代码整洁有序,并使未来的维护更加容易。 ```javascript // src/services/apiService.js import axios from 'axios'; const apiClient = axios.create({ baseURL: 'https://deepseek.example.com/', // 替换成实际的 DeepSeek API 地址 headers: { Accept: 'application/json', 'Content-Type': 'application/json' } }); export default class ApiService { static async getEndpoint(endpoint) { try { const response = await apiClient.get(endpoint); return response.data; } catch (error) { console.error(`There was an error fetching the data! ${error}`); } } static async postEndpoint(endpoint, payload) { try { const response = await apiClient.post(endpoint, payload); return response.data; } catch (error) { console.error(`There was an error posting the data! ${error}`); } } } ``` #### 使用 API 服务组件内 现在可以在任何 Vue 组件内部轻松导入此服务并执行 GET 或 POST 请求来交互 DeepSeek API 数据源。 ```html <!-- src/components/ExampleComponent.vue --> <template> <div> <!-- Your template here --> </div> </template> <script> import ApiService from '../services/apiService'; export default { name: 'example-component', data() { return { items: [] }; }, methods: { fetchItems() { ApiService.getEndpoint('/items') .then((response) => { this.items = response; // 假设响应是一个数组 }); }, addItem(itemData) { ApiService.postEndpoint('/items', itemData) .then(() => { this.fetchItems(); // 刷新列表显示新添加项 }); } }, mounted() { this.fetchItems(); } }; </script> ``` 通过这种方式,在 Vue 项目的上下文中实现了与外部 RESTful API(如假设中的 DeepSeek API)的有效通信[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值