File size: 5,953 Bytes
a7060a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
---
title: Connect to LLM Backend
type: plugins
category: Automation
cat: automation
order: 40
meta_title: Connect to LLM Backend
meta_description: Sends prompts to an LLM URL
---

<img src="/images/plugins/llm-backend-thumb.png" alt="" class="gif-border" style="max-width: 552px !important;" />

!!! note
     For information about modifying this plugin or creating your own custom plugins, see [Customize and Build Your Own Plugins](custom).

     For general plugin information, see [Plugins for projects](/guide/plugins) and [Plugin FAQ](faq).

## About

This plugin connects to an open LLM endpoint, allowing you to generate responses as part of the annotation workflow. 

![Screenshot of LLM analyzing](/images/plugins/llm-backend.gif)

## Plugin

Before using this, replace the `MY_URL_ROOT` value with a URL that does not require authentication. This would typically be an internal service or an LLM behind a proxy. 

```javascript 
window.LSI = LSI;

const baseUrl = "MY_URL_ROOT";

/**
 * Makes a request to the configured LLM sending the given prompt
 */
async function fetchLLM(prompt) {
	const params = {
		prompt,
		llm_endpoint_name: "chatgpt",
		redteam_categories: ["cat1"],
	};

	const searchParams = new URLSearchParams(params).toString();
	const url = `${baseUrl}?${searchParams}`;

	const response = await fetch(url, {
		method: "POST",
		headers: {
			"Content-Type": "application/json",
			// No auth needed because the API is open
		},
	});

	const data = await response.json();
}

/**
 * Sends the introduced prompt to the LLM endpoint and attaches the given results to the annotation
 */
async function sendPrompt() {
	const promptTag = LSI.annotation.names.get("prompt");
	promptTag.submitChanges();
	const prompt = promptTag.result?.value.text.join("\n");

	if (!prompt) {
		Htx.showModal("The prompt is empty", "error");
		return false;
	}

	let response;

	// console.log("Input prompt:" + prompt);
	try {
		response = await fetchLLM(prompt);
	} catch (error) {
		Htx.showModal(
			`Error fetching the LLM endpoint "${baseUrl}": ${error.message}`,
			"error",
		);
		return false;
	}
	const results = [];

	const llmResponse = response.LLM_response;
	if (llmResponse) {
		const llmResult = {
			from_name: "response",
			to_name: "placeholder",
			type: "textarea",
			value: { text: [] },
		};
		results.push(llmResult);
	}
	// console.log("Response:" + llmResponse["LLM_response"]);

	const category = response.Category?.category;
	if (category?.length) {
		const attackResult = {
			from_name: "category",
			to_name: "placeholder",
			type: "choices",
			value: { choices: category },
		};
		results.push(attackResult);
		// console.log("Category:" + category);
	}

	const reasonText = response.Type?.reason;
	if (reasonText) {
		const reasonResult = {
			from_name: "reason",
			to_name: "placeholder",
			type: "textarea",
			value: { text: [reasonText] },
		};
		results.push(reasonResult);
		// console.log("Reason:" + reason);
	}

	LSI.annotation.deserializeResults(results);
}

/**
 * Sets up the onClick event of the template to trigger the LLM request
 */
function setup() {
	const aBtn = document.querySelector(".analyzeButton");
	const button = document.createElement("button");
	button.textContent = "Analyze"; // Set the button text

	// Attach an onclick event to the button
	button.onclick = sendPrompt;

	// Insert the button into the div
	aBtn.replaceChildren(button);
}

setup();

```

**Related LSI instance methods:**

* [.annotation)](custom#LSI-annotation)


## Labeling config

!!! info Tip
    You can add `value="$text"` to the TextArea parameters of the prompt to pre-fill the text from your data. 

```xml
<View>
    <Style>
      .lsf-main-content.lsf-requesting .prompt::before { content: ' loading...'; color: #808080; }
      .placeholder { display: none; }
      .llm_response textarea { background-color: #f0f0f0; /* Optional: gives a "disabled" feel */ }
      .analyzeButton button {
        background: #1890ff;
        border-color: #1890ff;
        box-shadow: 0 2px 0 rgba(0, 0, 0, .045);
        color: #fff;
        text-shadow: 0 -1px 0 rgba(0, 0, 0, .12);
        padding: 4px 15px;
      }
    </Style>

    <View className="placeholder">
    	<Text name="placeholder" value="placeholder" />
  	</View>

    <Header value="Enter Prompt to Analyze:"/>
    <TextArea
      name="prompt"
      toName="placeholder"
      transcription="true"
      showSubmitButton="false"
      editable="true"
      rows="4"
      maxSubmissions="1"
      placeholder="Type the prompt here..."
    />
    <View className="analyzeButton"></View>

    <Header value="LLM Response:"/>
    <View className="llm_response">
      <TextArea
        name="response"
        toName="placeholder"
        transcription="true"
        rows="6"
        showSubmitButton="false"
        editable="false"
        smart="false"
        maxSubmissions="1"
        placeholder="The LLM response will appear here..."
      />
    </View>

    <Header value="Category:"/>
    <Choices name="category" toName="placeholder" choice="single" showInLine="true">
      <Choice value="none" />
      <Choice value="cat1" />
      <Choice value="cat2" />
      <Choice value="cat3" />
    </Choices>

    <Header value="Reason"/>
    <TextArea
      name="reason"
      toName="placeholder"
      rows="2"
      editable="false"
      smart="false"
      maxSubmissions="1"
      placeholder="Write something here..."
    />
</View>
```

**Related tags:**

* [View](/tags/view.html)
* [Style](/tags/style.html)
* [Text](/tags/text.html)
* [Header](/tags/header.html)
* [TextArea](/tags/textarea.html)
* [Choices](/tags/choices.html)

## Sample data

```json
[
   {
      "data": {
         "text": "What is the closest relative to an opossum?"
      }
   },
   {
      "data": {
         "text": "What is a fun opossum fact?"
      }
   },
   {
      "data": {
         "text": "Why are opossums cool?"
      }
   }
]
```