搜索了一下DeepSeek,发现有人已经实现了SAP的对接,
不登录网页,SAP如何使用DeepSeek快速编程,ABAP起飞啦~
按照对应的注册流程和方法。总算做出了第一个能够直连DeepSeek的API abap程序。
效果不错。
report ZTOOL_ABAP_CALL_DS.
data:
LV_JSON_REQUEST type STRING .
data LV_CONT type STRING.
LV_CONT = 'SAP ewm是什么?请用中文回答我'.
* 构建JSON请求体
LV_JSON_REQUEST = '{"model":"deepseek-chat",'
&& '"messages":['
&& ' {"role":"system","content":"' && LV_CONT && '"},'
&& ' {"role":"user","content":"Hello!"}'
&& '],'
&& '"stream":false}'.
data GV_OK type C length 6.
data LV_STR type STRING.
data LV_RET type STRING.
perform FRM_TEST using LV_JSON_REQUEST changing LV_RET.
form FRM_TEST using UV_JSON_REQUEST changing CV_RET.
data: LV_STATUS type I,
LV_ERROR_OCCURRED type FLAG,
LV_ERROR_MSG type STRING,
LV_RESPONSE_BODY type STRING.
clear CV_RET.
do 1 times.
perform SEND_JSON using
'https://api.deepseek.com/chat/completions' "
UV_JSON_REQUEST " JSON报文
changing LV_STATUS
LV_RESPONSE_BODY
LV_ERROR_OCCURRED
LV_ERROR_MSG.
enddo.
* Show result
format color col_heading.
write: / 'Response status:', LV_STATUS.
write: / '调用成功次数:', GV_OK.
if LV_ERROR_OCCURRED = 'X'.
format color col_negative.
write: / 'Error occurred:', LV_ERROR_MSG.
endif.
format color col_normal.
write: / 'Response:', LV_RESPONSE_BODY.
if LV_RESPONSE_BODY cs `"content":"`.
perform FRM_CUT using LV_RESPONSE_BODY
`"content":"`
`"},"logprobs":`
changing CV_RET.
endif.
endform. "start
form SEND_JSON using IV_URL type STRING
IV_JSON_DATA type STRING
changing CV_STATUS type I
CV_RESPONSE_BODY type STRING
CV_ERROR_OCCURRED type FLAG
CV_ERROR_MSG type STRING.
statics: LO_CLIENT type ref to IF_HTTP_CLIENT.
clear: CV_ERROR_MSG,
CV_STATUS,
CV_ERROR_OCCURRED,
CV_ERROR_MSG.
if IV_URL is initial.
message E349(SBDS) into CV_ERROR_MSG.
CV_ERROR_OCCURRED = 'X'.
return.
endif.
statics SV_BEGIN type C length 1.
if SV_BEGIN is initial.
call method CL_HTTP_CLIENT=>CREATE_BY_URL
exporting
URL = IV_URL
importing
CLIENT = LO_CLIENT
exceptions
ARGUMENT_NOT_FOUND = 1
PLUGIN_NOT_ACTIVE = 2
INTERNAL_ERROR = 3
others = 4.
if SY-SUBRC ne 0.
message id SY-MSGID type SY-MSGTY number SY-MSGNO
with SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4
into CV_ERROR_MSG.
CV_ERROR_OCCURRED = 'X'.
return.
endif.
SV_BEGIN = 'X'.
LO_CLIENT->REQUEST->SET_CONTENT_TYPE( 'application/json' ).
LO_CLIENT->REQUEST->SET_METHOD( 'POST' ).
* 自定义参数
LO_CLIENT->REQUEST->SET_HEADER_FIELD( NAME = 'InterfaceName' VALUE = 'HTTP://WWW.HOT583.COM' ). "//保留。
LO_CLIENT->REQUEST->SET_HEADER_FIELD( NAME = 'Authorization' VALUE = 'Bearer sk-f28ade71f6748ae8adfa98ka8132' ). "换算成自己的DeepSeek网址申请密钥吧。
默认有10元赠送金额,相当于免费。过了之后支付宝或者微信采购即可。
endif.
LO_CLIENT->REQUEST->SET_CDATA( IV_JSON_DATA ).
call method LO_CLIENT->SEND
exceptions
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3
others = 4.
if SY-SUBRC ne 0.
LO_CLIENT->GET_LAST_ERROR( importing MESSAGE = CV_ERROR_MSG ).
CV_ERROR_OCCURRED = 'X'.
return.
endif.
LO_CLIENT->RECEIVE( exceptions others = 1 ).
if SY-SUBRC ne 0.
LO_CLIENT->GET_LAST_ERROR( importing MESSAGE = CV_ERROR_MSG ).
CV_ERROR_OCCURRED = 'X'.
return.
endif.
CV_RESPONSE_BODY = LO_CLIENT->RESPONSE->GET_CDATA( ).
LO_CLIENT->RESPONSE->GET_STATUS( importing CODE = CV_STATUS ).
* CALL METHOD LO_CLIENT->CLOSE( )."如果多开需要关闭。
GV_OK = GV_OK + 1.
endform.
*&---------------------------------------------------------------------*
*& Form FRM_CUT
*&---------------------------------------------------------------------*
*& 截取AB中间的保留到C。
*&---------------------------------------------------------------------*
form FRM_CUT using UV_INPUT
P_A
P_B
changing CV_RET.
data LV_TMP type STRING.
split UV_INPUT at P_A into LV_TMP CV_RET.
clear LV_TMP.
split CV_RET at P_B into CV_RET LV_TMP.
endform.