Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/include/tencentcloud/core/NetworkProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define TENCENTCLOUD_CORE_NETWORKPROXY_H_

#include <string>
#include <cstdint>

namespace TencentCloud
{
Expand Down
7 changes: 7 additions & 0 deletions core/include/tencentcloud/core/http/HttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef TENCENTCLOUD_CORE_HTTP_HTTPCLIENT_H_
#define TENCENTCLOUD_CORE_HTTP_HTTPCLIENT_H_

#include <functional>
#include <curl/curl.h>
#include <tencentcloud/core/Outcome.h>
#include <tencentcloud/core/Error.h>
Expand All @@ -29,6 +30,8 @@ namespace TencentCloud
{
public:
typedef Outcome<Core::Error, HttpResponse> HttpResponseOutcome;
typedef std::function<bool(std::string)> HttpStreamCallback;


HttpClient();
~HttpClient();
Expand All @@ -40,6 +43,9 @@ namespace TencentCloud

void SetProxy(const NetworkProxy &proxy);

void SetStreamCallback(HttpStreamCallback callback);
bool WriteStreamFunction(std::string user_data);

static void InitGlobalState();
static void CleanupGlobalState();

Expand All @@ -48,6 +54,7 @@ namespace TencentCloud
int64_t m_reqTimeout;
int64_t m_connectTimeout;
NetworkProxy m_proxy;
HttpStreamCallback m_streamCallback;
#ifdef ENABLE_COMPRESS_MODULE
int GzipDecompress(const char *src, int srcLen, const char *dst, int* dstLen);
bool TryDecompress(const char *src, int srcLen, std::string &decompressData);
Expand Down
11 changes: 10 additions & 1 deletion core/include/tencentcloud/core/profile/HttpProfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
#define TENCENTCLOUD_CORE_HTTPPROFILE_H_

#include <string>
#include <functional>

namespace TencentCloud
{
static const int64_t TM_MINUTE_SECOND = 60;
typedef std::function<bool(std::string)> HttpStreamCallback;


class HttpProfile
{
Expand All @@ -48,14 +51,18 @@ namespace TencentCloud
void SetKeepAlive(bool flag=false);
bool IsKeepAlive() const;
HttpProfile::Scheme GetProtocol() const;
void SetHTTPStreamCallback(HttpStreamCallback callback);
HttpStreamCallback GetHTTPStreamCallback();


HttpProfile(const HttpProfile &o) :
m_reqMethod(o.m_reqMethod),
m_endpoint(o.m_endpoint),
m_protocol(o.m_protocol),
m_reqTimeout(o.m_reqTimeout),
m_connectTimeout(o.m_connectTimeout),
m_keepAlive(o.m_keepAlive)
m_keepAlive(o.m_keepAlive),
m_streamCallback(o.m_streamCallback)
{
}

Expand All @@ -69,6 +76,7 @@ namespace TencentCloud
m_reqTimeout = o.m_reqTimeout;
m_connectTimeout = o.m_connectTimeout;
m_keepAlive = o.m_keepAlive;
m_streamCallback = o.m_streamCallback;
}
return *this;
}
Expand All @@ -80,6 +88,7 @@ namespace TencentCloud
int64_t m_reqTimeout;
int64_t m_connectTimeout;
bool m_keepAlive;
HttpStreamCallback m_streamCallback;
};
}

Expand Down
1 change: 1 addition & 0 deletions core/src/AbstractClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ HttpClient::HttpResponseOutcome AbstractClient::DoRequest(const std::string &act
GenerateSignature(httpRequest);
m_httpClient->SetReqTimeout(httpProfile.GetReqTimeout()*1000);
m_httpClient->SetConnectTimeout(httpProfile.GetConnectTimeout()*1000);
m_httpClient->SetStreamCallback(httpProfile.GetHTTPStreamCallback());

return m_httpClient->SendRequest(httpRequest);
}
Expand Down
34 changes: 30 additions & 4 deletions core/src/http/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,19 @@ using namespace TencentCloud;

