|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 12, |
| 6 | + "id": "e98f3b99-5996-47a2-ac5c-b6ed9ca19c1c", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "!pip install -qq llama_index llama-index-llms-openai-like" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 42, |
| 16 | + "id": "33c2a1c7-aa33-4261-8e0f-857377b97409", |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "from llama_index.llms.openai_like import OpenAILike\n", |
| 21 | + "\n", |
| 22 | + "client = OpenAI()\n", |
| 23 | + "\n", |
| 24 | + "llm = OpenAILike(\n", |
| 25 | + " model=\"granite3.3\",\n", |
| 26 | + " api_base=\"http://localhost:11434/v1\",\n", |
| 27 | + " temperature=0,\n", |
| 28 | + ")\n", |
| 29 | + "\n", |
| 30 | + "llm = OpenAILike(\n", |
| 31 | + " model=\"mistralai/Mixtral-8x7B-Instruct-v0.1\",\n", |
| 32 | + " api_base=\"https://mixtral-8x7b-instruct-v0-1-maas-apicast-production.apps.prod.rhoai.rh-aiservices-bu.com:443/v1\",\n", |
| 33 | + " api_key=\"a8230601c7cfc3c891ab744108417f8e\"\n", |
| 34 | + " temperature=0,\n", |
| 35 | + ")\n", |
| 36 | + " " |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "code", |
| 41 | + "execution_count": 47, |
| 42 | + "id": "faef8d7a-aa25-46a9-9598-7413e149d4bc", |
| 43 | + "metadata": {}, |
| 44 | + "outputs": [ |
| 45 | + { |
| 46 | + "name": "stdout", |
| 47 | + "output_type": "stream", |
| 48 | + "text": [ |
| 49 | + "{\"fact_single\": \"What type of document can be verified for authenticity using the given method?\", \"fact_single_answer\": \"Government-issued photo identification documents\", \"summary\": \"What aspects should be checked to ensure a government-issued photo identification document is valid and current?\", \"summary_answer\": \"The original physical document's characteristics, security features, and its status in relation to alterations or expiration\", \"reasoning\": \"Why is it important to verify the authenticity, validity, and currency of a government-issued photo identification document in person with the person being identified?\"}\n" |
| 50 | + ] |
| 51 | + } |
| 52 | + ], |
| 53 | + "source": [ |
| 54 | + "from llama_index.core.prompts import PromptTemplate\n", |
| 55 | + "\n", |
| 56 | + "context_str = \"\"\"You can determine whether a government-issued photo identification\n", |
| 57 | + "document is authentic, valid and current by viewing it in person, and by\n", |
| 58 | + "looking at the characteristics of the original physical document and its\n", |
| 59 | + "security features (or markers, as applicable) in the presence of the person\n", |
| 60 | + "being identified. This will allow you to be satisfied that the identification\n", |
| 61 | + "document is authentic, as issued by the competent authority (federal,\n", |
| 62 | + "provincial, or territorial government), valid (unaltered, not counterfeit) and\n", |
| 63 | + "current (not expired).\"\"\"\n", |
| 64 | + "\n", |
| 65 | + "prompt_template = (\n", |
| 66 | + " \"I will provide you a text passage. I need you to generate three questions that \"\n", |
| 67 | + " \"must be answered only with information contained in this passage, and nothing \"\n", |
| 68 | + " \"else.\\n\"\n", |
| 69 | + " 'The first question is of type \"fact_single\", which means that the answer to this '\n", |
| 70 | + " \"question is a simple, single piece of factual information contained in the \"\n", |
| 71 | + " \"context.\\n\"\n", |
| 72 | + " 'The second question is of type \"summary\", which means that the answer to this '\n", |
| 73 | + " \"question summarizes different pieces of factual information contained in the \"\n", |
| 74 | + " \"context.\\n\"\n", |
| 75 | + " 'The third question is of type \"reasoning\", which is a question that requires the '\n", |
| 76 | + " \"reader to think critically and make an inference or draw a conclusion based on \"\n", |
| 77 | + " \"the information provided in the passage.\\n\"\n", |
| 78 | + " \"Make sure that the three questions are different.\\n\"\n", |
| 79 | + " \"\\n\"\n", |
| 80 | + " \"You will format your generation as a python dictionary, such as:\\n\"\n", |
| 81 | + " '{\"fact_single\": <The \"fact_single\" type question you thought of>, '\n", |
| 82 | + " '\"fact_single_answer: <Answer to the \"fact_single\" question>, \"summary\": <the '\n", |
| 83 | + " '\"summary\" type question you thought of>, \"summary_answer\": <Answer to the '\n", |
| 84 | + " '\"summary\" question>, \"reasoning\": <the \"reasoning\" type question you thought '\n", |
| 85 | + " 'of>, \"reasoning_answer\": <Answer to the \"reasoning\" question>}\\n'\n", |
| 86 | + " \"\\n\"\n", |
| 87 | + " \"Only provide the python dictionary as your output.\\n\"\n", |
| 88 | + " \"\\n\"\n", |
| 89 | + " \"Context: {context_str}\"\n", |
| 90 | + ")\n", |
| 91 | + "\n", |
| 92 | + "prompt = PromptTemplate(prompt_template).format(context_str=context_str, customization_statement=customization_statement)\n", |
| 93 | + "\n", |
| 94 | + "#print(prompt)\n", |
| 95 | + "response = llm.complete(prompt)\n", |
| 96 | + "print(str(response))" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": 51, |
| 102 | + "id": "98fcd2b4-498a-4af2-bdce-88b3d6dd21d0", |
| 103 | + "metadata": {}, |
| 104 | + "outputs": [ |
| 105 | + { |
| 106 | + "name": "stdout", |
| 107 | + "output_type": "stream", |
| 108 | + "text": [ |
| 109 | + "{\"fact_single\": \"What must I do to check if a government ID is real?\", \"fact_single_answer\": \"You need to view the ID in person and examine its characteristics and security features.\", \"summary\": \"How can I ensure an ID isn't fake or expired?\", \"summary_answer\": \"Check for original physical document traits, unaltered state, and that it's not past its expiration date.\", \"reasoning\": \"If I see these traits and features, can I be sure the ID is legitimate and still in use?\" , \"reasoning_answer\": \"Yes, by verifying the document's authenticity through its characteristics and security features in the person's presence, you can confirm it's a valid, current, and genuine government-issued ID.\"}\n" |
| 110 | + ] |
| 111 | + } |
| 112 | + ], |
| 113 | + "source": [ |
| 114 | + "domain = \"banking\"\n", |
| 115 | + "audience = \"junior internal employees\"\n", |
| 116 | + "use_case = \"chatbot\"\n", |
| 117 | + "\n", |
| 118 | + "customization_statement = f\"\"\"\n", |
| 119 | + "Stylize the generated questions and answers in the format that {audience} for a {domain} organization might write when using a {use_case}.\n", |
| 120 | + "Write at the fifth grade level. Prefer casual style and the first or second person.\n", |
| 121 | + "\"\"\"\n", |
| 122 | + "\n", |
| 123 | + "# customization_statement = f\"\"\"\n", |
| 124 | + "# Stylize the generated questions and answers in the format that {audience} for a {domain} organization might write when using a {use_case}.\n", |
| 125 | + "# Adjust the writing style and level of sophistication accordingly. Prefer casual style and the first or second person.\n", |
| 126 | + "# \"\"\"\n", |
| 127 | + "\n", |
| 128 | + "prompt_template = (\n", |
| 129 | + " \"I will provide you a text passage. I need you to generate three questions that \"\n", |
| 130 | + " \"must be answered only with information contained in this passage, and nothing \"\n", |
| 131 | + " \"else.\\n\"\n", |
| 132 | + " 'The first question is of type \"fact_single\", which means that the answer to this '\n", |
| 133 | + " \"question is a simple, single piece of factual information contained in the \"\n", |
| 134 | + " \"context.\\n\"\n", |
| 135 | + " 'The second question is of type \"summary\", which means that the answer to this '\n", |
| 136 | + " \"question summarizes different pieces of factual information contained in the \"\n", |
| 137 | + " \"context.\\n\"\n", |
| 138 | + " 'The third question is of type \"reasoning\", which is a question that requires the '\n", |
| 139 | + " \"reader to think critically and make an inference or draw a conclusion based on \"\n", |
| 140 | + " \"the information provided in the passage.\\n\"\n", |
| 141 | + " \"Make sure that the three questions are different.\\n\"\n", |
| 142 | + " \"\\n\"\n", |
| 143 | + " \"You will format your generation as a python dictionary, such as:\\n\"\n", |
| 144 | + " '{\"fact_single\": <The \"fact_single\" type question you thought of>, '\n", |
| 145 | + " '\"fact_single_answer: <Answer to the \"fact_single\" question>, \"summary\": <the '\n", |
| 146 | + " '\"summary\" type question you thought of>, \"summary_answer\": <Answer to the '\n", |
| 147 | + " '\"summary\" question>, \"reasoning\": <the \"reasoning\" type question you thought '\n", |
| 148 | + " 'of>, \"reasoning_answer\": <Answer to the \"reasoning\" question>}\\n'\n", |
| 149 | + " \"\\n\"\n", |
| 150 | + " \"Only provide the python dictionary as your output. Make sure you provide an answer for each question.\\n\"\n", |
| 151 | + " \"\\n\"\n", |
| 152 | + " \"{customization_statement}\\n\"\n", |
| 153 | + " \"\\n\"\n", |
| 154 | + " \"Context: {context_str}\"\n", |
| 155 | + ")\n", |
| 156 | + "\n", |
| 157 | + "prompt = PromptTemplate(prompt_template).format(context_str=context_str, customization_statement=customization_statement)\n", |
| 158 | + "\n", |
| 159 | + "#print(prompt)\n", |
| 160 | + "response = llm.complete(prompt)\n", |
| 161 | + "print(str(response))" |
| 162 | + ] |
| 163 | + } |
| 164 | + ], |
| 165 | + "metadata": { |
| 166 | + "kernelspec": { |
| 167 | + "display_name": "Python 3 (ipykernel)", |
| 168 | + "language": "python", |
| 169 | + "name": "python3" |
| 170 | + }, |
| 171 | + "language_info": { |
| 172 | + "codemirror_mode": { |
| 173 | + "name": "ipython", |
| 174 | + "version": 3 |
| 175 | + }, |
| 176 | + "file_extension": ".py", |
| 177 | + "mimetype": "text/x-python", |
| 178 | + "name": "python", |
| 179 | + "nbconvert_exporter": "python", |
| 180 | + "pygments_lexer": "ipython3", |
| 181 | + "version": "3.11.9" |
| 182 | + } |
| 183 | + }, |
| 184 | + "nbformat": 4, |
| 185 | + "nbformat_minor": 5 |
| 186 | +} |
0 commit comments