Skip to content

Commit 59f5d0e

Browse files
committed
feat: update argument handling logic
1 parent d8da8b2 commit 59f5d0e

File tree

2 files changed

+23
-34
lines changed

2 files changed

+23
-34
lines changed

examples/image_search/main.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,17 @@ def image_object_embedding_flow(
6969
)
7070
img_embeddings = data_scope.add_collector()
7171
with data_scope["images"].row() as img:
72-
caption_ds = img["content"].transform(
72+
img["caption"] = flow_builder.transform(
7373
cocoindex.functions.ExtractByLlm(
74-
llm_spec=cocoindex.llm.LlmSpec(
75-
api_type=cocoindex.LlmApiType.OLLAMA,
76-
model="llama3.1",
74+
llm_spec=cocoindex.LlmSpec(
75+
api_type=cocoindex.LlmApiType.GEMINI, model="gemini-2.0-flash"
7776
),
78-
# Replace by this spec below, to use OpenAI API model instead of ollama
77+
# Replace by this spec below, to use OpenAI API model instead of gemini
7978
# llm_spec=cocoindex.LlmSpec(
8079
# api_type=cocoindex.LlmApiType.OPENAI, model="gpt-4o"),
81-
# Replace by this spec below, to use Gemini API model
82-
# llm_spec=cocoindex.LlmSpec(
83-
# api_type=cocoindex.LlmApiType.GEMINI, model="gemini-2.0-flash"),
80+
# Replace by this spec below, to use Ollama API model
81+
# llm_spec=cocoindex.llm.LlmSpec(
82+
# api_type=cocoindex.LlmApiType.OLLAMA, model="llama3.1"),
8483
# Replace by this spec below, to use Anthropic API model
8584
# llm_spec=cocoindex.LlmSpec(
8685
# api_type=cocoindex.LlmApiType.ANTHROPIC, model="claude-3-5-sonnet-latest"),
@@ -91,14 +90,15 @@ def image_object_embedding_flow(
9190
"Mention what each animal is doing."
9291
),
9392
output_type=str,
94-
)
93+
),
94+
image=img["content"],
9595
)
96-
embedding_ds = img["content"].transform(embed_image)
96+
img["embedding"] = img["content"].transform(embed_image)
9797
img_embeddings.collect(
9898
id=cocoindex.GeneratedField.UUID,
9999
filename=img["filename"],
100-
caption=caption_ds,
101-
embedding=embedding_ds,
100+
caption=img["caption"],
101+
embedding=img["embedding"],
102102
)
103103

104104
img_embeddings.export(

src/ops/functions/extract_by_llm.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -130,28 +130,17 @@ impl SimpleFunctionFactoryBase for Factory {
130130
args_resolver: &mut OpArgsResolver<'a>,
131131
_context: &FlowInstanceContext,
132132
) -> Result<(Args, EnrichedValueType)> {
133-
let mut args = Args {
134-
text: None,
135-
image: None,
136-
};
137-
138-
// Handle positional argument
139-
if let Some(arg) = args_resolver.next_optional_arg("")? {
140-
match arg.typ.typ {
141-
ValueType::Basic(BasicValueType::Str) => args.text = Some(arg),
142-
ValueType::Basic(BasicValueType::Bytes) => args.image = Some(arg),
143-
_ => api_bail!(
144-
"Positional argument must be of type 'Str' or 'Bytes', got {}",
145-
arg.typ.typ
146-
),
147-
}
148-
}
149-
150-
if args.text.is_none() && args.image.is_none() {
151-
api_bail!("At least one of 'text' or 'image' must be provided");
152-
}
153-
154-
Ok((args, spec.output_type.clone()))
133+
Ok((
134+
Args {
135+
text: args_resolver
136+
.next_optional_arg("text")?
137+
.expect_type(&ValueType::Basic(BasicValueType::Str))?,
138+
image: args_resolver
139+
.next_optional_arg("image")?
140+
.expect_type(&ValueType::Basic(BasicValueType::Bytes))?,
141+
},
142+
spec.output_type.clone(),
143+
))
155144
}
156145

157146
async fn build_executor(

0 commit comments

Comments
 (0)