Skip to content

Commit 64719d5

Browse files
committed
feat(cli): add file input support for prompts and examples
- Introduced `read_text_file` function to read content from text files. - Updated CLI to accept prompt and example inputs from files. - Enhanced argument parsing for mutually exclusive groups.
1 parent 744c042 commit 64719d5

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

ask2api.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,39 @@ def convert_example_to_schema(example, _cache=None):
196196
return schema
197197

198198

199+
def read_text_file(path):
200+
"""Read content from a text file."""
201+
with open(path, "r", encoding="utf-8") as f:
202+
return f.read().strip()
203+
204+
199205
def main():
200206
parser = argparse.ArgumentParser(
201207
formatter_class=argparse.RawDescriptionHelpFormatter,
202208
epilog=Config.get_env_vars_help(),
203209
)
204-
parser.add_argument(
210+
prompt_group = parser.add_mutually_exclusive_group(required=True)
211+
prompt_group.add_argument(
205212
"-p",
206213
"--prompt",
207-
required=True,
208214
help="Natural language prompt",
209215
)
216+
prompt_group.add_argument(
217+
"-pf",
218+
"--prompt-file",
219+
help="Path to text file containing the prompt",
220+
)
210221
schema_group = parser.add_mutually_exclusive_group(required=True)
211222
schema_group.add_argument(
212223
"-e",
213224
"--example",
214225
help='JSON example as a string (e.g., \'{"country": "France", "city": "Paris"}\')',
215226
)
227+
schema_group.add_argument(
228+
"-ef",
229+
"--example-file",
230+
help="Path to text file containing JSON example",
231+
)
216232
schema_group.add_argument(
217233
"-sf",
218234
"--schema-file",
@@ -231,12 +247,18 @@ def main():
231247
)
232248
args = parser.parse_args()
233249

250+
# Get prompt from file or argument
251+
prompt = read_text_file(args.prompt_file) if args.prompt_file else args.prompt
252+
234253
# Load schema from file or parse from string
235254
if args.schema_file:
236255
with open(args.schema_file, "r", encoding="utf-8") as f:
237256
schema = json.load(f)
238257
else:
239-
example = json.loads(args.example)
258+
example_str = (
259+
read_text_file(args.example_file) if args.example_file else args.example
260+
)
261+
example = json.loads(example_str)
240262
schema = convert_example_to_schema(example)
241263

242264
system_prompt = """
@@ -252,12 +274,12 @@ def main():
252274
if args.image:
253275
# Multimodal content: text + image
254276
user_content = [
255-
{"type": "text", "text": args.prompt},
277+
{"type": "text", "text": prompt},
256278
prepare_image_content(args.image),
257279
]
258280
else:
259281
# Text-only content
260-
user_content = args.prompt
282+
user_content = prompt
261283

262284
config = Config.from_env()
263285

0 commit comments

Comments
 (0)