筆者之前的文章,已經(jīng)詳細介紹了 DeepSeek 的 API 調用方式: 之前已經(jīng)介紹過 ABAP 里一些開箱即用的調用 ChatGPT API 的工具和 SDK: 同理,在 ABAP 里通過代碼的方式,調用 DeepSeek 的 API 也非常容易。 在 DeepSeek API 控制臺上,能看到針對 curl 和其他編程語言調用的示例代碼。 把 curl 的代碼粘貼下來,直接扔給 ChatGPT,讓它翻譯成對應的 ABAP 代碼。 指令: 請你扮演一個資深的 ABAP 開發(fā)人員,請你把下面這段 curl 消費 API 的操作,翻譯成能夠運行的 ABAP 代碼。
聽說 ChatGPT 5 也快出了。目前 o3-mini-high, 應該是手頭能用的最強的代碼編寫工具了吧。 果然 ChatGPT 生成的代碼質量非常高,直接激活通過,一次成功。我只需要把代碼里的 <token> 占位符,替換成自己的 API key 就能運行了。 將代碼中要問 DeepSeek 的問題,硬編碼成 "What is ABAP?". 另存為一個 ABAP 報表,直接運行, 收到 DeepSeek API 的調用結果。 完整的 ABAP 代碼如下,100% 由 ChatGPT 生成: REPORT z. DATA: lo_http_client TYPE REF TO if_http_client, lv_url TYPE string VALUE 'https://api./v1/chat/completions', lv_token TYPE string VALUE '<此處替換成你自己的 API key>', lv_json_payload TYPE string, lv_response TYPE string. lv_json_payload = `{` && `"model": "deepseek-ai/DeepSeek-V3",` && `"messages": [` && `{` && `"role": "user",` && `"content": "What is ABAP?"` && `}` && `],` && `"stream": false,` && `"max_tokens": 512,` && `"stop": [` && `"null"` && `],` && `"temperature": 0.7,` && `"top_p": 0.7,` && `"top_k": 50,` && `"frequency_penalty": 0.5,` && `"n": 1,` && `"response_format": {` && `"type": "text"` && `},` && `"tools": [` && `{` && `"type": "function",` && `"function": {` && `"description": "<string>",` && `"name": "<string>",` && `"parameters": {},` && `"strict": false` && `}` && `}` && `]` && `}`. TRY. cl_http_client=>create_by_url( EXPORTING url = lv_url ssl_id = 'ANONYM' IMPORTING client = lo_http_client ). lo_http_client->request->set_method( if_http_request=>co_request_method_post ). lo_http_client->request->set_header_field( name = 'Authorization' value = |Bearer { lv_token }| ). lo_http_client->request->set_header_field( name = 'Content-Type' value = 'application/json' ). lo_http_client->request->set_cdata( lv_json_payload ). lo_http_client->send( ). lo_http_client->receive( ). lv_response = lo_http_client->response->get_cdata( ). WRITE: / 'Response:', lv_response. CATCH cx_root INTO DATA(lx_root). WRITE: / 'Error:', lx_root->get_text( ). ENDTRY.
|