Spaces:
Runtime error
Runtime error
File size: 24,105 Bytes
5536b2b | 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 | {
"cells": [
{
"cell_type": "markdown",
"id": "aed563e2",
"metadata": {},
"source": [
"# Retrieval"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "3c99cd9f",
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.vectorstores import FAISS\n",
"from langchain_ollama import OllamaEmbeddings\n",
"from langchain_ollama import ChatOllama\n",
"from langchain_core.prompts import PromptTemplate"
]
},
{
"cell_type": "markdown",
"id": "002e9bea",
"metadata": {},
"source": [
"#### Load Vectore_store"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "20eb543e",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 21,
"id": "93a1dad2",
"metadata": {},
"outputs": [],
"source": [
"embedding = OllamaEmbeddings(model=\"nomic-embed-text\")\n",
"\n",
"vector_store = FAISS.load_local(\n",
" \"faiss_index\",\n",
" embedding,\n",
" allow_dangerous_deserialization=True\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "2aa58c7c",
"metadata": {},
"outputs": [],
"source": [
"retriever = vector_store.as_retriever(search_type=\"similarity\", search_kwargs={\"k\": 1})"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "4ddc68fb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"VectorStoreRetriever(tags=['FAISS', 'OllamaEmbeddings'], vectorstore=<langchain_community.vectorstores.faiss.FAISS object at 0x000002832218BD10>, search_kwargs={'k': 1})"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"retriever"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "af9e08ab",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Document(id='d19b5983-6c58-4baa-9878-d1edcc1330f0', metadata={'topic': 'Distinct and Count Statement', 'question_no': 1}, page_content='1.Identify count of orders by counting ORDER_ID from ORDERS table. To count order_Id from table, you can write the following statement.\\n\\n\\n\\nselect \\n\\n count(order_id) \\n\\n from orders')]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"retriever.invoke('Identify count of orders by counting ORDER_ID from ORDERS table. To count order_Id from table, you can write the following statement')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18a0d111",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "18e9e4bd",
"metadata": {},
"source": [
"# Step 3 - Augmentation"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "bd69d69e",
"metadata": {},
"outputs": [],
"source": [
"llm = ChatOllama(\n",
" model=\"llama3.2\",\n",
" temperature=0\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "e4cef64b",
"metadata": {},
"outputs": [],
"source": [
"prompt = PromptTemplate(\n",
" template=\"\"\"\n",
"You are an SQL assistant.\n",
"\n",
"Use ONLY the provided context.\n",
"\n",
"Rules:\n",
"1. If the SQL query exists in the context, return ONLY that SQL query.\n",
"2. Do not explain.\n",
"3. Do not rewrite the question.\n",
"4. Do not generate a new query.\n",
"5. If the SQL query is missing, reply exactly:\n",
"I don't know.\n",
"\n",
"Context:\n",
"{context}\n",
"\n",
"Question:\n",
"{question}\n",
"\n",
"SQL:\n",
"\"\"\",\n",
" input_variables=[\"context\", \"question\"],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "708dbc11",
"metadata": {},
"outputs": [],
"source": [
"question = \"You can see that counting customer_id is going to give you the same result. It means that even if you use count(*), you are going to get the same number of rows. Try using COUNT(*) this time to count the number of rows in the table \"\n"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "dba263ce",
"metadata": {},
"outputs": [],
"source": [
"context_text = retriever.invoke(question)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "0f98f8d6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Document(id='579858b1-f5c5-475c-8958-b16098c1a20b', metadata={'topic': 'Distinct and Count Statement', 'question_no': 3}, page_content='3.You can see that counting customer_id is going to give you the same result. It means that even if you use count(*), you are going to get the same number of rows. Try using COUNT(*) this time to count the number of rows in the table\\n\\n\\n\\n select \\n\\n count(*) \\n\\n from orders')]"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"context_text"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "d5878f7b",
"metadata": {},
"outputs": [],
"source": [
"final_prompt = prompt.invoke({\"context\": context_text, \"question\": question})"
]
},
{
"cell_type": "markdown",
"id": "a31f0f2e",
"metadata": {},
"source": [
"# Step 4 - Generation"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "9166fe55",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"select count(*) from orders\n"
]
}
],
"source": [
"answer = llm.invoke(final_prompt)\n",
"print(answer.content)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ffae664d",
"metadata": {},
"outputs": [],
"source": [
"prompt= PromptTemplate(\n",
" template=\"\"\"you have to extract the SQL query from the image and all the other dat from the . If the SQL query is not present in the context, return \"I don't know.\" Do not provide any explanations or additional information.\n",
" \n",
" \"\"\"\n",
" \n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "13b8ce68",
"metadata": {},
"outputs": [],
"source": [
"response = chat(\n",
" model=\"qwen2.5vl:7b\",\n",
" messages=[\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": prompt,\n",
" \"images\": [image_path] # User uploaded image\n",
" }\n",
" ]\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "deec7ff4",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "6a8080fa",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "b020203d",
"metadata": {},
"source": [
"# Building a Chain"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "f31cfbd8",
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.runnables import RunnableParallel, RunnablePassthrough, RunnableLambda\n",
"from langchain_core.output_parsers import StrOutputParser"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "3580a60a",
"metadata": {},
"outputs": [],
"source": [
"parallel_chain = RunnableParallel({\n",
" 'context': retriever ,\n",
" 'question': RunnablePassthrough()\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "277f666d",
"metadata": {},
"outputs": [],
"source": [
"parser = StrOutputParser()"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "aec127e2",
"metadata": {},
"outputs": [],
"source": [
"main_chain = parallel_chain | prompt | llm | parser"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'select count(*) from orders'"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"question = \"You can see that counting customer_id is going to give you the same result. It means that even if you use count(*), you are going to get the same number of rows. Try using COUNT(*) this time to count the number of rows in the table \"\n",
"main_chain.invoke(question)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4372959",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "1956a2ec",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 59,
"id": "f9c5db7b",
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.prompts import ChatPromptTemplate\n",
"from langchain_core.messages import HumanMessage\n",
"\n",
"# Define the extraction instructions\n",
"system_instructions = \"\"\"\n",
"Act as an expert OCR and data extraction assistant. I will provide you with an image of a user asking a coding question on a learning platform.\n",
"\n",
"Please format your response clearly using the following headings:\n",
"### User Statement\n",
"Extract the introductory text written by the user at the very top of the image.\n",
"\n",
"### Question Description\n",
"Extract the problem statement from the left-hand panel under the \"Description\" tab.\n",
"\n",
"### SQL Query\n",
"Extract the exact SQL code written inside the dark-themed code editor on the right. Preserve the line breaks and formatting.\n",
"\n",
"### Error Message\n",
"Extract the red error message displayed in the terminal/result area at the bottom right.\n",
"\"\"\"\n",
"\n",
"# Create the ChatPromptTemplate for multimodal input\n",
"multimodal_prompt = ChatPromptTemplate.from_messages([\n",
" (\"system\", system_instructions),\n",
" (\"user\", [\n",
" {\"type\": \"text\", \"text\": \"Please extract the requested data from this image.\"},\n",
" {\"type\": \"image_url\", \"image_url\": \"data:image/png;base64,{image_base64}\"}\n",
" ])\n",
"])\n",
"\n",
"# Example usage (assuming 'image_base64' is your base64 encoded string of the image):\n",
"# chain = multimodal_prompt | vision_llm\n",
"# result = chain.invoke({\"image_base64\": base64_string})"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "9f054cc3",
"metadata": {},
"outputs": [
{
"ename": "ValidationError",
"evalue": "1 validation error for Message\ncontent\n Input should be a valid string [type=string_type, input_value=ChatPromptTemplate(input_... additional_kwargs={})]), input_type=ChatPromptTemplate]\n For further information visit https://errors.pydantic.dev/2.13/v/string_type",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mValidationError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[61]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m ollama \u001b[38;5;28;01mimport\u001b[39;00m chat\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m response = chat(\n\u001b[32m 3\u001b[39m model=\u001b[33m\"qwen2.5vl:7b\"\u001b[39m,\n\u001b[32m 4\u001b[39m messages=[\n\u001b[32m 5\u001b[39m {\n",
"\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\p4pri\\anaconda3\\envs\\langchain311\\Lib\\site-packages\\ollama\\_client.py:393\u001b[39m, in \u001b[36mClient.chat\u001b[39m\u001b[34m(self, model, messages, tools, stream, think, logprobs, top_logprobs, format, options, keep_alive)\u001b[39m\n\u001b[32m 340\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mchat\u001b[39m(\n\u001b[32m 341\u001b[39m \u001b[38;5;28mself\u001b[39m,\n\u001b[32m 342\u001b[39m model: \u001b[38;5;28mstr\u001b[39m = \u001b[33m'\u001b[39m\u001b[33m'\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 352\u001b[39m keep_alive: Optional[Union[\u001b[38;5;28mfloat\u001b[39m, \u001b[38;5;28mstr\u001b[39m]] = \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[32m 353\u001b[39m ) -> Union[ChatResponse, Iterator[ChatResponse]]:\n\u001b[32m 354\u001b[39m \u001b[38;5;250m \u001b[39m\u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m 355\u001b[39m \u001b[33;03m Create a chat response using the requested model.\u001b[39;00m\n\u001b[32m 356\u001b[39m \n\u001b[32m (...)\u001b[39m\u001b[32m 385\u001b[39m \u001b[33;03m Returns `ChatResponse` if `stream` is `False`, otherwise returns a `ChatResponse` generator.\u001b[39;00m\n\u001b[32m 386\u001b[39m \u001b[33;03m \"\"\"\u001b[39;00m\n\u001b[32m 387\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m._request(\n\u001b[32m 388\u001b[39m ChatResponse,\n\u001b[32m 389\u001b[39m \u001b[33m'\u001b[39m\u001b[33mPOST\u001b[39m\u001b[33m'\u001b[39m,\n\u001b[32m 390\u001b[39m \u001b[33m'\u001b[39m\u001b[33m/api/chat\u001b[39m\u001b[33m'\u001b[39m,\n\u001b[32m 391\u001b[39m json=ChatRequest(\n\u001b[32m 392\u001b[39m model=model,\n\u001b[32m--> \u001b[39m\u001b[32m393\u001b[39m messages=\u001b[30;43mlist\u001b[39;49m\u001b[30;43m(\u001b[39;49m\u001b[30;43m_copy_messages\u001b[39;49m\u001b[30;43m(\u001b[39;49m\u001b[30;43mmessages\u001b[39;49m\u001b[30;43m)\u001b[39;49m\u001b[30;43m)\u001b[39;49m,\n\u001b[32m 394\u001b[39m tools=\u001b[38;5;28mlist\u001b[39m(_copy_tools(tools)),\n\u001b[32m 395\u001b[39m stream=stream,\n\u001b[32m 396\u001b[39m think=think,\n\u001b[32m 397\u001b[39m logprobs=logprobs,\n\u001b[32m 398\u001b[39m top_logprobs=top_logprobs,\n\u001b[32m 399\u001b[39m \u001b[38;5;28mformat\u001b[39m=\u001b[38;5;28mformat\u001b[39m,\n\u001b[32m 400\u001b[39m options=options,\n\u001b[32m 401\u001b[39m keep_alive=keep_alive,\n\u001b[32m 402\u001b[39m ).model_dump(exclude_none=\u001b[38;5;28;01mTrue\u001b[39;00m),\n\u001b[32m 403\u001b[39m stream=stream,\n\u001b[32m 404\u001b[39m )\n",
"\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\p4pri\\anaconda3\\envs\\langchain311\\Lib\\site-packages\\ollama\\_client.py:1323\u001b[39m, in \u001b[36m_copy_messages\u001b[39m\u001b[34m(messages)\u001b[39m\n\u001b[32m 1321\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34m_copy_messages\u001b[39m(messages: Optional[Sequence[Union[Mapping[\u001b[38;5;28mstr\u001b[39m, Any], Message]]]) -> Iterator[Message]:\n\u001b[32m 1322\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m message \u001b[38;5;129;01min\u001b[39;00m messages \u001b[38;5;129;01mor\u001b[39;00m []:\n\u001b[32m-> \u001b[39m\u001b[32m1323\u001b[39m \u001b[38;5;28;01myield\u001b[39;00m \u001b[30;43mMessage\u001b[39;49m\u001b[30;43m.\u001b[39;49m\u001b[30;43mmodel_validate\u001b[39;49m\u001b[30;43m(\u001b[39;49m\n\u001b[32m 1324\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43m{\u001b[39;49m\u001b[30;43mk\u001b[39;49m\u001b[30;43m:\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43mlist\u001b[39;49m\u001b[30;43m(\u001b[39;49m\u001b[30;43m_copy_images\u001b[39;49m\u001b[30;43m(\u001b[39;49m\u001b[30;43mv\u001b[39;49m\u001b[30;43m)\u001b[39;49m\u001b[30;43m)\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43;01mif\u001b[39;49;00m\u001b[30;43m \u001b[39;49m\u001b[30;43mk\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43m==\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43m'\u001b[39;49m\u001b[30;43mimages\u001b[39;49m\u001b[30;43m'\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43;01melse\u001b[39;49;00m\u001b[30;43m \u001b[39;49m\u001b[30;43mv\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43;01mfor\u001b[39;49;00m\u001b[30;43m \u001b[39;49m\u001b[30;43mk\u001b[39;49m\u001b[30;43m,\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43mv\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43;01min\u001b[39;49;00m\u001b[30;43m \u001b[39;49m\u001b[30;43mdict\u001b[39;49m\u001b[30;43m(\u001b[39;49m\u001b[30;43mmessage\u001b[39;49m\u001b[30;43m)\u001b[39;49m\u001b[30;43m.\u001b[39;49m\u001b[30;43mitems\u001b[39;49m\u001b[30;43m(\u001b[39;49m\u001b[30;43m)\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43;01mif\u001b[39;49;00m\u001b[30;43m \u001b[39;49m\u001b[30;43mv\u001b[39;49m\u001b[30;43m}\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 1325\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43m)\u001b[39;49m\n",
"\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\p4pri\\anaconda3\\envs\\langchain311\\Lib\\site-packages\\pydantic\\main.py:732\u001b[39m, in \u001b[36mBaseModel.model_validate\u001b[39m\u001b[34m(cls, obj, strict, extra, from_attributes, context, by_alias, by_name)\u001b[39m\n\u001b[32m 726\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m by_alias \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m by_name \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[32m 727\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m PydanticUserError(\n\u001b[32m 728\u001b[39m \u001b[33m'\u001b[39m\u001b[33mAt least one of `by_alias` or `by_name` must be set to True.\u001b[39m\u001b[33m'\u001b[39m,\n\u001b[32m 729\u001b[39m code=\u001b[33m'\u001b[39m\u001b[33mvalidate-by-alias-and-name-false\u001b[39m\u001b[33m'\u001b[39m,\n\u001b[32m 730\u001b[39m )\n\u001b[32m--> \u001b[39m\u001b[32m732\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[30;43mcls\u001b[39;49m\u001b[30;43m.\u001b[39;49m\u001b[30;43m__pydantic_validator__\u001b[39;49m\u001b[30;43m.\u001b[39;49m\u001b[30;43mvalidate_python\u001b[39;49m\u001b[30;43m(\u001b[39;49m\n\u001b[32m 733\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mobj\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 734\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mstrict\u001b[39;49m\u001b[30;43m=\u001b[39;49m\u001b[30;43mstrict\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 735\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mextra\u001b[39;49m\u001b[30;43m=\u001b[39;49m\u001b[30;43mextra\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 736\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mfrom_attributes\u001b[39;49m\u001b[30;43m=\u001b[39;49m\u001b[30;43mfrom_attributes\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 737\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mcontext\u001b[39;49m\u001b[30;43m=\u001b[39;49m\u001b[30;43mcontext\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 738\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mby_alias\u001b[39;49m\u001b[30;43m=\u001b[39;49m\u001b[30;43mby_alias\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 739\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mby_name\u001b[39;49m\u001b[30;43m=\u001b[39;49m\u001b[30;43mby_name\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 740\u001b[39m \u001b[30;43m\u001b[39;49m\u001b[30;43m)\u001b[39;49m\n",
"\u001b[31mValidationError\u001b[39m: 1 validation error for Message\ncontent\n Input should be a valid string [type=string_type, input_value=ChatPromptTemplate(input_... additional_kwargs={})]), input_type=ChatPromptTemplate]\n For further information visit https://errors.pydantic.dev/2.13/v/string_type"
]
}
],
"source": [
"from ollama import chat\n",
"response = chat(\n",
" model=\"qwen2.5vl:7b\",\n",
" messages=[\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": multimodal_prompt,\n",
" \"images\": ['sql_eorror.png'] # User uploaded image\n",
" }\n",
" ]\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "58284ff3",
"metadata": {},
"outputs": [],
"source": [
"response"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "2c273a9c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"### User Statement\n",
"No user statement is provided in the image.\n",
"\n",
"### Question Description\n",
"The SQL script attempts to alter a table named 'CAL' by setting a column as NOT NULL and adding a PRIMARY KEY constraint, followed by creating an index on that column. However, it encounters errors due to constraints related to nullability of columns.\n",
"\n",
"### SQL Query\n",
"```sql\n",
"IF NOT EXISTS(\n",
" SELECT *\n",
" FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS\n",
" WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME = 'CAL'\n",
")\n",
"BEGIN\n",
" DROP INDEX IF EXISTS CAL$01 ON dbo.CAL;\n",
" \n",
" ALTER TABLE BTS.dbo.CAL\n",
" ALTER COLUMN Intern \n",
" INT NOT NULL;\n",
"\n",
" ALTER TABLE BTS.dbo.CAL\n",
" ADD CONSTRAINT PK_CAL_Intern PRIMARY KEY (Intern);\n",
"\n",
" CREATE INDEX CAL$01\n",
" ON CAL (Intern);\n",
"END\n",
"```\n",
"\n",
"### Error Message\n",
"Msg 8111, Level 16, State 1, Line 111 \n",
"Cannot define PRIMARY KEY constraint on nullable column in table 'CAL'. \n",
"\n",
"Msg 1750, Level 16, State 0, Line 111 \n",
"Could not create constraint or index. See previous errors.\n"
]
}
],
"source": [
"from ollama import chat\n",
"\n",
"system_prompt = \"\"\"\n",
"Act as an expert OCR and data extraction assistant.\n",
"\n",
"Please format your response clearly using the following headings:\n",
"\n",
"### User Statement\n",
"Extract the introductory text written by the user at the very top of the image.\n",
"\n",
"### Question Description\n",
"Extract the problem statement from the left-hand panel under the Description tab.\n",
"\n",
"### SQL Query\n",
"Extract the exact SQL code written inside the dark-themed code editor on the right.\n",
"\n",
"### Error Message\n",
"Extract the red error message displayed at the bottom right.\n",
"\"\"\"\n",
"\n",
"response = chat(\n",
" model=\"qwen2.5vl:7b\",\n",
" messages=[\n",
" {\n",
" \"role\": \"system\",\n",
" \"content\": system_prompt\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"Extract all requested information from this image.\",\n",
" \"images\": [\"sql_eorror.png\"]\n",
" }\n",
" ]\n",
")\n",
"\n",
"print(response.message.content)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee298127",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "langchain311",
"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.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|