|
20 | 20 | "AZURE_OPENAI_SERVICE = os.environ.get(\"AZURE_OPENAI_SERVICE\") or \"myopenai\"\n",
|
21 | 21 | "AZURE_OPENAI_GPT_DEPLOYMENT = os.environ.get(\"AZURE_OPENAI_GPT_DEPLOYMENT\") or \"davinci\"\n",
|
22 | 22 | "AZURE_OPENAI_CHATGPT_DEPLOYMENT = os.environ.get(\"AZURE_OPENAI_CHATGPT_DEPLOYMENT\") or \"chat\"\n",
|
| 23 | + "AZURE_OPENAI_CHATGPT_MODEL = os.environ.get(\"AZURE_OPENAI_CHATGPT_MODEL\") or \"gpt-35-turbo\"\n", |
23 | 24 | "\n",
|
24 | 25 | "KB_FIELDS_CONTENT = os.environ.get(\"KB_FIELDS_CONTENT\") or \"content\"\n",
|
25 | 26 | "KB_FIELDS_CATEGORY = os.environ.get(\"KB_FIELDS_CATEGORY\") or \"category\"\n",
|
|
33 | 34 | "# Used by the OpenAI SDK\n",
|
34 | 35 | "openai.api_type = \"azure\"\n",
|
35 | 36 | "openai.api_base = f\"https://{AZURE_OPENAI_SERVICE}.openai.azure.com\"\n",
|
36 |
| - "openai.api_version = \"2022-12-01\"\n", |
| 37 | + "openai.api_version = \"2023-05-15\"\n", |
37 | 38 | "\n",
|
38 | 39 | "# Comment these two lines out if using keys, set your API key in the OPENAI_API_KEY environment variable instead\n",
|
39 | 40 | "openai.api_type = \"azure_ad\"\n",
|
|
52 | 53 | "metadata": {},
|
53 | 54 | "outputs": [],
|
54 | 55 | "source": [
|
55 |
| - "# ChatGPT uses a particular set of tokens to indicate turns in conversations\n", |
56 |
| - "prompt_prefix = \"\"\"<|im_start|>system\n", |
57 |
| - "Assistant helps the company employees with their healthcare plan questions and employee handbook questions. \n", |
58 |
| - "Answer ONLY with the facts listed in the list of sources below. If there isn't enough information below, say you don't know. Do not generate answers that don't use the sources below. If asking a clarifying question to the user would help, ask the question. \n", |
59 |
| - "Each source has a name followed by colon and the actual information, always include the source name for each fact you use in the response. Use square brakets to reference the source, e.g. [info1.txt]. Don't combine sources, list each source separately, e.g. [info1.txt][info2.pdf].\n", |
60 |
| - "\n", |
61 |
| - "Sources:\n", |
62 |
| - "{sources}\n", |
63 |
| - "\n", |
64 |
| - "<|im_end|>\"\"\"\n", |
65 |
| - "\n", |
66 |
| - "turn_prefix = \"\"\"\n", |
67 |
| - "<|im_start|>user\n", |
68 |
| - "\"\"\"\n", |
69 |
| - "\n", |
70 |
| - "turn_suffix = \"\"\"\n", |
71 |
| - "<|im_end|>\n", |
72 |
| - "<|im_start|>assistant\n", |
| 56 | + "# Chat roles\n", |
| 57 | + "SYSTEM = \"system\"\n", |
| 58 | + "USER = \"user\"\n", |
| 59 | + "ASSISTANT = \"assistant\"\n", |
| 60 | + "\n", |
| 61 | + "system_message_chat_conversation = \"\"\"Assistant helps the company employees with their healthcare plan questions, and questions about the employee handbook. Be brief in your answers.\n", |
| 62 | + "Answer ONLY with the facts listed in the list of sources below. If there isn't enough information below, say you don't know. Do not generate answers that don't use the sources below. If asking a clarifying question to the user would help, ask the question.\n", |
| 63 | + "Each source has a name followed by colon and the actual information, always include the source name for each fact you use in the response. Use square brackets to reference the source, e.g. [info1.txt]. Don't combine sources, list each source separately, e.g. [info1.txt][info2.pdf].\n", |
73 | 64 | "\"\"\"\n",
|
74 |
| - "\n", |
75 |
| - "prompt_history = turn_prefix\n", |
76 |
| - "\n", |
77 |
| - "history = []\n", |
| 65 | + "chat_conversations = [{\"role\" : SYSTEM, \"content\" : system_message_chat_conversation}]\n", |
78 | 66 | "\n",
|
79 | 67 | "summary_prompt_template = \"\"\"Below is a summary of the conversation so far, and a new question asked by the user that needs to be answered by searching in a knowledge base. Generate a search query based on the conversation and the new question. Source names are not good search terms to include in the search query.\n",
|
80 | 68 | "\n",
|
|
85 | 73 | "{question}\n",
|
86 | 74 | "\n",
|
87 | 75 | "Search query:\n",
|
88 |
| - "\"\"\"" |
| 76 | + "\"\"\"\n" |
89 | 77 | ]
|
90 | 78 | },
|
91 | 79 | {
|
|
100 | 88 | "# Exclude category, to simulate scenarios where there's a set of docs you can't see\n",
|
101 | 89 | "exclude_category = None\n",
|
102 | 90 | "\n",
|
103 |
| - "if len(history) > 0:\n", |
104 |
| - " completion = openai.Completion.create(\n", |
| 91 | + "if len(chat_conversations) > 1:\n", |
| 92 | + " query_completion = openai.Completion.create(\n", |
105 | 93 | " engine=AZURE_OPENAI_GPT_DEPLOYMENT,\n",
|
106 |
| - " prompt=summary_prompt_template.format(summary=\"\\n\".join(history), question=user_input),\n", |
| 94 | + " prompt=summary_prompt_template.format(summary=str(chat_conversations), question=user_input),\n", |
107 | 95 | " temperature=0.7,\n",
|
108 | 96 | " max_tokens=32,\n",
|
109 | 97 | " stop=[\"\\n\"])\n",
|
110 |
| - " search = completion.choices[0].text\n", |
| 98 | + " search = query_completion.choices[0].text\n", |
111 | 99 | "else:\n",
|
112 | 100 | " search = user_input\n",
|
113 | 101 | "\n",
|
|
125 | 113 | "results = [doc[KB_FIELDS_SOURCEPAGE] + \": \" + doc[KB_FIELDS_CONTENT].replace(\"\\n\", \"\").replace(\"\\r\", \"\") for doc in r]\n",
|
126 | 114 | "content = \"\\n\".join(results)\n",
|
127 | 115 | "\n",
|
128 |
| - "prompt = prompt_prefix.format(sources=content) + prompt_history + user_input + turn_suffix\n", |
| 116 | + "user_content = user_input + \" \\nSOURCES:\\n\" + content\n", |
129 | 117 | "\n",
|
130 |
| - "completion = openai.Completion.create(\n", |
131 |
| - " engine=AZURE_OPENAI_CHATGPT_DEPLOYMENT, \n", |
132 |
| - " prompt=prompt, \n", |
133 |
| - " temperature=0.7, \n", |
134 |
| - " max_tokens=1024,\n", |
135 |
| - " stop=[\"<|im_end|>\", \"<|im_start|>\"])\n", |
| 118 | + "chat_conversations.append({\"role\": USER, \"content\": user_content })\n", |
136 | 119 | "\n",
|
137 |
| - "prompt_history += user_input + turn_suffix + completion.choices[0].text + \"\\n<|im_end|>\" + turn_prefix\n", |
138 |
| - "history.append(\"user: \" + user_input)\n", |
139 |
| - "history.append(\"assistant: \" + completion.choices[0].text)\n", |
140 |
| - "\n", |
141 |
| - "print(\"\\n-------------------\\n\".join(history))\n", |
142 |
| - "print(\"\\n-------------------\\nPrompt:\\n\" + prompt)" |
| 120 | + "chat_completion = openai.ChatCompletion.create(\n", |
| 121 | + " deployment_id=AZURE_OPENAI_CHATGPT_DEPLOYMENT,\n", |
| 122 | + " model=AZURE_OPENAI_CHATGPT_MODEL,\n", |
| 123 | + " messages=chat_conversations, \n", |
| 124 | + " temperature=0.7, \n", |
| 125 | + " max_tokens=1024, \n", |
| 126 | + " n=1)\n", |
| 127 | + "chat_content = chat_completion.choices[0].message.content\n", |
| 128 | + "'''\n", |
| 129 | + "reset user content to avoid sources in conversation history\n", |
| 130 | + "add source as a single shot in query conversation\n", |
| 131 | + "'''\n", |
| 132 | + "chat_conversations[-1][\"content\"] = user_input\n", |
| 133 | + "chat_conversations.append({\"role\":ASSISTANT, \"content\": chat_content})\n", |
| 134 | + "\n", |
| 135 | + "print(\"\\n-------------------\\n\")\n", |
| 136 | + "[print(conversation) for conversation in chat_conversations]" |
143 | 137 | ]
|
144 | 138 | }
|
145 | 139 | ],
|
|
159 | 153 | "name": "python",
|
160 | 154 | "nbconvert_exporter": "python",
|
161 | 155 | "pygments_lexer": "ipython3",
|
162 |
| - "version": "3.10.10" |
| 156 | + "version": "3.11.4" |
163 | 157 | },
|
164 | 158 | "orig_nbformat": 4,
|
165 | 159 | "vscode": {
|
|
0 commit comments