<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: rio lo</title>
    <description>The latest articles on DEV Community by rio lo (@rio_lo_ed48208b1c6338e992).</description>
    <link>https://dev.to/rio_lo_ed48208b1c6338e992</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3259673%2Ff3fa59f6-99e6-47e9-b884-5b921746ef32.png</url>
      <title>DEV Community: rio lo</title>
      <link>https://dev.to/rio_lo_ed48208b1c6338e992</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rio_lo_ed48208b1c6338e992"/>
    <language>en</language>
    <item>
      <title>HarmonyOS application integration DeepSeek API complete development tutorial</title>
      <dc:creator>rio lo</dc:creator>
      <pubDate>Mon, 23 Jun 2025 09:29:36 +0000</pubDate>
      <link>https://dev.to/rio_lo_ed48208b1c6338e992/harmonyos-application-integration-deepseek-api-complete-development-tutorial-5d7</link>
      <guid>https://dev.to/rio_lo_ed48208b1c6338e992/harmonyos-application-integration-deepseek-api-complete-development-tutorial-5d7</guid>
      <description>&lt;p&gt;preface&lt;/p&gt;

&lt;p&gt;In the field of AI application development, DeepSeek, as an advanced large language model, provides powerful natural language processing capabilities. This article will detail how to integrate the DeepSeek API in the HarmonyOS to implement AI conversational capabilities. Whether you are a HarmonyOS app developer or an AI technology enthusiast, you can quickly master the integration method with this tutorial.&lt;/p&gt;

&lt;p&gt;Environment preparation&lt;/p&gt;

&lt;p&gt;Development tools&lt;/p&gt;

&lt;p&gt;DevEco Studio 3.1 or later (official IDE for HarmonyOS App Development)&lt;/p&gt;

&lt;p&gt;HarmonyOS SDK API 9 or later&lt;/p&gt;

&lt;p&gt;Account preparation&lt;/p&gt;

