Skip to content

Commit

Permalink
[Python API] Clean up utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-lavrenov committed Dec 31, 2024
1 parent 653b2ae commit 9bbdfd7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/python/py_llm_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ py::object call_common_generate(
DecodedResults res = pipe.generate(string_input, updated_config, streamer);
// If input was a string return a single string otherwise return DecodedResults.
if (updated_config.has_value() && (*updated_config).num_return_sequences == 1) {
results = py::cast<py::object>(pyutils::handle_utf8(res.texts)[0]);
results = py::cast<py::object>(pyutils::handle_utf8(res.texts[0]));
} else {
results = py::cast(res);
}
Expand Down
9 changes: 6 additions & 3 deletions src/python/py_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ py::list handle_utf8(const std::vector<std::string>& decoded_res) {
return res;
}

namespace {

bool py_object_is_any_map(const py::object& py_obj) {
if (!py::isinstance<py::dict>(py_obj)) {
return false;
Expand Down Expand Up @@ -290,15 +292,16 @@ ov::Any py_object_to_any(const py::object& py_obj, std::string property_name) {
OPENVINO_THROW("Property \"" + property_name + "\" got unsupported type.");
}

std::map<std::string, ov::Any> properties_to_any_map(const std::map<std::string, py::object>& properties) {
} // namespace

ov::AnyMap properties_to_any_map(const std::map<std::string, py::object>& properties) {
std::map<std::string, ov::Any> properties_to_cpp;
for (const auto& property : properties) {
properties_to_cpp[property.first] = py_object_to_any(property.second, property.first);
}
return properties_to_cpp;
}


ov::AnyMap kwargs_to_any_map(const py::kwargs& kwargs) {
ov::AnyMap params = {};

Expand Down Expand Up @@ -356,7 +359,7 @@ ov::genai::OptionalGenerationConfig update_config_from_kwargs(const ov::genai::O
return std::nullopt;

ov::genai::GenerationConfig res_config;
if(config.has_value())
if (config.has_value())
res_config = *config;

if (!kwargs.empty())
Expand Down
8 changes: 1 addition & 7 deletions src/python/py_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ py::list handle_utf8(const std::vector<std::string>& decoded_res);

py::str handle_utf8(const std::string& text);

ov::Any py_object_to_any(const py::object& py_obj, std::string property_name);

bool py_object_is_any_map(const py::object& py_obj);

ov::AnyMap py_object_to_any_map(const py::object& py_obj);

std::map<std::string, ov::Any> properties_to_any_map(const std::map<std::string, py::object>& properties);
ov::AnyMap properties_to_any_map(const std::map<std::string, py::object>& properties);

ov::AnyMap kwargs_to_any_map(const py::kwargs& kwargs);

Expand Down

0 comments on commit 9bbdfd7

Please sign in to comment.