Skip to content

Commit c571b89

Browse files
author
Raja Phanindra Chava
committed
refactor
1 parent c97cde3 commit c571b89

3 files changed

Lines changed: 9 additions & 14 deletions

File tree

sdk/cpp/src/openai_embedding_client.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ namespace foundry_local {
2020

2121
namespace {
2222
/// True for strings that are empty or contain only whitespace characters.
23-
/// Equivalent to C#'s IsNullOrWhiteSpace, JS's trim() === '', Python's .strip() == "".
2423
bool IsBlank(std::string_view s) {
2524
for (char c : s) {
2625
if (!std::isspace(static_cast<unsigned char>(c))) {

sdk/python/src/openai/embedding_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def generate_embeddings(self, inputs: List[str]) -> CreateEmbeddingResponse:
9898
ValueError: If *inputs* is empty or contains empty strings.
9999
FoundryLocalException: If the underlying native embeddings command fails.
100100
"""
101-
if not inputs or len(inputs) == 0:
101+
if not inputs:
102102
raise ValueError("Inputs must be a non-empty list of strings.")
103103

104104
for text in inputs:

sdk/rust/src/openai/embedding_client.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,23 @@ impl EmbeddingClient {
5555
.execute_command_async("embeddings".into(), Some(params))
5656
.await?;
5757

58-
// Patch the response to add fields required by async_openai types
59-
// that the server doesn't return (object on each item, usage)
58+
// The server omits two fields that async_openai's CreateEmbeddingResponse
59+
// requires: per-item `object` and top-level `usage`. Inject defaults before
60+
// deserializing.
6061
let mut response_value: Value = serde_json::from_str(&raw)?;
6162
if let Some(data) = response_value
6263
.get_mut("data")
6364
.and_then(|d| d.as_array_mut())
6465
{
6566
for item in data {
66-
if item.get("object").is_none() {
67-
item.as_object_mut()
68-
.map(|m| m.insert("object".into(), json!("embedding")));
67+
if let Some(obj) = item.as_object_mut() {
68+
obj.entry("object").or_insert_with(|| json!("embedding"));
6969
}
7070
}
7171
}
72-
if response_value.get("usage").is_none() {
73-
response_value.as_object_mut().map(|m| {
74-
m.insert(
75-
"usage".into(),
76-
json!({"prompt_tokens": 0, "total_tokens": 0}),
77-
)
78-
});
72+
if let Some(root) = response_value.as_object_mut() {
73+
root.entry("usage")
74+
.or_insert_with(|| json!({"prompt_tokens": 0, "total_tokens": 0}));
7975
}
8076

8177
let parsed: CreateEmbeddingResponse = serde_json::from_value(response_value)?;

0 commit comments

Comments
 (0)