Just made this PHP function, a simple way to use the new ultra cheap Deepseek API.
For API-key, sign up here: https://platform.deepseek.com/. You can use credit card or Paypal to top up account with like $2 for testing.
See pricing: https://api-docs.deepseek.com/quick_start/pricing
Ref docs: https://api-docs.deepseek.com/api/create-chat-completion
For API-key, sign up here: https://platform.deepseek.com/. You can use credit card or Paypal to top up account with like $2 for testing.
See pricing: https://api-docs.deepseek.com/quick_start/pricing
PHP:
<?php $dsKey = "yourKey";
global $dsKey;
// https://api-docs.deepseek.com/quick_start/pricing
// Models: deepseek-reasoner or deepseek-chat
function queryDeepSeekConversation($messages, $model = 'deepseek-chat', $temp = 1, $tokens = 0, $topP = 1, $freqPenalty = 0.4, $presencePenalty = 0.2) {
global $dsKey; // Ensure this is set correctly
// Debug: Check if API key is set
if (empty($dsKey)) {
return ['error' => 'API key is missing or empty'];
}
$url = 'https://api.deepseek.com/v1/chat/completions'; // Updated endpoint
$postData = [
'model' => $model,
'messages' => $messages,
'temperature' => $temp,
'top_p' => $topP,
'frequency_penalty' => $freqPenalty,
'presence_penalty' => $presencePenalty,
'response_format' => [
'type' => 'text'
],
'stop' => null,
'stream' => false,
'stream_options' => null,
'tools' => null,
'tool_choice' => 'none',
'logprobs' => false,
'top_logprobs' => null
];
// Add max_tokens only if it's explicitly set
if (!empty($tokens)) {
$postData['max_tokens'] = $tokens;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
"Authorization: Bearer {$dsKey}",
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
curl_close($ch);
return ['error' => "cURL Error: $error_msg"];
}
curl_close($ch);
$decodedResponse = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return ['error' => 'Failed to decode JSON'];
}
if (isset($decodedResponse['error'])) {
return ['error' => "DeepSeek API Error: " . $decodedResponse['error']['message']];
}
// Return the assistant's response content
return $decodedResponse['choices'][0]['message']['content'];
}
$messages = [
[
"content" => "You are a helpful assistant.",
"role" => "system"
],
[
"content" => "What is the capital of France?",
"role" => "user"
]
];
$response = queryDeepSeekConversation($messages);
if (isset($response['error'])) {
echo "Error: " . $response['error'];
} else {
echo "Assistant: " . $response;
}
?>
Ref docs: https://api-docs.deepseek.com/api/create-chat-completion