&lt;p&gt;Visit the official DeepSeek platform (&lt;a href="https://api.deepseek.com" rel="noopener noreferrer"&gt;https://api.deepseek.com&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Sign up for a developer account and create an app&lt;/p&gt;

&lt;p&gt;Get the API key in the console (sk-xxx format)&lt;/p&gt;

&lt;p&gt;Description of the project structure&lt;/p&gt;

&lt;p&gt;First, create a tool folder in the HarmonyOS project, and the example structure in this document is as follows:&lt;/p&gt;

&lt;p&gt;plaintext&lt;/p&gt;

&lt;p&gt;src/main/ets/&lt;/p&gt;

&lt;p&gt;├── utils/&lt;/p&gt;

&lt;p&gt;│   └── AIRequest_Util.ts  # ai request util&lt;/p&gt;

&lt;p&gt;├── pages/&lt;/p&gt;

&lt;p&gt;│   └── testHttp.ets      # testPage&lt;/p&gt;

&lt;p&gt;└── entry/&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;└── main_pages.json   # page router
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Full code implementation&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;AI request util（AIRequest_Util.ts）&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;typescript&lt;/p&gt;

&lt;p&gt;import http from '@ohos.net.http';&lt;/p&gt;

&lt;p&gt;import hilog from '@ohos.hilog';&lt;/p&gt;

&lt;p&gt;/**&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;DeepSeek AI API请求工具类&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;封装HTTP请求逻辑与响应处理&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*/&lt;/p&gt;

&lt;p&gt;export class AIRequest_Util {&lt;/p&gt;

&lt;p&gt;/**&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a class="mentioned-user" href="https://dev.to/param"&gt;@param&lt;/a&gt; question &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a class="mentioned-user" href="https://dev.to/param"&gt;@param&lt;/a&gt; callback &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*/&lt;/p&gt;

&lt;p&gt;request(question: string, callback: (text: string) =&amp;gt; void) {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hilog.info(0x0000, 'testTag', 'ALiYunHttpUtils request invoke. question: %{public}s', question);






let httpRequest = http.createHttp();





const requestData = {

  model: "deepseek-chat", 

  messages: [

    { role: "system", content: "你是人工智能AI" },

    { role: "user", content: question } 

  ],

  "stream": false 

};






httpRequest.request(

  "https://api.deepseek.com/chat/completions", // DeepSeek API

  {

    method: http.RequestMethod.POST, 

    header: {

      "Content-Type": "application/json", 

      "Authorization": "Bearer sk-3e4695d9f07c454c8cb9d59822d27987" // APIkey

    },

    extraData: JSON.stringify(requestData), 

    readTimeout: 60000, 

    connectTimeout: 60000, 

    usingProtocol: http.HttpProtocol.HTTP1_1

  }, (err, data: http.HttpResponse) =&amp;gt; {



    if (err) {

      hilog.error(0x0000, 'testTag', '请求失败: %{public}s', JSON.stringify(err));

      httpRequest.destroy();

      callback("请求失败，请检查网络");

      return;

    }




    try {

      // 5. 解析响应数据

      const response = JSON.parse(data.result as string);

      hilog.info(0x0000, 'testTag', '完整响应: %{public}s', JSON.stringify(response));




      // 检查API错误

      if (response.error) {

        hilog.error(0x0000, 'testTag', 'API错误: %{public}s', response.error.message);

        callback(`API错误: ${response.error.message}`);

        return;

      }




      // 提取AI回答内容

      if (response.choices &amp;amp;&amp;amp; response.choices.length &amp;gt; 0) {

        const content = response.choices[0].message.content;

        if (content) {

          hilog.info(0x0000, 'testTag', '提取到的文本: %{public}s', content);

          callback(content);

          return;

        }

      }




      throw new Error("未找到有效响应内容");

    } catch (e) {

      hilog.error(0x0000, 'testTag', '解析失败: %{public}s', e.message);

      callback("解析响应失败");

    } finally {

      // 释放资源

      httpRequest.destroy();

    }

  }

)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// 导出单例对象&lt;/p&gt;

&lt;p&gt;export const DeepSeek = new AIRequest_Util();&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;page（testHttp.ets）&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;typescript&lt;/p&gt;

&lt;p&gt;import { DeepSeek } from '../utils/AIRequest_Util'; // 导入工具类&lt;/p&gt;

&lt;p&gt;@Entry&lt;/p&gt;

&lt;p&gt;@Component&lt;/p&gt;

&lt;p&gt;struct testHttp {&lt;/p&gt;

&lt;p&gt;@State answer: string = '' // 存储AI回答&lt;/p&gt;

&lt;p&gt;build() {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Column() {

  // 交互按钮

  Button('click')

    .width('50%')

    .onClick(() =&amp;gt; {

      // 调用DeepSeek API

      DeepSeek.request("你是谁", (answer) =&amp;gt; {

        // 更新UI显示回答

        this.answer = answer

      })

    })



  // 显示回答内容

  Text(this.answer)

    .fontSize(30)

    .margin({ top: 20 })

}

.height('100%')

.width('100%')

.justifyContent(FlexAlign.Center)

.alignItems(HorizontalAlign.Center)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;page router（main_pages.json）&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;json&lt;/p&gt;

&lt;p&gt;{&lt;/p&gt;

&lt;p&gt;"src": [&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{

  "pages": [

    "pages/testHttp"

  ],

  "name": "entry",

  "window": {

    "designWidth": 720,

    "autoDesignWidth": true

  }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;]&lt;/p&gt;

&lt;p&gt;Analysis of functional modules&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The core process of API requests&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The DeepSeek API call follows these steps:&lt;/p&gt;

&lt;p&gt;Create an instance of the HTTP request&lt;/p&gt;

&lt;p&gt;Build an OpenAI-compliant request body (DeepSeek-compatible OpenAI interface specification)&lt;/p&gt;

&lt;p&gt;Set the request header (including the API key and content type)&lt;/p&gt;

&lt;p&gt;Send a POST request to the DeepSeek API endpoint&lt;/p&gt;

&lt;p&gt;Process response data and extract AI responses&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Request parameters&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Parameter Name, Type, and description&lt;/p&gt;

&lt;p&gt;model string is the name of the model, and deepseek-chat is the dialogue model&lt;/p&gt;

&lt;p&gt;Messages array, which contains system prompts and user questions&lt;/p&gt;

&lt;p&gt;messages.role string is the message role, system is the system prompt, user is the user input, and assistant is the AI answer&lt;/p&gt;

&lt;p&gt;messages.content string The content of the message&lt;/p&gt;

&lt;p&gt;Whether stream boolean enables streaming response, false is off (used in this example)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Error Handling Mechanism&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Three layers of error handling are implemented in the code:&lt;/p&gt;

&lt;p&gt;Network request errors (e.g., timeouts, connection failures)&lt;/p&gt;

&lt;p&gt;API interface errors (e.g., authentication failures, parameter errors)&lt;/p&gt;

&lt;p&gt;Response parsing errors (e.g., data format is abnormal)&lt;/p&gt;

&lt;p&gt;Results:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9j69a6uyi17syijhua09.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9j69a6uyi17syijhua09.png" alt="Image description" width="398" height="841"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Problems you may encounter:&lt;/p&gt;

&lt;p&gt;05-31 23:37:56.504   40524-28380   A00000/testTag                                        E     API error: Insufficient Balance&lt;/p&gt;

&lt;p&gt;If the balance of the API key is insufficient, go to the official website to charge money&lt;/p&gt;

</description>
      <category>harmonyos</category>
    </item>
    <item>
      <title>A date component development based on ARKTS</title>
      <dc:creator>rio lo</dc:creator>
      <pubDate>Thu, 12 Jun 2025 12:53:09 +0000</pubDate>
      <link>https://dev.to/rio_lo_ed48208b1c6338e992/a-date-component-development-based-on-arkts-443</link>
      <guid>https://dev.to/rio_lo_ed48208b1c6338e992/a-date-component-development-based-on-arkts-443</guid>
      <description>&lt;p&gt;Project Overview&lt;br&gt;
This is a card component used to display date information (including Gregorian calendar, lunar calendar, and day of the week), developed based on ArkJS/ArkTS, with modular design, processing date logic through the DateTransfer class, and the DateCard struct is responsible for UI display.&lt;br&gt;
﻿&lt;br&gt;
Development steps&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Select entry in the empty project, right-click and select Atomic Service (meta service)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa90ahd4qzxnay40vn3jb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa90ahd4qzxnay40vn3jb.png" alt="448ec1286bee0ea732d97002218a646" width="794" height="697"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Select a template and configure the information&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm7pqosq69esa3gg5gjdp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm7pqosq69esa3gg5gjdp.png" alt="f7f635a917e09259146a6b2e41d58a6" width="800" height="532"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add the card page to the pages folder in the detecard directory&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fim0ajr5sxgqhrj9m38rm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fim0ajr5sxgqhrj9m38rm.png" alt="img_4" width="378" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The date data processing module (DateTransfer) is responsible for processing the date logic, using the third-party library cjcalendar to obtain the lunar calendar, and entering ohpm install cjcalendar in the terminal to download the third-party library. To use it, you can create a date data object new DateTransfer() to get real-time date data.
﻿
﻿&lt;/li&gt;
&lt;li&gt;Adapt the system color theme in the resource file
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foo6pu3js5gmflis0gm4g.png" alt="img_5" width="800" height="429"&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  result
&lt;/h2&gt;

&lt;h3&gt;
  
  
  dark mode
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F71fql24aqia7s77dxjc5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F71fql24aqia7s77dxjc5.png" alt="7baab3a8672bdcf80d43fabbf4e77b6" width="577" height="1035"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  light mode
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5u6r22xwcosl41a40r9q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5u6r22xwcosl41a40r9q.png" alt="28574d20f2a0884ee78465abbfa90a0" width="577" height="1035"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
