{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ec4a75e1-aa9b-495f-a28e-3c276e7dc482",
   "metadata": {},
   "outputs": [],
   "source": [
    "#AIE 1.6 ONLY - from AIE NB\n",
    "domain_name = \"hpepcai-ingress.pcai.hpecic.net\"\n",
    "rag_name = \"pdf-extractor-1748400159158\"\n",
    "token_loc=\"/etc/secrets/ezua/.auth_token\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7de69964-6b4a-439a-8d19-d45b8967cf19",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "# NOT REQ\n",
    "#from platfrom \n",
    "#kubectl -n ezaddon-system get secret hpecp-bootstrap-authconfig -o jsonpath={.data.OIDC_CLIENT_SECRET} | base64 -d\n",
    "CLIENT_TOKEN=''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "659b5ea5-3a8b-4d14-a416-f45952d43dd6",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "# NOT REQ\n",
    "# generate access token\n",
    "UA_DOMAIN=\"ingress.\"+domain_name\n",
    "KC_ADDR=f\"keycloak.{UA_DOMAIN}\"\n",
    "\n",
    "# generate by logging in to platform with 2FA if applicable\n",
    "with open(\"refresh-token.txt\",'r') as file:\n",
    "    REFRESH_TOKEN = file.read()\n",
    "\n",
    "payload_refresh = {\n",
    "    \"grant_type\": \"refresh_token\",\n",
    "    \"client_id\": \"ua\",\n",
    "    \"refresh_token\": REFRESH_TOKEN,\n",
    "    \"client_secret\": CLIENT_TOKEN\n",
    "}\n",
    "\n",
    "results = requests.post(f\"https://{KC_ADDR}/realms/UA/protocol/openid-connect/token\", data=payload_refresh)\n",
    "\n",
    "ACCESS_TOKEN = results.json()['access_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2fd3a4cb-d448-445d-a4b1-cf9b8a02c351",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Token successfully refreshed.\n"
     ]
    }
   ],
   "source": [
    "%update_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "043db135-925b-4079-bea2-008f184a3298",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJEUllCZThPMzdnUjA2anFYTFEtUFFtbDBNVVJVaXNtRG11VnhwN0JHSlhvIn0.eyJleHAiOjE3NTkyMjEwMDcsImlhdCI6MTc1OTIxOTIwNywiYXV0aF90aW1lIjoxNzU5MjEyMDU5LCJqdGkiOiJiOTllMjkxMi05ZDk4LTRhMTEtODI4Zi1iZGQxY2Y3ZDljNjEiLCJpc3MiOiJodHRwczovL2tleWNsb2FrLmFpLWFwcGxpY2F0aW9uLmhvdS1wY2FpLmhwZWNpYy5uZXQvcmVhbG1zL1VBIiwic3ViIjoiNjg4YjE1YjgtZTUyNC00Y2RiLTgxMTMtNTU2NDQ3YTMzMmJmIiwidHlwIjoiQmVhcmVyIiwiYXpwIjoidWEiLCJub25jZSI6Il90dnd2eC1PUUZobTZmWS1vSlh5aHR1VDhKam1mdmZIbzRrY2NVb3dSRDAiLCJzZXNzaW9uX3N0YXRlIjoiMWY5ZGFiYTEtMzVlYS00MzA3LTk1MDAtMWE4MjY4NDk0OGM4IiwiYWNyIjoiMSIsInNjb3BlIjoib3BlbmlkIGhvdXBjYWlocGU6dWEgb2ZmbGluZV9hY2Nlc3MgcHJvZmlsZSBlbWFpbCIsInNpZCI6IjFmOWRhYmExLTM1ZWEtNDMwNy05NTAwLTFhODI2ODQ5NDhjOCIsInVpZCI6IjEwMDAwMjI1IiwiZ2lkIjoiMTAwMSIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwibmFtZXNwYWNlIjoiaGFyc2hhbC1wYXRpbC1nLTJjMjJiNGQ3IiwibmFtZSI6IkhhcnNoYWwgUGF0aWwiLCJncm91cHMiOlsidWEtZW5hYmxlZCIsIm9mZmxpbmVfYWNjZXNzIiwiYWRtaW4iLCJkZWZhdWx0LXJvbGVzLXVhIiwidW1hX2F1dGhvcml6YXRpb24iXSwicHJlZmVycmVkX3VzZXJuYW1lIjoiaGFyc2hhbC5wYXRpbC1nbWFpbC5jb20iLCJnaXZlbl9uYW1lIjoiSGFyc2hhbCIsInBvc2l4X3VzZXJuYW1lIjoiaGFyc2hhbC5wYXRpbC1nbWFpbC5jb20iLCJmYW1pbHlfbmFtZSI6IlBhdGlsIiwiZW1haWwiOiJoYXJzaGFsLnBhdGlsQGdtYWlsLmNvbSJ9.CHZpYptYXyZtZ8tDi63mcjT3BxlwCo0B_gkVrG3hPhmTmfMyE7VjA9oErspbRGkpGFnbnpgEklgLBmKbxrT5zDvVaEpBFyx_JVK6-ceiY6bmP6pRGHFXYkPJAIqhQ22usoL8j0gABFAaz6MIFAc0cYygnHNKH2q39-6TCEcgM_t7ZBz1CVGaMLC0G2iE2A1HYlwXoGX3LDNuXLtuj80-WGn8WJMpdSWiXoyJtelXs-9E_xZLGtc6NVaVwnkTntw5duxBQ3xjW-BYtwTiOaKTjVIyxpdtQsuLUkf-b8TN7Xk5mWMpT_jFQhywIRJRCIjef05ByojhasxfInzzCEKFRA\n"
     ]
    }
   ],
   "source": [
    "%update_token\n",
    "### alternatively Access Token from system\n",
    "#kubectl -n $NAMESPACE get secret access-token -o jsonpath='{.data.AUTH_TOKEN}' | base64 -d\n",
    "with open(token_loc,\"r\") as file:\n",
    "    ACCESS_TOKEN=file.read()\n",
    "    print(ACCESS_TOKEN)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fe109732-4809-4f6b-88e3-79e3ac4125a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cbf2721f-3131-4699-898d-65ff581e79e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# call directly if access token is present\n",
    "url = \"https://rag-coordinator.\"+domain_name+\"/v1/chat/completions\"\n",
    "prompt_list = [\n",
    "#    \"in context you are given invoice data. based on invoice number 86865 for polychemtex what is the shipping method - only provide shipping method nothing else - provide accurate information only\",\n",
    "#    \"in context you are given invoice data. based on invoice number 86865 for polychemtex - in the same invoice provide me item, description, qty, unit, unit price, rebate, line total in table format - only provide what is asked nothing else - provide accurate information only\",\n",
    "#    \"in context you are given invoice data. based on invoice number 86865 for polychemtex - in the same invoice provide me item, description, qty, unit, unit price, rebate, line total in table format - provide line 1 only - if there is no line 5 then say no-line - only provide what is asked nothing else - provide accurate information only\",\n",
    "\"in context you are given invoice data. based on invoice number 86865 for polychemtex - in the same invoice provide me item, description, qty, unit, unit price, rebate, line total in table format - provide first 3 lines only - only provide what is asked nothing else - provide accurate information only\"\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4b307e3e-b033-415b-aa1e-86a9b6268061",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.11/site-packages/urllib3/connectionpool.py:1064: InsecureRequestWarning: Unverified HTTPS request is being made to host 'rag-coordinator.hpepcai-ingress.pcai.hpecic.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " ----------- \n",
      "in context you are given invoice data. based on invoice number 86865 for polychemtex - in the same invoice provide me item, description, qty, unit, unit price, rebate, line total in table format - provide first 3 lines only - only provide what is asked nothing else - provide accurate information only\n",
      "<Response [200]>\n",
      "| Item | Description | QTY | Unit | Unit Price | Rebate | Line Total |\n",
      "| --- | --- | --- | --- | --- | --- | --- |\n",
      "| M0061 | Catalyst 1 liter | 10 | Ea. | 59.99 | 9.99 | 500.00 |\n",
      "| M0062 | Resin | 10 | Ea. | 59.99 | 9.99 | 500.00 |\n",
      "| M2001 | Deionized water | 5 | Ea. | 1.99 |  | 9.95 |\n"
     ]
    }
   ],
   "source": [
    "for prompt in prompt_list:\n",
    "    payload = {\n",
    "    \"model\": \"meta/llama-3.1-8b-instruct\",\n",
    "    \"messages\": [\n",
    "       {\n",
    "           \"role\": \"user\",\n",
    "           \"content\": prompt\n",
    "       },\n",
    "    ],\n",
    "    \"max_tokens\": 2048,\n",
    "    \"temperature\": 0,\n",
    "    \"frequency_penalty\": 1\n",
    "   }\n",
    "    headers = {\n",
    "      \"x-sa-name\": rag_name,\n",
    "      \"Authorization\": f\"Bearer {ACCESS_TOKEN}\"\n",
    "  }\n",
    "\n",
    "    response = requests.request(\"POST\", url, verify=False, json=payload,headers=headers)\n",
    "    print(\" ----------- \")\n",
    "    print(prompt)\n",
    "    print(response)\n",
    "    print(response.json()['choices'][0]['message']['content'])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1b70928-3206-465e-a5d9-19e6dbdc787c",
   "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.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
