{
    "cells": [
        {
            "language": "markdown",
            "source": [
                "# Prompt - Name entity recognition (NER) - UX Job titles"
            ],
            "outputs": []
        },
        {
            "language": "markdown",
            "source": [
                "### Add Prompt module and get API key from .env file"
            ],
            "outputs": []
        },
        {
            "language": "javascript",
            "source": [
                "const Prompt = require('../index.js')\nconst {Prompter, OpenAI} = Prompt;\n\nconst DotEnv = require('dotenv');\nDotEnv.config({path: '../.env'});\nconst apiKey = process.env.OPENAI_API_KEY;"
            ],
            "outputs": []
        },
        {
            "language": "markdown",
            "source": [
                "### Load the model for OpenAI"
            ],
            "outputs": []
        },
        {
            "language": "javascript",
            "source": [
                "let model = new OpenAI(apiKey);\nlet prompt = new Prompter(model);"
            ],
            "outputs": []
        },
        {
            "language": "markdown",
            "source": [
                "### Call the prompt module \n* The `domain` property helps fucus the language model.\n* The `textInput` property is the text to be parsed."
            ],
            "outputs": []
        },
        {
            "language": "javascript",
            "source": [
                "let result = await prompt.fit('ner', {\n    domain: 'ux design',\n    textInput: 'Senior UX Researcher, part-time 6 month contract, Edinburgh - Hybrid',\n  });\nconsole.log(result);"
            ],
            "outputs": [
                {
                    "items": [
                        {
                            "mime": "application/vnd.code.notebook.stdout",
                            "value": [
                                "4.795371791958809 seconds",
                                "{",
                                "  promptTokens: 152,",
                                "  completionTokens: 99,",
                                "  totalTokens: 251,",
                                "  data: [",
                                "    { label: 'Position', text: 'Senior UX Researcher' },",
                                "    { label: 'Contract Type', text: 'part-time' },",
                                "    { label: 'Contract Duration', text: '6 month' },",
                                "    { label: 'Location', text: 'Edinburgh' },",
                                "    { label: 'Work Type', text: 'Hybrid' },",
                                "    { branch: 'UX Design', group: 'Research' }",
                                "  ]",
                                "}",
                                ""
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "language": "markdown",
            "source": [
                "### With labels\n* The `domain` property helps fucus the language model.\n* The `textInput` property is the text to be parsed.\n* The `labels` array is a list entities that the model should extract. "
            ],
            "outputs": []
        },
        {
            "language": "javascript",
            "source": [
                "let result = await prompt.fit('ner', {\n    domain: 'ux design',\n    labels: [\"JOBTITLE\", \"SENORITY\", \"EMPLOYMENT_TYPE\", \"DURATION\", \"LOCATION\", \"REMOTE_OPTION\"],\n    textInput: 'Senior Product Designer (UX)'\n  });\nconsole.log(result);"
            ],
            "outputs": [
                {
                    "items": [
                        {
                            "mime": "application/vnd.code.notebook.stdout",
                            "value": [
                                "2.788457082986832 seconds",
                                "{",
                                "  promptTokens: 172,",
                                "  completionTokens: 57,",
                                "  totalTokens: 229,",
                                "  data: [",
                                "    { label: 'JOBTITLE', text: 'Senior Product Designer' },",
                                "    { label: 'SENORITY', text: 'Senior' },",
                                "    { label: 'EMPLOYMENT_TYPE', text: 'UX' }",
                                "  ]",
                                "}",
                                ""
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "language": "markdown",
            "source": [
                "### With inline fine tuning\n* The `domain` property helps fucus the language model.\n* The `textInput` property is the text to be parsed.\n* The `labels` array is a list entities that the model should extract. \n* The `examples` array take an array of arrays. Each of which has two properties. The example input text and the labels data you would expect to be returned. You nay add muiltple examples, but remember there is a limit to how big the prompt can be, which is set by the model you are using."
            ],
            "outputs": []
        },
        {
            "language": "javascript",
            "source": [
                "let text = \"Senior UX Researcher, part-time 6 month contract, Edinburgh - Hybrid\"\nlet labels = [\n  { label: 'JOBTITLE', text: 'Senior UX Researcher' },\n  { label: 'SENIORITY', text: 'Senior' },\n  { label: 'EMPLOYMENT_TYPE', text: 'part-time' },\n  { label: 'EMPLOYMENT_TYPE', text: 'contract' },\n  { label: 'DURATION', text: '6 month' },\n  { label: 'LOCATION', text: 'Edinburgh' },\n  { label: 'REMOTE_OPTION', text: 'Hybrid' },\n  { branch: 'UX Design', group: 'Research' }\n]\nlet fineTune = [\n  [text, labels]\n]\n\nlet jobTitle = 'UX Copy Writer Team Leader, Edinburgh or Hybrid'\nlet result = await prompt.fit('ner.njk', {\n    domain: 'ux design',\n    labels: [\"JOBTITLE\", \"SENORITY\", \"EMPLOYMENT_TYPE\", \"DURATION\", \"LOCATION\", \"REMOTE_OPTION\"],\n    textInput: jobTitle,\n    examples: fineTune\n    });\nconsole.log(result);"
            ],
            "outputs": [
                {
                    "items": [
                        {
                            "mime": "application/vnd.code.notebook.stdout",
                            "value": [
                                "5.108870041012764 seconds",
                                "{",
                                "  promptTokens: 413,",
                                "  completionTokens: 108,",
                                "  totalTokens: 521,",
                                "  data: [",
                                "    { label: 'JOBTITLE', text: 'UX Copy Writer Team Leader' },",
                                "    { label: 'LOCATION', text: 'Edinburgh' },",
                                "    { label: 'REMOTE_OPTION', text: 'Hybrid' },",
                                "    { branch: 'UX Design', group: 'Copy Writing' }",
                                "  ]",
                                "}",
                                ""
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "language": "markdown",
            "source": [
                "### Function to get the start and end positions of the labels"
            ],
            "outputs": []
        },
        {
            "language": "javascript",
            "source": [
                "function addStartEnd(text, entities){\n    let newEntities = [];\n    for (let entity of entities) {\n        let start = 0;\n        let end = 0;\n        start = text.indexOf(entity.text);\n        if(start > -1){\n            end = start + entity.text.length;\n            newEntities.push({\n                text: entity.text,\n                start: start,\n                end: end,\n                label: entity.label\n            });\n        }else{\n            console.log('could not find entity in text: ', entity);\n        }\n    }\n    return newEntities;\n}"
            ],
            "outputs": []
        },
        {
            "language": "javascript",
            "source": [
                "addStartEnd(jobTitle, result.data);"
            ],
            "outputs": [
                {
                    "items": [
                        {
                            "mime": "application/vnd.code.notebook.stdout",
                            "value": [
                                "could not find entity in text:  { branch: 'UX Design', group: 'Copy Writing' }",
                                ""
                            ]
                        }
                    ]
                },
                {
                    "items": [
                        {
                            "mime": "text/plain",
                            "value": [
                                "[",
                                "  {",
                                "    text: \u001b[32m'UX Copy Writer Team Leader'\u001b[39m,",
                                "    start: \u001b[33m0\u001b[39m,",
                                "    end: \u001b[33m26\u001b[39m,",
                                "    label: \u001b[32m'JOBTITLE'\u001b[39m",
                                "  },",
                                "  {",
                                "    text: \u001b[32m'Edinburgh'\u001b[39m,",
                                "    start: \u001b[33m28\u001b[39m,",
                                "    end: \u001b[33m37\u001b[39m,",
                                "    label: \u001b[32m'LOCATION'\u001b[39m",
                                "  },",
                                "  {",
                                "    text: \u001b[32m'Hybrid'\u001b[39m,",
                                "    start: \u001b[33m41\u001b[39m,",
                                "    end: \u001b[33m47\u001b[39m,",
                                "    label: \u001b[32m'REMOTE_OPTION'\u001b[39m",
                                "  }",
                                "]"
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}