{ "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=, 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 }