namespace
{
struct HttpWriteUserData {
std::ostringstream *out;
HttpClient* client;
};

size_t recvBody(char *ptr, size_t size, size_t nmemb, void *userdata)
{
std::ostringstream &out = *static_cast<std::ostringstream*>(userdata);
out << std::string(ptr, nmemb*size);
return nmemb * size;
struct HttpWriteUserData *data = static_cast<struct HttpWriteUserData *>(userdata);
auto recv_size = nmemb * size;
*data->out << std::string(ptr,recv_size);
bool result = data->client->WriteStreamFunction(ptr);

return result? nmemb * size : 0;
}

size_t recvHeaders(char *buffer, size_t size, size_t nitems, void *userdata)
Expand Down Expand Up @@ -124,6 +132,19 @@ void HttpClient::SetProxy(const NetworkProxy &proxy)
m_proxy = proxy;
}

void HttpClient::SetStreamCallback(HttpStreamCallback callback){
m_streamCallback = callback;
}

bool HttpClient::WriteStreamFunction(std::string user_data){
if (m_streamCallback){
return m_streamCallback(user_data);
}

return true;
}


HttpClient::HttpResponseOutcome HttpClient::SendRequest(const HttpRequest &request)
{
curl_easy_reset(m_curlHandle);
Expand Down Expand Up @@ -177,8 +198,13 @@ HttpClient::HttpResponseOutcome HttpClient::SendRequest(const HttpRequest &reque
}
curl_easy_setopt(m_curlHandle, CURLOPT_HTTPHEADER, header_list);
std::ostringstream out;
curl_easy_setopt(m_curlHandle, CURLOPT_WRITEDATA, &out);
struct HttpWriteUserData data;
data.out = &out;
data.client = this;
curl_easy_setopt(m_curlHandle, CURLOPT_WRITEDATA, &data);
curl_easy_setopt(m_curlHandle, CURLOPT_WRITEFUNCTION, recvBody);


setCUrlProxy(m_curlHandle, m_proxy);

char errbuf[CURL_ERROR_SIZE];
Expand Down
15 changes: 14 additions & 1 deletion core/src/profile/HttpProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ HttpProfile::HttpProfile() :
m_protocol(HttpProfile::Scheme::HTTPS),
m_reqTimeout(TM_MINUTE_SECOND),
m_connectTimeout(TM_MINUTE_SECOND),
m_keepAlive(false)
m_keepAlive(false),
m_streamCallback(nullptr)
{
}

Expand Down Expand Up @@ -82,3 +83,15 @@ bool HttpProfile::IsKeepAlive() const
{
return m_keepAlive;
}


void HttpProfile::SetHTTPStreamCallback(HttpStreamCallback callback){
m_streamCallback = std::move(callback);
}

HttpStreamCallback HttpProfile::GetHTTPStreamCallback(){
return m_streamCallback;
}



Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string>
#include <vector>
#include <map>
#include <cstdint>
#include <tencentcloud/core/AbstractModel.h>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ namespace TencentCloud
*/
bool SearchInfoHasBeenSet() const;


std::vector<std::string> SplitFullStreamedData(const std::string& data);
CoreInternalOutcome ParseFinshStreamData(const std::string &data);
void RemoveStrings(std::string& s, std::string p);
private:

/**
Expand Down
9 changes: 8 additions & 1 deletion hunyuan/src/v20230901/HunyuanClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <tencentcloud/hunyuan/v20230901/HunyuanClient.h>
#include <tencentcloud/core/Executor.h>
#include <tencentcloud/core/Runnable.h>
#include <iostream>

using namespace TencentCloud;
using namespace TencentCloud::Hunyuan::V20230901;
Expand Down Expand Up @@ -91,7 +92,13 @@ HunyuanClient::ChatCompletionsOutcome HunyuanClient::ChatCompletions(const ChatC
auto r = outcome.GetResult();
string payload = string(r.Body(), r.BodySize());
ChatCompletionsResponse rsp = ChatCompletionsResponse();
auto o = rsp.Deserialize(payload);
TencentCloud::CoreInternalOutcome o;
if (request.GetStream()) {
o = rsp.ParseFinshStreamData(payload);
} else {
o = rsp.Deserialize(payload);
}

if (o.IsSuccess())
return ChatCompletionsOutcome(rsp);
else
Expand Down
92 changes: 92 additions & 0 deletions hunyuan/src/v20230901/model/ChatCompletionsResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include <tencentcloud/core/utils/rapidjson/writer.h>
#include <tencentcloud/core/utils/rapidjson/stringbuffer.h>

#include <algorithm>
#include <vector>

using TencentCloud::CoreInternalOutcome;
using namespace TencentCloud::Hunyuan::V20230901::Model;
using namespace std;
Expand All @@ -35,6 +38,95 @@ ChatCompletionsResponse::ChatCompletionsResponse() :
{
}


std::vector<std::string> ChatCompletionsResponse::SplitFullStreamedData(const std::string& data) {
if (data.empty()) {
return {};
}

std::vector<std::string> split_data;
std::string temp;
std::istringstream iss(data);
while (std::getline(iss, temp)) {
if (temp.empty()) {
split_data.push_back(temp);
} else {
split_data.push_back(temp);
}
}

// remove empty strings from the vector
split_data.erase(std::remove_if(split_data.begin(), split_data.end(), [](const std::string& s) { return s.empty(); }), split_data.end());

return split_data;
}

void ChatCompletionsResponse::RemoveStrings(std::string& s, std::string p) {
std::string::size_type i = s.find(p);
while (i != std::string::npos) {
s.erase(i, p.length());
i = s.find(p, i);
}
}

CoreInternalOutcome ChatCompletionsResponse::ParseFinshStreamData(const std::string &data){
std::vector<std::string> data_lines = SplitFullStreamedData(data);

if (data_lines.empty()) {
return CoreInternalOutcome(Core::Error("response data is empty"));
}

std::vector<Choice> choices_list;
Message full_msg;
full_msg.SetRole("assistant");
for (auto& line : data_lines){
RemoveStrings(line, "data: ");

rapidjson::Document d;
d.Parse(line.c_str());
if (d.HasParseError() || !d.IsObject()){
return CoreInternalOutcome(Core::Error("response not json format"));
}

if (d.HasMember("Choices") && !d["Choices"].IsNull())
{
if (!d["Choices"].IsArray())
return CoreInternalOutcome(Core::Error("response `Choices` is not array type"));

const rapidjson::Value &tmpValue = d["Choices"];

for (rapidjson::Value::ConstValueIterator itr = tmpValue.Begin(); itr != tmpValue.End(); ++itr)
{
const rapidjson::Value &value = *itr;
string requestId = "";
if (value.HasMember("Delta") && !value["Delta"].IsNull())
{
if (!value["Delta"].IsObject())
{
return CoreInternalOutcome(Core::Error("response `Choice.Delta` is not object type").SetRequestId(requestId));
}

Delta current_delta;
CoreInternalOutcome outcome = current_delta.Deserialize(value["Delta"]);
if (!outcome.IsSuccess())
{
outcome.GetError().SetRequestId(requestId);
return outcome;
}
full_msg.SetContent(full_msg.GetContent() + current_delta.GetContent());
}
}
}
}

Choice item;
item.SetMessage(full_msg);
m_choices.push_back(item);
m_choicesHasBeenSet = true;

return CoreInternalOutcome(true);
}

CoreInternalOutcome ChatCompletionsResponse::Deserialize(const string &payload)
{
rapidjson::Document d;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string>
#include <vector>
#include <map>
#include <cstdint>
#include <tencentcloud/core/AbstractModel.h>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string>
#include <vector>
#include <map>
#include <cstdint>
#include <tencentcloud/core/AbstractModel.h>


Expand Down