From 87a11f2158fdd21e47bd3be376f4a3ee0065916c Mon Sep 17 00:00:00 2001 From: arionrefat Date: Thu, 23 Mar 2023 13:27:22 +0600 Subject: [PATCH] added multilevel classification support --- config/multiLevelClassification.js | 18 +++++ config/summarization.js | 2 +- examples/data/mutliLevelClassification.js | 84 +++++++++++++++++++++ examples/multiLevelClassificationExample.js | 17 +++++ 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 config/multiLevelClassification.js create mode 100644 examples/data/mutliLevelClassification.js create mode 100644 examples/multiLevelClassificationExample.js diff --git a/config/multiLevelClassification.js b/config/multiLevelClassification.js new file mode 100644 index 0000000..aa19e57 --- /dev/null +++ b/config/multiLevelClassification.js @@ -0,0 +1,18 @@ +export const multiLevelClassification = ({ description = '', examples = [], context = '' }) => { + if (!context) throw new Error('context is required'); + + const _example = + examples && examples.length > 0 + ? `Examples: ${examples.map( + (ex) => `Input: ${ex.context} Output: [${JSON.stringify(ex.expected_answer)}]` + )}` + : ''; + + return ` + ${description} + You are a highly intelligent and can accurately classify Multi-Label Text Classification. You take the above passage as input and return the multi level text classification as output from the Paragraph. Your output format is only {{ output_format|default("{'Summary' : 'Extracted Answer'}") }} form, no other form. + ${_example} + Context: ${context} + Output: + `; +}; diff --git a/config/summarization.js b/config/summarization.js index 77a03d4..ac179f6 100644 --- a/config/summarization.js +++ b/config/summarization.js @@ -4,7 +4,7 @@ export const summarization = ({ description = '', examples = [], context = '' }) const _example = examples && examples.length > 0 ? `Examples: ${examples.map( - (ex) => `Input ${ex.context} Output [${ex.extracted_ans}]` + (ex) => `Input ${ex.context} Output [${ex.extracted_answer}]` )}` : ''; return ` diff --git a/examples/data/mutliLevelClassification.js b/examples/data/mutliLevelClassification.js new file mode 100644 index 0000000..cc7462e --- /dev/null +++ b/examples/data/mutliLevelClassification.js @@ -0,0 +1,84 @@ +export const multilabelClassificationData = { + samples: [ + { + context: `The Vietnam War (also known by other names) was a conflict in Vietnam, Laos, and Cambodia from 1 November 1955 to the fall of Saigon on 30 April 1975. It was the second of the Indochina Wars and was officially fought between North Vietnam and South Vietnam. The north was supported by the Soviet Union, China, and other communist states, while the south was supported by the United States and other anti-communist allies. The war is widely considered to be a Cold War-era proxy war. It lasted almost 20 years, with direct U.S. involvement ending in 1973. The conflict also spilled over into neighboring states, exacerbating the Laotian Civil War and the Cambodian Civil War, which ended with all three countries becoming communist states by 1975.`, + expected_answer: { + 'main class': 'Vietnam War', + 1: { + 'sub-class': 'Causes and Background', + branch: 'History', + group: 'Political', + }, + 2: { + 'sub-class': 'Military Tactics and Strategies', + branch: 'Military Science', + group: 'Warfare', + }, + 3: { + 'sub-class': 'International Relations', + branch: 'Political Science', + group: 'Diplomacy', + }, + 4: { + 'sub-class': 'Social and Cultural Impact', + branch: 'Sociology', + group: 'Culture and Society', + }, + 5: { + 'sub-class': 'Legacy and Aftermath', + branch: 'History', + group: 'Political', + }, + }, + }, + { + context: `MRI was originally called NMRI (nuclear magnetic resonance imaging), but "nuclear" was dropped to avoid negative associations. Certain atomic nuclei are able to absorb radio frequency energy when placed in an external magnetic field; the resultant evolving spin polarization can induce a RF signal in a radio frequency coil and thereby be detected. In clinical and research MRI, hydrogen atoms are most often used to generate a macroscopic polarization that is detected by antennas close to the subject being examined. Hydrogen atoms are naturally abundant in humans and other biological organisms, particularly in water and fat. For this reason, most MRI scans essentially map the location of water and fat in the body. Pulses of radio waves excite the nuclear spin energy transition, and magnetic field gradients localize the polarization in space. By varying the parameters of the pulse sequence, different contrasts may be generated between tissues based on the relaxation properties of the hydrogen atoms therein.`, + expected_answer: { + 'main class': 'MRI', + 1: { + 'sub-class': 'Principles', + branch: 'Physics', + group: 'Medical imaging', + }, + 2: { + 'sub-class': 'Applications', + branch: 'Medicine', + group: 'Diagnostic imaging', + }, + 3: { + 'sub-class': 'Technique', + branch: 'Radiology', + group: 'Imaging', + }, + }, + }, + { + context: `A quantum computer is a computer that exploits quantum mechanical phenomena. At small scales, physical matter exhibits properties of both particles and waves, and quantum computing leverages this behavior using specialized hardware. Classical physics cannot explain the operation of these quantum devices, and a scalable quantum computer could perform some calculations exponentially faster than any modern "classical" computer. In particular, a large-scale quantum computer could break widely used encryption schemes and aid physicists in performing physical simulations; however, the current state of the art is still largely experimental and impractical. + +The basic unit of information in quantum computing is the qubit, similar to the bit in traditional digital electronics. Unlike a classical bit, a qubit can exist in a superposition of its two "basis" states, which loosely means that it is in both states simultaneously. When measuring a qubit, the result is a probabilistic output of a classical bit. If a quantum computer manipulates the qubit in a particular way, wave interference effects can amplify the desired measurement results. The design of quantum algorithms involves creating procedures that allow a quantum computer to perform calculations efficiently.`, + expected_answer: { + 'main class': 'Quantum Computing', + 1: { + 'sub-class': 'Fundamentals', + branch: 'Physics', + group: 'Quantum Mechanics', + }, + 2: { + 'sub-class': 'Hardware', + branch: 'Engineering', + group: 'Quantum Engineering', + }, + 3: { + 'sub-class': 'Algorithms', + branch: 'Computer Science', + group: 'Quantum Information Theory', + }, + 4: { + 'sub-class': 'Applications', + branch: 'Interdisciplinary', + group: 'Quantum Computing Applications', + }, + }, + }, + ], +}; diff --git a/examples/multiLevelClassificationExample.js b/examples/multiLevelClassificationExample.js new file mode 100644 index 0000000..c483d04 --- /dev/null +++ b/examples/multiLevelClassificationExample.js @@ -0,0 +1,17 @@ +import { multiLevelClassification } from '../config/multiLevelClassification.js'; +import { multilabelClassificationData } from '../examples/data/mutliLevelClassification.js'; +import { OpenAI } from '../models/openai.js'; +import { Prompter } from '../promptify/index.js'; + +const model = OpenAI('api-key'); + +const prompt = multiLevelClassification({ + examples: multilabelClassificationData.samples, + context: `The first web browser, called WorldWideWeb, was created in 1990 by Sir Tim Berners-Lee. He then recruited Nicola Pellow to write the Line Mode Browser, which displayed web pages on dumb terminals. The Mosaic web browser was released in April 1993, and was later credited as the first web browser to find mainstream popularity. Its innovative graphical user interface made the World Wide Web easy to navigate and thus more accessible to the average person. This, in turn, sparked the Internet boom of the 1990s, when the Web grew at a very rapid rate. Marc Andreessen, the leader of the Mosaic team, started his own company, Netscape, which released the Mosaic-influenced Netscape Navigator in 1994. Navigator quickly became the most popular browser. + +Microsoft debuted Internet Explorer in 1995, leading to a browser war with Netscape. Within a few years, Microsoft gained a dominant position in the browser market for two reasons: it bundled Internet Explorer with Microsoft Windows, their popular operating system and did so as freeware with no restrictions on usage. The market share of Internet Explorer peaked at over 95% in the early 2000s. In 1998, Netscape launched what would become the Mozilla Foundation to create a new browser using the open-source software model. This work evolved into the Firefox browser, first released by Mozilla in 2004. Firefox's market share peaked at 32% in 2010. Apple released its Safari browser in 2003. Safari remains the dominant browser on Apple devices, though it did not become popular elsewhere.`, +}); + +const result = await Prompter(model, prompt, 'text-davinci-003'); + +console.log(result);