{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Load the API key and libaries." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from src.LLM_Evaluation import GPT\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Load the Constants" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "PATH = 'data/Portuguese_test.csv'\n", "MODEL = \"gpt-3.5-turbo\"\n", "TEMPERATURE = 0.3\n", "N_REPETITIONS = 3\n", "REASONING = True\n", "LANGUAGES = ['english', 'portuguese']\n", "MAX_TOKENS = 500" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an Instance of the model" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "model = GPT(model=MODEL, temperature=TEMPERATURE, n_repetitions=N_REPETITIONS, reasoning=REASONING, languages=LANGUAGES, path=PATH, max_tokens=MAX_TOKENS)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### See characteristics of the model" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "gpt-3.5-turbo\n" ] } ], "source": [ "print(model.model)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " You will be provided with medical queries in this languages: english, portuguese. The medical query will be delimited with #### characters.\n", " Each question will have 4 possible answer options. provide the letter with the answer and a short sentence answering why the answer was selected. \n", "\n", " Provide your output in json format with the keys: response, reasoning.\n", "\n", " Responses: A, B, C, D.\n", " \n" ] } ], "source": [ "print(model.system_message)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Test the model" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "question = \"\"\"What is the primary function of the cornea in the human eye?\n", "A) Refracting light onto the retina\n", "B) Producing aqueous humor\n", "C) Regulating pupil size\n", "D) Transmitting visual signals to the brain\"\"\"" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Response:\n", "{'response': 'A', 'reasoning': 'The primary function of the cornea in the human eye is to refract light onto the retina. This is because the cornea is the clear, outermost layer of the eye that helps to focus light onto the retina, which then sends visual signals to the brain.'}\n", "Answer: A\n", "Reasoning: The primary function of the cornea in the human eye is to refract light onto the retina. This is because the cornea is the clear, outermost layer of the eye that helps to focus light onto the retina, which then sends visual signals to the brain.\n" ] } ], "source": [ "response = model.get_completion_from_messages(question)\n", "\n", "print('Response:')\n", "print(response)\n", "\n", "print(f'Answer: {response[\"response\"]}')\n", "print(f'Reasoning: {response[\"reasoning\"]}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Modify the model" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " You will be provided with medical queries in this languages: english, portuguese. The medical query will be delimited with #### characters.\n", " Each question will have 4 possible answer options. provide the letter with the answer and a short sentence answering why the answer was selected. Also print the area of the medicine to which the question refers to.\n", "\n", " Provide your output in json format with the keys: response, reasoning, area.\n", "\n", " Responses: A, B, C, D.\n", " \n" ] } ], "source": [ "model.add_extra_message('Also print the area of the medicine to which the question refers to.')\n", "model.add_output_key('area')\n", "print(model.system_message)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Response:\n", "{'response': 'A', 'reasoning': 'The primary function of the cornea in the human eye is to refract light onto the retina. This helps to focus the incoming light and contribute to clear vision.', 'area': 'Ophthalmology'}\n", "Area: Ophthalmology\n", "Answer: A\n", "Reasoning: The primary function of the cornea in the human eye is to refract light onto the retina. This helps to focus the incoming light and contribute to clear vision.\n" ] } ], "source": [ "response = model.get_completion_from_messages(question)\n", "\n", "print('Response:')\n", "print(response)\n", "\n", "print(f'Area: {response[\"area\"]}')\n", "print(f'Answer: {response[\"response\"]}')\n", "print(f'Reasoning: {response[\"reasoning\"]}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run multiple experiments using the csv file" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "**************************************************\n", "Question 1: \n", "Language: english\n", "Question: \n", "In which ocular region are caliciform cells physiologically found?\n", "a) Cornea.\n", "b) Corneoscleral limbus.\n", "c) Gray line.\n", "d) Semilunar fold.\n", "Test #0: \n", "{'response': 'b', 'reasoning': 'The correct answer is b) Corneoscleral limbus. Caliciform cells are found in the corneoscleral limbus, which is the transitional zone between the cornea and the sclera.', 'area': 'Ophthalmology'}\n", "Test #1: \n", "{'response': 'b', 'reasoning': 'The correct answer is b) Corneoscleral limbus. Caliciform cells are found in the corneoscleral limbus, which is the transition zone between the cornea and the sclera.', 'area': 'Ophthalmology'}\n", "Test #2: \n", "{'response': 'b', 'reasoning': 'The correct answer is b) Corneoscleral limbus. Caliciform cells are found in the corneoscleral limbus, which is the transitional zone between the cornea and the sclera.', 'area': 'Ophthalmology'}\n", "Language: portuguese\n", "Question: \n", "Em qual região ocular células caliciformes são fisiologicamente encontradas?\n", "a)Córnea.\n", "b)Limbo corneoescleral.\n", "c)Linha cinzenta.\n", "d)Prega semilunar.\n", "Test #0: \n", "{'response': 'c', 'reasoning': 'As células caliciformes são encontradas na linha cinzenta.', 'area': 'Oftalmologia'}\n", "Test #1: \n", "{'response': 'c', 'reasoning': 'As células caliciformes são fisiologicamente encontradas na linha cinzenta.', 'area': 'Oftalmologia'}\n", "Test #2: \n", "{'response': 'c', 'reasoning': 'As células caliciformes são encontradas na linha cinzenta.', 'area': 'Oftalmologia'}\n", "**************************************************\n", "**************************************************\n", "Question 2: \n", "Language: english\n", "Question: \n", "Mark the alternative that best correlates the histological characteristics with the respective ocular tissues:\n", "\n", "I. Monolayer of cells tightly joined together by junctional complexes.\n", "II. Parallel and regular striations observed under optical microscopy, perpendicular to the epithelium.\n", "III. It contains bipolar cells, amacrine cells, horizontal cells and Muller cells.\n", "IV. It contains magnocellular, parvocellular and coniocellular cells.\n", "\n", "A. Photoreceptors.\n", "B. Retinal pigmented epithelium.\n", "C. Retinal ganglionic layer.\n", "D. Inner nuclear layer.\n", "Test #0: \n", "{'response': 'C', 'reasoning': 'The histological characteristics described in options III match the retinal ganglionic layer, which contains bipolar cells, amacrine cells, horizontal cells, and Muller cells.', 'area': 'Ophthalmology'}\n", "Test #1: \n", "{'response': 'C', 'reasoning': 'The histological characteristics described in options III and IV are consistent with the retinal ganglionic layer, which contains bipolar cells, amacrine cells, horizontal cells, Muller cells, magnocellular cells, parvocellular cells, and coniocellular cells.', 'area': 'Ophthalmology'}\n", "Test #2: \n", "{'response': 'C', 'reasoning': 'The histological characteristics described in option III, including the presence of bipolar cells, amacrine cells, horizontal cells, and Muller cells, are consistent with the retinal ganglionic layer.', 'area': 'Ophthalmology'}\n", "Language: portuguese\n", "Question: \n", "Assinale a alternativa que melhor correlaciona as características histológicas com os respectivos tecidos oculares:\n", "\n", "I. Monocamada de células fortemente unidas por complexos juncionais.\n", "II. Estriações paralelas e regulares observadas à microscopia óptica, perpendiculares ao epitélio.\n", "III. Contém células bipolares, células amácrinas, células horizontais e células de Muller.\n", "IV. Contém células magnocelulares, parvocelulares e coniocelulares.\n", "\n", "A. Fotorreceptores.\n", "B. Epitélio pigmentado da pigmentado da retina.\n", "C. Camada ganglionar retiniana.\n", "D. Camada nuclear interna.\n", "Test #0: \n", "{'response': 'C', 'reasoning': 'A camada ganglionar retiniana é caracterizada por conter células bipolares, células amácrinas, células horizontais e células de Muller.', 'area': 'Oftalmologia'}\n", "Test #1: \n", "{'response': 'B', 'reasoning': 'A alternativa B é a correta, pois descreve as características histológicas do epitélio pigmentado da retina, que inclui células fortemente unidas por complexos juncionais e estriações paralelas e regulares observadas à microscopia óptica, perpendiculares ao epitélio.', 'area': 'Oftalmologia'}\n", "Test #2: \n", "{'response': 'C', 'reasoning': 'A descrição das características histológicas corresponde à camada ganglionar retiniana, que contém células bipolares, células amácrinas, células horizontais e células de Muller.', 'area': 'Oftalmologia'}\n", "**************************************************\n", "**************************************************\n", "Question 3: \n", "Language: english\n", "Question: \n", "Order the three cell names found in the corneal epithelium, starting with the most superficial, followed by the intermediate and the deep.\n", "a) Flat, wing, basal.\n", "b) wing, basal, flat.\n", "c) Basal, flat, wing.\n", "d) wing, flat, basal.\n", "Test #0: \n", "{'response': 'c', 'reasoning': 'The correct order of the three cell names found in the corneal epithelium, starting with the most superficial, followed by the intermediate and the deep, is basal, flat, wing.', 'area': 'Ophthalmology'}\n", "Test #1: \n", "{'response': 'c', 'reasoning': 'The correct order of the three cell names found in the corneal epithelium, starting with the most superficial, followed by the intermediate and the deep, is basal, flat, wing.', 'area': 'Ophthalmology'}\n", "Test #2: \n", "{'response': 'c', 'reasoning': 'The correct order of the three cell names found in the corneal epithelium, starting with the most superficial, followed by the intermediate and the deep, is basal, flat, wing.', 'area': 'Ophthalmology'}\n", "Language: portuguese\n", "Question: \n", "Ordene as três denominações celulares encontradas no epitélio da córnea, iniciando pelo mais superficial, seguido do intermediário e do profundo.\n", "a)Plana, alada, basal.\n", "b)Alada, basal, plana.\n", "c)Basal, plana, alada.\n", "d)Alada, plana, basal.\n", "Test #0: \n", "{'response': 'c', 'reasoning': 'A ordem correta das denominações celulares encontradas no epitélio da córnea é: basal, plana, alada. Portanto, a opção c é a correta.', 'area': 'Oftalmologia'}\n", "Test #1: \n", "{'response': 'c', 'reasoning': 'A ordem correta das três denominações celulares encontradas no epitélio da córnea é: basal, plana, alada.', 'area': 'Oftalmologia'}\n", "Test #2: \n", "{'response': 'c', 'reasoning': 'A resposta correta é a letra c) Basal, plana, alada. O epitélio da córnea é composto por três camadas celulares: a camada basal, a camada plana e a camada alada. A camada basal é a mais profunda, seguida pela camada plana e, por fim, a camada alada.', 'area': 'Oftalmologia'}\n", "**************************************************\n" ] }, { "data": { "text/html": [ "
\n", " | ID | \n", "year | \n", "test | \n", "theme | \n", "subtheme | \n", "portuguese | \n", "english | \n", "answer | \n", "response_english_0 | \n", "reasoning_english_0 | \n", "... | \n", "area_english_2 | \n", "response_portuguese_0 | \n", "reasoning_portuguese_0 | \n", "area_portuguese_0 | \n", "response_portuguese_1 | \n", "reasoning_portuguese_1 | \n", "area_portuguese_1 | \n", "response_portuguese_2 | \n", "reasoning_portuguese_2 | \n", "area_portuguese_2 | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "1 | \n", "2022 | \n", "Teórica I | \n", "Anatomia | \n", "cornea | \n", "Em qual região ocular células caliciformes são... | \n", "In which ocular region are caliciform cells ph... | \n", "D | \n", "b | \n", "The correct answer is b) Corneoscleral limbus.... | \n", "... | \n", "Ophthalmology | \n", "c | \n", "As células caliciformes são encontradas na lin... | \n", "Oftalmologia | \n", "c | \n", "As células caliciformes são fisiologicamente e... | \n", "Oftalmologia | \n", "c | \n", "As células caliciformes são encontradas na lin... | \n", "Oftalmologia | \n", "
1 | \n", "2 | \n", "2022 | \n", "Teórica I | \n", "Anatomia | \n", "retina | \n", "Assinale a alternativa que melhor correlaciona... | \n", "Mark the alternative that best correlates the ... | \n", "B | \n", "C | \n", "The histological characteristics described in ... | \n", "... | \n", "Ophthalmology | \n", "C | \n", "A camada ganglionar retiniana é caracterizada ... | \n", "Oftalmologia | \n", "B | \n", "A alternativa B é a correta, pois descreve as ... | \n", "Oftalmologia | \n", "C | \n", "A descrição das características histológicas c... | \n", "Oftalmologia | \n", "
2 | \n", "3 | \n", "2022 | \n", "Teórica I | \n", "Anatomia | \n", "cornea | \n", "Ordene as três denominações celulares encontra... | \n", "Order the three cell names found in the cornea... | \n", "A | \n", "c | \n", "The correct order of the three cell names foun... | \n", "... | \n", "Ophthalmology | \n", "c | \n", "A ordem correta das denominações celulares enc... | \n", "Oftalmologia | \n", "c | \n", "A ordem correta das três denominações celulare... | \n", "Oftalmologia | \n", "c | \n", "A resposta correta é a letra c) Basal, plana, ... | \n", "Oftalmologia | \n", "
3 rows × 26 columns
\n", "\n", " | ID | \n", "year | \n", "test | \n", "theme | \n", "subtheme | \n", "portuguese | \n", "english | \n", "answer | \n", "response_english_0 | \n", "area_english_0 | \n", "response_english_1 | \n", "area_english_1 | \n", "response_english_2 | \n", "area_english_2 | \n", "response_portuguese_0 | \n", "area_portuguese_0 | \n", "response_portuguese_1 | \n", "area_portuguese_1 | \n", "response_portuguese_2 | \n", "area_portuguese_2 | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "1 | \n", "2022 | \n", "Teórica I | \n", "Anatomia | \n", "cornea | \n", "Em qual região ocular células caliciformes são... | \n", "In which ocular region are caliciform cells ph... | \n", "D | \n", "b | \n", "Ophthalmology | \n", "b | \n", "Ophthalmology | \n", "b | \n", "Ophthalmology | \n", "c | \n", "Oftalmologia | \n", "c | \n", "Oftalmologia | \n", "c | \n", "Oftalmologia | \n", "
1 | \n", "2 | \n", "2022 | \n", "Teórica I | \n", "Anatomia | \n", "retina | \n", "Assinale a alternativa que melhor correlaciona... | \n", "Mark the alternative that best correlates the ... | \n", "B | \n", "C | \n", "Ophthalmology | \n", "C | \n", "Ophthalmology | \n", "C | \n", "Retina | \n", "C | \n", "Oftalmologia | \n", "B | \n", "Oftalmologia | \n", "C | \n", "Oftalmologia | \n", "
2 | \n", "3 | \n", "2022 | \n", "Teórica I | \n", "Anatomia | \n", "cornea | \n", "Ordene as três denominações celulares encontra... | \n", "Order the three cell names found in the cornea... | \n", "A | \n", "c | \n", "Ophthalmology | \n", "c | \n", "Ophthalmology | \n", "c | \n", "Ophthalmology | \n", "c | \n", "Oftalmologia | \n", "c | \n", "Oftalmologia | \n", "c | \n", "Oftalmologia | \n", "