|
11 | 11 | "cell_type": "markdown",
|
12 | 12 | "metadata": {},
|
13 | 13 | "source": [
|
14 |
| - "The `PodCommandLineCodeExecutor` of the autogen.coding.kubernetes module is to execute a code block using a pod in Kubernetes.\n", |
15 |
| - "It is like DockerCommandLineCodeExecutor, but creates container on kubernetes Pod.\n", |
| 14 | + "The `PodCommandLineCodeExecutor` in the `autogen.coding.kubernetes` module is designed to execute code blocks using a pod in Kubernetes.\n", |
| 15 | + "It functions similarly to the `DockerCommandLineCodeExecutor`, but specifically creates container within Kubernetes environments.\n", |
16 | 16 | "\n",
|
17 | 17 | "There are two condition to use PodCommandLineCodeExecutor.\n",
|
18 |
| - "- accessible to kubernetes cluster\n", |
19 |
| - "- install autogen with extra feature kubernetes\n", |
| 18 | + "- Access to a Kubernetes cluster\n", |
| 19 | + "- installation `autogen` with the extra requirements `'pyautogen[kubernetes]'`\n", |
20 | 20 | "\n",
|
21 |
| - "This documents uses minikube cluster on local environment.\n", |
22 |
| - "You can refer to the link below for installation and execution of minikube.\n", |
| 21 | + "For local development and testing, this document uses a Minikube cluster.\n", |
23 | 22 | "\n",
|
24 |
| - "https://minikube.sigs.k8s.io/docs/start/" |
| 23 | + "Minikube is a tool that allows you to run a single-node Kubernetes cluster on you local machine. \n", |
| 24 | + "You can refer to the link below for installation and setup of Minikube.\n", |
| 25 | + "\n", |
| 26 | + "🔗 https://minikube.sigs.k8s.io/docs/start/" |
25 | 27 | ]
|
26 | 28 | },
|
27 | 29 | {
|
|
37 | 39 | "source": [
|
38 | 40 | "There are four options PodCommandLineCodeExecutor to access kubernetes API server.\n",
|
39 | 41 | "- default kubeconfig file path: `~/.kube/config`\n",
|
40 |
| - "- To provide kubeconfig file path to `kube_config_file` argument of `PodCommandLineCodeExecutor`.\n", |
41 |
| - "- To provide kubeconfig file path to `KUBECONFIG` environment variable.\n", |
42 |
| - "- To provide token from kubernetes ServiceAccount which has enough permissions" |
| 42 | + "- Provide a custom kubeconfig file path using the `kube_config_file` argument of `PodCommandLineCodeExecutor`.\n", |
| 43 | + "- Set the kubeconfig file path using the `KUBECONFIG` environment variable.\n", |
| 44 | + "- Provide token from Kubernetes ServiceAccount with sufficient permissions" |
43 | 45 | ]
|
44 | 46 | },
|
45 | 47 | {
|
|
48 | 50 | "source": [
|
49 | 51 | "Generally, if kubeconfig file is located in `~/.kube/config`, there's no need to provide kubeconfig file path on parameter or environment variables.\n",
|
50 | 52 | "\n",
|
51 |
| - "the Tutorial of providing ServiceAccount Token is in the last section" |
| 53 | + "The tutorial of providing ServiceAccount Token is in the last section" |
52 | 54 | ]
|
53 | 55 | },
|
54 | 56 | {
|
|
57 | 59 | "source": [
|
58 | 60 | "## Example\n",
|
59 | 61 | "\n",
|
60 |
| - "In order to use kubernetes Pod based code executor, kubernetes python sdk is required.\n", |
61 |
| - "It can be installed by `pip install 'kubernetes>=27'` or with the extra kubernetes" |
| 62 | + "In order to use kubernetes Pod based code executor, you need to install Kubernetes Python SDK.\n", |
| 63 | + "\n", |
| 64 | + "You can do this by running the following command:" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "code", |
| 69 | + "execution_count": null, |
| 70 | + "metadata": {}, |
| 71 | + "outputs": [], |
| 72 | + "source": [ |
| 73 | + "pip install 'kubernetes>=27'" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "markdown", |
| 78 | + "metadata": {}, |
| 79 | + "source": [ |
| 80 | + "Alternatively, you can install it with the extra features for Kubernetes:" |
62 | 81 | ]
|
63 | 82 | },
|
64 | 83 | {
|
|
67 | 86 | "metadata": {},
|
68 | 87 | "outputs": [],
|
69 | 88 | "source": [
|
70 |
| - "%pip install 'pyautogen[kubernetes]'\n", |
71 |
| - "# or\n", |
72 |
| - "# pip install pyautogen 'kubernetes>=27'" |
| 89 | + "pip install 'pyautogen[kubernetes]'" |
73 | 90 | ]
|
74 | 91 | },
|
75 | 92 | {
|
|
85 | 102 | "metadata": {},
|
86 | 103 | "outputs": [],
|
87 | 104 | "source": [
|
88 |
| - "# import os\n", |
89 |
| - "# # If kubeconfig file is not located on ~/.kube/config but other path, \n", |
90 |
| - "# # you can set path of kubeconfig file on KUBECONFIG environment variables \n", |
91 |
| - "# os.environ[\"KUBECONFIG\"] = \"kubeconfig/file/path\"" |
| 105 | + "import os\n", |
| 106 | + "# Set the KUBECONFIG environment variable \n", |
| 107 | + "# if the kubeconfig file is not in the default location(~/.kube/cofig).\n", |
| 108 | + "os.environ[\"KUBECONFIG\"] = \"path/to/your/kubeconfig\"" |
92 | 109 | ]
|
93 | 110 | },
|
94 | 111 | {
|
|
121 | 138 | " ) as executor:\n",
|
122 | 139 | " print(\n",
|
123 | 140 | " executor.execute_code_blocks(\n",
|
| 141 | + " # Example of executing a simple Python code block within a Kubernetes pod.\n", |
124 | 142 | " code_blocks=[\n",
|
125 | 143 | " CodeBlock(language=\"python\", code=\"print('Hello, World!')\"),\n",
|
126 | 144 | " ]\n",
|
|
132 | 150 | "cell_type": "markdown",
|
133 | 151 | "metadata": {},
|
134 | 152 | "source": [
|
135 |
| - "With context manager(with statement), the pod created by PodCommandLineCodeExecutor deleted automatically after tasks done.\n", |
| 153 | + "Using a context manager(the `with` statement), the pod created by `PodCommandLineCodeExecutor` is automatically deleted after the tasks are completed.\n", |
136 | 154 | "\n",
|
137 |
| - "To delete the pod manually, you can use `stop()` method" |
| 155 | + "Although the pod is automatically deleted when using a context manager, you might sometimes need to delete it manually. You can do this using `stop()` method, as shown below:" |
138 | 156 | ]
|
139 | 157 | },
|
140 | 158 | {
|
|
162 | 180 | ],
|
163 | 181 | "source": [
|
164 | 182 | "%%bash\n",
|
165 |
| - "# default pod name is autogen-code-exec-{uuid.uuid4()}\n", |
| 183 | + "# This command lists all pods in the default namespace. \n", |
| 184 | + "# The default pod name follows the format autogen-code-exec-{uuid.uuid4()}.\n", |
166 | 185 | "kubectl get pod -n default"
|
167 | 186 | ]
|
168 | 187 | },
|
|
181 | 200 | ],
|
182 | 201 | "source": [
|
183 | 202 | "%%bash\n",
|
184 |
| - "# default container image is python:3-slim\n", |
| 203 | + "# This command shows container's image in the pod.\n", |
| 204 | + "# The default container image is python:3-slim\n", |
185 | 205 | "kubectl get pod autogen-code-exec-afd217ac-f77b-4ede-8c53-1297eca5ec64 -o jsonpath={.spec.containers[0].image}"
|
186 | 206 | ]
|
187 | 207 | },
|
|
198 | 218 | "cell_type": "markdown",
|
199 | 219 | "metadata": {},
|
200 | 220 | "source": [
|
201 |
| - "If you use another container image for code executor pod, you can provide image tag to `image` argument.\n", |
| 221 | + "To use a different container image for code executor pod, specify the desired image tag using `image` argument.\n", |
202 | 222 | "\n",
|
203 |
| - "PodCommandLineCode Executor has default execution policy that allows python and shell script code blocks.\n", |
204 |
| - "\n", |
205 |
| - "It can be allowed other languages with `execution_policies` argument." |
| 223 | + "`PodCommandLineCodeExecutor` has a default execution policy that allows Python and shell script code blocks. You can enable other languages with `execution_policies` argument." |
206 | 224 | ]
|
207 | 225 | },
|
208 | 226 | {
|
|
220 | 238 | ],
|
221 | 239 | "source": [
|
222 | 240 | "with PodCommandLineCodeExecutor(\n",
|
223 |
| - " image=\"node:22-alpine\", # you can provide runtime environments through container image\n", |
| 241 | + " image=\"node:22-alpine\", # Specifies the runtime environments using a container image\n", |
224 | 242 | " namespace=\"default\",\n",
|
225 |
| - " work_dir=\"./app\", # workspace directory for code block files\n", |
226 |
| - " timeout=10, # timeout(seconds) value for waiting pod creation, execution of code blocks. default is 60 seconds\n", |
227 |
| - " execution_policies = {\"javascript\": True} # allowed execution languages can be updated with execution_policy dictionary\n", |
| 243 | + " work_dir=\"./app\", # Directory within the container where code block files are stored\n", |
| 244 | + " timeout=10, # Timeout in seconds for pod creation and code block execution (default is 60 seconds)\n", |
| 245 | + " execution_policies = {\"javascript\": True} # Enable execution of Javascript code blocks by updating execution policies\n", |
228 | 246 | ") as executor:\n",
|
229 | 247 | " print(\n",
|
230 | 248 | " executor.execute_code_blocks(\n",
|
|
239 | 257 | "cell_type": "markdown",
|
240 | 258 | "metadata": {},
|
241 | 259 | "source": [
|
242 |
| - "If you want to give custom settings for executor pod, such as annotations, environment variables, commands, volumes etc., \n", |
243 |
| - "you can provide custom pod specification with `kubernetes.client.V1Pod` format.\n", |
| 260 | + "If you want to apply custom settings for executor pod, such as annotations, environment variables, commands, volumes etc., \n", |
| 261 | + "you can provide a custom pod specification using `kubernetes.client.V1Pod` format.\n", |
244 | 262 | "\n",
|
245 |
| - "`container_name` argument should also be provided because PodCommandLineCodeExecutor do not automatically recognize a container where code blocks will be executed " |
| 263 | + "The `container_name` argument should also be provided because `PodCommandLineCodeExecutor` does not automatically recognize the container where code blocks will be executed." |
246 | 264 | ]
|
247 | 265 | },
|
248 | 266 | {
|
|
261 | 279 | " containers=[client.V1Container(\n",
|
262 | 280 | " args=[\"-c\", \"while true;do sleep 5; done\"],\n",
|
263 | 281 | " command=[\"/bin/sh\"],\n",
|
264 |
| - " name=\"abcd\", # container name where code blocks will be executed should be provided to `container_name` argument\n", |
| 282 | + " name=\"abcd\", # container name where code blocks will be executed should be provided using `container_name` argument\n", |
265 | 283 | " image=\"python:3.11-slim\",\n",
|
266 | 284 | " env=[\n",
|
267 | 285 | " client.V1EnvVar(\n",
|
|
299 | 317 | "source": [
|
300 | 318 | "with PodCommandLineCodeExecutor(\n",
|
301 | 319 | " pod_spec=pod, # custom executor pod spec\n",
|
302 |
| - " container_name=\"abcd\", # If custom executor pod spec is provided, container_name where code block will be executed should be specified\n", |
| 320 | + " container_name=\"abcd\", # To use custom executor pod spec, container_name where code block will be executed should be specified\n", |
303 | 321 | " work_dir=\"/autogen\",\n",
|
304 | 322 | " timeout=60,\n",
|
305 | 323 | ") as executor:\n",
|
|
331 | 349 | "cell_type": "markdown",
|
332 | 350 | "metadata": {},
|
333 | 351 | "source": [
|
334 |
| - "PodCommandLineCodeExecutor can be integrated with Agents." |
| 352 | + "`PodCommandLineCodeExecutor` can be integrated with Agents." |
335 | 353 | ]
|
336 | 354 | },
|
337 | 355 | {
|
|
427 | 445 | "from autogen import ConversableAgent\n",
|
428 | 446 | "\n",
|
429 | 447 | "# The code writer agent's system message is to instruct the LLM on how to\n",
|
430 |
| - "# use the Jupyter code executor with IPython kernel.\n", |
| 448 | + "# use the code executor with python or shell script code\n", |
431 | 449 | "code_writer_system_message = \"\"\"\n",
|
432 | 450 | "You have been given coding capability to solve tasks using Python code.\n",
|
433 | 451 | "In the following cases, suggest python code (in a python coding block) or shell script (in a sh coding block) for the user to execute.\n",
|
|
585 | 603 | "cell_type": "markdown",
|
586 | 604 | "metadata": {},
|
587 | 605 | "source": [
|
588 |
| - "If a PodCommandLineCodeExecutor instance executes on inside of kubernetes Pod, instance can use token which is generated from ServiceAccount to access kubernetes API server.\n", |
| 606 | + "If a `PodCommandLineCodeExecutor` instance runs inside of Kubernetes Pod, it can use a token generated from a ServiceAccount to access Kubernetes API server.\n", |
589 | 607 | "\n",
|
590 |
| - "PodCommandLineCodeExecutor needs permissions of verbs `create`, `get`, `delete` on resource `pods`, and verb `get` for resources `pods/status`, `pods/exec`.\n", |
| 608 | + "The `PodCommandLineCodeExecutor` requires the following permissions:\n", |
| 609 | + "the verbs `create`, `get`, `delete` for `pods` resource, and the verb `get` for resources `pods/status`, `pods/exec`.\n", |
591 | 610 | "\n",
|
592 |
| - "You can make ServiceAccount, ClusterRole and RoleBinding with kubectl." |
| 611 | + "You can create a ServiceAccount, ClusterRole and RoleBinding with `kubectl` as shown below:" |
593 | 612 | ]
|
594 | 613 | },
|
595 | 614 | {
|
|
607 | 626 | ],
|
608 | 627 | "source": [
|
609 | 628 | "%%bash\n",
|
610 |
| - "# create ServiceAccount on default namespace\n", |
| 629 | + "# Create ServiceAccount on default namespace\n", |
611 | 630 | "kubectl create sa autogen-executor-sa"
|
612 | 631 | ]
|
613 | 632 | },
|
|
626 | 645 | ],
|
627 | 646 | "source": [
|
628 | 647 | "%%bash\n",
|
629 |
| - "# create ClusterRole\n", |
| 648 | + "# Create ClusterRole that has sufficient permissions\n", |
630 | 649 | "kubectl create clusterrole autogen-executor-role \\\n",
|
631 | 650 | " --verb=get,create,delete --resource=pods,pods/status,pods/exec"
|
632 | 651 | ]
|
|
646 | 665 | ],
|
647 | 666 | "source": [
|
648 | 667 | "%%bash\n",
|
649 |
| - "# create RoleBinding\n", |
| 668 | + "# Create RoleBinding that binds ClusterRole and ServiceAccount\n", |
650 | 669 | "kubectl create rolebinding autogen-executor-rolebinding \\\n",
|
651 | 670 | " --clusterrole autogen-executor-role --serviceaccount default:autogen-executor-sa"
|
652 | 671 | ]
|
|
655 | 674 | "cell_type": "markdown",
|
656 | 675 | "metadata": {},
|
657 | 676 | "source": [
|
658 |
| - "Pod with serviceAccount created before can be launched by below" |
| 677 | + "A pod with a previously created ServiceAccount can be launched using the following command." |
659 | 678 | ]
|
660 | 679 | },
|
661 | 680 | {
|
|
683 | 702 | "cell_type": "markdown",
|
684 | 703 | "metadata": {},
|
685 | 704 | "source": [
|
686 |
| - "Execute PodCommandLineCodeExecutor in autogen-executor Pod" |
| 705 | + "You can execute `PodCommandLineCodeExecutor` inside the Python interpreter process from `autogen-executor` Pod.\n", |
| 706 | + "\n", |
| 707 | + "It creates new pod for code execution using token generated from `autogen-executor-sa` ServiceAccount." |
687 | 708 | ]
|
688 | 709 | },
|
689 | 710 | {
|
|
0 commit comments