Skip to content

Commit 0c13079

Browse files
authored
Pass headers to openai requests (#575)
1 parent c791606 commit 0c13079

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

src/c++/perf_analyzer/client_backend/openai/openai_client.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ ChatCompletionClient::ResponseHandler(
159159
Error
160160
ChatCompletionClient::AsyncInfer(
161161
std::function<void(InferResult*)> callback,
162-
std::string& serialized_request_body, const std::string& request_id)
162+
std::string& serialized_request_body, const std::string& request_id,
163+
const Headers& headers)
163164
{
164165
if (callback == nullptr) {
165166
return Error(
@@ -186,7 +187,7 @@ ChatCompletionClient::AsyncInfer(
186187
serialized_request_body.size());
187188

188189
CURL* multi_easy_handle = curl_easy_init();
189-
Error err = PreRunProcessing(multi_easy_handle, raw_request);
190+
Error err = PreRunProcessing(multi_easy_handle, raw_request, headers);
190191
if (!err.IsOk()) {
191192
curl_easy_cleanup(multi_easy_handle);
192193
return err;
@@ -200,7 +201,7 @@ ChatCompletionClient::AsyncInfer(
200201

201202
Error
202203
ChatCompletionClient::PreRunProcessing(
203-
CURL* curl, ChatCompletionRequest* request)
204+
CURL* curl, ChatCompletionRequest* request, const Headers& headers)
204205
{
205206
curl_easy_setopt(curl, CURLOPT_URL, url_.c_str());
206207
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
@@ -235,6 +236,12 @@ ChatCompletionClient::PreRunProcessing(
235236
struct curl_slist* list = nullptr;
236237
list = curl_slist_append(list, "Expect:");
237238
list = curl_slist_append(list, "Content-Type: application/json");
239+
240+
for (const auto& pr : headers) {
241+
std::string hdr = pr.first + ": " + pr.second;
242+
list = curl_slist_append(list, hdr.c_str());
243+
}
244+
238245
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
239246

240247
// The list will be freed when the request is destructed

src/c++/perf_analyzer/client_backend/openai/openai_client.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,15 @@ class ChatCompletionClient : public HttpClient {
156156
/// with a OpenAI-compatible server in both streaming and non-streaming case.
157157
Error AsyncInfer(
158158
std::function<void(InferResult*)> callback,
159-
std::string& serialized_request_body, const std::string& request_id);
159+
std::string& serialized_request_body, const std::string& request_id,
160+
const Headers& headers);
160161

161162
const InferStat& ClientInferStat() { return infer_stat_; }
162163

163164
private:
164165
// setup curl handle
165-
Error PreRunProcessing(CURL* curl, ChatCompletionRequest* request);
166+
Error PreRunProcessing(
167+
CURL* curl, ChatCompletionRequest* request, const Headers& headers);
166168

167169
static size_t ResponseHandler(
168170
void* contents, size_t size, size_t nmemb, void* userp);

src/c++/perf_analyzer/client_backend/openai/openai_client_backend.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ OpenAiClientBackend::AsyncInfer(
6767
auto raw_input = dynamic_cast<OpenAiInferInput*>(inputs[0]);
6868
raw_input->PrepareForRequest();
6969
RETURN_IF_CB_ERROR(http_client_->AsyncInfer(
70-
callback, raw_input->GetRequestBody(), options.request_id_));
70+
callback, raw_input->GetRequestBody(), options.request_id_,
71+
*http_headers_));
7172
return Error::Success;
7273
}
7374

0 commit comments

Comments
 (0)