Skip to content

Commit 7a6b18a

Browse files
committed
fixed:stream history and done handle incorrect for ai module
delete:macro version handle fir ai module
1 parent dc5cd0e commit 7a6b18a

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

XEngine_Example/AIApi_APPExample/AIApi_APPExample.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ int main()
4242
XNETHANDLE xhToken = 0;
4343

4444
LPCXSTR lpszAPIUrl = _X("https://api.zhizengzeng.com/v1/chat/completions");
45-
LPCXSTR lpszAPIKey = _X("sk-zk29d91bb8d095c79c32a2a7edaca603137678e1b2a9316a");
45+
LPCXSTR lpszAPIKey = _X("sk-1");
4646
LPCXSTR lpszAPIModel = _X("gpt-4o");
4747

4848
//LPCXSTR lpszAPIUrl = _X("https://api.hunyuan.cloud.tencent.com/v1/chat/completions");
49-
//LPCXSTR lpszAPIKey = _X("sk-kguobQDvmlzonvUXFBgYyMEZjZZjjcNp17bME2Si6SjrGNKp");
49+
//LPCXSTR lpszAPIKey = _X("sk-1");
5050
//LPCXSTR lpszAPIModel = _X("hunyuan-turbos-latest");
5151

5252
if (!AIApi_Chat_Create(&xhToken, lpszAPIUrl, lpszAPIKey, XEngine_AIApi_CBRecv))
@@ -57,7 +57,7 @@ int main()
5757
AIApi_Chat_SetRole(xhToken, _X("You are a helpful assistant."));
5858
LPCXSTR lpszMSGBuffer = _X("hello");
5959
int nMSGLen = strlen(lpszMSGBuffer);
60-
if (!AIApi_Chat_Excute(xhToken, lpszAPIModel, lpszMSGBuffer, nMSGLen))
60+
if (!AIApi_Chat_Excute(xhToken, lpszAPIModel, lpszMSGBuffer, nMSGLen, false))
6161
{
6262
printf("AIApi_Chat_Excute:%lX\n", AIApi_GetLastError());
6363
return 0;
@@ -67,12 +67,21 @@ int main()
6767
AIApi_Chat_GetStatus(xhToken, &bCompleted);
6868
lpszMSGBuffer = _X("what can you do for me?");
6969
nMSGLen = strlen(lpszMSGBuffer);
70-
if (!AIApi_Chat_Excute(xhToken, lpszAPIModel, lpszMSGBuffer, nMSGLen, false))
70+
if (!AIApi_Chat_Excute(xhToken, lpszAPIModel, lpszMSGBuffer, nMSGLen, true))
7171
{
7272
printf("AIApi_Chat_Excute:%lX\n", AIApi_GetLastError());
7373
return 0;
7474
}
75+
bCompleted = false;
76+
AIApi_Chat_GetStatus(xhToken, &bCompleted);
7577

78+
lpszMSGBuffer = _X("李白是谁?");
79+
nMSGLen = strlen(lpszMSGBuffer);
80+
if (!AIApi_Chat_Excute(xhToken, lpszAPIModel, lpszMSGBuffer, nMSGLen, true))
81+
{
82+
printf("AIApi_Chat_Excute:%lX\n", AIApi_GetLastError());
83+
return 0;
84+
}
7685
bCompleted = false;
7786
AIApi_Chat_GetStatus(xhToken, &bCompleted);
7887
AIApi_Chat_Destory(xhToken);

XEngine_Module/XEngine_AIApi/AIApi_Chat/AIApi_Chat.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,13 @@ bool CAIApi_Chat::AIApi_Chat_Excute(XNETHANDLE xhToken, LPCXSTR lpszModelName, L
221221
st_JsonRoot["messages"] = st_JsonArray;
222222

223223
xstring m_StrBody = Json::writeString(st_JsonBuilder, st_JsonRoot);
224-
#if XENGINE_VERSION_KERNEL >= 9 && XENGINE_VERSION_MAIN >= 21
225224
if (!APIClient_Http_Excute(xhToken, m_StrBody.c_str(), m_StrBody.length(), stl_MapIterator->second->tszAPIHdr))
226225
{
227226
AIApi_IsErrorOccur = true;
228227
AIApi_dwErrorCode = APIClient_GetLastError();
229228
st_Locker.unlock_shared();
230229
return false;
231230
}
232-
#endif
233231
st_Locker.unlock_shared();
234232
return true;
235233
}
@@ -347,11 +345,7 @@ bool CAIApi_Chat::AIApi_Chat_GetStatus(XNETHANDLE xhToken, bool* pbComplete, int
347345
return false;
348346
}
349347

350-
#if XENGINE_VERSION_KERNEL >= 9 && XENGINE_VERSION_MAIN >= 21
351348
if (!APIClient_Http_GetResult(xhToken, pbComplete, pInt_HTTPCode, bWaitExist))
352-
#else
353-
if (!APIClient_Http_GetResult(xhToken, pbComplete, pInt_HTTPCode))
354-
#endif
355349
{
356350
AIApi_IsErrorOccur = true;
357351
AIApi_dwErrorCode = APIClient_GetLastError();
@@ -453,10 +447,21 @@ bool CAIApi_Chat::AIApi_Chat_Parse(AICLIENT_CHAT* pSt_AIClient, LPCXSTR lpszMSGB
453447
XCHAR tszGBKBuffer[8192] = {};
454448
BaseLib_Charset_UTFToAnsi(st_JsonMessage["content"].asString().c_str(), tszGBKBuffer, &nGBKLen);
455449
pSt_AIClient->lpCall_Chat(pSt_AIClient->xhToken, st_JsonRoot["model"].asCString(), tszGBKBuffer, nGBKLen, pSt_AIClient->lParam);
450+
if (bSSEReply)
451+
{
452+
//流式数据需要单独处理保存
453+
memcpy(pSt_AIClient->st_HisStream.tszRoleContent + pSt_AIClient->st_HisStream.nCLen, tszGBKBuffer, nGBKLen);
454+
pSt_AIClient->st_HisStream.nCLen += nGBKLen;
455+
}
456456
#else
457457
pSt_AIClient->lpCall_Chat(pSt_AIClient->xhToken, st_JsonRoot["model"].asCString(), st_JsonMessage["content"].asString().c_str(), st_JsonMessage["content"].asString().length(), pSt_AIClient->lParam);
458+
if (bSSEReply)
459+
{
460+
memcpy(pSt_AIClient->st_HisStream.tszRoleContent + pSt_AIClient->st_HisStream.nCLen, st_JsonMessage["content"].asString().c_str(), st_JsonMessage["content"].asString().length());
461+
pSt_AIClient->st_HisStream.nCLen += st_JsonMessage["content"].asString().length();
462+
}
458463
#endif
459-
if (pSt_AIClient->bHistory)
464+
if (pSt_AIClient->bHistory && !bSSEReply)
460465
{
461466
pSt_AIClient->pStl_ListHistory->push_back(st_AIHistory);
462467
}
@@ -483,9 +488,9 @@ void CAIApi_Chat::AIApi_Chat_CBRecv(XNETHANDLE xhToken, XPVOID lpszMsgBuffer, in
483488
if (pSt_AIClient->bStream)
484489
{
485490
//SSE
486-
printf("%s\n", (LPCXSTR)lpszMsgBuffer);
487491
int nPos = 0;
488492
LPCXSTR lpszSSEStr = _X("data: ");
493+
LPCXSTR lpszSSEEnd = _X("[DONE]");
489494

490495
XCHAR* ptszStart = pSt_AIClient->ptszMSGBuffer;
491496
XCHAR* ptszEnd = ptszStart + pSt_AIClient->nMSGLen;
@@ -507,6 +512,19 @@ void CAIApi_Chat::AIApi_Chat_CBRecv(XNETHANDLE xhToken, XPVOID lpszMsgBuffer, in
507512
{
508513
nPos = _tcsxlen(lpszSSEStr);
509514
}
515+
if (_tcsxnicmp(lpszSSEEnd, ptszStart + nPos, _tcsxlen(lpszSSEEnd)) == 0)
516+
{
517+
//结束
518+
if (pSt_AIClient->bHistory)
519+
{
520+
_xstrcpy(pSt_AIClient->st_HisStream.tszRoleName, _X("assistant"), sizeof(pSt_AIClient->st_HisStream.tszRoleName));
521+
pSt_AIClient->pStl_ListHistory->push_back(pSt_AIClient->st_HisStream);
522+
memset(&pSt_AIClient->st_HisStream, '\0', sizeof(AICLIENT_HISTORY));
523+
}
524+
pSt_AIClient->nMSGLen = 0;
525+
memset(pSt_AIClient->ptszMSGBuffer, '\0', XENGINE_MEMORY_SIZE_MAX);
526+
break;
527+
}
510528
// 解析当前消息体
511529
if (!pClass_This->AIApi_Chat_Parse(pSt_AIClient, ptszStart + nPos, nOneMsgLen - nPos, true))
512530
{

XEngine_Module/XEngine_AIApi/AIApi_Chat/AIApi_Chat.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@
1212
*********************************************************************/
1313
typedef struct
1414
{
15+
int nCLen;
1516
XCHAR tszRoleContent[8192];
1617
XCHAR tszRoleName[128];
1718
}AICLIENT_HISTORY;
1819
typedef struct
1920
{
21+
AICLIENT_HISTORY st_HisStream; //流式有用
2022
XCHAR tszAPIUrl[XPATH_MAX];
2123
XCHAR tszAPIHdr[XPATH_MAX];
2224
XCHAR tszAPIRole[XPATH_MAX];
2325
XNETHANDLE xhToken;
2426
time_t nTimeStart;
2527
int nMSGLen;
26-
28+
2729
bool bStream;
2830
bool bHistory;
2931
std::unique_ptr<std::list<AICLIENT_HISTORY>> pStl_ListHistory;

0 commit comments

Comments
 (0)