Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated openai example notebook to use the new generative text task type #2526

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
2899dea
Added info about required packages
kartikc727 Oct 15, 2023
80b0c34
Update responsibleaidashboard-question-answering-model-debugging.ipynb
kartikc727 Oct 15, 2023
af00993
show example prediction
kartikc727 Oct 15, 2023
6c19f0f
Update responsibleaidashboard-question-answering-model-debugging.ipynb
kartikc727 Oct 15, 2023
6278234
Merge branch 'main' into main
kartikc727 Oct 17, 2023
88fe881
Merge branch 'microsoft:main' into main
kartikc727 Dec 21, 2023
b9fae62
Merge branch 'microsoft:main' into main
kartikc727 Jan 10, 2024
85b6a70
Merge branch 'main' of https://github.com/kartik727/responsible-ai-to…
kartikc727 Jan 12, 2024
9f17287
Merge branch 'main' of https://github.com/kartik727/responsible-ai-to…
kartikc727 Jan 26, 2024
c678d79
Merge branch 'main' of https://github.com/microsoft/responsible-ai-to…
kartikc727 Jan 29, 2024
fe5ac4f
Merge branch 'main' of https://github.com/microsoft/responsible-ai-to…
kartikc727 Jan 30, 2024
d21d496
Merge branch 'main' of https://github.com/microsoft/responsible-ai-to…
kartikc727 Feb 1, 2024
db11388
Merge branch 'main' of https://github.com/microsoft/responsible-ai-to…
kartikc727 Feb 2, 2024
6f59a06
Updated openai example notebook to use the new generative text task type
kartikc727 Feb 2, 2024
8e72081
Merge branch 'main' into kartikch/genai-notebook-example
kartikc727 Feb 2, 2024
59f4ac4
Merge branch 'main' into kartikch/genai-notebook-example
kartikc727 Feb 8, 2024
0ec2fba
Merge branch 'main' into kartikch/genai-notebook-example
kartikc727 Feb 16, 2024
376babe
Merge branch 'main' into kartikch/genai-notebook-example
kartikc727 Mar 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,6 @@
"dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "46cd83d3",
"metadata": {},
"outputs": [],
"source": [
"def replace_error_chars(message):\n",
" message = message.replace('`', '')\n",
" return message"
]
},
{
"cell_type": "markdown",
"id": "f1bffd45",
Expand All @@ -128,13 +116,14 @@
"metadata": {},
"outputs": [],
"source": [
"questions = []\n",
"context = []\n",
"answers = []\n",
"template = ('Answer the question given the context.\\n\\n'\n",
" 'context:\\n{context}\\n\\n'\n",
" 'question:\\n{question}')\n",
"prompts = []\n",
"for row in dataset:\n",
" context.append(row['context'])\n",
" questions.append(row['question'])\n",
" answers.append(replace_error_chars(row['answers']['text'][0]))"
" templated_prompt = template.format(context=row['context'],\n",
" question=row['question'])\n",
" prompts.append(templated_prompt)"
]
},
{
Expand All @@ -144,17 +133,7 @@
"metadata": {},
"outputs": [],
"source": [
"data = pd.DataFrame({'context': context, 'questions': questions, 'answers': answers})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6f87e9c",
"metadata": {},
"outputs": [],
"source": [
"data.iloc[:5]['answers'][0]"
"data = pd.DataFrame({'prompt': prompts})"
]
},
{
Expand Down Expand Up @@ -189,7 +168,13 @@
"api_key = \"put_your_secret_key_here\"\n",
"\n",
"# Openai Wrapper that calls endpoint with given questions\n",
"openai_model = OpenaiWrapperModel(api_type, api_base, api_version, api_key, engine='gpt-4')"
"openai_model = OpenaiWrapperModel(api_type, api_base, api_version,\n",
" api_key, engine='gpt-35-turbo-16k')\n",
"\n",
"# Openai Wrapper for the evaluation model that will judge the quality of the\n",
"# answers\n",
"openai_eval_model = OpenaiWrapperModel(api_type, api_base, api_version,\n",
" api_key, engine='gpt-4')"
]
},
{
Expand Down Expand Up @@ -217,21 +202,35 @@
" self.model = model\n",
"\n",
" def predict(self, dataset):\n",
" template = 'Answer the question given the context.'\n",
" for i, (context, question) in enumerate(zip(dataset['context'], dataset['questions'])):\n",
" templated_question = template + '\\n\\ncontext: ' + context + '\\nquestion: ' + question\n",
" if isinstance(dataset, pd.DataFrame):\n",
" dataset.iloc[i]['questions'] = templated_question\n",
" else:\n",
" dataset['questions'] = templated_question\n",
" # NOTE: Remove this patch when calling your openai model\n",
" with patch('ml_wrappers.model.OpenaiWrapperModel.predict') as mock_predict:\n",
" # dummy return value\n",
" dummy_prediction = 'This is a dummy prediction'\n",
"\n",
" # wrap return value in mock class\n",
" if isinstance(dataset, pd.DataFrame):\n",
" mock_predict.return_value = np.array(data['answers'][dataset.index])\n",
" prediction = [dummy_prediction] * len(dataset)\n",
" else:\n",
" mock_predict.return_value = np.array(data['answers'][0])\n",
" context = {}\n",
" prediction = [dummy_prediction]\n",
" mock_predict.return_value = np.array(prediction)\n",
" \n",
" # Note: When calling a real openai model just return this line here\n",
" return self.model.predict(dataset)\n",
" \n",
"class template_eval(object):\n",
" def __init__(self, model):\n",
" self.model = model\n",
"\n",
" def predict(self, dataset):\n",
" # NOTE: Remove this patch when calling your openai model\n",
" with patch('ml_wrappers.model.OpenaiWrapperModel.predict') as mock_predict:\n",
" # dummy return value\n",
" dummy_prediction = 4\n",
"\n",
" # wrap return value in mock class\n",
" prediction = [dummy_prediction] * len(dataset)\n",
" mock_predict.return_value = np.array(prediction)\n",
"\n",
" # Note: When calling a real openai model just return this line here\n",
" return self.model.predict(dataset)"
]
Expand All @@ -258,6 +257,18 @@
"pipeline.predict(test_data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "010112e5",
"metadata": {},
"outputs": [],
"source": [
"eval_pipeline = template_eval(openai_eval_model)\n",
"eval_data = pipeline.predict(test_data)\n",
"eval_pipeline.predict(eval_data)"
]
},
{
"cell_type": "markdown",
"id": "9fa4f8f2",
Expand Down Expand Up @@ -294,9 +305,9 @@
"metadata": {},
"outputs": [],
"source": [
"rai_insights = RAITextInsights(pipeline, test_data,\n",
" \"answers\",\n",
" task_type=ModelTask.QUESTION_ANSWERING)"
"rai_insights = RAITextInsights(pipeline, test_data, None,\n",
" task_type=ModelTask.GENERATIVE_TEXT,\n",
" text_column='prompt', eval_model=eval_pipeline)"
]
},
{
Expand Down Expand Up @@ -372,7 +383,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.17"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down