autogen

Форк
0
/
agentchat_transform_messages.ipynb 
588 строк · 25.0 Кб
1
{
2
 "cells": [
3
  {
4
   "cell_type": "markdown",
5
   "id": "b7549a27-bc4a-4609-bb25-cc7d95cf8c23",
6
   "metadata": {},
7
   "source": [
8
    "# Preprocessing Chat History with `TransformMessages`\n",
9
    "\n",
10
    "## Introduction\n",
11
    "This notebook illustrates how to use `TransformMessages` give any `ConversableAgent` the ability to handle long contexts, sensitive data, and more.\n",
12
    "\n",
13
    "````{=mdx}\n",
14
    ":::info Requirements\n",
15
    "Install `pyautogen`:\n",
16
    "```bash\n",
17
    "pip install pyautogen\n",
18
    "```\n",
19
    "\n",
20
    "For more information, please refer to the [installation guide](/docs/installation/).\n",
21
    ":::\n",
22
    "````"
23
   ]
24
  },
25
  {
26
   "cell_type": "code",
27
   "execution_count": 1,
28
   "id": "47773f79-c0fd-4993-bc6e-3d1a57690118",
29
   "metadata": {},
30
   "outputs": [],
31
   "source": [
32
    "import copy\n",
33
    "import pprint\n",
34
    "import re\n",
35
    "from typing import Dict, List, Tuple\n",
36
    "\n",
37
    "import autogen\n",
38
    "from autogen.agentchat.contrib.capabilities import transform_messages, transforms"
39
   ]
40
  },
41
  {
42
   "cell_type": "code",
43
   "execution_count": 2,
44
   "id": "9f09246b-a7d0-4238-b62c-1e72c7d815b3",
45
   "metadata": {},
46
   "outputs": [],
47
   "source": [
48
    "config_list = autogen.config_list_from_json(\n",
49
    "    env_or_file=\"OAI_CONFIG_LIST\",\n",
50
    ")\n",
51
    "# Define your llm config\n",
52
    "llm_config = {\"config_list\": config_list}"
53
   ]
54
  },
55
  {
56
   "cell_type": "markdown",
57
   "id": "ea68962a-048d-42e9-9fca-cd944c56184d",
58
   "metadata": {},
59
   "source": [
60
    "````{=mdx}\n",
61
    ":::tip\n",
62
    "Learn more about configuring LLMs for agents [here](/docs/topics/llm_configuration).\n",
63
    ":::\n",
64
    "````"
65
   ]
66
  },
67
  {
68
   "cell_type": "code",
69
   "execution_count": 3,
70
   "id": "84d0e5ad-8b35-4b30-847e-4723e9c76f7c",
71
   "metadata": {},
72
   "outputs": [],
73
   "source": [
74
    "# Define your agent; the user proxy and an assistant\n",
75
    "assistant = autogen.AssistantAgent(\n",
76
    "    \"assistant\",\n",
77
    "    llm_config=llm_config,\n",
78
    ")\n",
79
    "user_proxy = autogen.UserProxyAgent(\n",
80
    "    \"user_proxy\",\n",
81
    "    human_input_mode=\"NEVER\",\n",
82
    "    is_termination_msg=lambda x: \"TERMINATE\" in x.get(\"content\", \"\"),\n",
83
    "    max_consecutive_auto_reply=10,\n",
84
    ")"
85
   ]
86
  },
87
  {
88
   "cell_type": "markdown",
89
   "id": "180aa953-45be-469a-a94f-0ed0b4ef5ddf",
90
   "metadata": {},
91
   "source": [
92
    "## Handling Long Contexts\n",
93
    "\n",
94
    "Imagine a scenario where the LLM generates an extensive amount of text, surpassing the token limit imposed by your API provider. To address this issue, you can leverage `TransformMessages` along with its constituent transformations, `MessageHistoryLimiter` and `MessageTokenLimiter`.\n",
95
    "\n",
96
    "- `MessageHistoryLimiter`: You can restrict the total number of messages considered as context history. This transform is particularly useful when you want to limit the conversational context to a specific number of recent messages, ensuring efficient processing and response generation.\n",
97
    "- `MessageTokenLimiter`: Enables you to cap the total number of tokens, either on a per-message basis or across the entire context history (or both). This transformation is invaluable when you need to adhere to strict token limits imposed by your API provider, preventing unnecessary costs or errors caused by exceeding the allowed token count. Additionally, a `min_tokens` threshold can be applied, ensuring that the transformation is only applied when the number of tokens is not less than the specified threshold."
98
   ]
99
  },
100
  {
101
   "cell_type": "code",
102
   "execution_count": 4,
103
   "id": "34b943a2-ec58-41bc-a449-d9118c4bbdea",
104
   "metadata": {},
105
   "outputs": [],
106
   "source": [
107
    "# Limit the message history to the 3 most recent messages\n",
108
    "max_msg_transfrom = transforms.MessageHistoryLimiter(max_messages=3)\n",
109
    "\n",
110
    "# Limit the token limit per message to 10 tokens\n",
111
    "token_limit_transform = transforms.MessageTokenLimiter(max_tokens_per_message=3, min_tokens=10)"
112
   ]
113
  },
114
  {
115
   "cell_type": "markdown",
116
   "id": "679c1026-4e1b-4c07-85cc-86594cc0b87b",
117
   "metadata": {},
118
   "source": [
119
    "## Example 1: Limiting number of messages\n",
120
    "Let's take a look at how these transformations will effect the messages. Below we see that by applying the `MessageHistoryLimiter`, we can see that we limited the context history to the 3 most recent messages."
121
   ]
122
  },
123
  {
124
   "cell_type": "code",
125
   "execution_count": 5,
126
   "id": "61a2ead4-5f8b-4108-b1f0-3b51b41e2231",
127
   "metadata": {},
128
   "outputs": [
129
    {
130
     "name": "stdout",
131
     "output_type": "stream",
132
     "text": [
133
      "[{'content': 'how', 'role': 'user'},\n",
134
      " {'content': [{'text': 'are you doing?', 'type': 'text'}], 'role': 'assistant'},\n",
135
      " {'content': 'very very very very very very long string', 'role': 'user'}]\n"
136
     ]
137
    }
138
   ],
139
   "source": [
140
    "messages = [\n",
141
    "    {\"role\": \"user\", \"content\": \"hello\"},\n",
142
    "    {\"role\": \"assistant\", \"content\": [{\"type\": \"text\", \"text\": \"there\"}]},\n",
143
    "    {\"role\": \"user\", \"content\": \"how\"},\n",
144
    "    {\"role\": \"assistant\", \"content\": [{\"type\": \"text\", \"text\": \"are you doing?\"}]},\n",
145
    "    {\"role\": \"user\", \"content\": \"very very very very very very long string\"},\n",
146
    "]\n",
147
    "\n",
148
    "processed_messages = max_msg_transfrom.apply_transform(copy.deepcopy(messages))\n",
149
    "pprint.pprint(processed_messages)"
150
   ]
151
  },
152
  {
153
   "cell_type": "markdown",
154
   "id": "610739af-b812-404e-82d2-b3ed796b8b6c",
155
   "metadata": {},
156
   "source": [
157
    "## Example 2: Limiting number of tokens\n",
158
    "\n",
159
    "Now let's test limiting the number of tokens in messages. We can see that we can limit the number of tokens to 3, which is equivalent to 3 words in this instance."
160
   ]
161
  },
162
  {
163
   "cell_type": "code",
164
   "execution_count": 6,
165
   "id": "739dd260-fa95-4e5d-ae84-9cb7f40de975",
166
   "metadata": {},
167
   "outputs": [
168
    {
169
     "name": "stdout",
170
     "output_type": "stream",
171
     "text": [
172
      "[{'content': 'hello', 'role': 'user'},\n",
173
      " {'content': [{'text': 'there', 'type': 'text'}], 'role': 'assistant'},\n",
174
      " {'content': 'how', 'role': 'user'},\n",
175
      " {'content': [{'text': 'are you doing', 'type': 'text'}], 'role': 'assistant'},\n",
176
      " {'content': 'very very very', 'role': 'user'}]\n"
177
     ]
178
    }
179
   ],
180
   "source": [
181
    "processed_messages = token_limit_transform.apply_transform(copy.deepcopy(messages))\n",
182
    "\n",
183
    "pprint.pprint(processed_messages)"
184
   ]
185
  },
186
  {
187
   "cell_type": "markdown",
188
   "id": "86a98e08",
189
   "metadata": {},
190
   "source": [
191
    "Also, the `min_tokens` threshold is set to 10, indicating that the transformation will not be applied if the total number of tokens in the messages is less than that. This is especially beneficial when the transformation should only occur after a certain number of tokens has been reached, such as in the context window of the model. An example is provided below."
192
   ]
193
  },
194
  {
195
   "cell_type": "code",
196
   "execution_count": 7,
197
   "id": "05c42ffc",
198
   "metadata": {},
199
   "outputs": [
200
    {
201
     "name": "stdout",
202
     "output_type": "stream",
203
     "text": [
204
      "[{'content': 'hello there, how are you?', 'role': 'user'},\n",
205
      " {'content': [{'text': 'hello', 'type': 'text'}], 'role': 'assistant'}]\n"
206
     ]
207
    }
208
   ],
209
   "source": [
210
    "short_messages = [\n",
211
    "    {\"role\": \"user\", \"content\": \"hello there, how are you?\"},\n",
212
    "    {\"role\": \"assistant\", \"content\": [{\"type\": \"text\", \"text\": \"hello\"}]},\n",
213
    "]\n",
214
    "\n",
215
    "processed_short_messages = token_limit_transform.apply_transform(copy.deepcopy(short_messages))\n",
216
    "\n",
217
    "pprint.pprint(processed_short_messages)"
218
   ]
219
  },
220
  {
221
   "cell_type": "markdown",
222
   "id": "35fa2844-bd83-42ac-8275-959f093b7bc7",
223
   "metadata": {},
224
   "source": [
225
    "## Example 3: Combining transformations\n",
226
    "\n",
227
    "Let's test these transforms with agents (the upcoming test is replicated from the agentchat_capability_long_context_handling notebook). We will see that the agent without the capability to handle long context will result in an error, while the agent with that capability will have no issues."
228
   ]
229
  },
230
  {
231
   "cell_type": "code",
232
   "execution_count": 8,
233
   "id": "80e53623-2830-41b7-8ae2-bf3668071657",
234
   "metadata": {},
235
   "outputs": [
236
    {
237
     "name": "stdout",
238
     "output_type": "stream",
239
     "text": [
240
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
241
      "\n",
242
      "plot and save a graph of x^2 from -10 to 10\n",
243
      "\n",
244
      "--------------------------------------------------------------------------------\n",
245
      "Encountered an error with the base assistant\n",
246
      "Error code: 400 - {'error': {'message': \"This model's maximum context length is 16385 tokens. However, your messages resulted in 1009487 tokens. Please reduce the length of the messages.\", 'type': 'invalid_request_error', 'param': 'messages', 'code': 'context_length_exceeded'}}\n",
247
      "\n",
248
      "\n",
249
      "\n",
250
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
251
      "\n",
252
      "plot and save a graph of x^2 from -10 to 10\n",
253
      "\n",
254
      "--------------------------------------------------------------------------------\n",
255
      "\u001b[33mRemoved 1991 messages. Number of messages reduced from 2001 to 10.\u001b[0m\n",
256
      "\u001b[33mTruncated 3804 tokens. Number of tokens reduced from 4019 to 215\u001b[0m\n",
257
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
258
      "\n",
259
      "```python\n",
260
      "# filename: plot_x_squared.py\n",
261
      "import matplotlib.pyplot as plt\n",
262
      "import numpy as np\n",
263
      "\n",
264
      "# Generate an array of x values from -10 to 10\n",
265
      "x = np.linspace(-10, 10, 400)\n",
266
      "# Calculate the y values by squaring the x values\n",
267
      "y = x**2\n",
268
      "\n",
269
      "# Create the plot\n",
270
      "plt.figure()\n",
271
      "plt.plot(x, y)\n",
272
      "\n",
273
      "# Title and labels\n",
274
      "plt.title('Graph of y = x^2')\n",
275
      "plt.xlabel('x')\n",
276
      "plt.ylabel('y')\n",
277
      "\n",
278
      "# Save the plot as a file\n",
279
      "plt.savefig('x_squared_plot.png')\n",
280
      "\n",
281
      "# Show the plot\n",
282
      "plt.show()\n",
283
      "```\n",
284
      "\n",
285
      "Please save the above code into a file named `plot_x_squared.py`. After saving the code, you can execute it to generate and save the graph of y = x^2 from -10 to 10. The graph will also be displayed to you and the file `x_squared_plot.png` will be created in the current directory. Make sure you have `matplotlib` and `numpy` libraries installed in your Python environment before executing the code. If they are not installed, you can install them using `pip`:\n",
286
      "\n",
287
      "```sh\n",
288
      "pip install matplotlib numpy\n",
289
      "```\n",
290
      "\n",
291
      "--------------------------------------------------------------------------------\n",
292
      "\u001b[31m\n",
293
      ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
294
      "\u001b[31m\n",
295
      ">>>>>>>> EXECUTING CODE BLOCK 1 (inferred language is sh)...\u001b[0m\n",
296
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
297
      "\n",
298
      "exitcode: 0 (execution succeeded)\n",
299
      "Code output: \n",
300
      "Figure(640x480)\n",
301
      "\n",
302
      "Requirement already satisfied: matplotlib in c:\\users\\bt314mc\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (3.8.0)\n",
303
      "Requirement already satisfied: numpy in c:\\users\\bt314mc\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (1.26.0)\n",
304
      "Requirement already satisfied: contourpy>=1.0.1 in c:\\users\\bt314mc\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from matplotlib) (1.1.1)\n",
305
      "Requirement already satisfied: cycler>=0.10 in c:\\users\\bt314mc\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from matplotlib) (0.11.0)\n",
306
      "Requirement already satisfied: fonttools>=4.22.0 in c:\\users\\bt314mc\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from matplotlib) (4.42.1)\n",
307
      "Requirement already satisfied: kiwisolver>=1.0.1 in c:\\users\\bt314mc\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from matplotlib) (1.4.5)\n",
308
      "Requirement already satisfied: packaging>=20.0 in c:\\users\\bt314mc\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from matplotlib) (23.2)\n",
309
      "Requirement already satisfied: pillow>=6.2.0 in c:\\users\\bt314mc\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from matplotlib) (10.0.1)\n",
310
      "Requirement already satisfied: pyparsing>=2.3.1 in c:\\users\\bt314mc\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from matplotlib) (3.1.1)\n",
311
      "Requirement already satisfied: python-dateutil>=2.7 in c:\\users\\bt314mc\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from matplotlib) (2.8.2)\n",
312
      "Requirement already satisfied: six>=1.5 in c:\\users\\bt314mc\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)\n",
313
      "\n",
314
      "\n",
315
      "--------------------------------------------------------------------------------\n",
316
      "\u001b[33mRemoved 1993 messages. Number of messages reduced from 2003 to 10.\u001b[0m\n",
317
      "\u001b[33mTruncated 3523 tokens. Number of tokens reduced from 3788 to 265\u001b[0m\n",
318
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
319
      "\n",
320
      "It appears that the matplotlib library is already installed on your system, and the previous script started successfully but did not finish because the plotting code was incomplete.\n",
321
      "\n",
322
      "I will provide you with the full code to plot and save the graph of \\( x^2 \\) from -10 to 10.\n",
323
      "\n",
324
      "```python\n",
325
      "# filename: plot_x_squared.py\n",
326
      "import matplotlib.pyplot as plt\n",
327
      "import numpy as np\n",
328
      "\n",
329
      "# Generate an array of x values from -10 to 10\n",
330
      "x = np.linspace(-10, 10, 400)\n",
331
      "# Calculate the y values based on the x values\n",
332
      "y = x**2\n",
333
      "\n",
334
      "# Create the plot\n",
335
      "plt.figure(figsize=(8, 6))\n",
336
      "plt.plot(x, y, label='y = x^2')\n",
337
      "\n",
338
      "# Add a title and labels\n",
339
      "plt.title('Plot of y = x^2')\n",
340
      "plt.xlabel('x')\n",
341
      "plt.ylabel('y')\n",
342
      "\n",
343
      "# Add a legend\n",
344
      "plt.legend()\n",
345
      "\n",
346
      "# Save the figure\n",
347
      "plt.savefig('plot_x_squared.png')\n",
348
      "\n",
349
      "# Show the plot\n",
350
      "plt.show()\n",
351
      "```\n",
352
      "\n",
353
      "Please execute this Python code in its entirety. It will create a graph of \\( y = x^2 \\) with x values ranging from -10 to 10, and then it will save the graph as a PNG file named 'plot_x_squared.png' in the current working directory. It will also display the plot window with the graph.\n",
354
      "\n",
355
      "--------------------------------------------------------------------------------\n",
356
      "\u001b[31m\n",
357
      ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
358
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
359
      "\n",
360
      "exitcode: 0 (execution succeeded)\n",
361
      "Code output: \n",
362
      "Figure(800x600)\n",
363
      "\n",
364
      "\n",
365
      "--------------------------------------------------------------------------------\n",
366
      "\u001b[33mRemoved 1995 messages. Number of messages reduced from 2005 to 10.\u001b[0m\n",
367
      "\u001b[33mTruncated 2802 tokens. Number of tokens reduced from 3086 to 284\u001b[0m\n",
368
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
369
      "\n",
370
      "It seems the graph has been generated, but the output doesn't tell us if the graph was saved. The expected behavior was to have a file saved in the current working directory. Can you please check in your current directory for a file named `plot_x_squared.png`? If it exists, then the task is complete.\n",
371
      "\n",
372
      "If you don't find the file, let me know, and I will troubleshoot further.\n",
373
      "\n",
374
      "--------------------------------------------------------------------------------\n"
375
     ]
376
    }
377
   ],
378
   "source": [
379
    "assistant_base = autogen.AssistantAgent(\n",
380
    "    \"assistant\",\n",
381
    "    llm_config=llm_config,\n",
382
    ")\n",
383
    "\n",
384
    "assistant_with_context_handling = autogen.AssistantAgent(\n",
385
    "    \"assistant\",\n",
386
    "    llm_config=llm_config,\n",
387
    ")\n",
388
    "# suppose this capability is not available\n",
389
    "context_handling = transform_messages.TransformMessages(\n",
390
    "    transforms=[\n",
391
    "        transforms.MessageHistoryLimiter(max_messages=10),\n",
392
    "        transforms.MessageTokenLimiter(max_tokens=1000, max_tokens_per_message=50, min_tokens=500),\n",
393
    "    ]\n",
394
    ")\n",
395
    "\n",
396
    "context_handling.add_to_agent(assistant_with_context_handling)\n",
397
    "\n",
398
    "user_proxy = autogen.UserProxyAgent(\n",
399
    "    \"user_proxy\",\n",
400
    "    human_input_mode=\"NEVER\",\n",
401
    "    is_termination_msg=lambda x: \"TERMINATE\" in x.get(\"content\", \"\"),\n",
402
    "    code_execution_config={\n",
403
    "        \"work_dir\": \"coding\",\n",
404
    "        \"use_docker\": False,\n",
405
    "    },\n",
406
    "    max_consecutive_auto_reply=2,\n",
407
    ")\n",
408
    "\n",
409
    "# suppose the chat history is large\n",
410
    "# Create a very long chat history that is bound to cause a crash\n",
411
    "# for gpt 3.5\n",
412
    "for i in range(1000):\n",
413
    "    # define a fake, very long messages\n",
414
    "    assitant_msg = {\"role\": \"assistant\", \"content\": \"test \" * 1000}\n",
415
    "    user_msg = {\"role\": \"user\", \"content\": \"\"}\n",
416
    "\n",
417
    "    assistant_base.send(assitant_msg, user_proxy, request_reply=False, silent=True)\n",
418
    "    assistant_with_context_handling.send(assitant_msg, user_proxy, request_reply=False, silent=True)\n",
419
    "    user_proxy.send(user_msg, assistant_base, request_reply=False, silent=True)\n",
420
    "    user_proxy.send(user_msg, assistant_with_context_handling, request_reply=False, silent=True)\n",
421
    "\n",
422
    "try:\n",
423
    "    user_proxy.initiate_chat(assistant_base, message=\"plot and save a graph of x^2 from -10 to 10\", clear_history=False)\n",
424
    "except Exception as e:\n",
425
    "    print(\"Encountered an error with the base assistant\")\n",
426
    "    print(e)\n",
427
    "    print(\"\\n\\n\")\n",
428
    "\n",
429
    "try:\n",
430
    "    user_proxy.initiate_chat(\n",
431
    "        assistant_with_context_handling, message=\"plot and save a graph of x^2 from -10 to 10\", clear_history=False\n",
432
    "    )\n",
433
    "except Exception as e:\n",
434
    "    print(e)"
435
   ]
436
  },
437
  {
438
   "cell_type": "markdown",
439
   "id": "5e380678-a923-43cb-91b1-f9c9e8deede2",
440
   "metadata": {},
441
   "source": [
442
    "## Handling Sensitive Data\n",
443
    "\n",
444
    "You can use the `MessageTransform` protocol to create custom message transformations that redact sensitive data from the chat history. This is particularly useful when you want to ensure that sensitive information, such as API keys, passwords, or personal data, is not exposed in the chat history or logs.\n",
445
    "\n",
446
    "Now, we will create a custom message transform to detect any OpenAI API key and redact it."
447
   ]
448
  },
449
  {
450
   "cell_type": "code",
451
   "execution_count": 9,
452
   "id": "74429344-3c0a-4057-aba3-27358fbf059c",
453
   "metadata": {},
454
   "outputs": [],
455
   "source": [
456
    "# The transform must adhere to transform_messages.MessageTransform protocol.\n",
457
    "class MessageRedact:\n",
458
    "    def __init__(self):\n",
459
    "        self._openai_key_pattern = r\"sk-([a-zA-Z0-9]{48})\"\n",
460
    "        self._replacement_string = \"REDACTED\"\n",
461
    "\n",
462
    "    def apply_transform(self, messages: List[Dict]) -> List[Dict]:\n",
463
    "        temp_messages = copy.deepcopy(messages)\n",
464
    "\n",
465
    "        for message in temp_messages:\n",
466
    "            if isinstance(message[\"content\"], str):\n",
467
    "                message[\"content\"] = re.sub(self._openai_key_pattern, self._replacement_string, message[\"content\"])\n",
468
    "            elif isinstance(message[\"content\"], list):\n",
469
    "                for item in message[\"content\"]:\n",
470
    "                    if item[\"type\"] == \"text\":\n",
471
    "                        item[\"text\"] = re.sub(self._openai_key_pattern, self._replacement_string, item[\"text\"])\n",
472
    "        return temp_messages\n",
473
    "\n",
474
    "    def get_logs(self, pre_transform_messages: List[Dict], post_transform_messages: List[Dict]) -> Tuple[str, bool]:\n",
475
    "        keys_redacted = self._count_redacted(post_transform_messages) - self._count_redacted(pre_transform_messages)\n",
476
    "        if keys_redacted > 0:\n",
477
    "            return f\"Redacted {keys_redacted} OpenAI API keys.\", True\n",
478
    "        return \"\", False\n",
479
    "\n",
480
    "    def _count_redacted(self, messages: List[Dict]) -> int:\n",
481
    "        # counts occurrences of \"REDACTED\" in message content\n",
482
    "        count = 0\n",
483
    "        for message in messages:\n",
484
    "            if isinstance(message[\"content\"], str):\n",
485
    "                if \"REDACTED\" in message[\"content\"]:\n",
486
    "                    count += 1\n",
487
    "            elif isinstance(message[\"content\"], list):\n",
488
    "                for item in message[\"content\"]:\n",
489
    "                    if isinstance(item, dict) and \"text\" in item:\n",
490
    "                        if \"REDACTED\" in item[\"text\"]:\n",
491
    "                            count += 1\n",
492
    "        return count"
493
   ]
494
  },
495
  {
496
   "cell_type": "code",
497
   "execution_count": 10,
498
   "id": "8a79c0b4-5ff8-49c5-b8a6-c54ca4c7cca2",
499
   "metadata": {},
500
   "outputs": [
501
    {
502
     "name": "stdout",
503
     "output_type": "stream",
504
     "text": [
505
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
506
      "\n",
507
      "What are the two API keys that I just provided\n",
508
      "\n",
509
      "--------------------------------------------------------------------------------\n",
510
      "\u001b[33mRedacted 2 OpenAI API keys.\u001b[0m\n",
511
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
512
      "\n",
513
      "As an AI, I must inform you that it is not safe to share API keys publicly as they can be used to access your private data or services that can incur costs. Given that you've typed \"REDACTED\" instead of the actual keys, it seems you are aware of the privacy concerns and are likely testing my response or simulating an exchange without exposing real credentials, which is a good practice for privacy and security reasons.\n",
514
      "\n",
515
      "To respond directly to your direct question: The two API keys you provided are both placeholders indicated by the text \"REDACTED\", and not actual API keys. If these were real keys, I would have reiterated the importance of keeping them secure and would not display them here.\n",
516
      "\n",
517
      "Remember to keep your actual API keys confidential to prevent unauthorized use. If you've accidentally exposed real API keys, you should revoke or regenerate them as soon as possible through the corresponding service's API management console.\n",
518
      "\n",
519
      "--------------------------------------------------------------------------------\n",
520
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
521
      "\n",
522
      "\n",
523
      "\n",
524
      "--------------------------------------------------------------------------------\n",
525
      "\u001b[33mRedacted 2 OpenAI API keys.\u001b[0m\n"
526
     ]
527
    }
528
   ],
529
   "source": [
530
    "assistant_with_redact = autogen.AssistantAgent(\n",
531
    "    \"assistant\",\n",
532
    "    llm_config=llm_config,\n",
533
    "    max_consecutive_auto_reply=1,\n",
534
    ")\n",
535
    "# suppose this capability is not available\n",
536
    "redact_handling = transform_messages.TransformMessages(transforms=[MessageRedact()])\n",
537
    "\n",
538
    "redact_handling.add_to_agent(assistant_with_redact)\n",
539
    "\n",
540
    "user_proxy = autogen.UserProxyAgent(\n",
541
    "    \"user_proxy\",\n",
542
    "    human_input_mode=\"NEVER\",\n",
543
    "    max_consecutive_auto_reply=1,\n",
544
    ")\n",
545
    "\n",
546
    "messages = [\n",
547
    "    {\"content\": \"api key 1 = sk-7nwt00xv6fuegfu3gnwmhrgxvuc1cyrhxcq1quur9zvf05fy\"},  # Don't worry, randomly generated\n",
548
    "    {\"content\": [{\"type\": \"text\", \"text\": \"API key 2 = sk-9wi0gf1j2rz6utaqd3ww3o6c1h1n28wviypk7bd81wlj95an\"}]},\n",
549
    "]\n",
550
    "\n",
551
    "for message in messages:\n",
552
    "    user_proxy.send(message, assistant_with_redact, request_reply=False, silent=True)\n",
553
    "\n",
554
    "result = user_proxy.initiate_chat(\n",
555
    "    assistant_with_redact, message=\"What are the two API keys that I just provided\", clear_history=False\n",
556
    ")"
557
   ]
558
  }
559
 ],
560
 "metadata": {
561
  "front_matter": {
562
   "description": "Preprocessing chat history with `TransformMessages`",
563
   "tags": [
564
    "long context handling",
565
    "capability"
566
   ]
567
  },
568
  "kernelspec": {
569
   "display_name": "Python 3 (ipykernel)",
570
   "language": "python",
571
   "name": "python3"
572
  },
573
  "language_info": {
574
   "codemirror_mode": {
575
    "name": "ipython",
576
    "version": 3
577
   },
578
   "file_extension": ".py",
579
   "mimetype": "text/x-python",
580
   "name": "python",
581
   "nbconvert_exporter": "python",
582
   "pygments_lexer": "ipython3",
583
   "version": "3.11.5"
584
  }
585
 },
586
 "nbformat": 4,
587
 "nbformat_minor": 5
588
}
589

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.