|
| 1 | +from config.settings.base import LLAMA_MODEL_DIR |
| 2 | +from llama_cpp import Llama |
| 3 | +import os |
| 4 | + |
| 5 | +class functionsLlama: |
| 6 | + |
| 7 | + def getReference(text_reference): |
| 8 | + llm = Llama(model_path = LLAMA_MODEL_DIR+"/llama-3.2-3b-instruct-q4_k_m.gguf") |
| 9 | + |
| 10 | + messages = [ |
| 11 | + { 'role': 'system', |
| 12 | + 'content': 'You are an assistant who distinguishes all the components of a citation in an article with output in JSON' |
| 13 | + }, |
| 14 | + { 'role': 'user', |
| 15 | + 'content': 'Bachman, S., J. Moat, A. W. Hill, J. de la Torre and B. Scott. 2011. Supporting Red List threat assessments with GeoCAT: geospatial conservation assessment tool. ZooKeys 150: 117-126. DOI: https://doi.org/10.3897/zookeys.150.2109' |
| 16 | + }, |
| 17 | + { 'role': 'assistant', |
| 18 | + 'content': { |
| 19 | + 'reftype': 'journal', |
| 20 | + 'authors': [ |
| 21 | + { 'surname': 'Bachman', 'fname': 'S.' }, |
| 22 | + { 'surname': 'Moat', 'fname': 'J.' }, |
| 23 | + { 'surname': 'Hill', 'fname': 'A. W.' }, |
| 24 | + { 'surname': 'de la Torre', 'fname': 'J.' }, |
| 25 | + { 'surname': 'Scott', 'fname': 'B.' }, |
| 26 | + ], |
| 27 | + 'date': 2011, |
| 28 | + 'title': 'Supporting Red List threat assessments with GeoCAT: geospatial conservation assessment tool', |
| 29 | + 'source': 'ZooKeys', |
| 30 | + 'vol': '150', |
| 31 | + 'num': None, |
| 32 | + 'pages': '117-126', |
| 33 | + 'doi': '10.3897/zookeys.150.2109', |
| 34 | + } |
| 35 | + }, |
| 36 | + { 'role': 'user', |
| 37 | + 'content': 'Brunel, J. F. 1987. Sur le genre Phyllanthus L. et quelques genres voisins de la Tribu des Phyllantheae Dumort. (Euphorbiaceae, Phyllantheae) en Afrique intertropicale et à Madagascar. Thèse de doctorat de l’Université L. Pasteur. Strasbourg, France. 760 pp.' |
| 38 | + }, |
| 39 | + { 'role': 'assistant', |
| 40 | + 'content': { |
| 41 | + 'reftype': 'Thesis', |
| 42 | + 'authors': [ |
| 43 | + { 'surname': 'Brunel', 'fname': 'J. F.' }, |
| 44 | + ], |
| 45 | + 'date': 1987, |
| 46 | + 'title': 'Sur le genre Phyllanthus L. et quelques genres voisins de la Tribu des Phyllantheae Dumort. (Euphorbiaceae, Phyllantheae) en Afrique intertropicale et à Madagascar', |
| 47 | + 'degree': 'doctorat', |
| 48 | + 'organization': 'l’Université L. Pasteur', |
| 49 | + 'city': 'Strasbourg', |
| 50 | + 'country': 'France', |
| 51 | + 'num_pages': 760 |
| 52 | + }, |
| 53 | + }, |
| 54 | + { |
| 55 | + 'role': 'user', |
| 56 | + 'content': 'Hernández-López, L. 1995. The endemic flora of Jalisco, Mexico: Centers of endemism and implications for conservation. Tesis de maestría. Universidad de Wisconsin. Madison, USA. 74 pp.' |
| 57 | + }, |
| 58 | + { 'role': 'assistant', |
| 59 | + 'content': { |
| 60 | + 'reftype': 'Thesis', |
| 61 | + 'authors': [ |
| 62 | + { 'surname': 'Hernández-López', 'fname': 'L.' }, |
| 63 | + ], |
| 64 | + 'date': 1995, |
| 65 | + 'title': 'The endemic flora of Jalisco, Mexico: Centers of endemism and implications for conservation', |
| 66 | + 'degree': 'maestría', |
| 67 | + 'organization': 'Universidad de Wisconsin', |
| 68 | + 'city': 'Madison', |
| 69 | + 'country': 'USA', |
| 70 | + 'num_pages': 74 |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + 'role': 'user', |
| 75 | + 'content': 'Schimper, A. F. W. 1903. Plant geography upon a physiological basis. Clarendon Press. Oxford, UK. 839 pp.' |
| 76 | + }, |
| 77 | + { |
| 78 | + 'role': 'assistant', |
| 79 | + 'content': { |
| 80 | + 'reftype': 'book', |
| 81 | + 'authors':[ |
| 82 | + { 'surname': 'Schimper', 'fname': 'A. F. W.' }, |
| 83 | + ], |
| 84 | + 'date': 1903, |
| 85 | + 'title': 'Plant geography upon a physiological basis', |
| 86 | + 'organization': 'Clarendon Press', |
| 87 | + 'city': 'Oxford', |
| 88 | + 'country': 'UK', |
| 89 | + 'num_pages': 839 |
| 90 | + } |
| 91 | + }, |
| 92 | + { |
| 93 | + 'role': 'user', |
| 94 | + 'content': 'Correa, M. D., C. Galdames and M. Stapf. 2004. Catálogo de Plantas Vasculares de Panamá. Smithsonian Tropical Research Institute. Ciudad de Panamá, Panamá. 599 pp.' |
| 95 | + }, |
| 96 | + { |
| 97 | + 'role': 'assistant', |
| 98 | + 'content': { |
| 99 | + 'reftype': 'book', |
| 100 | + 'authors':[ |
| 101 | + { 'surname': 'Correa', 'fname': 'M. D.' }, |
| 102 | + { 'surname': 'Galdames', 'fname': 'C.' }, |
| 103 | + { 'surname': 'Stapf', 'fname': 'M.' }, |
| 104 | + ], |
| 105 | + 'date': 2004, |
| 106 | + 'title': 'Catálogo de Plantas Vasculares de Panamá', |
| 107 | + 'organization': 'Smithsonian Tropical Research Institute', |
| 108 | + 'city': 'Ciudad de Panamá', |
| 109 | + 'country': 'Panamá', |
| 110 | + 'num_pages': 599 |
| 111 | + } |
| 112 | + }, |
| 113 | + { |
| 114 | + 'role': 'user', |
| 115 | + 'content': 'Hernández-López, L. 2019. Las especies endémicas de plantas en el estado de Jalisco: su distribución y conservación. Comisión Nacional para el Conocimiento y Uso de la Biodiversidad (CONABIO). Cd. Mx., México. https://doi.org/10.15468/ktvqds (consultado diciembre de 2019).' |
| 116 | + }, |
| 117 | + { |
| 118 | + 'role': 'assistant', |
| 119 | + 'content': { |
| 120 | + 'reftype': 'data', |
| 121 | + 'authors':[ |
| 122 | + { 'surname': 'Hernández-López', 'fname': 'L.' }, |
| 123 | + ], |
| 124 | + 'date': 2019, |
| 125 | + 'title': 'Las especies endémicas de plantas en el estado de Jalisco: su distribución y conservación', |
| 126 | + 'organization': 'Comisión Nacional para el Conocimiento y Uso de la Biodiversidad (CONABIO)', |
| 127 | + 'city': 'Cd. Mx.', |
| 128 | + 'country': 'México', |
| 129 | + 'doi': '10.15468/ktvqds', |
| 130 | + 'access_date': 'diciembre de 2019' |
| 131 | + } |
| 132 | + }, |
| 133 | + { |
| 134 | + 'role': 'user', |
| 135 | + 'content': 'INAFED. 2010. Enciclopedia de los Municipios y Delegaciones de México: Jalisco. Instituto Nacional para el Federalismo y el Desarrollo Municipal. http://www.inafed.gob.mx/ work/enciclopedia/EMM21puebla/index.html (consultado diciembre de 2018).' |
| 136 | + }, |
| 137 | + { |
| 138 | + 'role': 'assistant', |
| 139 | + 'content': { |
| 140 | + 'reftype': 'webpage', |
| 141 | + 'authors':[ |
| 142 | + { 'collab': 'INAFED' }, |
| 143 | + ], |
| 144 | + 'date': 2010, |
| 145 | + 'title': 'Enciclopedia de los Municipios y Delegaciones de México: Jalisco', |
| 146 | + 'organization': 'Instituto Nacional para el Federalismo y el Desarrollo Municipal', |
| 147 | + 'uri': 'http://www.inafed.gob.mx/ work/enciclopedia/EMM21puebla/index.html', |
| 148 | + 'access_date': 'diciembre de 2018' |
| 149 | + } |
| 150 | + }, |
| 151 | + { |
| 152 | + 'role': 'user', |
| 153 | + 'content': 'Nikon Corporation. 1991-2006. NIS- Elements, version 2.33. Tokio, Japón.' |
| 154 | + }, |
| 155 | + { |
| 156 | + 'role': 'assistant', |
| 157 | + 'content': { |
| 158 | + 'reftype': 'software', |
| 159 | + 'authors':[ |
| 160 | + { 'collab': 'Nikon Corporation' }, |
| 161 | + ], |
| 162 | + 'date': 2006, |
| 163 | + 'source': 'NIS- Elements', |
| 164 | + 'version': '2.33' |
| 165 | + 'city': 'Tokio', |
| 166 | + 'country': 'Japón', |
| 167 | + } |
| 168 | + }, |
| 169 | + { |
| 170 | + 'role': 'user', |
| 171 | + 'content': text_reference |
| 172 | + } |
| 173 | + ] |
| 174 | + |
| 175 | + response_format={ |
| 176 | + 'type': 'json_object', |
| 177 | + 'schema':{ |
| 178 | + 'type': 'object', |
| 179 | + 'properties': { |
| 180 | + 'reftype': {'type': 'string', 'enum': ['journal', 'thesis', 'book', 'data', 'webpage', 'software']}, |
| 181 | + 'authors': {'type': 'array', |
| 182 | + 'items': { |
| 183 | + 'type': 'object', |
| 184 | + 'properties': { |
| 185 | + 'surname': {'type': 'string'}, |
| 186 | + 'fname': {'type': 'string'}, |
| 187 | + 'collab': {'type': 'string'} |
| 188 | + } |
| 189 | + } |
| 190 | + }, |
| 191 | + 'date': {'type': 'integer'}, |
| 192 | + 'title': {'type': 'string'}, |
| 193 | + 'source': {'type': 'string'}, |
| 194 | + 'vol': {'type': 'integer'}, |
| 195 | + 'num': {'type': 'integer'}, |
| 196 | + 'pages': {'type': 'string'}, |
| 197 | + 'doi': {'type': 'string'}, |
| 198 | + 'degree': {'type': 'string'}, |
| 199 | + 'organization': {'type': 'string'}, |
| 200 | + 'city': {'type': 'string'}, |
| 201 | + 'country': {'type': 'string'}, |
| 202 | + 'num_pages': {'type': 'integer'}, |
| 203 | + 'uri': {'type': 'string'}, |
| 204 | + 'access_date': {'type': 'string'}, |
| 205 | + 'version': {'type': 'string'}, |
| 206 | + }, |
| 207 | + 'required':{ |
| 208 | + 'journal': ['reftype', 'authors', 'date', 'title', 'source', 'vol', 'num', 'pages', 'doi'], |
| 209 | + 'thesis': ['reftype', 'authors', 'date', 'title', 'degree', 'organization', 'city', 'country', 'num_pages'], |
| 210 | + 'book': ['reftype', 'authors', 'date', 'title', 'organization', 'city', 'country', 'num_pages'], |
| 211 | + 'data': ['reftype', 'authors', 'date', 'title', 'organization', 'city', 'country', 'num_pages', 'doi', 'access_date'], |
| 212 | + 'webpage': ['reftype', 'authors', 'date', 'title', 'organization', 'uri', 'access_date'], |
| 213 | + 'software': ['reftype', 'authors', 'date', 'source', 'version', 'city', 'country'] |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + output = llm.create_chat_completion(messages=messages, response_format=response_format, max_tokens=20000, temperature=0.5, top_p=0.5) |
| 219 | + return(output['choices'][0]['message']['content']) |
0 commit comments