Skip to content

Commit

Permalink
Expanded example openAI node to submit a more sophisticated request with
Browse files Browse the repository at this point in the history
an image
  • Loading branch information
yassiezar committed Aug 26, 2024
1 parent 6dc7b8d commit 1a3bcea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class CbHttpRequest : public cl_http::CbHttpRequestBase
void runtimeConfigure() override
{
// Get OpenAI key from environemt
char const * env_key = std::getenv("OPENAI_API_KEY");
const auto env_key = std::getenv("OPENAI_API_KEY");
openai_key_ = env_key == NULL ? std::string() : std::string(env_key);

this->requiresClient(cl_http_);
Expand All @@ -74,28 +74,35 @@ class CbHttpRequest : public cl_http::CbHttpRequestBase
}

// Gather image data
auto image = cv::imread("/home/jaycee/Downloads/wood_table.jpg");
auto image = cv::imread("~/Downloads/wood_table.jpg");
std::vector<uchar> img_buf;
cv::imencode(".jpg", image, img_buf);
const auto *encoded_img_b64 = reinterpret_cast<unsigned char *>(img_buf.data());
const std::string encoded_img = base64_encode(encoded_img_b64, img_buf.size());

// Construct HTTP request
nlohmann::json req_body_json;
req_body_json["model"] = "gpt-4o";
req_body_json["model"] = "gpt-4o-mini";
req_body_json["max_tokens"] = 300;
req_body_json["messages"][0]["role"] = "user";
req_body_json["messages"][0]["content"][0]["type"] = "text";
req_body_json["messages"][0]["content"][0]["text"] = "Is this a door, yes or no";
req_body_json["messages"][0]["content"][1]["type"] = "image_url";
req_body_json["messages"][0]["content"][1]["image_url"]["url"] = ("data:image/jpg;base64," + encoded_img);
req_body_json["messages"][0]["content"][1]["image_url"]["url"] = "data:image/jpeg;base64," + encoded_img;

const std::string req_body = req_body_json.dump();
cl_http_->makeRequest(cl_http::ClHttp::kHttpRequestMethod::POST, "/", req_body);

std::unordered_map<std::string, std::string> headers;
headers["Authorization"] = "Bearer " + openai_key_;
headers["Content-Type"] = "application/json";
headers["Content-Length"] = std::to_string(req_body.length());

cl_http_->makeRequest(cl_http::ClHttp::kHttpRequestMethod::POST, "/v1/chat/completions", req_body, headers);
}

void onResponseReceived(const cl_http::ClHttp::TResponse & response) override
{
RCLCPP_INFO_STREAM(this->getNode()->get_logger(), "Responce recieved: ");
RCLCPP_INFO_STREAM(this->getNode()->get_logger(), "Response recieved: ");
RCLCPP_INFO_STREAM(this->getNode()->get_logger(), response.body());
triggerTranstition();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class OrHttp : public smacc2::Orthogonal<OrHttp> {
void onInitialize() override {
auto http_client =
this->createClient<cl_http::ClHttp>(
"https://api.openai.com/v1/chat/completions");
"https://api.openai.com");
}
};
} // namespace sm_atomic_openai

0 comments on commit 1a3bcea

Please sign in to comment.