/ examples / instructor_query_decomposition_agent.ipynb
instructor_query_decomposition_agent.ipynb
  1  {
  2   "cells": [
  3    {
  4     "cell_type": "markdown",
  5     "metadata": {},
  6     "source": [
  7      "install ollama and instructor\n",
  8      "```\n",
  9      "pip install instructor\n",
 10      "```\n",
 11      "Ollama download instruction are available here \n",
 12      "https://ollama.com/download\n",
 13      "\n",
 14      "Download the heremes pro model \n",
 15      "Heremes pro is latest model from NousResearch. Somebody already uploaded to ollama model library\n",
 16      "\n",
 17      "```\n",
 18      "ollama pull adrienbrault/nous-hermes2pro:Q8_0\n",
 19      "```\n",
 20      "And then you can use the model in the code below\n",
 21      "\n"
 22     ]
 23    },
 24    {
 25     "cell_type": "code",
 26     "execution_count": 51,
 27     "metadata": {},
 28     "outputs": [],
 29     "source": [
 30      "from openai import OpenAI\n",
 31      "from pydantic import BaseModel\n",
 32      "import instructor\n",
 33      "import json"
 34     ]
 35    },
 36    {
 37     "cell_type": "code",
 38     "execution_count": 3,
 39     "metadata": {},
 40     "outputs": [],
 41     "source": [
 42      "# enables `response_model` in create call\n",
 43      "client = instructor.patch(\n",
 44      "    OpenAI(\n",
 45      "        base_url=\"http://localhost:11434/v1\",\n",
 46      "        api_key=\"ollama\",  # required, but unused\n",
 47      "    ),\n",
 48      "    mode=instructor.Mode.JSON,\n",
 49      ")"
 50     ]
 51    },
 52    {
 53     "cell_type": "code",
 54     "execution_count": 2,
 55     "metadata": {},
 56     "outputs": [
 57      {
 58       "data": {
 59        "text/plain": [
 60         "{'properties': {'question': {'title': 'Question', 'type': 'string'},\n",
 61         "  'answer': {'title': 'Answer', 'type': 'string'},\n",
 62         "  'citations': {'items': {'type': 'string'},\n",
 63         "   'title': 'Citations',\n",
 64         "   'type': 'array'}},\n",
 65         " 'required': ['question', 'answer', 'citations'],\n",
 66         " 'title': 'QuestionAnswer',\n",
 67         " 'type': 'object'}"
 68        ]
 69       },
 70       "execution_count": 2,
 71       "metadata": {},
 72       "output_type": "execute_result"
 73      }
 74     ],
 75     "source": [
 76      "from typing import List\n",
 77      "class QuestionAnswer(BaseModel):\n",
 78      "    question: str\n",
 79      "    answer: str\n",
 80      "    citations: List[str]\n",
 81      "QuestionAnswer.model_json_schema()"
 82     ]
 83    },
 84    {
 85     "cell_type": "code",
 86     "execution_count": 32,
 87     "metadata": {},
 88     "outputs": [],
 89     "source": [
 90      "\n",
 91      "def ask_agent(question: str, context: str) -> QuestionAnswer:\n",
 92      "    messages=[\n",
 93      "            {\n",
 94      "                \"role\": \"system\",\n",
 95      "                \"content\": \"You are a world class agentic framework to answer questions with correct and exact citations.\",\n",
 96      "            },\n",
 97      "            {\"role\": \"user\", \"content\": f\"{context}\"},\n",
 98      "            {\"role\": \"user\", \"content\": f\"Question: {question}\"},\n",
 99      "        ]\n",
100      "    return client.chat.completions.create(\n",
101      "        model=\"adrienbrault/nous-hermes2pro:Q4_0\",\n",
102      "        temperature=0.1,\n",
103      "        response_model=QuestionAnswer,\n",
104      "        messages=messages,\n",
105      "    )"
106     ]
107    },
108    {
109     "cell_type": "code",
110     "execution_count": 44,
111     "metadata": {},
112     "outputs": [
113      {
114       "data": {
115        "text/plain": [
116         "{'properties': {'scratchpad': {'title': 'Scratchpad', 'type': 'string'},\n",
117         "  'sub_questions': {'items': {'type': 'string'},\n",
118         "   'title': 'Sub Questions',\n",
119         "   'type': 'array'}},\n",
120         " 'required': ['scratchpad', 'sub_questions'],\n",
121         " 'title': 'SelfReflection',\n",
122         " 'type': 'object'}"
123        ]
124       },
125       "execution_count": 44,
126       "metadata": {},
127       "output_type": "execute_result"
128      }
129     ],
130     "source": [
131      "from typing import List\n",
132      "class SelfReflection(BaseModel):\n",
133      "    scratchpad: str\n",
134      "    sub_questions: List[str]\n",
135      "SelfReflection.model_json_schema()"
136     ]
137    },
138    {
139     "cell_type": "code",
140     "execution_count": 58,
141     "metadata": {},
142     "outputs": [],
143     "source": [
144      "from concurrent.futures import ThreadPoolExecutor\n",
145      "\n",
146      "def ask_ai(question: str) -> SelfReflection:\n",
147      "    messages=[\n",
148      "            {\n",
149      "                \"role\": \"system\",\n",
150      "                \"content\": \"You are a world class agentic framework to decompose a given question into 5 sub-questions to help collect evidences for final answer. You must plan your query step-by-step inside the <scratchpad></scratchpad> tags\",\n",
151      "            },\n",
152      "            {\"role\": \"user\", \"content\": f\"{question}\"},\n",
153      "        ]\n",
154      "    return client.chat.completions.create(\n",
155      "        model=\"adrienbrault/nous-hermes2pro:Q4_0\",\n",
156      "        temperature=0.1,\n",
157      "        response_model=SelfReflection,\n",
158      "        messages=messages,\n",
159      "    ), messages\n",
160      "\n",
161      "def self_reflection_loop(question: str) -> QuestionAnswer:\n",
162      "    response, messages = ask_ai(question)\n",
163      "    \n",
164      "    agent_calls = \"\"\n",
165      "    for query in response.sub_questions:\n",
166      "        agent_calls += f\"<agent>\\n{query}\\n</agent>\\n\"\n",
167      "\n",
168      "    content = f\"<scratchpad> {response.scratchpad} </scratchpad>\\n\"\n",
169      "    content += agent_calls\n",
170      "    messages.append({\n",
171      "        \"role\": \"assistant\",\n",
172      "        \"content\": content\n",
173      "    })\n",
174      "\n",
175      "    context = \"\"\n",
176      "\n",
177      "    with ThreadPoolExecutor() as executor:\n",
178      "        futures = []\n",
179      "\n",
180      "        for query in response.sub_questions:\n",
181      "            future = executor.submit(ask_agent, query, context)\n",
182      "            futures.append(future)\n",
183      "\n",
184      "        agent_messages = [] \n",
185      "        for i in range(len(futures)):\n",
186      "            result = futures[i].result()\n",
187      "\n",
188      "            agent_messages.append(\n",
189      "                {\n",
190      "                    \"role\": f\"agent-{i}\",\n",
191      "                    \"content\": response.sub_questions[i]\n",
192      "                }\n",
193      "            )\n",
194      "            agent_messages.append(\n",
195      "                {\n",
196      "                    \"role\": f\"agent-{i}-response\",\n",
197      "                    \"content\": result.answer\n",
198      "                }\n",
199      "            )\n",
200      "            context += f\"<agent id={i}>\\n\"\n",
201      "            context += f\"sub_query: {response.sub_questions[i]}\\n\"\n",
202      "            context += f\"answer: {result.answer}\\n\"\n",
203      "            context += f\"</agent>\\n\"\n",
204      "    print(context)\n",
205      "    messages.append({\n",
206      "        \"role\": \"agents\",\n",
207      "        \"content\": agent_messages\n",
208      "    })\n",
209      "    final_answer = ask_agent(question, context)\n",
210      "    messages.append({\n",
211      "        \"role\": \"assistant\",\n",
212      "        \"content\": final_answer.answer\n",
213      "    })\n",
214      "    return final_answer, messages"
215     ]
216    },
217    {
218     "cell_type": "code",
219     "execution_count": 59,
220     "metadata": {},
221     "outputs": [
222      {
223       "name": "stdout",
224       "output_type": "stream",
225       "text": [
226        "<agent id=0>\n",
227        "sub_query: In which book or story can Jay Gatsby be found?\n",
228        "answer: Jay Gatsby can be found in the book 'The Great Gatsby'.\n",
229        "</agent>\n",
230        "<agent id=1>\n",
231        "sub_query: Who is the author of the work featuring Jay Gatsby?\n",
232        "answer: The author of the work featuring Jay Gatsby is F. Scott Fitzgerald.\n",
233        "</agent>\n",
234        "<agent id=2>\n",
235        "sub_query: What is the full name and background of Jay Gatsby?\n",
236        "answer: Jay Gatsby, full name James Gatz, was a fictional character in F. Scott Fitzgerald's 1925 novel, The Great Gatsby. He came from an impoverished background and gained wealth through his involvement in the underworld during World War I.\n",
237        "</agent>\n",
238        "<agent id=3>\n",
239        "sub_query: What is the main character's relationship with Jay Gatsby?\n",
240        "answer: The main character, Nick Carraway, is a friend and narrator of the story. He lives next door to Jay Gatsby and becomes closely acquainted with him throughout the novel.\n",
241        "</agent>\n",
242        "<agent id=4>\n",
243        "sub_query: How does Jay Gatsby's past influence his actions in the story?\n",
244        "answer: Jay Gatsby's past, particularly his love for Daisy Buchanan and his desire to recreate the past, heavily influences his actions throughout the story. He becomes obsessed with regaining Daisy's affection and reclaiming the idealized version of their past relationship, leading him to manipulate and deceive others in pursuit of this goal.\n",
245        "</agent>\n",
246        "\n"
247       ]
248      }
249     ],
250     "source": [
251      "question = \"Who is Jay Gatsby?\"\n",
252      "response, messages = self_reflection_loop(question)"
253     ]
254    },
255    {
256     "cell_type": "code",
257     "execution_count": 60,
258     "metadata": {},
259     "outputs": [
260      {
261       "data": {
262        "text/plain": [
263         "\"Jay Gatsby is a fictional character in F. Scott Fitzgerald's 1925 novel, The Great Gatsby. He comes from an impoverished background and gained wealth through his involvement in the underworld during World War I.\""
264        ]
265       },
266       "execution_count": 60,
267       "metadata": {},
268       "output_type": "execute_result"
269      }
270     ],
271     "source": [
272      "response.answer"
273     ]
274    },
275    {
276     "cell_type": "code",
277     "execution_count": 61,
278     "metadata": {},
279     "outputs": [
280      {
281       "name": "stdout",
282       "output_type": "stream",
283       "text": [
284        "[{'role': 'system', 'content': \"You are a world class agentic framework to decompose a given question into 5 sub-questions to help collect evidences for final answer. You must plan your query step-by-step inside the <scratchpad></scratchpad> tags\\n\\n\\nAs a genius expert, your task is to understand the content and provide\\nthe parsed objects in json that match the following json_schema:\\n\\n\\n{'properties': {'scratchpad': {'title': 'Scratchpad', 'type': 'string'}, 'sub_questions': {'items': {'type': 'string'}, 'title': 'Sub Questions', 'type': 'array'}}, 'required': ['scratchpad', 'sub_questions'], 'title': 'SelfReflection', 'type': 'object'}\\n\\nMake sure to return an instance of the JSON, not the schema itself\\n\"}, {'role': 'user', 'content': 'Who is Jay Gatsby?'}, {'role': 'assistant', 'content': \"<scratchpad> Who is Jay Gatsby? </scratchpad>\\n<agent>\\nIn which book or story can Jay Gatsby be found?\\n</agent>\\n<agent>\\nWho is the author of the work featuring Jay Gatsby?\\n</agent>\\n<agent>\\nWhat is the full name and background of Jay Gatsby?\\n</agent>\\n<agent>\\nWhat is the main character's relationship with Jay Gatsby?\\n</agent>\\n<agent>\\nHow does Jay Gatsby's past influence his actions in the story?\\n</agent>\\n\"}, {'role': 'agents', 'content': [{'role': 'agent-0', 'content': 'In which book or story can Jay Gatsby be found?'}, {'role': 'agent-0-response', 'content': \"Jay Gatsby can be found in the book 'The Great Gatsby'.\"}, {'role': 'agent-1', 'content': 'Who is the author of the work featuring Jay Gatsby?'}, {'role': 'agent-1-response', 'content': 'The author of the work featuring Jay Gatsby is F. Scott Fitzgerald.'}, {'role': 'agent-2', 'content': 'What is the full name and background of Jay Gatsby?'}, {'role': 'agent-2-response', 'content': \"Jay Gatsby, full name James Gatz, was a fictional character in F. Scott Fitzgerald's 1925 novel, The Great Gatsby. He came from an impoverished background and gained wealth through his involvement in the underworld during World War I.\"}, {'role': 'agent-3', 'content': \"What is the main character's relationship with Jay Gatsby?\"}, {'role': 'agent-3-response', 'content': 'The main character, Nick Carraway, is a friend and narrator of the story. He lives next door to Jay Gatsby and becomes closely acquainted with him throughout the novel.'}, {'role': 'agent-4', 'content': \"How does Jay Gatsby's past influence his actions in the story?\"}, {'role': 'agent-4-response', 'content': \"Jay Gatsby's past, particularly his love for Daisy Buchanan and his desire to recreate the past, heavily influences his actions throughout the story. He becomes obsessed with regaining Daisy's affection and reclaiming the idealized version of their past relationship, leading him to manipulate and deceive others in pursuit of this goal.\"}]}, {'role': 'assistant', 'content': \"Jay Gatsby is a fictional character in F. Scott Fitzgerald's 1925 novel, The Great Gatsby. He comes from an impoverished background and gained wealth through his involvement in the underworld during World War I.\"}]\n"
285       ]
286      }
287     ],
288     "source": [
289      "print(messages)"
290     ]
291    },
292    {
293     "cell_type": "code",
294     "execution_count": 63,
295     "metadata": {},
296     "outputs": [
297      {
298       "name": "stdout",
299       "output_type": "stream",
300       "text": [
301        "[\n",
302        "  {\n",
303        "    \"role\": \"system\",\n",
304        "    \"content\": \"You are a world class agentic framework to decompose a given question into 5 sub-questions to help collect evidences for final answer. You must plan your query step-by-step inside the <scratchpad></scratchpad> tags\\n\\n\\nAs a genius expert, your task is to understand the content and provide\\nthe parsed objects in json that match the following json_schema:\\n\\n\\n{'properties': {'scratchpad': {'title': 'Scratchpad', 'type': 'string'}, 'sub_questions': {'items': {'type': 'string'}, 'title': 'Sub Questions', 'type': 'array'}}, 'required': ['scratchpad', 'sub_questions'], 'title': 'SelfReflection', 'type': 'object'}\\n\\nMake sure to return an instance of the JSON, not the schema itself\\n\"\n",
305        "  },\n",
306        "  {\n",
307        "    \"role\": \"user\",\n",
308        "    \"content\": \"Who is Jay Gatsby?\"\n",
309        "  },\n",
310        "  {\n",
311        "    \"role\": \"assistant\",\n",
312        "    \"content\": \"<scratchpad> Who is Jay Gatsby? </scratchpad>\\n<agent>\\nIn which book or story can Jay Gatsby be found?\\n</agent>\\n<agent>\\nWho is the author of the work featuring Jay Gatsby?\\n</agent>\\n<agent>\\nWhat is the full name and background of Jay Gatsby?\\n</agent>\\n<agent>\\nWhat is the main character's relationship with Jay Gatsby?\\n</agent>\\n<agent>\\nHow does Jay Gatsby's past influence his actions in the story?\\n</agent>\\n\"\n",
313        "  },\n",
314        "  {\n",
315        "    \"role\": \"agents\",\n",
316        "    \"content\": [\n",
317        "      {\n",
318        "        \"role\": \"agent-0\",\n",
319        "        \"content\": \"In which book or story can Jay Gatsby be found?\"\n",
320        "      },\n",
321        "      {\n",
322        "        \"role\": \"agent-0-response\",\n",
323        "        \"content\": \"Jay Gatsby can be found in the book 'The Great Gatsby'.\"\n",
324        "      },\n",
325        "      {\n",
326        "        \"role\": \"agent-1\",\n",
327        "        \"content\": \"Who is the author of the work featuring Jay Gatsby?\"\n",
328        "      },\n",
329        "      {\n",
330        "        \"role\": \"agent-1-response\",\n",
331        "        \"content\": \"The author of the work featuring Jay Gatsby is F. Scott Fitzgerald.\"\n",
332        "      },\n",
333        "      {\n",
334        "        \"role\": \"agent-2\",\n",
335        "        \"content\": \"What is the full name and background of Jay Gatsby?\"\n",
336        "      },\n",
337        "      {\n",
338        "        \"role\": \"agent-2-response\",\n",
339        "        \"content\": \"Jay Gatsby, full name James Gatz, was a fictional character in F. Scott Fitzgerald's 1925 novel, The Great Gatsby. He came from an impoverished background and gained wealth through his involvement in the underworld during World War I.\"\n",
340        "      },\n",
341        "      {\n",
342        "        \"role\": \"agent-3\",\n",
343        "        \"content\": \"What is the main character's relationship with Jay Gatsby?\"\n",
344        "      },\n",
345        "      {\n",
346        "        \"role\": \"agent-3-response\",\n",
347        "        \"content\": \"The main character, Nick Carraway, is a friend and narrator of the story. He lives next door to Jay Gatsby and becomes closely acquainted with him throughout the novel.\"\n",
348        "      },\n",
349        "      {\n",
350        "        \"role\": \"agent-4\",\n",
351        "        \"content\": \"How does Jay Gatsby's past influence his actions in the story?\"\n",
352        "      },\n",
353        "      {\n",
354        "        \"role\": \"agent-4-response\",\n",
355        "        \"content\": \"Jay Gatsby's past, particularly his love for Daisy Buchanan and his desire to recreate the past, heavily influences his actions throughout the story. He becomes obsessed with regaining Daisy's affection and reclaiming the idealized version of their past relationship, leading him to manipulate and deceive others in pursuit of this goal.\"\n",
356        "      }\n",
357        "    ]\n",
358        "  },\n",
359        "  {\n",
360        "    \"role\": \"assistant\",\n",
361        "    \"content\": \"Jay Gatsby is a fictional character in F. Scott Fitzgerald's 1925 novel, The Great Gatsby. He comes from an impoverished background and gained wealth through his involvement in the underworld during World War I.\"\n",
362        "  }\n",
363        "]\n"
364       ]
365      }
366     ],
367     "source": [
368      "print(json.dumps(messages, indent=2))"
369     ]
370    },
371    {
372     "cell_type": "code",
373     "execution_count": null,
374     "metadata": {},
375     "outputs": [],
376     "source": []
377    }
378   ],
379   "metadata": {
380    "kernelspec": {
381     "display_name": "func-call-demo",
382     "language": "python",
383     "name": "python3"
384    },
385    "language_info": {
386     "codemirror_mode": {
387      "name": "ipython",
388      "version": 3
389     },
390     "file_extension": ".py",
391     "mimetype": "text/x-python",
392     "name": "python",
393     "nbconvert_exporter": "python",
394     "pygments_lexer": "ipython3",
395     "version": "3.12.2"
396    }
397   },
398   "nbformat": 4,
399   "nbformat_minor": 2
400  }