Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cli auto-prompt analyze - prompt analyzer in cli #288

Merged
merged 6 commits into from
Aug 29, 2024

Conversation

wenzhe-log10
Copy link
Collaborator

@wenzhe-log10 wenzhe-log10 commented Aug 27, 2024

Three input format allowed:

  1. string prompt, e.g. "Summarize this article in 3 sentences."
  2. openai chat messages, e.g. [{"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi"}]
  3. log10 completion (can be downloaded via cli log10 completions get), e.g.
{..., "request": {..., "messages": [{"role": "user", "content": "Hello"}, {"role":
"assistant", "content": "Hi"}], ...}, "response": {...}}

Doc:

❯ log10 auto-prompt analyze --help
Usage: log10 auto-prompt analyze [OPTIONS]

  Analyze a prompt or messages and provide suggestions on how to improve it.

Options:
  -p, --prompt TEXT  The prompt to analyze. Provide a string or a file
                     containing the prompt. We allow three formats: 1) string
                     prompt, e.g. "Summarize this article in 3 sentences." 2)
                     messages, e.g. [{"role": "user", "content": "Hello"},
                     {"role": "assistant", "content": "Hi"}] 3) log10
                     completion, e.g. {..., "request": {..., "messages":
                     [{"role": "user", "content": "Hello"}, {"role":
                     "assistant", "content": "Hi"}], ...}, "response": {...}}

e.g.
log10 auto-prompt analyze -p "this is a test prompt for autoprompt cli"
or pass a file
log10 auto-prompt analyze -p test.prompt

@wenzhe-log10 wenzhe-log10 requested review from kxtran and nqn August 27, 2024 06:10
Comment on lines 48 to 57
prompt = "\n\n".join([f'{m["role"]}: {m["content"]}' for m in prompt_json if m["role"] in ALLOWED_ROLES])
elif isinstance(prompt_json, dict) and "request" in prompt_json and "messages" in prompt_json["request"]:
# prompt is a log10 completion
prompt = "\n\n".join(
[
f'{m["role"]}: {m["content"]}'
for m in prompt_json["request"]["messages"]
if m["role"] in ALLOWED_ROLES
]
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can extract the building prompt in its own function, wdyt?

if isinstance(prompt_json, list) and all("role" in item and "content" in item for item in prompt_json):
# prompt is a list of messages
prompt = "\n\n".join([f'{m["role"]}: {m["content"]}' for m in prompt_json if m["role"] in ALLOWED_ROLES])
elif isinstance(prompt_json, dict) and "request" in prompt_json and "messages" in prompt_json["request"]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
elif isinstance(prompt_json, dict) and "request" in prompt_json and "messages" in prompt_json["request"]:
elif isinstance(prompt_json, dict) and "messages" in prompt_json.get("request", {}):

from log10.prompt_analyzer import PromptAnalyzer, display_prompt_analyzer_suggestions


# ignor "tool" and "funciton" roles
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# ignor "tool" and "funciton" roles
# ignore "tool" and "function" roles

Analyze a prompt or messages and provide suggestions on how to improve it.

Options:
-p, --prompt TEXT The prompt to analyze. Provide a string or a file
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we have a separate flag for file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are checking the file internally and keeping the option simple for users. Similar to what we have for other cli option, like --ids for benchmark_models cli.


# try to parse the prompt as a json
try:
prompt_json = json.loads(prompt)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if prompt is empty after parsing the text or reading from the file, should we exit immediately?

@wenzhe-log10 wenzhe-log10 force-pushed the wenzhe/eng-1060-cli-auto-prompt branch from ee52421 to 5f9bdb5 Compare August 29, 2024 02:04
@wenzhe-log10 wenzhe-log10 merged commit 79abd19 into main Aug 29, 2024
3 checks passed
@wenzhe-log10 wenzhe-log10 deleted the wenzhe/eng-1060-cli-auto-prompt branch August 29, 2024 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants