|
| 1 | +--- |
| 2 | +title: Prompts examples |
| 3 | +short: Examples |
| 4 | +tier: enterprise |
| 5 | +type: guide |
| 6 | +order: 0 |
| 7 | +order_enterprise: 236 |
| 8 | +meta_title: Prompts examples |
| 9 | +meta_description: Example use cases for Prompts |
| 10 | +section: Prompts |
| 11 | +date: 2025-01-15 12:11:22 |
| 12 | +--- |
| 13 | + |
| 14 | + |
| 15 | +## Autolabel image captions |
| 16 | + |
| 17 | +This example demonstrates how to set up Prompts to predict image captions. |
| 18 | + |
| 19 | +1. [Create a new label studio project](setup_project) by importing image data via [cloud storage](storage). |
| 20 | + |
| 21 | +!!! note |
| 22 | + Prompts does not currently support image data uploaded as raw images. Only image references (HTTP URIs to images) or images imported via cloud storage are supported. |
| 23 | + |
| 24 | +!!! info Tip |
| 25 | + If you’d like to, you can generate a dataset to test the process using [https://data.heartex.net](https://data.heartex.net). |
| 26 | + |
| 27 | +2. Create a [label config](setup) for image captioning, for example: |
| 28 | + |
| 29 | +```xml |
| 30 | +<View> |
| 31 | + <Image name="image" value="$image"/> |
| 32 | + <Header value="Describe the image:"/> |
| 33 | + <TextArea name="caption" toName="image" placeholder="Enter description here..." |
| 34 | + rows="5" maxSubmissions="1"/> |
| 35 | +</View> |
| 36 | +``` |
| 37 | +3. Navigate to **Prompts** from the sidebar, and [create a prompt](prompts_create) for the project |
| 38 | + |
| 39 | +!!! note |
| 40 | + If you have not yet set up API the keys you want to use, do that now: [API keys](prompts_create#Model-provider-API-keys). |
| 41 | + |
| 42 | +4. Add the instruction you’d like to provide the LLM to caption your images. For example: |
| 43 | + |
| 44 | + *Explain the contents of the following image: `{image}`* |
| 45 | + |
| 46 | +!!! note |
| 47 | + Ensure you include `{image}` in your instructions. Click `image` above the instruction field to insert it. |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +!!! info Tip |
| 52 | + You can also automatically generate the instructions using the [**Enhance Prompt** action](prompts_draft#Enhance-prompt). Before you can use this action, you must at least add the variable name `{image}` and then click **Save**. |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +5. Run the prompt. View predictions to accept or correct. |
| 57 | + |
| 58 | + You can [read more about evaluation metrics](prompts_draft#Evaluation-results) and ways to assess your prompt performance. |
| 59 | + |
| 60 | +!!! info Tip |
| 61 | + Use the drop-down menu above the results field to change the subset of data being used (e.g. only data with Ground Truth annotations, or a small sample of records). |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +6. Accept the [predictions as annotations](prompts_predictions#Create-annotations-from-predictions). |
| 66 | + |
| 67 | + |
| 68 | +## Evaluate LLM outputs for toxicity |
| 69 | + |
| 70 | +This example demonstrates how to set up Prompts to evaluate if the LLM-generated output text is classified as harmful, offensive, or inappropriate. |
| 71 | + |
| 72 | +1. [Create a new label studio project](setup_project) by importing text data of LLM-generated outputs. |
| 73 | + |
| 74 | + You can use this preprocessed sample of the [jigsaw_toxicity](https://huggingface.co/datasets/tasksource/jigsaw_toxicity) dataset as an example. See [the appendix](#Appendix-Generate-dataset) for how this was generated. |
| 75 | +2. Create a [label config](setup) for toxicity detection, for example: |
| 76 | + |
| 77 | +```xml |
| 78 | +<View> |
| 79 | + <Header value="Comment" /> |
| 80 | + <Text name="comment" value="$comment_text"/> |
| 81 | + |
| 82 | + <Header value="Toxic" size="3"/> |
| 83 | + <Choices name="toxic" toName="comment" choice="single" showInline="true"> |
| 84 | + <Choice value="Yes" alias="1"/> |
| 85 | + <Choice value="No" alias="0"/> |
| 86 | + </Choices> |
| 87 | + <Header value="Severely Toxic" size="3"/> |
| 88 | + <Choices name="severe_toxic" toName="comment" choice="single" showInline="true"> |
| 89 | + <Choice value="Yes" alias="1"/> |
| 90 | + <Choice value="No" alias="0"/> |
| 91 | + </Choices> |
| 92 | + <Header value="Insult" size="3"/> |
| 93 | + <Choices name="insult" toName="comment" choice="single" showInline="true"> |
| 94 | + <Choice value="Yes" alias="1"/> |
| 95 | + <Choice value="No" alias="0"/> |
| 96 | + </Choices> |
| 97 | + <Header value="Threat" size="3"/> |
| 98 | + <Choices name="threat" toName="comment" choice="single" showInline="true"> |
| 99 | + <Choice value="Yes" alias="1"/> |
| 100 | + <Choice value="No" alias="0"/> |
| 101 | + </Choices> |
| 102 | + <Header value="Obscene" size="3"/> |
| 103 | + <Choices name="obscene" toName="comment" choice="single" showInline="true"> |
| 104 | + <Choice value="Yes" alias="1"/> |
| 105 | + <Choice value="No" alias="0"/> |
| 106 | + </Choices> |
| 107 | + <Header value="Identity Hate" size="3"/> |
| 108 | + <Choices name="identity_hate" toName="comment" choice="single" showInline="true"> |
| 109 | + <Choice value="Yes" alias="1"/> |
| 110 | + <Choice value="No" alias="0"/> |
| 111 | + </Choices> |
| 112 | + |
| 113 | + <Header value="Reasoning" size="3"/> |
| 114 | + <TextArea name="reasoning" toName="comment" editable="true" placeholder="Provide reasoning for your choices here..."/> |
| 115 | +</View> |
| 116 | +``` |
| 117 | + |
| 118 | +3. Navigate to **Prompts** from the sidebar, and [create a prompt](prompts_create) for the project |
| 119 | + |
| 120 | +!!! note |
| 121 | + If you have not yet set up API the keys you want to use, do that now: [API keys](prompts_create#Model-provider-API-keys). |
| 122 | + |
| 123 | +4. Add the instruction you’d like to provide the LLM to best evaluate the text. For example: |
| 124 | + |
| 125 | + *Determine whether the following text falls into any of the following categories (for each, provide a "0" for False and "1" for True):* |
| 126 | + |
| 127 | + *toxic, severe_toxic, insult, threat, obscene, and identity_hate.* |
| 128 | + |
| 129 | + *Comment:* |
| 130 | + |
| 131 | + *`{comment_text}`* |
| 132 | + |
| 133 | + |
| 134 | +!!! note |
| 135 | + Ensure you include `{comment_text}` in your instructions. Click `comment_text` above the instruction field to insert it. |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +!!! info Tip |
| 140 | + You can also automatically generate the instructions using the [**Enhance Prompt** action](prompts_draft#Enhance-prompt). Before you can use this action, you must at least add the variable name `{comment_text}` and then click **Save**. |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | +5. Run the prompt. View predictions to accept or correct. |
| 145 | + |
| 146 | + You can [read more about evaluation metrics](prompts_draft#Evaluation-results) and ways to assess your prompt performance. |
| 147 | + |
| 148 | +!!! info Tip |
| 149 | + Use the drop-down menu above the results field to change the subset of data being used (e.g. only data with Ground Truth annotations, or a small sample of records). |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +6. Accept the [predictions as annotations](prompts_predictions#Create-annotations-from-predictions). |
| 154 | + |
| 155 | +### Appendix: Generate dataset |
| 156 | + |
| 157 | +Download the jigsaw_toxicity dataset, then downsample/format using the following script (modify the `INPUT_PATH` and `OUTPUT_PATH` to suit your needs): |
| 158 | + |
| 159 | +```python |
| 160 | +import pandas as pd |
| 161 | +import json |
| 162 | + |
| 163 | + |
| 164 | +def gen_task(row): |
| 165 | + labels = [ |
| 166 | + { |
| 167 | + "from_name": field, |
| 168 | + "to_name": "comment", |
| 169 | + "type": "choices", |
| 170 | + "value": {"choices": [str(int(row._asdict()[field]))]}, |
| 171 | + } |
| 172 | + for field in [ |
| 173 | + "toxic", |
| 174 | + "severe_toxic", |
| 175 | + "insult", |
| 176 | + "threat", |
| 177 | + "obscene", |
| 178 | + "identity_hate", |
| 179 | + ] |
| 180 | + ] |
| 181 | + return { |
| 182 | + "data": {"comment_text": row.comment_text}, |
| 183 | + "annotations": [ |
| 184 | + { |
| 185 | + "result": labels, |
| 186 | + "ground_truth": True, |
| 187 | + "was_cancelled": False, |
| 188 | + } |
| 189 | + ], |
| 190 | + } |
| 191 | + |
| 192 | + |
| 193 | +INPUT_PATH = "/Users/pakelley/Downloads/Jigsaw Toxicity Train.csv" |
| 194 | +OUTPUT_PATH = "/Users/pakelley/Downloads/toxicity-sample-ls-format.json" |
| 195 | + |
| 196 | +df = pd.read_csv(INPUT_PATH) |
| 197 | +sample = df.sample(n=100) |
| 198 | +label_studio_tasks = [gen_task(row) for row in sample.itertuples()] |
| 199 | +with open(OUTPUT_PATH, "w") as f: |
| 200 | + json.dump(label_studio_tasks, f) |
| 201 | +``` |
| 202 | + |
| 203 | +If you choose to, you could also easily change how many records to use (or use the entire dataset by removing the sample step). |
0 commit comments