<p><span lang="EN-US">This guide provides a quick start to integrating <strong>DeepSeek API</strong> with<strong> Qodly</strong>. We’ll be using the Qodly’s HTTPRequest class to send user queries to DeepSeek and retrieve AI-generated responses effortlessly.</span></p><p> </p><h2>1. Request your API key:</h2><p><span lang="EN-US">Before getting started, you’ll need to request your API key from DeepSeek’s website to authenticate your requests in your application.</span></p><p><span lang="EN-US"><strong>Base URL:</strong></span></p><p><span lang="EN-US">The API url we’ll be using in this demo :</span><code><span lang="EN-US"> </span></code><a href="https://api.deepseek.com/v1/chat/completions"><code><span lang="EN-US">https://api.deepseek.com/v1/chat/completions</span></code></a></p><p class="text-align-justify"><span lang="EN-US"><sup>The /v1/chat/completions endpoint will allow us to send chat-like queries to DeepSeek's API and receive AI-generated responses, enabling interactive, dynamic conversations within our application.</sup></span></p><p class="text-align-justify"> </p><h2 class="text-align-justify">2. Integrate DeepSeek with Qodly:</h2><p class="text-align-justify">We’ll demonstrate how to connect to DeepSeek using Qodly’s HTTPRequest class within a singleton pattern, ensuring efficient API usage.<span lang="EN-US"></span></p><pre><code class="language-4d language-plaintext">shared singleton constructor()
this.apiKey = "your_api_key"
this.apiUrl="https://api.deepseek.com/v1/chat/completions"
exposed function chatbot(prompt : string):string
var jsonBody,content : string
var data : object = newObject()
var request : 4D.HTTPRequest
var requestOptions,jsonResponse:object
data.model = "deepseek-chat"
data.messages = [(newObject("role", "user", "content",prompt))]
data.temperature = 0.1
data.max_tokens = 500
jsonBody = jsonStringify(data)
requestOptions = {}
requestOptions.method = "POST"
requestOptions.headers = newObject("Authorization", "Bearer "+this.apiKey, "Content-Type", "application/json")
requestOptions.body = jsonBody
requestOptions.dataType = "text"
request = 4D.HTTPRequest.new(this.apiUrl, requestOptions)
request.wait()
if ((request.response.status == 200) && (valueType(request.response.body) == kString))
try
jsonResponse = jsonParse(request.response.body)
content = jsonResponse.choices[0].message.content
catch
content = "Error : Parsing went wrong."
end
else
content = "Error : HTTP status: "+string(request.response.status)
end
return content</code></pre><ul><li><span style="font-family:Symbol;" lang="EN-US"></span><span lang="EN-US">API Initialization: The singleton constructor sets up the API key and URL. The singleton pattern ensures that only one instance of the connection is used throughout the application.</span></li><li><p style="line-height:normal;"><span lang="EN-US">Sending Requests: The chatbot function sends a user prompt to DeepSeek’s API via a POST request, with necessary headers (Authorization, Content-Type).</span></p></li></ul><pre><code class="language-4d language-plaintext">data.model = "deepseek-chat"
data.messages = [(newObject("role", "user", "content",prompt))]
data.temperature = 0.1
data.max_tokens = 500</code></pre><p style="margin-left:.25in;"><span lang="EN-US">Here’s a breakdown of those lines:</span></p><ul><li><p style="line-height:normal;"><span lang="EN-US"><strong>data.model = "deepseek-chat"</strong></span><em><span lang="EN-US">:</span></em><span lang="EN-US"> Specifies the AI model to use for generating responses.</span></p></li><li><p style="line-height:normal;"><span lang="EN-US"><strong>data.messages = [(newObject("role", "user", "content", prompt))]:</strong> Defines the messages for the chat, with the user's input (prompt) being passed in as the message content. The "role" is set to "user" to indicate the sender.</span></p></li><li><p style="line-height:normal;"><span lang="EN-US"><strong>data.temperature = 0.1</strong>: Controls the randomness of the response. The lower the value, the more deterministic and focused the response becomes.</span></p></li><li><p style="line-height:normal;"><span lang="EN-US"><strong>data.max_tokens = 500</strong>: Sets the limit of the response ‘length.</span></p></li></ul><pre><code class="language-4d language-plaintext"> jsonBody = jsonStringify(data)
requestOptions = {}
requestOptions.method = "POST"
requestOptions.headers = newObject("Authorization", "Bearer "+this.apiKey, "Content-Type", "application/json")
requestOptions.body = jsonBody
requestOptions.dataType = "text"
request = 4D.HTTPRequest.new(this.apiUrl, requestOptions)
request.wait()</code></pre><p style="line-height:normal;"> </p><p style="line-height:normal;"><span lang="EN-US">The code snippet performs one main action:</span></p><ul><li><p style="line-height:normal;"><span lang="EN-US"> Setting up and sending the request, where data is stringified, HTTP request options are configured (method, headers, body, and response type), and the request is sent via 4D.HTTPRequest.new(), followed by waiting for the response. </span></p></li><li><p style="line-height:normal;"><span lang="EN-US">It then handles the response by parsing it and extracting the chatbot's reply, with error handling for any HTTP or parsing failures.</span></p></li></ul><p style="line-height:normal;"> </p><pre><code class="language-4d language-plaintext"> if ((request.response.status == 200) && (valueType(request.response.body) == kString))
try
jsonResponse = jsonParse(request.response.body)
content = jsonResponse.choices[0].message.content
catch
content = "Error : Parsing went wrong."
end
else
content = "Error : HTTP status: "+string(request.response.status)
end
return content</code></pre><p style="line-height:normal;"> </p><ul><li><p style="line-height:normal;"><span lang="EN-US">This approach efficiently manages API connections and allows easy integration with DeepSeek’s AI-driven chat responses.</span></p><p style="line-height:normal;"><span style="font-size:14.0pt;" lang="EN-US"></span></p></li></ul><h2>3. Building the Interface</h2><p><span lang="EN-US">For the interface, you can create a simple layout with:</span><span style="font-size:14.0pt;line-height:115%;" lang="EN-US"></span></p><ul><li><span lang="EN-US">A <strong>Text Input</strong> that captures the user's question, which is then sent to the DeepSeek API.</span></li><li><span lang="EN-US">A <strong>Button</strong> that triggers the function from the singleton class. When clicked, it initiates the API call, sending the user input to the DeepSeek service and retrieving the response.</span></li><li><p><span lang="EN-US">A simple <strong>Text </strong>to show the API response.</span></p><p><span lang="EN-US">This ensures easy interaction with DeepSeek, allowing users to input questions and view results.</span></p></li></ul><img src="/sites/default/files/deepseek-qodly-integration.png" data-entity-uuid="fc25f7df-7906-4455-af8a-be4027900e72" data-entity-type="file" alt="" width="1020" height="415"><h2>4. Demo</h2><img src="/sites/default/files/deepSeekQodly.webp" width="906" height="428"><p> </p><h2>5. Conclusion</h2><p><span lang="EN-US">By integrating DeepSeek’s AI capabilities with Qodly, you can easily enhance your application’s data analysis features. The HTTPRequest class and singleton pattern streamline the process, making it easier to build scalable, data-driven solutions.</span></p>