{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "ae13d278-e1c5-496e-8871-3936ccb71b5d", "metadata": {}, "outputs": [], "source": [ "from mistralai import Mistral\n", "import pandas as pd\n", "import minsearch\n", "import os\n", "from groq import Groq\n", "from dotenv import load_dotenv\n", "from tqdm.auto import tqdm\n", "import pandas as pd\n", "import minsearch\n", "import os\n", "from groq import Groq\n", "from dotenv import load_dotenv\n", "from tqdm.auto import tqdm\n" ] }, { "cell_type": "markdown", "id": "048c2b93-82c0-4d5b-bcaa-47b8531506cf", "metadata": {}, "source": [ "## Environment variables Loading" ] }, { "cell_type": "code", "execution_count": 3, "id": "7c5f1265-31da-4ad6-bd8c-bd912b39e5b1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "load_dotenv()\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "4c4fa421-e6b2-48a3-9329-215c91c26ade", "metadata": {}, "outputs": [], "source": [ "# Access the API keys\n", "mistral_api_key = os.getenv(\"MISTRAL_API_KEY\")\n", "groq_api_key = os.getenv(\"GROQ_API_KEY\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "722521c0-1558-430a-852f-a5afdd082a49", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Variable Type Data/Info\n", "---------------------------------------\n", "Groq type \n", "Mistral type \n", "groq_api_key str gsk_32VwUDXWlbcss91AZEhIW<...>Yv5D56tMak8RwoyfSBXePPdht\n", "load_dotenv function \n", "minsearch module notebooks\\\\minsearch.py'>\n", "mistral_api_key str 8liS0F7zb7zNgujwgo3dgTbr6HbfvmeI\n", "os module n\\\\Python39\\\\lib\\\\os.py'>\n", "pd module es\\\\pandas\\\\__init__.py'>\n", "tqdm type \n" ] } ], "source": [ "%whos" ] }, { "cell_type": "code", "execution_count": 6, "id": "069f3cf6-4941-46f5-adeb-c56813c8db3f", "metadata": {}, "outputs": [], "source": [ "df=pd.read_csv(\"../dataset/Mental_Health_FAQ.csv\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "a8e4b926-85a9-4130-8107-d416abd56a97", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Question_IDQuestionsAnswers
01590140What does it mean to have a mental illness?Mental illnesses are health conditions that di...
12110618Who does mental illness affect?It is estimated that mental illness affects 1 ...
26361820What causes mental illness?It is estimated that mental illness affects 1 ...
39434130What are some of the warning signs of mental i...Symptoms of mental health disorders vary depen...
47657263Can people with mental illness recover?When healing from mental illness, early identi...
\n", "
" ], "text/plain": [ " Question_ID Questions \\\n", "0 1590140 What does it mean to have a mental illness? \n", "1 2110618 Who does mental illness affect? \n", "2 6361820 What causes mental illness? \n", "3 9434130 What are some of the warning signs of mental i... \n", "4 7657263 Can people with mental illness recover? \n", "\n", " Answers \n", "0 Mental illnesses are health conditions that di... \n", "1 It is estimated that mental illness affects 1 ... \n", "2 It is estimated that mental illness affects 1 ... \n", "3 Symptoms of mental health disorders vary depen... \n", "4 When healing from mental illness, early identi... " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "code", "execution_count": 8, "id": "5c69465d-0018-432f-b134-2fa929045e83", "metadata": {}, "outputs": [], "source": [ "documents=df.to_dict(orient='records')" ] }, { "cell_type": "code", "execution_count": 9, "id": "5297116d-cf4b-44ae-aaad-72079a906855", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Question_ID': 6361820,\n", " 'Questions': 'What causes mental illness?',\n", " 'Answers': 'It is estimated that mental illness affects 1 in 5 adults in America, and that 1 in 24 adults have a serious mental illness. Mental illness does not discriminate; it can affect anyone, regardless of gender, age, income, social status, ethnicity, religion, sexual orientation, or background. Although mental illness can affect anyone, certain conditions may be more common in different populations. For instance, eating disorders tend to occur more often in females, while disorders such as attention deficit/hyperactivity disorder is more prevalent in children. Additionally, all ages are susceptible, but the young and the old are especially vulnerable. Mental illnesses usually strike individuals in the prime of their lives, with 75 percent of mental health conditions developing by the age of 24. This makes identification and treatment of mental disorders particularly difficult, because the normal personality and behavioral changes of adolescence may mask symptoms of a mental health condition. Parents and caretakers should be aware of this fact, and take notice of changes in their child’s mood, personality, personal habits, and social withdrawal. When these occur in children under 18, they are referred to as serious emotional disturbances (SEDs).'}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "documents[2]" ] }, { "cell_type": "code", "execution_count": 10, "id": "604c32fc-bf9b-465d-b2d6-e55e8947ef4d", "metadata": {}, "outputs": [], "source": [ "prompt_template=\"\"\"You are emulating a patient concerned about mental health. For each answer, \n", "formulate 5 questions that this patient might ask. The questions should be complete, not too short, \n", "and use as few words as possible from the original answer.\n", "\n", "The record:\n", "Question_ID:{Question_ID}\n", "Questions:{Questions}\n", "Answer: {Answers}\n", "\n", "Provide the output in parsable JSON format without using code blocks. The output should be structured like this:\n", "\"\"\".strip()" ] }, { "cell_type": "code", "execution_count": 11, "id": "dacc3ea2-df3b-4fad-b9cd-1cc3a88a6e1c", "metadata": {}, "outputs": [], "source": [ "prompt=prompt_template.format(**documents[0])" ] }, { "cell_type": "code", "execution_count": 15, "id": "53b634ad-dc6b-4848-b6b4-b882e90076cb", "metadata": {}, "outputs": [], "source": [ "client = Mistral(api_key=mistral_api_key)" ] }, { "cell_type": "code", "execution_count": 23, "id": "079bc5f5-2977-43da-8eca-c340dcb884d4", "metadata": {}, "outputs": [], "source": [ "def llm(prompt,model=\"mistral-large-latest\"):\n", " response = client.chat.complete(\n", " model=model,\n", " messages=[{\"role\": \"user\", \"content\": prompt}])\n", " return response.choices[0].message.content\n", " " ] }, { "cell_type": "code", "execution_count": 24, "id": "d227d497-bc50-49d8-a141-8e8a182aecf4", "metadata": {}, "outputs": [], "source": [ "quiz=llm(prompt)" ] }, { "cell_type": "code", "execution_count": 25, "id": "e7a63537-5edd-475b-a032-e5bb7380e160", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "```json\n", "{\n", " \"Question1\": \"What are the main aspects of life affected by mental illnesses?\",\n", " \"Question2\": \"How do mental illnesses vary in terms of severity and impact?\",\n", " \"Question3\": \"Are mental illnesses related to a person's character or intelligence?\",\n", " \"Question4\": \"How is mental illness treated, and what is the success rate?\",\n", " \"Question5\": \"Can someone with a mental illness lead an independent and successful life?\"\n", "}\n", "```\n" ] } ], "source": [ "print(quiz)" ] }, { "cell_type": "code", "execution_count": 20, "id": "a5f31769-9b1d-4ca2-a565-24298207bdb4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"question1\": \"What is the definition of mental illness?\",\n", " \"question2\": \"How do mental illnesses affect a person's daily functioning?\",\n", " \"question3\": \"What are the different types of mental illnesses?\",\n", " \"question4\": \"What are the treatment options for mental illnesses?\",\n", " \"question5\": \"What is the impact of proper treatment on a person's life with mental illness?\"\n", "}\n" ] } ], "source": [ "print(quiz)" ] }, { "cell_type": "code", "execution_count": 22, "id": "a2286fdc-116b-44c5-af27-21fdab4e8099", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n" ] } ], "source": [ "for quiz in documents:\n", " print(len(quiz))" ] }, { "cell_type": "code", "execution_count": 21, "id": "71dedb46-3b27-4aa0-a972-0907fae7a5b1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "377" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(quiz)" ] }, { "cell_type": "code", "execution_count": null, "id": "5fbc9707-e590-4c86-bcfd-4374e07b659f", "metadata": {}, "outputs": [], "source": [ "df.info()" ] }, { "cell_type": "code", "execution_count": null, "id": "b611ea3d-ff42-49b0-82c3-c1c970cedcf2", "metadata": {}, "outputs": [], "source": [ "import os\n", "from mistralai import Mistral\n", "\n", "client = Mistral(api_key=mistral_api_key)\n", "\n", "model = \"codestral-latest\"\n", "message = [{\"role\": \"user\", \"content\": \"Write a function for fibonacci\"}]\n", "chat_response = client.chat.complete(\n", " model = model,\n", " messages = message\n", ")\n", "print(chat_response.choices[0].message.content)" ] }, { "cell_type": "code", "execution_count": null, "id": "5b254557-6c35-4306-aada-bae7a264ba89", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }