/ examples / instructor_ollama.ipynb
instructor_ollama.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": 1,
 27     "metadata": {},
 28     "outputs": [
 29      {
 30       "name": "stdout",
 31       "output_type": "stream",
 32       "text": [
 33        "name='Jason' age=30\n"
 34       ]
 35      }
 36     ],
 37     "source": [
 38      "from openai import OpenAI\n",
 39      "from pydantic import BaseModel\n",
 40      "import instructor\n",
 41      "\n",
 42      "\n",
 43      "class UserDetail(BaseModel):\n",
 44      "    name: str\n",
 45      "    age: int\n",
 46      "\n",
 47      "\n",
 48      "# enables `response_model` in create call\n",
 49      "client = instructor.patch(\n",
 50      "    OpenAI(\n",
 51      "        base_url=\"http://localhost:11434/v1\",\n",
 52      "        api_key=\"ollama\",  # required, but unused\n",
 53      "    ),\n",
 54      "    mode=instructor.Mode.JSON,\n",
 55      ")\n",
 56      "\n",
 57      "user = client.chat.completions.create(\n",
 58      "    model=\"adrienbrault/nous-hermes2pro:Q8_0\",\n",
 59      "    messages=[\n",
 60      "        {\n",
 61      "            \"role\": \"user\",\n",
 62      "            \"content\": \"Jason is 30 years old\",\n",
 63      "        }\n",
 64      "    ],\n",
 65      "    response_model=UserDetail,\n",
 66      ")\n",
 67      "\n",
 68      "print(user)\n",
 69      "#> name='Jason' age=30"
 70     ]
 71    },
 72    {
 73     "cell_type": "markdown",
 74     "metadata": {},
 75     "source": [
 76      "you can use instructor to generate the desire response model response model is used to validate the response. \n",
 77      "\n",
 78      "\n",
 79      "\n",
 80      "And we can use the same for variety of task to ensure the model is wokring with existing system\n"
 81     ]
 82    },
 83    {
 84     "cell_type": "code",
 85     "execution_count": 2,
 86     "metadata": {},
 87     "outputs": [
 88      {
 89       "name": "stdout",
 90       "output_type": "stream",
 91       "text": [
 92        "name='John Smith' age=28\n",
 93        "name='Mary Johnson' age=35\n",
 94        "name='William Anderson' age=41\n",
 95        "name='Ava Taylor' age=26\n",
 96        "name='Samuel Brown' age=53\n"
 97       ]
 98      }
 99     ],
100     "source": [
101      "### synthatitc data\n",
102      "from typing import Iterable\n",
103      "\n",
104      "def generate_fake_users(count: int) -> Iterable[UserDetail]:\n",
105      "    return client.chat.completions.create(\n",
106      "        model=\"adrienbrault/nous-hermes2pro:Q8_0\",\n",
107      "        response_model=Iterable[UserDetail],\n",
108      "        messages=[\n",
109      "            {\"role\": \"user\", \"content\": f\"Generate a {count} synthetic users\"},\n",
110      "        ],\n",
111      "    )\n",
112      "\n",
113      "\n",
114      "for user in generate_fake_users(5):\n",
115      "    print(user)"
116     ]
117    },
118    {
119     "cell_type": "code",
120     "execution_count": 3,
121     "metadata": {},
122     "outputs": [
123      {
124       "data": {
125        "text/plain": [
126         "{'properties': {'question': {'title': 'Question', 'type': 'string'},\n",
127         "  'answer': {'title': 'Answer', 'type': 'string'},\n",
128         "  'citations': {'items': {'type': 'string'},\n",
129         "   'title': 'Citations',\n",
130         "   'type': 'array'}},\n",
131         " 'required': ['question', 'answer', 'citations'],\n",
132         " 'title': 'QuestionAnswer',\n",
133         " 'type': 'object'}"
134        ]
135       },
136       "execution_count": 3,
137       "metadata": {},
138       "output_type": "execute_result"
139      }
140     ],
141     "source": [
142      "from typing import List\n",
143      "class QuestionAnswer(BaseModel):\n",
144      "    question: str\n",
145      "    answer: str\n",
146      "    citations: List[str]\n",
147      "QuestionAnswer.model_json_schema()"
148     ]
149    },
150    {
151     "cell_type": "code",
152     "execution_count": 4,
153     "metadata": {},
154     "outputs": [],
155     "source": [
156      "\n",
157      "def ask_ai(question: str, context: str) -> QuestionAnswer:\n",
158      "    return client.chat.completions.create(\n",
159      "        model=\"adrienbrault/nous-hermes2pro:Q8_0\",\n",
160      "        temperature=0.1,\n",
161      "        response_model=QuestionAnswer,\n",
162      "        messages=[\n",
163      "            {\n",
164      "                \"role\": \"system\",\n",
165      "                \"content\": \"You are a world class algorithm to answer questions with correct and exact citations.\",\n",
166      "            },\n",
167      "            {\"role\": \"user\", \"content\": f\"{context}\"},\n",
168      "            {\"role\": \"user\", \"content\": f\"Question: {question}\"},\n",
169      "        ],\n",
170      "        validation_context={\"text_chunk\": context},\n",
171      "    )"
172     ]
173    },
174    {
175     "cell_type": "code",
176     "execution_count": 5,
177     "metadata": {},
178     "outputs": [],
179     "source": [
180      "question = \"What did the author do during college?\"\n",
181      "context = \"\"\"\n",
182      "My name is Jason Liu, and I grew up in Toronto Canada but I was born in China.\n",
183      "I went to an arts high school but in university I studied Computational Mathematics and physics.\n",
184      "As part of coop I worked at many companies including Stitchfix, Facebook.\n",
185      "I also started the Data Science club at the University of Waterloo and I was the president of the club for 2 years.\n",
186      "\"\"\"\n",
187      "\n",
188      "response = ask_ai(question, context)"
189     ]
190    },
191    {
192     "cell_type": "code",
193     "execution_count": 6,
194     "metadata": {},
195     "outputs": [
196      {
197       "data": {
198        "text/plain": [
199         "'During college, the author studied Computational Mathematics and physics and worked at many companies including Stitchfix and Facebook. They also started the Data Science club at the University of Waterloo and served as its president for 2 years.'"
200        ]
201       },
202       "metadata": {},
203       "output_type": "display_data"
204      }
205     ],
206     "source": [
207      "from IPython.display import display\n",
208      "display(response.answer)\n"
209     ]
210    },
211    {
212     "cell_type": "code",
213     "execution_count": 7,
214     "metadata": {},
215     "outputs": [
216      {
217       "data": {
218        "text/plain": [
219         "['My name is Jason Liu, and I grew up in Toronto Canada but I was born in China.\\nI went to an arts high school but in university I studied Computational Mathematics and physics.\\nAs part of coop I worked at many companies including Stitchfix, Facebook. \\nI also started the Data Science club at the University of Waterloo and I was the president of the club for 2 years.']"
220        ]
221       },
222       "metadata": {},
223       "output_type": "display_data"
224      }
225     ],
226     "source": [
227      "display(response.citations)"
228     ]
229    },
230    {
231     "cell_type": "code",
232     "execution_count": 9,
233     "metadata": {},
234     "outputs": [],
235     "source": [
236      "question1 = \"Where was John born?\"\n",
237      "context1 = \"\"\"\n",
238      "John Doe is a software engineer who was born in New York, USA. \n",
239      "He studied Computer Science at the Massachusetts Institute of Technology. \n",
240      "During his studies, he interned at Google and Microsoft. \n",
241      "He also founded the Artificial Intelligence club at his university and served as its president for three years.\n",
242      "\"\"\"\n",
243      "\n",
244      "\n",
245      "question2 = \"What did Emily study in university?\"\n",
246      "context2 = \"\"\"\n",
247      "Emily Smith is a data scientist from London, England. \n",
248      "She attended the University of Cambridge where she studied Statistics and Machine Learning. \n",
249      "She interned at IBM and Amazon during her summer breaks. \n",
250      "Emily was also the head of the Women in Tech society at her university.\n",
251      "\"\"\"\n",
252      "\n",
253      "question3 = \"Which companies did Robert intern at?\"\n",
254      "context3 = \"\"\"\n",
255      "Robert Johnson, originally from Sydney, Australia, is a renowned cybersecurity expert. \n",
256      "He studied Information Systems at the University of Melbourne. \n",
257      "Robert interned at several cybersecurity firms including NortonLifeLock and McAfee. \n",
258      "He was also the leader of the Cybersecurity club at his university.\n",
259      "\"\"\"\n",
260      "\n",
261      "\n",
262      "question4 = \"What club did Alice start at her university?\"\n",
263      "context4 = \"\"\"\n",
264      "Alice Williams, a native of Dublin, Ireland, is a successful web developer. \n",
265      "She studied Software Engineering at Trinity College Dublin. \n",
266      "Alice interned at several tech companies including Shopify and Squarespace. \n",
267      "She started the Web Development club at her university and was its president for two years.\n",
268      "\"\"\"\n",
269      "\n",
270      "\n",
271      "question5 = \"What did Michael study in high school?\"\n",
272      "context5 = \"\"\"\n",
273      "Michael Brown is a game developer from Tokyo, Japan. \n",
274      "He attended a specialized high school where he studied Game Design. \n",
275      "He later attended the University of Tokyo where he studied Computer Science. \n",
276      "Michael interned at Sony and Nintendo during his university years. \n",
277      "He also started the Game Developers club at his university.\n",
278      "\"\"\""
279     ]
280    },
281    {
282     "cell_type": "code",
283     "execution_count": 10,
284     "metadata": {},
285     "outputs": [
286      {
287       "data": {
288        "text/plain": [
289         "'Where was John born?'"
290        ]
291       },
292       "metadata": {},
293       "output_type": "display_data"
294      },
295      {
296       "data": {
297        "text/plain": [
298         "'John Doe was born in New York, USA.'"
299        ]
300       },
301       "metadata": {},
302       "output_type": "display_data"
303      },
304      {
305       "data": {
306        "text/plain": [
307         "['John Doe is a software engineer who was born in New York, USA.']"
308        ]
309       },
310       "metadata": {},
311       "output_type": "display_data"
312      },
313      {
314       "name": "stdout",
315       "output_type": "stream",
316       "text": [
317        "\n",
318        "\n",
319        "\n"
320       ]
321      },
322      {
323       "data": {
324        "text/plain": [
325         "'What did Emily study in university?'"
326        ]
327       },
328       "metadata": {},
329       "output_type": "display_data"
330      },
331      {
332       "data": {
333        "text/plain": [
334         "'Emily studied Statistics and Machine Learning at the University of Cambridge.'"
335        ]
336       },
337       "metadata": {},
338       "output_type": "display_data"
339      },
340      {
341       "data": {
342        "text/plain": [
343         "['Emily Smith is a data scientist from London, England. She attended the University of Cambridge where she studied Statistics and Machine Learning.']"
344        ]
345       },
346       "metadata": {},
347       "output_type": "display_data"
348      },
349      {
350       "name": "stdout",
351       "output_type": "stream",
352       "text": [
353        "\n",
354        "\n",
355        "\n"
356       ]
357      },
358      {
359       "data": {
360        "text/plain": [
361         "'Which companies did Robert intern at?'"
362        ]
363       },
364       "metadata": {},
365       "output_type": "display_data"
366      },
367      {
368       "data": {
369        "text/plain": [
370         "'NortonLifeLock and McAfee.'"
371        ]
372       },
373       "metadata": {},
374       "output_type": "display_data"
375      },
376      {
377       "data": {
378        "text/plain": [
379         "['Robert Johnson, originally from Sydney, Australia, is a renowned cybersecurity expert. He studied Information Systems at the University of Melbourne. Robert interned at several cybersecurity firms including NortonLifeLock and McAfee. He was also the leader of the Cybersecurity club at his university.']"
380        ]
381       },
382       "metadata": {},
383       "output_type": "display_data"
384      },
385      {
386       "name": "stdout",
387       "output_type": "stream",
388       "text": [
389        "\n",
390        "\n",
391        "\n"
392       ]
393      },
394      {
395       "data": {
396        "text/plain": [
397         "'What club did Alice start at her university?'"
398        ]
399       },
400       "metadata": {},
401       "output_type": "display_data"
402      },
403      {
404       "data": {
405        "text/plain": [
406         "'Alice started the Web Development club at her university.'"
407        ]
408       },
409       "metadata": {},
410       "output_type": "display_data"
411      },
412      {
413       "data": {
414        "text/plain": [
415         "['Alice Williams, a native of Dublin, Ireland, is a successful web developer. She studied Software Engineering at Trinity College Dublin. Alice interned at several tech companies including Shopify and Squarespace. She started the Web Development club at her university and was its president for two years.']"
416        ]
417       },
418       "metadata": {},
419       "output_type": "display_data"
420      },
421      {
422       "name": "stdout",
423       "output_type": "stream",
424       "text": [
425        "\n",
426        "\n",
427        "\n"
428       ]
429      },
430      {
431       "data": {
432        "text/plain": [
433         "'What did Michael study in high school?'"
434        ]
435       },
436       "metadata": {},
437       "output_type": "display_data"
438      },
439      {
440       "data": {
441        "text/plain": [
442         "'Michael studied Game Design in high school.'"
443        ]
444       },
445       "metadata": {},
446       "output_type": "display_data"
447      },
448      {
449       "data": {
450        "text/plain": [
451         "['Michael Brown is a game developer from Tokyo, Japan. He attended a specialized high school where he studied Game Design.']"
452        ]
453       },
454       "metadata": {},
455       "output_type": "display_data"
456      },
457      {
458       "name": "stdout",
459       "output_type": "stream",
460       "text": [
461        "\n",
462        "\n",
463        "\n"
464       ]
465      }
466     ],
467     "source": [
468      "for question, context in [\n",
469      "    (question1, context1),\n",
470      "    (question2, context2),\n",
471      "    (question3, context3),\n",
472      "    (question4, context4),\n",
473      "    (question5, context5),\n",
474      "]:\n",
475      "    response = ask_ai(question, context)\n",
476      "    display(question)\n",
477      "    display(response.answer)\n",
478      "    display(response.citations)\n",
479      "    print(\"\\n\\n\")"
480     ]
481    },
482    {
483     "cell_type": "code",
484     "execution_count": null,
485     "metadata": {},
486     "outputs": [],
487     "source": []
488    }
489   ],
490   "metadata": {
491    "kernelspec": {
492     "display_name": "hf",
493     "language": "python",
494     "name": "python3"
495    },
496    "language_info": {
497     "codemirror_mode": {
498      "name": "ipython",
499      "version": 3
500     },
501     "file_extension": ".py",
502     "mimetype": "text/x-python",
503     "name": "python",
504     "nbconvert_exporter": "python",
505     "pygments_lexer": "ipython3",
506     "version": "3.10.13"
507    }
508   },
509   "nbformat": 4,
510   "nbformat_minor": 2
511  }