|
41 | 41 | remove_alias, |
42 | 42 | ) |
43 | 43 | from llm.models import _BaseConversation, ChainResponse |
| 44 | +from .cli_utils import prompt_output |
44 | 45 |
|
45 | 46 | from .migrations import migrate |
46 | 47 | from .plugins import pm, load_plugins |
@@ -471,6 +472,7 @@ def cli(): |
471 | 472 | is_flag=True, |
472 | 473 | help="Extract last fenced code block", |
473 | 474 | ) |
| 475 | +@click.option("--render", is_flag=True, help="Render for terminal") |
474 | 476 | def prompt( |
475 | 477 | prompt, |
476 | 478 | system, |
@@ -502,6 +504,7 @@ def prompt( |
502 | 504 | usage, |
503 | 505 | extract, |
504 | 506 | extract_last, |
| 507 | + render, |
505 | 508 | ): |
506 | 509 | """ |
507 | 510 | Execute a prompt |
@@ -834,59 +837,37 @@ def read_prompt(): |
834 | 837 | if async_: |
835 | 838 |
|
836 | 839 | async def inner(): |
837 | | - if should_stream: |
838 | | - response = prompt_method( |
839 | | - prompt, |
840 | | - attachments=resolved_attachments, |
841 | | - system=system, |
842 | | - schema=schema, |
843 | | - fragments=resolved_fragments, |
844 | | - system_fragments=resolved_system_fragments, |
845 | | - **kwargs, |
846 | | - ) |
847 | | - async for chunk in response: |
848 | | - print(chunk, end="") |
849 | | - sys.stdout.flush() |
850 | | - print("") |
851 | | - else: |
852 | | - response = prompt_method( |
853 | | - prompt, |
854 | | - fragments=resolved_fragments, |
855 | | - attachments=resolved_attachments, |
856 | | - schema=schema, |
857 | | - system=system, |
858 | | - system_fragments=resolved_system_fragments, |
859 | | - **kwargs, |
860 | | - ) |
861 | | - text = await response.text() |
862 | | - if extract or extract_last: |
863 | | - text = ( |
864 | | - extract_fenced_code_block(text, last=extract_last) or text |
865 | | - ) |
866 | | - print(text) |
867 | | - return response |
| 840 | + return prompt_output( |
| 841 | + prompt_method, |
| 842 | + prompt, |
| 843 | + should_stream, |
| 844 | + render, |
| 845 | + extract, |
| 846 | + extract_last, |
| 847 | + resolved_fragments, |
| 848 | + resolved_attachments, |
| 849 | + system, |
| 850 | + schema, |
| 851 | + resolved_system_fragments, |
| 852 | + kwargs, |
| 853 | + ) |
868 | 854 |
|
869 | 855 | response = asyncio.run(inner()) |
870 | 856 | else: |
871 | | - response = prompt_method( |
| 857 | + response = prompt_output( |
| 858 | + prompt_method, |
872 | 859 | prompt, |
873 | | - fragments=resolved_fragments, |
874 | | - attachments=resolved_attachments, |
875 | | - system=system, |
876 | | - schema=schema, |
877 | | - system_fragments=resolved_system_fragments, |
878 | | - **kwargs, |
| 860 | + should_stream, |
| 861 | + render, |
| 862 | + extract, |
| 863 | + extract_last, |
| 864 | + resolved_fragments, |
| 865 | + resolved_attachments, |
| 866 | + system, |
| 867 | + schema, |
| 868 | + resolved_system_fragments, |
| 869 | + kwargs, |
879 | 870 | ) |
880 | | - if should_stream: |
881 | | - for chunk in response: |
882 | | - print(chunk, end="") |
883 | | - sys.stdout.flush() |
884 | | - print("") |
885 | | - else: |
886 | | - text = response.text() |
887 | | - if extract or extract_last: |
888 | | - text = extract_fenced_code_block(text, last=extract_last) or text |
889 | | - print(text) |
890 | 871 | # List of exceptions that should never be raised in pytest: |
891 | 872 | except (ValueError, NotImplementedError) as ex: |
892 | 873 | raise click.ClickException(str(ex)) |
|
0 commit comments