write a javascript program to call the deepseek api
- User
write a javascript program to call the deepseek api
- Assistant
To create a JavaScript program that calls the DeepSeek API, you'll need to send HTTP requests to the appropriate endpoint using
fetchor a library likeaxios. Below is an example of how you can set up the JavaScript code to interact with the DeepSeek API.Example JavaScript Program
This example uses the
fetchAPI to send a POST request to the DeepSeek API. Make sure to replaceYOUR_API_KEYwith your actual API key if required.javascript// Define the DeepSeek API endpoint const DEEPSEEK_API_URL = 'https://platform.deepseek.com/api/v1/generate'; // Update the endpoint as necessary // Function to call the DeepSeek API async function callDeepSeekAPI(userPrompt) { try { const response = await fetch(DEEPSEEK_API_URL, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer YOUR_API_KEY` // Include authorization if needed }, body: JSON.stringify({ prompt: userPrompt, max_tokens: 150, // Adjust the max tokens as needed temperature: 0.6, // Adjust temperature based on your needs top_p: 0.95 // Adjust top_p based on your needs }) }); // Check if the response is OK (status in the range 200-299) if (!response.ok) { throw new Error(`Error: ${response.status} ${response.statusText}`); } const data = await response.json(); console.log('Response from DeepSeek API:', data); } catch (error) { console.error('Error calling DeepSeek API:', error); } } // Example usage const userPrompt = "What are the key features of DeepSeek-R1?"; callDeepSeekAPI(userPrompt);