用deepseek写deepseek的api调用代码

更新时间:2025-03-01 分类:阅读杂记 浏览量:6

提示语言:写个php代码:调用deepseek的api接口,并实现对话框模式的功能。

要实现一个调用DeepSeek API接口并实现对话框模式的PHP代码,你可以按照以下步骤进行。假设DeepSeek API是一个对话式AI接口,类似于ChatGPT,我们可以通过PHP发送请求并接收响应,然后在网页上以对话框的形式展示对话内容。

代码实现

以下是一个简单的PHP代码示例,调用DeepSeek API并实现对话框模式:

<?php
// DeepSeek API的URL和API密钥
$api_url = "https://api.deepseek.com/v1/chat/completions"; // 替换为实际的API URL
$api_key = "your_api_key_here"; // 替换为你的API密钥

// 初始化对话历史
session_start();
if (!isset($_SESSION['chat_history'])) {
    $_SESSION['chat_history'] = [];
}

// 处理用户输入
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['message'])) {
    $user_message = trim($_POST['message']);

    if (!empty($user_message)) {
        // 将用户消息添加到对话历史
        $_SESSION['chat_history'][] = ['role' => 'user', 'content' => $user_message];

        // 调用DeepSeek API
        $response = callDeepSeekAPI($user_message, $_SESSION['chat_history']);

        // 将AI回复添加到对话历史
        if ($response && isset($response['choices'][0]['message']['content'])) {
            $ai_message = $response['choices'][0]['message']['content'];
            $_SESSION['chat_history'][] = ['role' => 'assistant', 'content' => $ai_message];
        }
    }
}

// 调用DeepSeek API的函数
function callDeepSeekAPI($message, $history) {
    global $api_url, $api_key;

    // 构造请求数据
    $data = [
        'model' => 'deepseek-chat', // 替换为实际的模型名称
        'messages' => array_merge($history, [['role' => 'user', 'content' => $message]]),
        'max_tokens' => 150,
        'temperature' => 0.7,
    ];

    // 初始化cURL
    $ch = curl_init($api_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: Bearer ' . $api_key,
    ]);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

    // 发送请求并获取响应
    $response = curl_exec($ch);
    curl_close($ch);

    // 返回解析后的响应
    return json_decode($response, true);
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DeepSeek 对话框</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .chat-container {
            width: 400px;
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            display: flex;
            flex-direction: column;
        }
        .chat-history {
            flex: 1;
            padding: 20px;
            overflow-y: auto;
            border-bottom: 1px solid #ddd;
        }
        .message {
            margin-bottom: 15px;
            display: flex;
            flex-direction: column;
        }
        .message.user {
            align-items: flex-end;
        }
        .message.assistant {
            align-items: flex-start;
        }
        .message-content {
            max-width: 80%;
            padding: 10px;
            border-radius: 10px;
        }
        .message.user .message-content {
            background: #007bff;
            color: #fff;
        }
        .message.assistant .message-content {
            background: #f1f1f1;
            color: #333;
        }
        .chat-input {
            display: flex;
            padding: 10px;
            background: #f9f9f9;
        }
        .chat-input input {
            flex: 1;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            margin-right: 10px;
        }
        .chat-input button {
            padding: 10px 20px;
            background: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        .chat-input button:hover {
            background: #0056b3;
        }
    </style>
</head>
<body>
    <div class="chat-container">
        <div class="chat-history">
            <?php
            // 显示对话历史
            if (!empty($_SESSION['chat_history'])) {
                foreach ($_SESSION['chat_history'] as $message) {
                    $class = $message['role'] === 'user' ? 'user' : 'assistant';
                    echo "<div class='message $class'>
                            <div class='message-content'>{$message['content']}</div>
                          </div>";
                }
            }
            ?>
        </div>
        <form class="chat-input" method="POST">
            <input type="text" name="message" placeholder="输入消息..." required>
            <button type="submit">发送</button>
        </form>
    </div>
</body>
</html>

使用方法:

  1. 将代码保存为index.php文件。
  2. 替换$api_url$api_key为实际的DeepSeek API地址和密钥。
  3. 部署到支持PHP的服务器上(如Apache或Nginx)。
  4. 访问页面即可体验对话框功能。

注意事项:

  • 确保服务器支持curl扩展。
  • 如果API需要其他参数(如temperaturemax_tokens等),可以根据文档调整请求数据。
  • 如果需要更复杂的对话逻辑(如上下文管理),可以进一步扩展代码。