Spaces:
Runtime error
Runtime error
File size: 18,527 Bytes
df420ed |
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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
{
"cells": [
{
"cell_type": "markdown",
"id": "c96b164c",
"metadata": {},
"source": [
"# Overview "
]
},
{
"cell_type": "markdown",
"id": "22c9cad3",
"metadata": {},
"source": [
"<span style=\"color:red; font-size:18px; font-weight:bold;\">In this notebook, we will try to create a workflow between Langchain and Mixtral LLM.</br>\n",
"We want to accomplish the following:</span>\n",
"1. Establish a pipeline to read in a csv and pass it to our LLM. \n",
"2. Establish a Basic Inference Agent.\n",
"3. Test the Basic Inference on a few tasks."
]
},
{
"cell_type": "markdown",
"id": "3bd62bd1",
"metadata": {},
"source": [
"# Setting up the Environment "
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "76b3d212",
"metadata": {},
"outputs": [],
"source": [
"####################################################################################################\n",
"import os\n",
"import re\n",
"import subprocess\n",
"import sys\n",
"\n",
"from langchain import hub\n",
"from langchain.agents.agent_types import AgentType\n",
"from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent\n",
"from langchain_experimental.utilities import PythonREPL\n",
"from langchain.agents import Tool\n",
"from langchain.agents.format_scratchpad.openai_tools import (\n",
" format_to_openai_tool_messages,\n",
")\n",
"from langchain.agents.output_parsers.openai_tools import OpenAIToolsAgentOutputParser\n",
"\n",
"from langchain.agents import AgentExecutor\n",
"\n",
"from langchain.agents import create_structured_chat_agent\n",
"from langchain.memory import ConversationBufferWindowMemory\n",
"\n",
"from langchain.output_parsers import PandasDataFrameOutputParser\n",
"from langchain.prompts import PromptTemplate\n",
"\n",
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
"\n",
"from langchain_community.chat_models import ChatAnyscale\n",
"import pandas as pd \n",
"import matplotlib.pyplot as plt \n",
"import numpy as np\n",
"\n",
"plt.style.use('ggplot')\n",
"####################################################################################################"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "9da52e1f",
"metadata": {},
"outputs": [],
"source": [
"# insert your API key here\n",
"os.environ[\"ANYSCALE_API_KEY\"] = \"esecret_8btufnh3615vnbpd924s1t3q7p\"\n",
"memory_key = \"history\""
]
},
{
"cell_type": "markdown",
"id": "edcd6ca7",
"metadata": {},
"source": [
"# Reading the Dataframe and saving it \n",
"<span style=\"font-size:16px;color:red;\">We are saving the dataframe in a csv file in the cwd because we will iteratively update this df if need be between each inference. This way the model will continuously have the most upto date version of the dataframe at its disposal</span>"
]
},
{
"cell_type": "markdown",
"id": "64a086f2",
"metadata": {},
"source": [
"# DECLARE YOUR LLM HERE "
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3f686305",
"metadata": {},
"outputs": [],
"source": [
"llm = ChatAnyscale(model_name='mistralai/Mixtral-8x7B-Instruct-v0.1', temperature=0)"
]
},
{
"cell_type": "markdown",
"id": "4b1d8f8f",
"metadata": {},
"source": [
"# REPL Tool \n",
"<span style=\"font-size:16px;color:red;\">This tool enables our LLM to 'execute' python code so that we can actually display the results to the end-user.</span>"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "23045c5d",
"metadata": {},
"outputs": [],
"source": [
"python_repl = PythonREPL()\n",
"\n",
"repl_tool = Tool(\n",
" name=\"python_repl\",\n",
" description=\"\"\"A Python shell. Shell can dislay charts too. Use this to execute python commands.\\\n",
" You have access to all libraries in python including but not limited to sklearn, pandas, numpy,\\\n",
" matplotlib.pyplot, seaborn etc. Input should be a valid python command. If the user has not explicitly\\\n",
" asked you to plot the results, always print the final output using print(...)\"\"\",\n",
" func=python_repl.run,\n",
")\n",
"\n",
"tools = [repl_tool]"
]
},
{
"cell_type": "markdown",
"id": "c5caa356",
"metadata": {},
"source": [
"# Building an Agent "
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "44c07305",
"metadata": {},
"outputs": [],
"source": [
"prompt = ChatPromptTemplate.from_messages(\n",
" [\n",
" (\n",
" \"system\",\n",
" \"\"\"You are Machine Learning Inference agent. Your job is to use your tools to answer a user query\\\n",
" in the best manner possible.\\\n",
" The user gives you two things:\n",
" 1. A query, and a target task.\n",
" Your job is to extract what you need to do from this query and then accomplish that task.\\\n",
" In case the user asks you to do things like regression/classification, you will need to follow this:\\\n",
" READ IN YOUR DATAFRAME FROM: 'df.csv'\n",
" 1. Split the dataframe into X and y (y= target, X=features)\n",
" 2. Perform some preprocessing on X as per user instructions. \n",
" 3. Split preprocessed X and y into train and test sets.\n",
" 4. If a model is specified, use that model or else pick your model of choice for the given task.\n",
" 5. Run this model on train set. \n",
" 6. Predict on Test Set.\n",
" 5. Print accuracy score by predicting on the test.\n",
" \n",
" Provide no explanation for your code. Enclose all your code between triple backticks ``` \"\"\",\n",
" ),\n",
" (\"user\", \"Dataframe named df:\\n{df}\\nQuery: {input}\\nList of Tools: {tools}\"),\n",
" MessagesPlaceholder(variable_name=\"agent_scratchpad\"),\n",
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0784c5a9",
"metadata": {},
"outputs": [],
"source": [
"os.mkdir('code')"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "f1da1de1",
"metadata": {},
"outputs": [],
"source": [
"def run_code(code):\n",
" \n",
" with open(f'{os.getcwd()}/code/code.py', 'w') as file:\n",
" file.write(code)\n",
" \n",
" try:\n",
" print(\"Running code ...\\n\")\n",
" result = subprocess.run([sys.executable, 'code/code.py'], capture_output=True, text=True, check=True, timeout=20)\n",
" return result.stdout, False\n",
" \n",
" except subprocess.CalledProcessError as e:\n",
" print(\"Error in code!\\n\")\n",
" return e.stdout + e.stderr, True\n",
" \n",
" except subprocess.TimeoutExpired:\n",
" return \"Execution timed out.\", True"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "9602539f",
"metadata": {},
"outputs": [],
"source": [
"def infer(user_input, df, llm):\n",
"\n",
" agent = (\n",
" {\n",
" \"input\": lambda x: x[\"input\"],\n",
" \"tools\": lambda x:x['tools'],\n",
" \"df\": lambda x:x['df'],\n",
" \"agent_scratchpad\": lambda x: format_to_openai_tool_messages(\n",
" x[\"intermediate_steps\"]\n",
" )\n",
" }\n",
" | prompt\n",
" | llm\n",
" | OpenAIToolsAgentOutputParser()\n",
" )\n",
" \n",
" agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)\n",
" \n",
" error_flag = True \n",
" \n",
" while error_flag:\n",
" result = list(agent_executor.stream({\"input\": user_input, \n",
" \"df\":pd.read_csv('df.csv'), \"tools\":tools}))\n",
" \n",
" pattern = r\"```python\\n(.*?)\\n```\"\n",
" matches = re.findall(pattern, result[0]['output'], re.DOTALL)\n",
" code = \"\\n\".join(matches)\n",
" code += \"\\ndf.to_csv('./df.csv', index=False)\"\n",
" \n",
" res, error_flag = run_code(code)\n",
" \n",
" print(res)\n",
" \n",
" # execute the code\n",
" return res"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "692483e9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m ```python\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.preprocessing import LabelEncoder, PolynomialFeatures\n",
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"df = pd.read_csv('df.csv')\n",
"\n",
"# Drop rows with NaN values and non-numerical columns\n",
"df = df.dropna().select_dtypes(include=['int64', 'float64'])\n",
"\n",
"X = df.drop('Fare', axis=1)\n",
"y = df['Fare']\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
"\n",
"model = LinearRegression()\n",
"model.fit(X_train, y_train)\n",
"\n",
"y_pred = model.predict(X_test)\n",
"\n",
"print(model.score(X_test, y_test))\n",
"```\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"Running code ...\n",
"\n",
"0.3610845260081037\n",
"\n"
]
},
{
"data": {
"text/plain": [
"'0.3610845260081037\\n'"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"infer('help me predict the fare using linear regression. Drop Nan rows and non-numerical columns.', df, llm)"
]
},
{
"cell_type": "markdown",
"id": "2b119c18",
"metadata": {},
"source": [
"# Testing the Agent"
]
},
{
"cell_type": "markdown",
"id": "786a9a3e",
"metadata": {},
"source": [
"### First we perform some data cleaning. Observe how each infer call updates the df and passes it onwards."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "fbef0fff",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>PassengerId</th>\n",
" <th>Survived</th>\n",
" <th>Pclass</th>\n",
" <th>Name</th>\n",
" <th>Sex</th>\n",
" <th>Age</th>\n",
" <th>SibSp</th>\n",
" <th>Parch</th>\n",
" <th>Ticket</th>\n",
" <th>Fare</th>\n",
" <th>Cabin</th>\n",
" <th>Embarked</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" <td>Braund, Mr. Owen Harris</td>\n",
" <td>male</td>\n",
" <td>22.0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>A/5 21171</td>\n",
" <td>7.25</td>\n",
" <td>NaN</td>\n",
" <td>S</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" PassengerId Survived Pclass Name Sex Age SibSp \\\n",
"0 1 0 3 Braund, Mr. Owen Harris male 22.0 1 \n",
"\n",
" Parch Ticket Fare Cabin Embarked \n",
"0 0 A/5 21171 7.25 NaN S "
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head(1)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "634e0004",
"metadata": {},
"outputs": [],
"source": [
"# infer(\"Please drop the following columns inplace from the original dataframe: Name, Ticket, Cabin, PassengerId.\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "53ad6af5",
"metadata": {},
"outputs": [],
"source": [
"# infer(\"Please map the strings in the following columns of the original dataframe itself to integers: sex, embarked\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "6000364b",
"metadata": {},
"outputs": [],
"source": [
"# infer(\"Please remove NaNs with most frequent value in the respective column. Do this inplace in original dataframe.\")"
]
},
{
"cell_type": "markdown",
"id": "2c9d0c90",
"metadata": {},
"source": [
"### Linear Regression to predict fare"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "93e026c0",
"metadata": {},
"outputs": [],
"source": [
"infer(\"\"\"Now I want you to help me run linear regression to predict the Fare. Please drop NaN rows and drop \\n\n",
" non numerical columns too.\"\"\")"
]
},
{
"cell_type": "markdown",
"id": "ded19d06",
"metadata": {},
"source": [
"### SVR to predict Fare"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "586cad9b",
"metadata": {},
"outputs": [],
"source": [
"infer(\"\"\"Now I want you to follow all these steps in order:\\n\n",
"1. Split the dataframe into X and y. Where y is Fare column.\n",
"2. Split X and y into train, test sets.\n",
"3. Fit a SVR model to the train set.\n",
"4. Predict on test set.\n",
"5. print the MSE loss on the predictions.\n",
"\n",
"Run all steps from 1 to 5.\n",
"\"\"\")"
]
},
{
"cell_type": "markdown",
"id": "d1d8d758",
"metadata": {},
"source": [
"### Logistic Regression to Classify Survival"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3e0c12cd",
"metadata": {},
"outputs": [],
"source": [
"infer(\"\"\"Now I want you to follow all these steps in order:\\n\n",
"1. Split the dataframe into X and y. Where y is Survived column.\n",
"2. Split X and y into train, test sets.\n",
"3. Fit a Logistic Regression model to the train set.\n",
"4. Predict on test set.\n",
"5. print the accuracy of the predictions. Round to 4 decimal places and print it out neatly.\n",
"\n",
"Run all steps from 1 to 5.\n",
"\"\"\")"
]
},
{
"cell_type": "markdown",
"id": "77dd622b",
"metadata": {},
"source": [
"### SVM to Classify Survival "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f7cdf6a",
"metadata": {},
"outputs": [],
"source": [
"infer(\"\"\"Now I want you to follow all these steps in order:\\n\n",
"1. Split the dataframe into X and y. Where y is Survived column.\n",
"2. Split X and y into train, test sets.\n",
"3. Fit a Gaussian SVM with Poly kernel to the train set.\n",
"4. Predict on test set.\n",
"5. print the accuracy of the predictions. Round to 4 decimal places and print it out neatly.\n",
"\n",
"Run all steps from 1 to 5.\n",
"\"\"\")"
]
},
{
"cell_type": "markdown",
"id": "583e657d",
"metadata": {},
"source": [
"### Naive Bayes to Classify Survival"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cd88d9d7",
"metadata": {},
"outputs": [],
"source": [
"infer(\"\"\"Now I want you to follow all these steps in order:\\n\n",
"1. Split the dataframe into X and y. Where y is Survived column.\n",
"2. Split X and y into train, test sets.\n",
"3. Fit a Naive Bayes model to the train set.\n",
"4. Predict on test set.\n",
"5. print the accuracy of the predictions. Round to 4 decimal places and print it out neatly.\n",
"\n",
"Run all steps from 1 to 5.\n",
"\"\"\")"
]
},
{
"cell_type": "markdown",
"id": "4a6e463b",
"metadata": {},
"source": [
"### KNN to Classify Survival\n",
"<span style=\"font-size:16px;\">We perform a grid search to figure out the best n_neighbors parameter!</span>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d978330c",
"metadata": {},
"outputs": [],
"source": [
"infer(\"\"\"Now I want you to follow all these steps in order:\\n\n",
"1. Split the dataframe into X and y. Where y is Survived column.\n",
"2. Split X and y into train and test sets.\n",
"3. Fit a KNN model to the train set.\n",
"4. Find the best n_neighbors parameter out of range (1,10). Import some grid search method to do this.\n",
"5. Predict with best model on test set.\n",
"6. print the accuracy of the predictions. Round to 4 decimal places and print it out neatly.\n",
"\n",
"Run all steps from 1 to 6.\n",
"\"\"\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|