{
 "cells": [
  {
   "cell_type": "code",
   "source": [
    "import optimizer.optimizer_mistral as opt\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv('/Users/renaud/repos/codeflash/django/aiservice/.env')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-29T09:51:36.284629Z",
     "start_time": "2024-02-29T09:51:36.263674Z"
    }
   },
   "id": "1aca8c639f7c0f59",
   "execution_count": 9,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "dc01671371dcd28b"
  },
  {
   "cell_type": "code",
   "source": [
    "code0 = \"\"\"def sorter(arr):\n",
    "    for i in range(len(arr)):\n",
    "        for j in range(len(arr) - 1):\n",
    "            if arr[j] > arr[j + 1]:\n",
    "                temp = arr[j]\n",
    "                arr[j] = arr[j + 1]\n",
    "                arr[j + 1] = temp\n",
    "    return arr\n",
    "\"\"\"\n",
    "code1 = \"\"\"import numpy as np\n",
    "def _hamming_distance(a: np.ndarray, b: np.ndarray) -> np.floating:\n",
    "     return np.mean(a != b)\n",
    "\"\"\"\n",
    "code2 = \"\"\"def split_list_of_docs(\n",
    "    docs: List[Document], length_func: Callable, token_max: int, **kwargs: Any\n",
    ") -> List[List[Document]]:\n",
    "    new_result_doc_list = []\n",
    "    _sub_result_docs = []\n",
    "    for doc in docs:\n",
    "        _sub_result_docs.append(doc)\n",
    "        _num_tokens = length_func(_sub_result_docs, **kwargs)\n",
    "        if _num_tokens > token_max:\n",
    "            if len(_sub_result_docs) == 1:\n",
    "                raise ValueError(\n",
    "                    \"A single document was longer than the context length,\"\n",
    "                    \" we cannot handle this.\"\n",
    "                )\n",
    "            new_result_doc_list.append(_sub_result_docs[:-1])\n",
    "            _sub_result_docs = _sub_result_docs[-1:]\n",
    "    new_result_doc_list.append(_sub_result_docs)\n",
    "    return new_result_doc_list\n",
    "\"\"\"\n",
    "code3 = \"\"\"def compute_distance_matrix(Xs,D_current=None,i=None):\n",
    "    if i is None:\n",
    "        D_current = np.zeros((Xs.shape[0],Xs.shape[0]))\n",
    "            for k in range(len(Xs)):\n",
    "                for l in range(len(Xs)):\n",
    "                    D_current[k,l] = np.linalg.norm(Xs[k]-Xs[l],2)\n",
    "    else:\n",
    "        for k in range(len(Xs)):\n",
    "            D_current[k,i]=D_current[i,k] = np.linalg.norm(Xs[k]-Xs[i],2)\n",
    "    return D_current\n",
    "\"\"\"\n",
    "code4 = \"\"\"def mutinator(mylist):\n",
    "    mylist.append(0)\n",
    "    return len(mylist)\n",
    "\"\"\"\n",
    "\n",
    "openai_code, mistral_code = opt.optimize_python_code(code4)\n",
    "\n",
    "print('OpenAI code')\n",
    "for c in openai_code:\n",
    "    print(c)\n",
    "print('\\nMistral code')\n",
    "for c in mistral_code:\n",
    "    print(c)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-29T09:51:51.610499Z",
     "start_time": "2024-02-29T09:51:36.274552Z"
    }
   },
   "id": "e6f0f53319d68837",
   "execution_count": 10,
   "outputs": []
  },
  {
   "cell_type": "code",
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-29T09:51:51.614284Z",
     "start_time": "2024-02-29T09:51:51.607490Z"
    }
   },
   "id": "d65fcf3dbed8c36f",
   "execution_count": 10,
   "outputs": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
