{ "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 LLAMA\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 = \"Llama-2-13b\"\n", "TEMPERATURE = 0.3\n", "N_REPETITIONS = 3\n", "REASONING = True\n", "LANGUAGES = ['english', 'portuguese']\n", "MAX_TOKENS = 512 # If reasoning is True set a larger value (e.G. 1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an Instance of the model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model = LLAMA(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": [ "Llama-2-13b\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. Make sure to always use the those keys, do not modify the keys.\n", " Be very careful with the resulting JSON file, make sure to add curly braces, quotes to define the strings, and commas to separate the items within the JSON.\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 is to refract light onto the retina, allowing us to see clearly.'}\n", "Answer: A\n", "Reasoning: The primary function of the cornea is to refract light onto the retina, allowing us to see clearly.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n", "llama_print_timings: load time = 4030.37 ms\n", "llama_print_timings: sample time = 104.25 ms / 179 runs ( 0.58 ms per token, 1717.08 tokens per second)\n", "llama_print_timings: prompt eval time = 4030.28 ms / 235 tokens ( 17.15 ms per token, 58.31 tokens per second)\n", "llama_print_timings: eval time = 21840.72 ms / 178 runs ( 122.70 ms per token, 8.15 tokens per second)\n", "llama_print_timings: total time = 26418.34 ms\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": 8, "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. Make sure to always use the those keys, do not modify the keys.\n", " Be very careful with the resulting JSON file, make sure to add curly braces, quotes to define the strings, and commas to separate the items within the JSON.\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": null, "metadata": {}, "outputs": [], "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": null, "metadata": {}, "outputs": [], "source": [ "df = model.llm_language_evaluation(save=False)\n", "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "### Suggestion:\n", "# When running multiple experiments, it's recommended to change the value of REASONING to False, since running the reasoning multiple times can be time consuming.\n", "model.change_reasoning(False)\n", "\n", "df = model.llm_language_evaluation(save=False)\n", "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:nlp_bias_vpython=3_8_15]", "language": "python", "name": "conda-env-nlp_bias_vpython_3_8_15-py" }, "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.8.15" } }, "nbformat": 4, "nbformat_minor": 4 }