Pontificia Universidad Católica de Chile
Departamento de Ciencia de la Computación
2025 - Bimestre 5
Procesamiento de Lenguaje Natural Ayudantía 1: Introducción a NLP
Profesor: Marcelo MendozaAyudantía 1: Introducción a NLP
Ayudante: Miguel Fernández
Introducción¶
En este documento se presenta una mirada general de diferentes modelos que pueden ser utilizados en el contexto de Procesamiento del Lenguaje Natural (NLP). Para esto, se trabajará con los modelos abiertos disponibles en el sitio web de HuggingFace.
Se recomienda revisar su sitio web: https://huggingface.co/models
Reconocimiento de entidades¶
En primer lugar, se cargará la librería spacy.
!python3 -m spacy download es_core_news_md
import spacy
Collecting es-core-news-md==3.8.0
Downloading https://github.com/explosion/spacy-models/releases/download/es_core_news_md-3.8.0/es_core_news_md-3.8.0-py3-none-any.whl (42.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.3/42.3 MB 18.2 MB/s eta 0:00:00
Installing collected packages: es-core-news-md
Successfully installed es-core-news-md-3.8.0
✔ Download and installation successful
You can now load the package via spacy.load('es_core_news_md')
⚠ Restart to reload dependencies
If you are in a Jupyter or Colab notebook, you may need to restart Python in
order to load all the package's dependencies. You can do this by selecting the
'Restart kernel' or 'Restart runtime' option.
Utilizando el modelo es_core_news_md se obtendran las entidades nombradas en el siguiente texto:
Título: CENIA impulsa el primer modelo de lenguaje colaborativo de Latinoamérica
'LatamGPT es el nombre del primer modelo de lenguaje de gran tamaño (Large Language Model, LLM por sus siglas en inglés) que se está desarrollando de manera colaborativa en Latinoamérica y el Caribe. Coordinado por el Centro Nacional de Inteligencia Artificial (CENIA) e impulsado en conjunto con más de 30 instituciones de América Latina y el Caribe y más de 60 expertas y expertos, el proyecto cuenta con el apoyo del Ministerio de Ciencia, Tecnología, Conocimiento e Innovación en la coordinación internacional con los distintos estados de la región y el apoyo de recursos desde CAF para la ejecución del mismo
Actualmente, los modelos de lenguaje que han sido generados en el Norte Global y han estado expuesto a datos, no necesariamente reflejan la idiosincrasia y matices propios de Latinoamérica y el Caribe, una brecha de representatividad que conduce a limitaciones en su desempeño y puede generar inexactitudes o “alucinaciones” respecto de la información que involucra a nuestros países. El hecho se agrava con el uso cada más frecuente de datos sintéticos para el entrenamiento de modelos de otra generación
En febrero de 2023, se comenzó a trabajar en el primer LLM regional, en un modelo de lenguaje con un enfoque colaborativo en la construcción de las bases de datos, de modo que su entrenamiento reflejara la cultura, el lenguaje y la historia de Latinoamérica, ofreciendo información más precisa y representativa de los contextos locales.
“Uno de los problemas que tenemos en Latinoamérica y el Caribe es que los LLM que usamos, si bien funcionan en español y portugués, son bastante limitados en el conocimiento que tienen sobre la región. Al poco andar comienzan a alucinar, porque muy pocos de los datos con los que estos modelos fueron entrenados son de esta parte del mundo. Es importante que en la región podamos desarrollar capacidades para tener cierta independencia y tomar decisiones de cómo esta tecnología impacta a la sociedad. Hasta el momento no tenemos un modelo de lenguaje regional, y esta tarea no la puede asumir solo un grupo ni un solo país: es un desafío que requiere el esfuerzo de toda la región”, comenta Álvaro Soto, director de CENIA.
Por su parte, la ministra de Ciencia, Aisén Etcheverry, se refiere al proyecto como “un esfuerzo colectivo, abierto y que es reflejo de lo que esperamos sea este modelo de lenguaje. Cuando hablamos de Inteligencia Artificial, ésta tiene que proyectar el mundo que somos, su diversidad. Y en el caso de Latinoamérica, no solo hablar español o portugués, sino que entender nuestra idiosincrasia, aportar desde la cultura y desde la visión del mundo, y eso tiene que estar presente en el desarrollo”.
En comparación con otros modelos cerrados (o Propietary), el nuevo modelo de lenguaje LatamGPT será abierto, lo que implica que permitirá que más personas en América Latina y el Caribe lo estudien, usen y mejoren, construyendo sobre él. “Esto democratiza el conocimiento y promueve el desarrollo de capacidades para poder entender mejor los modelos de lenguaje, sus aplicaciones y límites”, explica el gerente de CENIA., Rodrigo Durán.'
Comente los resultados.
Texto completo disponible en: https://cenia.cl/2025/02/13/cenia-impulsa-el-primer-modelo-de-lenguaje-colaborativo-de-latinoamerica/
Se carga el modelo.
sp = spacy.load('es_core_news_md')
texto = ''' LatamGPT es el nombre del primer modelo de lenguaje de gran tamaño (Large Language Model, LLM por sus siglas en inglés) que se está desarrollando de manera colaborativa en Latinoamérica y el Caribe. Coordinado por el Centro Nacional de Inteligencia Artificial (CENIA) e impulsado en conjunto con más de 30 instituciones de América Latina y el Caribe y más de 60 expertas y expertos, el proyecto cuenta con el apoyo del Ministerio de Ciencia, Tecnología, Conocimiento e Innovación en la coordinación internacional con los distintos estados de la región y el apoyo de recursos desde CAF para la ejecución del mismo
Actualmente, los modelos de lenguaje que han sido generados en el Norte Global y han estado expuesto a datos, no necesariamente reflejan la idiosincrasia y matices propios de Latinoamérica y el Caribe, una brecha de representatividad que conduce a limitaciones en su desempeño y puede generar inexactitudes o “alucinaciones” respecto de la información que involucra a nuestros países. El hecho se agrava con el uso cada más frecuente de datos sintéticos para el entrenamiento de modelos de otra generación
En febrero de 2023, se comenzó a trabajar en el primer LLM regional, en un modelo de lenguaje con un enfoque colaborativo en la construcción de las bases de datos, de modo que su entrenamiento reflejara la cultura, el lenguaje y la historia de Latinoamérica, ofreciendo información más precisa y representativa de los contextos locales.
“Uno de los problemas que tenemos en Latinoamérica y el Caribe es que los LLM que usamos, si bien funcionan en español y portugués, son bastante limitados en el conocimiento que tienen sobre la región. Al poco andar comienzan a alucinar, porque muy pocos de los datos con los que estos modelos fueron entrenados son de esta parte del mundo. Es importante que en la región podamos desarrollar capacidades para tener cierta independencia y tomar decisiones de cómo esta tecnología impacta a la sociedad. Hasta el momento no tenemos un modelo de lenguaje regional, y esta tarea no la puede asumir solo un grupo ni un solo país: es un desafío que requiere el esfuerzo de toda la región”, comenta Álvaro Soto, director de CENIA.
Por su parte, la ministra de Ciencia, Aisén Etcheverry, se refiere al proyecto como “un esfuerzo colectivo, abierto y que es reflejo de lo que esperamos sea este modelo de lenguaje. Cuando hablamos de Inteligencia Artificial, ésta tiene que proyectar el mundo que somos, su diversidad. Y en el caso de Latinoamérica, no solo hablar español o portugués, sino que entender nuestra idiosincrasia, aportar desde la cultura y desde la visión del mundo, y eso tiene que estar presente en el desarrollo”.
En comparación con otros modelos cerrados (o Propietary), el nuevo modelo de lenguaje LatamGPT será abierto, lo que implica que permitirá que más personas en América Latina y el Caribe lo estudien, usen y mejoren, construyendo sobre él. “Esto democratiza el conocimiento y promueve el desarrollo de capacidades para poder entender mejor los modelos de lenguaje, sus aplicaciones y límites”, explica el gerente de CENIA., Rodrigo Durán.
'''
Se aplica el modelo sobre el texto.
doc = sp(texto)
Se muestran las entidades nombradas en el documento.
for entity in doc.ents:
print(entity.text + ' - ' + entity.label_ + ' - ' + str(spacy.explain(entity.label_)))
LatamGPT - MISC - Miscellaneous entities, e.g. events, nationalities, products or works of art Large Language Model - MISC - Miscellaneous entities, e.g. events, nationalities, products or works of art Latinoamérica - LOC - Non-GPE locations, mountain ranges, bodies of water Caribe - LOC - Non-GPE locations, mountain ranges, bodies of water Coordinado por el Centro Nacional de Inteligencia Artificial - MISC - Miscellaneous entities, e.g. events, nationalities, products or works of art CENIA - MISC - Miscellaneous entities, e.g. events, nationalities, products or works of art América Latina - LOC - Non-GPE locations, mountain ranges, bodies of water Caribe - LOC - Non-GPE locations, mountain ranges, bodies of water Ministerio de Ciencia - LOC - Non-GPE locations, mountain ranges, bodies of water Tecnología - MISC - Miscellaneous entities, e.g. events, nationalities, products or works of art CAF - ORG - Companies, agencies, institutions, etc. Norte Global - LOC - Non-GPE locations, mountain ranges, bodies of water Latinoamérica - LOC - Non-GPE locations, mountain ranges, bodies of water Caribe - LOC - Non-GPE locations, mountain ranges, bodies of water Latinoamérica - LOC - Non-GPE locations, mountain ranges, bodies of water Latinoamérica - LOC - Non-GPE locations, mountain ranges, bodies of water Caribe - LOC - Non-GPE locations, mountain ranges, bodies of water portugués - LOC - Non-GPE locations, mountain ranges, bodies of water Al - ORG - Companies, agencies, institutions, etc. Álvaro Soto - PER - Named person or family. CENIA - ORG - Companies, agencies, institutions, etc. Aisén Etcheverry - PER - Named person or family. Cuando hablamos de - MISC - Miscellaneous entities, e.g. events, nationalities, products or works of art Inteligencia Artificial - MISC - Miscellaneous entities, e.g. events, nationalities, products or works of art Latinoamérica - LOC - Non-GPE locations, mountain ranges, bodies of water portugués - PER - Named person or family. Propietary - PER - Named person or family. LatamGPT - MISC - Miscellaneous entities, e.g. events, nationalities, products or works of art América Latina - LOC - Non-GPE locations, mountain ranges, bodies of water el Caribe - LOC - Non-GPE locations, mountain ranges, bodies of water CENIA - ORG - Companies, agencies, institutions, etc. Rodrigo Durán - PER - Named person or family.
También es posible obtener una visualización que destaca las entidades en el texto.
spacy.displacy.render(doc, style='ent')
Actualmente, los modelos de lenguaje que han sido generados en el Norte Global LOC y han estado expuesto a datos, no necesariamente reflejan la idiosincrasia y matices propios de Latinoamérica LOC y el Caribe LOC , una brecha de representatividad que conduce a limitaciones en su desempeño y puede generar inexactitudes o “alucinaciones” respecto de la información que involucra a nuestros países. El hecho se agrava con el uso cada más frecuente de datos sintéticos para el entrenamiento de modelos de otra generación
En febrero de 2023, se comenzó a trabajar en el primer LLM regional, en un modelo de lenguaje con un enfoque colaborativo en la construcción de las bases de datos, de modo que su entrenamiento reflejara la cultura, el lenguaje y la historia de Latinoamérica LOC , ofreciendo información más precisa y representativa de los contextos locales.
“Uno de los problemas que tenemos en Latinoamérica LOC y el Caribe LOC es que los LLM que usamos, si bien funcionan en español y portugués LOC , son bastante limitados en el conocimiento que tienen sobre la región. Al ORG poco andar comienzan a alucinar, porque muy pocos de los datos con los que estos modelos fueron entrenados son de esta parte del mundo. Es importante que en la región podamos desarrollar capacidades para tener cierta independencia y tomar decisiones de cómo esta tecnología impacta a la sociedad. Hasta el momento no tenemos un modelo de lenguaje regional, y esta tarea no la puede asumir solo un grupo ni un solo país: es un desafío que requiere el esfuerzo de toda la región”, comenta Álvaro Soto PER , director de CENIA ORG .
Por su parte, la ministra de Ciencia, Aisén Etcheverry PER , se refiere al proyecto como “un esfuerzo colectivo, abierto y que es reflejo de lo que esperamos sea este modelo de lenguaje. Cuando hablamos de MISC Inteligencia Artificial MISC , ésta tiene que proyectar el mundo que somos, su diversidad. Y en el caso de Latinoamérica LOC , no solo hablar español o portugués PER , sino que entender nuestra idiosincrasia, aportar desde la cultura y desde la visión del mundo, y eso tiene que estar presente en el desarrollo”.
En comparación con otros modelos cerrados (o Propietary PER ), el nuevo modelo de lenguaje LatamGPT MISC será abierto, lo que implica que permitirá que más personas en América Latina LOC y el Caribe LOC lo estudien, usen y mejoren, construyendo sobre él. “Esto democratiza el conocimiento y promueve el desarrollo de capacidades para poder entender mejor los modelos de lenguaje, sus aplicaciones y límites”, explica el gerente de CENIA ORG ., Rodrigo Durán PER .
Creación de resúmenes¶
Para esta tarea, se trabajará con el dataset CNN Daily News. Este conjunto de datos fue creado para entrenar modelos que generan resúmenes.
En primer lugar, se creará una conexión entre google colab y google drive. Esto facilita el manejo de archivos de gran tamaño.
from google.colab import drive
drive.mount("/content/gdrive",force_remount=True)
Mounted at /content/gdrive
A continuación, se carga el dataset:
path = "/content/gdrive/MyDrive/DOCTORADO/Ayudantía NLP (COURSERA)/versión 2/Ayudantía/AYUDANTIA 1/"
import pandas as pd
df_summarization = pd.read_csv(path + "CNN daily.csv")
df_summarization
| id | article | highlights | |
|---|---|---|---|
| 0 | 92c514c913c0bdfe25341af9fd72b29db544099b | Ever noticed how plane seats appear to be gett... | Experts question if packed out planes are put... |
| 1 | 2003841c7dc0e7c5b1a248f9cd536d727f27a45a | A drunk teenage boy had to be rescued by secur... | Drunk teenage boy climbed into lion enclosure ... |
| 2 | 91b7d2311527f5c2b63a65ca98d21d9c92485149 | Dougie Freedman is on the verge of agreeing a ... | Nottingham Forest are close to extending Dougi... |
| 3 | caabf9cbdf96eb1410295a673e953d304391bfbb | Liverpool target Neto is also wanted by PSG an... | Fiorentina goalkeeper Neto has been linked wit... |
| 4 | 3da746a7d9afcaa659088c8366ef6347fe6b53ea | Bruce Jenner will break his silence in a two-h... | Tell-all interview with the reality TV star, 6... |
| ... | ... | ... | ... |
| 11485 | ed8674cc15b29a87d8df8de1efee353d71122272 | Our young Earth may have collided with a body ... | Oxford scientists say a Mercury-like body stru... |
| 11486 | 2f58d1a99e9c47914e4b1c31613e3a041cd9011e | A man facing trial for helping his former love... | Man accused of helping former lover kill woman... |
| 11487 | 411f6d57825161c3a037b4742baccd6cd227c0c3 | A dozen or more metal implements are arranged ... | Marianne Power tried the tuning fork facial at... |
| 11488 | b5683ef8342056b17b068e0d59bdbe87e3fe44ea | Brook Lopez dominated twin brother Robin with ... | Brooklyn Nets beat the Portland Trail Blazers ... |
| 11489 | a6a5491edb0c96c4391b6a8c4504416b3572b3a1 | A Chinese hospital is being painstakingly move... | Chinese hospital marked for demolition because... |
11490 rows × 3 columns
Ejemplo del primer texto.
df_summarization.article[3]
"Liverpool target Neto is also wanted by PSG and clubs in Spain as Brendan Rodgers faces stiff competition to land the Fiorentina goalkeeper, according to the Brazilian's agent Stefano Castagna. The Reds were linked with a move for the 25-year-old, whose contract expires in June, earlier in the season when Simon Mignolet was dropped from the side. A January move for Neto never materialised but the former Atletico Paranaense keeper looks certain to leave the Florence-based club in the summer. Neto rushes from his goal as Juan Iturbe bears down on him during Fiorentina's clash with Roma in March . Neto is wanted by a number of top European clubs including Liverpool and PSG, according to his agent . It had been reported that Neto had a verbal agreement to join Serie A champions Juventus at the end of the season but his agent has revealed no decision about his future has been made yet. And Castagna claims Neto will have his pick of top European clubs when the transfer window re-opens in the summer, including Brendan Rodgers' side. 'There are many European clubs interested in Neto, such as for example Liverpool and Paris Saint-Germain,' Stefano Castagna is quoted as saying by Gazzetta TV. Firoentina goalkeeper Neto saves at the feet of Tottenham midfielder Nacer Chadli in the Europa League . 'In Spain too there are clubs at the very top level who are tracking him. Real Madrid? We'll see. 'We have not made a definitive decision, but in any case he will not accept another loan move elsewhere.' Neto, who represented Brazil at the London 2012 Olympics but has not featured for the senior side, was warned against joining a club as a No 2 by national coach Dunga. Neto joined Fiorentina from\xa0Atletico Paranaense in 2011 and established himself as No1 in the last two seasons."
El largo del texto es de 308 palabras.
len(df_summarization.article[3].split())
308
El resumen correspondiente es:
df_summarization.highlights[3]
'Fiorentina goalkeeper Neto has been linked with Liverpool and Arsenal .\nNeto joined Firoentina\xa0from Brazilian outfit Atletico Paranaense in 2011 .\nHe is also wanted by PSG and Spanish clubs, according to his agent .\nCLICK HERE for the latest Liverpool news .'
A continuación, se instala la librería Transformers. Esta librería es fundamental para trabajar con modelos de código abierto.
Observación: Los siguientes modelos de aprendizaje profundo requieren contar con una GPU. Google nos entrega una alternativa gratis de 15 [GB]. Para activarlo, deben seguir los siguientes pasos:
- Entorno de ejecución
- Cambiar tipo de entorno de ejecución
- En acelerador de hardware se debe seleccionar GPU T4.
Es importante considerar que estos recursos gratuitos están disponibles por un tiempo acotado.
!pip install transformers
from transformers import pipeline
Requirement already satisfied: transformers in /usr/local/lib/python3.12/dist-packages (4.57.3) Requirement already satisfied: filelock in /usr/local/lib/python3.12/dist-packages (from transformers) (3.20.0) Requirement already satisfied: huggingface-hub<1.0,>=0.34.0 in /usr/local/lib/python3.12/dist-packages (from transformers) (0.36.0) Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.12/dist-packages (from transformers) (2.0.2) Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.12/dist-packages (from transformers) (25.0) Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.12/dist-packages (from transformers) (6.0.3) Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.12/dist-packages (from transformers) (2025.11.3) Requirement already satisfied: requests in /usr/local/lib/python3.12/dist-packages (from transformers) (2.32.4) Requirement already satisfied: tokenizers<=0.23.0,>=0.22.0 in /usr/local/lib/python3.12/dist-packages (from transformers) (0.22.1) Requirement already satisfied: safetensors>=0.4.3 in /usr/local/lib/python3.12/dist-packages (from transformers) (0.7.0) Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.12/dist-packages (from transformers) (4.67.1) Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub<1.0,>=0.34.0->transformers) (2025.3.0) Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub<1.0,>=0.34.0->transformers) (4.15.0) Requirement already satisfied: hf-xet<2.0.0,>=1.1.3 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub<1.0,>=0.34.0->transformers) (1.2.0) Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (from requests->transformers) (3.4.4) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.12/dist-packages (from requests->transformers) (3.11) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.12/dist-packages (from requests->transformers) (2.5.0) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.12/dist-packages (from requests->transformers) (2025.11.12)
En este caso, se utiliza el modelo BART. Para facilitar la manipulación del modelo, se trabaja con pipeline.
Más información en: https://huggingface.co/facebook/bart-large-cnn
summarizer = pipeline("summarization", model="facebook/bart-large-cnn") #Este modelo tiene un tamaño de 1.63 GB
Device set to use cuda:0
#1 minuto por frase sin usar GPU
ejemplo = summarizer(df_summarization.article[3], max_length=128, truncation=True)
Asking to truncate to max_length but no maximum length is provided and the model has no predefined maximum length. Default to no truncation.
Resumen creado por el modelo
ejemplo
[{'summary_text': 'Fiorentina goalkeeper Neto is wanted by a number of top European clubs, according to his agent Stefano Castagna. Liverpool were linked with a move for the 25-year-old earlier in the season when Simon Mignolet was dropped from the side. A January move for Neto never materialised but the former Atletico Paranaense keeper looks certain to leave the Florence-based club in the summer.'}] ejemplo[0]["summary_text"]
'Fiorentina goalkeeper Neto is wanted by a number of top European clubs, according to his agent Stefano Castagna. Liverpool were linked with a move for the 25-year-old earlier in the season when Simon Mignolet was dropped from the side. A January move for Neto never materialised but the former Atletico Paranaense keeper looks certain to leave the Florence-based club in the summer.'
Resumen anotado por humanos
df_summarization.highlights[3]
'Fiorentina goalkeeper Neto has been linked with Liverpool and Arsenal .\nNeto joined Firoentina\xa0from Brazilian outfit Atletico Paranaense in 2011 .\nHe is also wanted by PSG and Spanish clubs, according to his agent .\nCLICK HERE for the latest Liverpool news .'
Artículo original
df_summarization.article[3]
"Liverpool target Neto is also wanted by PSG and clubs in Spain as Brendan Rodgers faces stiff competition to land the Fiorentina goalkeeper, according to the Brazilian's agent Stefano Castagna. The Reds were linked with a move for the 25-year-old, whose contract expires in June, earlier in the season when Simon Mignolet was dropped from the side. A January move for Neto never materialised but the former Atletico Paranaense keeper looks certain to leave the Florence-based club in the summer. Neto rushes from his goal as Juan Iturbe bears down on him during Fiorentina's clash with Roma in March . Neto is wanted by a number of top European clubs including Liverpool and PSG, according to his agent . It had been reported that Neto had a verbal agreement to join Serie A champions Juventus at the end of the season but his agent has revealed no decision about his future has been made yet. And Castagna claims Neto will have his pick of top European clubs when the transfer window re-opens in the summer, including Brendan Rodgers' side. 'There are many European clubs interested in Neto, such as for example Liverpool and Paris Saint-Germain,' Stefano Castagna is quoted as saying by Gazzetta TV. Firoentina goalkeeper Neto saves at the feet of Tottenham midfielder Nacer Chadli in the Europa League . 'In Spain too there are clubs at the very top level who are tracking him. Real Madrid? We'll see. 'We have not made a definitive decision, but in any case he will not accept another loan move elsewhere.' Neto, who represented Brazil at the London 2012 Olympics but has not featured for the senior side, was warned against joining a club as a No 2 by national coach Dunga. Neto joined Fiorentina from\xa0Atletico Paranaense in 2011 and established himself as No1 in the last two seasons."
Análisis de sentimientos¶
Para esta tarea se puede utilizar la librería pysentimiento.
Más información en: https://github.com/pysentimiento/pysentimiento
!pip install pysentimiento
Collecting pysentimiento Downloading pysentimiento-0.7.3-py3-none-any.whl.metadata (7.7 kB) Requirement already satisfied: accelerate>=0.27.2 in /usr/local/lib/python3.12/dist-packages (from pysentimiento) (1.12.0) Requirement already satisfied: datasets>=2.10.1 in /usr/local/lib/python3.12/dist-packages (from pysentimiento) (4.0.0) Collecting emoji>=1.6.1 (from pysentimiento) Downloading emoji-2.15.0-py3-none-any.whl.metadata (5.7 kB) Requirement already satisfied: spacy>=3.5.0 in /usr/local/lib/python3.12/dist-packages (from pysentimiento) (3.8.11) Requirement already satisfied: torch!=2.0.1,>=2.0.0 in /usr/local/lib/python3.12/dist-packages (from pysentimiento) (2.9.0+cu126) Requirement already satisfied: transformers>=4.13.0 in /usr/local/lib/python3.12/dist-packages (from pysentimiento) (4.57.3) Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.12/dist-packages (from accelerate>=0.27.2->pysentimiento) (2.0.2) Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.12/dist-packages (from accelerate>=0.27.2->pysentimiento) (25.0) Requirement already satisfied: psutil in /usr/local/lib/python3.12/dist-packages (from accelerate>=0.27.2->pysentimiento) (5.9.5) Requirement already satisfied: pyyaml in /usr/local/lib/python3.12/dist-packages (from accelerate>=0.27.2->pysentimiento) (6.0.3) Requirement already satisfied: huggingface_hub>=0.21.0 in /usr/local/lib/python3.12/dist-packages (from accelerate>=0.27.2->pysentimiento) (0.36.0) Requirement already satisfied: safetensors>=0.4.3 in /usr/local/lib/python3.12/dist-packages (from accelerate>=0.27.2->pysentimiento) (0.7.0) Requirement already satisfied: filelock in /usr/local/lib/python3.12/dist-packages (from datasets>=2.10.1->pysentimiento) (3.20.0) Requirement already satisfied: pyarrow>=15.0.0 in /usr/local/lib/python3.12/dist-packages (from datasets>=2.10.1->pysentimiento) (18.1.0) Requirement already satisfied: dill<0.3.9,>=0.3.0 in /usr/local/lib/python3.12/dist-packages (from datasets>=2.10.1->pysentimiento) (0.3.8) Requirement already satisfied: pandas in /usr/local/lib/python3.12/dist-packages (from datasets>=2.10.1->pysentimiento) (2.2.2) Requirement already satisfied: requests>=2.32.2 in /usr/local/lib/python3.12/dist-packages (from datasets>=2.10.1->pysentimiento) (2.32.4) Requirement already satisfied: tqdm>=4.66.3 in /usr/local/lib/python3.12/dist-packages (from datasets>=2.10.1->pysentimiento) (4.67.1) Requirement already satisfied: xxhash in /usr/local/lib/python3.12/dist-packages (from datasets>=2.10.1->pysentimiento) (3.6.0) Requirement already satisfied: multiprocess<0.70.17 in /usr/local/lib/python3.12/dist-packages (from datasets>=2.10.1->pysentimiento) (0.70.16) Requirement already satisfied: fsspec<=2025.3.0,>=2023.1.0 in /usr/local/lib/python3.12/dist-packages (from fsspec[http]<=2025.3.0,>=2023.1.0->datasets>=2.10.1->pysentimiento) (2025.3.0) Requirement already satisfied: spacy-legacy<3.1.0,>=3.0.11 in /usr/local/lib/python3.12/dist-packages (from spacy>=3.5.0->pysentimiento) (3.0.12) Requirement already satisfied: spacy-loggers<2.0.0,>=1.0.0 in /usr/local/lib/python3.12/dist-packages (from spacy>=3.5.0->pysentimiento) (1.0.5) Requirement already satisfied: murmurhash<1.1.0,>=0.28.0 in /usr/local/lib/python3.12/dist-packages (from spacy>=3.5.0->pysentimiento) (1.0.15) Requirement already satisfied: cymem<2.1.0,>=2.0.2 in /usr/local/lib/python3.12/dist-packages (from spacy>=3.5.0->pysentimiento) (2.0.13) Requirement already satisfied: preshed<3.1.0,>=3.0.2 in /usr/local/lib/python3.12/dist-packages (from spacy>=3.5.0->pysentimiento) (3.0.12) Requirement already satisfied: thinc<8.4.0,>=8.3.4 in /usr/local/lib/python3.12/dist-packages (from spacy>=3.5.0->pysentimiento) (8.3.10) Requirement already satisfied: wasabi<1.2.0,>=0.9.1 in /usr/local/lib/python3.12/dist-packages (from spacy>=3.5.0->pysentimiento) (1.1.3) Requirement already satisfied: srsly<3.0.0,>=2.4.3 in /usr/local/lib/python3.12/dist-packages (from spacy>=3.5.0->pysentimiento) (2.5.2) Requirement already satisfied: catalogue<2.1.0,>=2.0.6 in /usr/local/lib/python3.12/dist-packages (from spacy>=3.5.0->pysentimiento) (2.0.10) Requirement already satisfied: weasel<0.5.0,>=0.4.2 in /usr/local/lib/python3.12/dist-packages (from spacy>=3.5.0->pysentimiento) (0.4.3) Requirement already satisfied: typer-slim<1.0.0,>=0.3.0 in /usr/local/lib/python3.12/dist-packages (from spacy>=3.5.0->pysentimiento) (0.20.0) Requirement already satisfied: pydantic!=1.8,!=1.8.1,<3.0.0,>=1.7.4 in /usr/local/lib/python3.12/dist-packages (from spacy>=3.5.0->pysentimiento) (2.12.3) Requirement already satisfied: jinja2 in /usr/local/lib/python3.12/dist-packages (from spacy>=3.5.0->pysentimiento) (3.1.6) Requirement already satisfied: setuptools in /usr/local/lib/python3.12/dist-packages (from spacy>=3.5.0->pysentimiento) (75.2.0) Requirement already satisfied: typing-extensions>=4.10.0 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (4.15.0) Requirement already satisfied: sympy>=1.13.3 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (1.14.0) Requirement already satisfied: networkx>=2.5.1 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (3.6.1) Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (12.6.77) Requirement already satisfied: nvidia-cuda-runtime-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (12.6.77) Requirement already satisfied: nvidia-cuda-cupti-cu12==12.6.80 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (12.6.80) Requirement already satisfied: nvidia-cudnn-cu12==9.10.2.21 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (9.10.2.21) Requirement already satisfied: nvidia-cublas-cu12==12.6.4.1 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (12.6.4.1) Requirement already satisfied: nvidia-cufft-cu12==11.3.0.4 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (11.3.0.4) Requirement already satisfied: nvidia-curand-cu12==10.3.7.77 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (10.3.7.77) Requirement already satisfied: nvidia-cusolver-cu12==11.7.1.2 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (11.7.1.2) Requirement already satisfied: nvidia-cusparse-cu12==12.5.4.2 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (12.5.4.2) Requirement already satisfied: nvidia-cusparselt-cu12==0.7.1 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (0.7.1) Requirement already satisfied: nvidia-nccl-cu12==2.27.5 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (2.27.5) Requirement already satisfied: nvidia-nvshmem-cu12==3.3.20 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (3.3.20) Requirement already satisfied: nvidia-nvtx-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (12.6.77) Requirement already satisfied: nvidia-nvjitlink-cu12==12.6.85 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (12.6.85) Requirement already satisfied: nvidia-cufile-cu12==1.11.1.6 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (1.11.1.6) Requirement already satisfied: triton==3.5.0 in /usr/local/lib/python3.12/dist-packages (from torch!=2.0.1,>=2.0.0->pysentimiento) (3.5.0) Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.12/dist-packages (from transformers>=4.13.0->pysentimiento) (2025.11.3) Requirement already satisfied: tokenizers<=0.23.0,>=0.22.0 in /usr/local/lib/python3.12/dist-packages (from transformers>=4.13.0->pysentimiento) (0.22.1) Requirement already satisfied: aiohttp!=4.0.0a0,!=4.0.0a1 in /usr/local/lib/python3.12/dist-packages (from fsspec[http]<=2025.3.0,>=2023.1.0->datasets>=2.10.1->pysentimiento) (3.13.2) Requirement already satisfied: hf-xet<2.0.0,>=1.1.3 in /usr/local/lib/python3.12/dist-packages (from huggingface_hub>=0.21.0->accelerate>=0.27.2->pysentimiento) (1.2.0) Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.12/dist-packages (from pydantic!=1.8,!=1.8.1,<3.0.0,>=1.7.4->spacy>=3.5.0->pysentimiento) (0.7.0) Requirement already satisfied: pydantic-core==2.41.4 in /usr/local/lib/python3.12/dist-packages (from pydantic!=1.8,!=1.8.1,<3.0.0,>=1.7.4->spacy>=3.5.0->pysentimiento) (2.41.4) Requirement already satisfied: typing-inspection>=0.4.2 in /usr/local/lib/python3.12/dist-packages (from pydantic!=1.8,!=1.8.1,<3.0.0,>=1.7.4->spacy>=3.5.0->pysentimiento) (0.4.2) Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (from requests>=2.32.2->datasets>=2.10.1->pysentimiento) (3.4.4) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.12/dist-packages (from requests>=2.32.2->datasets>=2.10.1->pysentimiento) (3.11) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.12/dist-packages (from requests>=2.32.2->datasets>=2.10.1->pysentimiento) (2.5.0) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.12/dist-packages (from requests>=2.32.2->datasets>=2.10.1->pysentimiento) (2025.11.12) Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.12/dist-packages (from sympy>=1.13.3->torch!=2.0.1,>=2.0.0->pysentimiento) (1.3.0) Requirement already satisfied: blis<1.4.0,>=1.3.0 in /usr/local/lib/python3.12/dist-packages (from thinc<8.4.0,>=8.3.4->spacy>=3.5.0->pysentimiento) (1.3.3) Requirement already satisfied: confection<1.0.0,>=0.0.1 in /usr/local/lib/python3.12/dist-packages (from thinc<8.4.0,>=8.3.4->spacy>=3.5.0->pysentimiento) (0.1.5) Requirement already satisfied: click>=8.0.0 in /usr/local/lib/python3.12/dist-packages (from typer-slim<1.0.0,>=0.3.0->spacy>=3.5.0->pysentimiento) (8.3.1) Requirement already satisfied: cloudpathlib<1.0.0,>=0.7.0 in /usr/local/lib/python3.12/dist-packages (from weasel<0.5.0,>=0.4.2->spacy>=3.5.0->pysentimiento) (0.23.0) Requirement already satisfied: smart-open<8.0.0,>=5.2.1 in /usr/local/lib/python3.12/dist-packages (from weasel<0.5.0,>=0.4.2->spacy>=3.5.0->pysentimiento) (7.5.0) Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.12/dist-packages (from jinja2->spacy>=3.5.0->pysentimiento) (3.0.3) Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.12/dist-packages (from pandas->datasets>=2.10.1->pysentimiento) (2.9.0.post0) Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.12/dist-packages (from pandas->datasets>=2.10.1->pysentimiento) (2025.2) Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.12/dist-packages (from pandas->datasets>=2.10.1->pysentimiento) (2025.2) Requirement already satisfied: aiohappyeyeballs>=2.5.0 in /usr/local/lib/python3.12/dist-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.3.0,>=2023.1.0->datasets>=2.10.1->pysentimiento) (2.6.1) Requirement already satisfied: aiosignal>=1.4.0 in /usr/local/lib/python3.12/dist-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.3.0,>=2023.1.0->datasets>=2.10.1->pysentimiento) (1.4.0) Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.12/dist-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.3.0,>=2023.1.0->datasets>=2.10.1->pysentimiento) (25.4.0) Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.12/dist-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.3.0,>=2023.1.0->datasets>=2.10.1->pysentimiento) (1.8.0) Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.12/dist-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.3.0,>=2023.1.0->datasets>=2.10.1->pysentimiento) (6.7.0) Requirement already satisfied: propcache>=0.2.0 in /usr/local/lib/python3.12/dist-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.3.0,>=2023.1.0->datasets>=2.10.1->pysentimiento) (0.4.1) Requirement already satisfied: yarl<2.0,>=1.17.0 in /usr/local/lib/python3.12/dist-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.3.0,>=2023.1.0->datasets>=2.10.1->pysentimiento) (1.22.0) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.12/dist-packages (from python-dateutil>=2.8.2->pandas->datasets>=2.10.1->pysentimiento) (1.17.0) Requirement already satisfied: wrapt in /usr/local/lib/python3.12/dist-packages (from smart-open<8.0.0,>=5.2.1->weasel<0.5.0,>=0.4.2->spacy>=3.5.0->pysentimiento) (2.0.1) Downloading pysentimiento-0.7.3-py3-none-any.whl (39 kB) Downloading emoji-2.15.0-py3-none-any.whl (608 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 608.4/608.4 kB 14.2 MB/s eta 0:00:00 Installing collected packages: emoji, pysentimiento Successfully installed emoji-2.15.0 pysentimiento-0.7.3
A continuación, se instancia el modelo para la tarea de identificación de sentimiento para español.
from pysentimiento import create_analyzer
analyzer = create_analyzer(task="sentiment", lang="es")
Ejemplos
analyzer.predict("Qué gran jugador es Messi")
AnalyzerOutput(output=POS, probas={POS: 0.946, NEU: 0.037, NEG: 0.017}) analyzer.predict("Esto es pésimo")
AnalyzerOutput(output=NEG, probas={NEG: 0.887, NEU: 0.098, POS: 0.014}) analyzer.predict("Qué es esto?")
AnalyzerOutput(output=NEU, probas={NEU: 0.548, NEG: 0.412, POS: 0.041}) Traducción automática¶
Con el siguiente modelo se puede traducir entre español e inglés
from transformers import pipeline
estoen = pipeline('translation', model='Helsinki-NLP/opus-mt-es-en')#, device="cuda"
entoes = pipeline('translation', model='Helsinki-NLP/opus-mt-en-es')#, device="cuda"
/usr/local/lib/python3.12/dist-packages/transformers/models/marian/tokenization_marian.py:175: UserWarning: Recommended: pip install sacremoses.
warnings.warn("Recommended: pip install sacremoses.")
Ejemplo para español a inglés.
english = estoen("Me interesa aprender python para crear modelos de inteligencia artificial")[0]['translation_text']
english
"I'm interested in learning Python to create artificial intelligence models"
Ejemplo para inglés a español.
spanish = entoes("I'm interested in learning Python to create artificial intelligence models")[0]['translation_text']
spanish
'Estoy interesado en aprender Python para crear modelos de inteligencia artificial'
Representación de conocimiento¶
Se pueden crear representaciones vectoriales de palabras, oraciones o documentos.
En este ejemplo, se trabajará con un dataset que incluye comentarios sobre 8 temáticas distintas.
En primer lugar, se carga el dataset.
import pandas as pd
df_sbert = pd.read_csv(path + "Dataset 3 - SentenceBERT.csv")
df_sbert
| text | target | title | date | |
|---|---|---|---|---|
| 0 | I was wondering if anyone out there could enli... | 7 | rec.autos | 2024-04-20 05:19:53.092157 |
| 1 | I recently posted an article asking what kind ... | 7 | rec.autos | 2024-04-20 05:19:53.092157 |
| 2 | \nIt depends on your priorities. A lot of peo... | 7 | rec.autos | 2024-04-20 05:19:53.092157 |
| 3 | an excellent automatic can be found in the sub... | 7 | rec.autos | 2024-04-20 05:19:53.092157 |
| 4 | : Ford and his automobile. I need information... | 7 | rec.autos | 2024-04-20 05:19:53.092157 |
| ... | ... | ... | ... | ... |
| 4757 | Secrecy in Clipper Chip\n\nThe serial number o... | 11 | sci.crypt | 2024-04-20 05:19:53.092157 |
| 4758 | Hi !\n\nI am interested in the source of FEAL ... | 11 | sci.crypt | 2024-04-20 05:19:53.092157 |
| 4759 | The actual algorithm is classified, however, t... | 11 | sci.crypt | 2024-04-20 05:19:53.092157 |
| 4760 | \n\tThis appears to be generic calling upon th... | 11 | sci.crypt | 2024-04-20 05:19:53.092157 |
| 4761 | \nProbably keep quiet and take it, lest they g... | 11 | sci.crypt | 2024-04-20 05:19:53.092157 |
4762 rows × 4 columns
A continuación, se instalan las librerías que son necesarias.
!pip install -U sentence-transformers
from sentence_transformers import SentenceTransformer
Requirement already satisfied: sentence-transformers in /usr/local/lib/python3.12/dist-packages (5.1.2) Collecting sentence-transformers Downloading sentence_transformers-5.2.0-py3-none-any.whl.metadata (16 kB) Requirement already satisfied: transformers<6.0.0,>=4.41.0 in /usr/local/lib/python3.12/dist-packages (from sentence-transformers) (4.57.3) Requirement already satisfied: tqdm in /usr/local/lib/python3.12/dist-packages (from sentence-transformers) (4.67.1) Requirement already satisfied: torch>=1.11.0 in /usr/local/lib/python3.12/dist-packages (from sentence-transformers) (2.9.0+cu126) Requirement already satisfied: scikit-learn in /usr/local/lib/python3.12/dist-packages (from sentence-transformers) (1.6.1) Requirement already satisfied: scipy in /usr/local/lib/python3.12/dist-packages (from sentence-transformers) (1.16.3) Requirement already satisfied: huggingface-hub>=0.20.0 in /usr/local/lib/python3.12/dist-packages (from sentence-transformers) (0.36.0) Requirement already satisfied: typing_extensions>=4.5.0 in /usr/local/lib/python3.12/dist-packages (from sentence-transformers) (4.15.0) Requirement already satisfied: filelock in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.20.0->sentence-transformers) (3.20.0) Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.20.0->sentence-transformers) (2025.3.0) Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.20.0->sentence-transformers) (25.0) Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.20.0->sentence-transformers) (6.0.3) Requirement already satisfied: requests in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.20.0->sentence-transformers) (2.32.4) Requirement already satisfied: hf-xet<2.0.0,>=1.1.3 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.20.0->sentence-transformers) (1.2.0) Requirement already satisfied: setuptools in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (75.2.0) Requirement already satisfied: sympy>=1.13.3 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (1.14.0) Requirement already satisfied: networkx>=2.5.1 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (3.6.1) Requirement already satisfied: jinja2 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (3.1.6) Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (12.6.77) Requirement already satisfied: nvidia-cuda-runtime-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (12.6.77) Requirement already satisfied: nvidia-cuda-cupti-cu12==12.6.80 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (12.6.80) Requirement already satisfied: nvidia-cudnn-cu12==9.10.2.21 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (9.10.2.21) Requirement already satisfied: nvidia-cublas-cu12==12.6.4.1 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (12.6.4.1) Requirement already satisfied: nvidia-cufft-cu12==11.3.0.4 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (11.3.0.4) Requirement already satisfied: nvidia-curand-cu12==10.3.7.77 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (10.3.7.77) Requirement already satisfied: nvidia-cusolver-cu12==11.7.1.2 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (11.7.1.2) Requirement already satisfied: nvidia-cusparse-cu12==12.5.4.2 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (12.5.4.2) Requirement already satisfied: nvidia-cusparselt-cu12==0.7.1 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (0.7.1) Requirement already satisfied: nvidia-nccl-cu12==2.27.5 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (2.27.5) Requirement already satisfied: nvidia-nvshmem-cu12==3.3.20 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (3.3.20) Requirement already satisfied: nvidia-nvtx-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (12.6.77) Requirement already satisfied: nvidia-nvjitlink-cu12==12.6.85 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (12.6.85) Requirement already satisfied: nvidia-cufile-cu12==1.11.1.6 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (1.11.1.6) Requirement already satisfied: triton==3.5.0 in /usr/local/lib/python3.12/dist-packages (from torch>=1.11.0->sentence-transformers) (3.5.0) Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.12/dist-packages (from transformers<6.0.0,>=4.41.0->sentence-transformers) (2.0.2) Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.12/dist-packages (from transformers<6.0.0,>=4.41.0->sentence-transformers) (2025.11.3) Requirement already satisfied: tokenizers<=0.23.0,>=0.22.0 in /usr/local/lib/python3.12/dist-packages (from transformers<6.0.0,>=4.41.0->sentence-transformers) (0.22.1) Requirement already satisfied: safetensors>=0.4.3 in /usr/local/lib/python3.12/dist-packages (from transformers<6.0.0,>=4.41.0->sentence-transformers) (0.7.0) Requirement already satisfied: joblib>=1.2.0 in /usr/local/lib/python3.12/dist-packages (from scikit-learn->sentence-transformers) (1.5.2) Requirement already satisfied: threadpoolctl>=3.1.0 in /usr/local/lib/python3.12/dist-packages (from scikit-learn->sentence-transformers) (3.6.0) Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.12/dist-packages (from sympy>=1.13.3->torch>=1.11.0->sentence-transformers) (1.3.0) Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.12/dist-packages (from jinja2->torch>=1.11.0->sentence-transformers) (3.0.3) Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (from requests->huggingface-hub>=0.20.0->sentence-transformers) (3.4.4) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.12/dist-packages (from requests->huggingface-hub>=0.20.0->sentence-transformers) (3.11) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.12/dist-packages (from requests->huggingface-hub>=0.20.0->sentence-transformers) (2.5.0) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.12/dist-packages (from requests->huggingface-hub>=0.20.0->sentence-transformers) (2025.11.12) Downloading sentence_transformers-5.2.0-py3-none-any.whl (493 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 493.7/493.7 kB 11.1 MB/s eta 0:00:00 Installing collected packages: sentence-transformers Attempting uninstall: sentence-transformers Found existing installation: sentence-transformers 5.1.2 Uninstalling sentence-transformers-5.1.2: Successfully uninstalled sentence-transformers-5.1.2 Successfully installed sentence-transformers-5.2.0
sentence_model = SentenceTransformer("bert-base-uncased") #El modelo BERT es una buena alternativa para trabajar en inglés.
WARNING:sentence_transformers.SentenceTransformer:No sentence-transformers model found with name bert-base-uncased. Creating a new one with mean pooling.
Se crea una lista con la columna text del dataset.
data = df_sbert["text"].astype(str).values.tolist()
data[0]
'I was wondering if anyone out there could enlighten me on this car I saw\nthe other day. It was a 2-door sports car, looked to be from the late 60s/\nearly 70s. It was called a Bricklin. The doors were really small. In addition,\nthe front bumper was separate from the rest of the body. This is \nall I know. If anyone can tellme a model name, engine specs, years\nof production, where this car is made, history, or whatever info you\nhave on this funky looking car, please e-mail.'
A continuación, se aplica el modelo para construir representaciones numéricas para cada frase.
#Si se utiliza GPU demora 1 minuto.
document_embeddings = sentence_model.encode(data, show_progress_bar=True)
Luego, se crea un dataframe con las presentaciones vectoriales de cada frase. Cada oración está representada por un vector de dimensión 768.
df_embedding_BERT = pd.DataFrame(document_embeddings)
df_embedding_BERT
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ... | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | -0.089775 | 0.013652 | 0.342424 | -0.029599 | 0.454611 | -0.155420 | 0.028683 | 0.677552 | 0.054108 | -0.056500 | ... | -0.201859 | -0.205297 | -0.024076 | -0.367480 | -0.179610 | -0.189014 | -0.010652 | -0.237815 | 0.097011 | -0.165627 |
| 1 | -0.129728 | 0.005115 | 0.654194 | -0.144817 | 0.432281 | -0.087327 | -0.049177 | 0.336230 | -0.070653 | -0.023974 | ... | -0.149205 | -0.078494 | 0.057314 | -0.424690 | -0.149908 | -0.230418 | -0.335527 | -0.137071 | -0.014233 | -0.140475 |
| 2 | 0.023834 | 0.067769 | 0.204187 | 0.062082 | 0.218925 | 0.072233 | 0.076897 | 0.612629 | 0.231372 | -0.226085 | ... | -0.374223 | -0.237243 | 0.032697 | -0.252716 | 0.097724 | -0.258354 | -0.008340 | -0.236641 | -0.093745 | -0.102768 |
| 3 | -0.368126 | -0.050276 | 0.384580 | -0.023271 | 0.238612 | 0.016748 | 0.161599 | 0.391614 | 0.187594 | -0.180459 | ... | -0.085896 | -0.236118 | -0.018853 | -0.278446 | -0.001339 | -0.128894 | 0.084995 | -0.207464 | 0.042922 | 0.077133 |
| 4 | -0.124665 | 0.285257 | 0.522662 | 0.029893 | 0.128232 | -0.094822 | 0.178583 | 0.625662 | -0.226715 | -0.113549 | ... | -0.096788 | -0.047858 | -0.019114 | -0.242549 | -0.114431 | -0.180721 | -0.013937 | -0.035261 | 0.236439 | -0.040709 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 4757 | -0.134307 | -0.024430 | 0.204336 | -0.020176 | 0.321668 | -0.077226 | 0.177534 | 0.139804 | 0.080416 | 0.053972 | ... | -0.111625 | -0.084802 | 0.034862 | -0.235520 | -0.188523 | 0.202765 | -0.036468 | -0.101199 | -0.088310 | 0.614945 |
| 4758 | 0.074847 | -0.069484 | 0.295646 | -0.109075 | 0.378729 | -0.353335 | 0.107715 | 0.469267 | -0.043433 | -0.132598 | ... | -0.084600 | -0.250242 | 0.111643 | -0.449706 | -0.131604 | 0.281609 | -0.072136 | -0.218927 | 0.111901 | 0.260192 |
| 4759 | 0.067195 | 0.100350 | 0.375749 | -0.027123 | 0.367559 | -0.249963 | 0.028483 | 0.231922 | 0.041288 | -0.070385 | ... | -0.242001 | -0.362328 | 0.031719 | -0.354739 | -0.290890 | 0.316412 | 0.212746 | -0.128654 | 0.203274 | 0.285137 |
| 4760 | 0.153559 | 0.170168 | 0.264868 | -0.013975 | 0.261926 | -0.105343 | 0.135711 | 0.454433 | -0.111993 | -0.034889 | ... | -0.356435 | -0.039878 | 0.050524 | -0.337748 | -0.151264 | -0.081425 | 0.088432 | -0.033736 | 0.207375 | 0.170841 |
| 4761 | 0.459215 | 0.184233 | 0.281273 | -0.202304 | -0.478573 | 0.073093 | 0.303992 | 0.597255 | -0.324255 | 0.025164 | ... | -0.540587 | 0.017862 | -0.105297 | -0.175923 | 0.004883 | -0.032813 | -0.260528 | 0.226979 | -0.021785 | -0.142210 |
4762 rows × 768 columns
En la siguiente celda se incluye la categoría a la que pertenece cada frase.
df_embedding_BERT["title"] = df_sbert["title"]
df_embedding_BERT.head(2)
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ... | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | title | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | -0.089775 | 0.013652 | 0.342424 | -0.029599 | 0.454611 | -0.155420 | 0.028683 | 0.677552 | 0.054108 | -0.056500 | ... | -0.205297 | -0.024076 | -0.36748 | -0.179610 | -0.189014 | -0.010652 | -0.237815 | 0.097011 | -0.165627 | rec.autos |
| 1 | -0.129728 | 0.005115 | 0.654194 | -0.144817 | 0.432281 | -0.087327 | -0.049177 | 0.336230 | -0.070653 | -0.023974 | ... | -0.078494 | 0.057314 | -0.42469 | -0.149908 | -0.230418 | -0.335527 | -0.137071 | -0.014233 | -0.140475 | rec.autos |
2 rows × 769 columns
Para crear una representación visual que permita reconocer la cercanía entre documentos, se reducirá dimensionalidad mediante las técnicas TSNE y UMAP.
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import seaborn as sns
X = df_embedding_BERT.iloc[:,:768]
target= df_embedding_BERT["title"]
#Este método toma 1 minuto
tsne = TSNE(n_components=2, perplexity=10, max_iter=250)
X_tsne = tsne.fit_transform(X)
plt.figure(figsize=(10,10))
labels = df_embedding_BERT["title"]
df = pd.DataFrame(X_tsne)
df["labels"] = labels
sns.scatterplot(data=df, x=0, y=1, hue="labels",palette='hls')
plt.title("Representación con T-SNE")
plt.xlim(-4,2) #Ajustar dependiendo del gráfico obtenido
plt.ylim(-2.5,1.5)
plt.show()
Creando la representación con UMAP
#Demora 2 minutos
import umap
umap_ = umap.UMAP(n_components=2, n_neighbors=5, min_dist=0.3, metric='euclidean')
X_umap = umap_.fit_transform(X)
plt.figure(figsize=(10,10))
labels = df_embedding_BERT["title"]
df = pd.DataFrame(X_umap)
df["labels"] = labels
sns.scatterplot(data=df, x=0, y=1, hue="labels",palette='hls')
plt.title("Representación con UMAP")
# plt.xlim(-2,4) #Ajustar dependiendo del gráfico obtenido
# plt.ylim(-1,2.5)
plt.show()
Generación de texto¶
Para este caso, se trabajará con el modelo GPT 3.5 turbo. Para su correcto funcionamiento deben contar con una api key.
A continuación, se instala la librería de openai.
!pip install openai
Requirement already satisfied: openai in /usr/local/lib/python3.12/dist-packages (2.9.0) Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.12/dist-packages (from openai) (4.12.0) Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.12/dist-packages (from openai) (1.9.0) Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.12/dist-packages (from openai) (0.28.1) Requirement already satisfied: jiter<1,>=0.10.0 in /usr/local/lib/python3.12/dist-packages (from openai) (0.12.0) Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.12/dist-packages (from openai) (2.12.3) Requirement already satisfied: sniffio in /usr/local/lib/python3.12/dist-packages (from openai) (1.3.1) Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.12/dist-packages (from openai) (4.67.1) Requirement already satisfied: typing-extensions<5,>=4.11 in /usr/local/lib/python3.12/dist-packages (from openai) (4.15.0) Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.12/dist-packages (from anyio<5,>=3.5.0->openai) (3.11) Requirement already satisfied: certifi in /usr/local/lib/python3.12/dist-packages (from httpx<1,>=0.23.0->openai) (2025.11.12) Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.12/dist-packages (from httpx<1,>=0.23.0->openai) (1.0.9) Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.12/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.16.0) Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.12/dist-packages (from pydantic<3,>=1.9.0->openai) (0.7.0) Requirement already satisfied: pydantic-core==2.41.4 in /usr/local/lib/python3.12/dist-packages (from pydantic<3,>=1.9.0->openai) (2.41.4) Requirement already satisfied: typing-inspection>=0.4.2 in /usr/local/lib/python3.12/dist-packages (from pydantic<3,>=1.9.0->openai) (0.4.2)
import openai
import time
Para no entregar su api key por error, se recomienda crear un archivo .py con la información y cargarlo en cada ejecución.
from parameter_api import API_KEY_personal
openai.api_key = API_KEY_personal #No entregar API KEY. Es mejor crear un archivo .py con esta información y cargarlo cada vez que se use.
def historia(objeto_1, objeto_2, temperature=0.7):
response = openai.chat.completions.create(
model = "gpt-3.5-turbo",
messages = [{"role": "user",
"content": f'''Crea una diálogo entre los siguientes dos personajes "{objeto_1}", "{objeto_2}". Cada uno debe interactuar tres veces.
La salida debe incluir el nombre del personaje con el texto señalado'''}],
temperature = temperature
)
answer = response.choices[0].message.content
return answer
salida_1 = historia("Lebron James","Michael Jackson")
print(salida_1)
Lebron James: Hola Michael, es un honor conocerte. Eres una leyenda en la música. Michael Jackson: Gracias Lebron, el honor es mío. Eres un verdadero icono en el baloncesto. Lebron James: ¿Cómo te inspiras para crear música tan increíble? Michael Jackson: La inspiración viene de todas partes, Lebron. Me gusta observar el mundo a mi alrededor y plasmarlo en mis canciones. Lebron James: Eso es fascinante. A mí me inspira la pasión por el juego y la dedicación al entrenamiento. ¿Qué te motiva a seguir creando música? Michael Jackson: Mi mayor motivación es el amor por la música y el deseo de llevar alegría a las personas a través de mis canciones. Esa es mi verdadera pasión, al igual que tu pasión por el baloncesto, Lebron. Lebron James: ¡Increíble! Gracias por compartir tus pensamientos conmigo, Michael. Eres realmente un artista excepcional.
Generación de texto con RAG¶
Para la creación de este sistema se trabajará con el framework LangChain.
Más información en: https://www.langchain.com/
!pip install langchain
Requirement already satisfied: langchain in /usr/local/lib/python3.12/dist-packages (1.1.2) Requirement already satisfied: langchain-core<2.0.0,>=1.1.0 in /usr/local/lib/python3.12/dist-packages (from langchain) (1.1.2) Requirement already satisfied: langgraph<1.1.0,>=1.0.2 in /usr/local/lib/python3.12/dist-packages (from langchain) (1.0.4) Requirement already satisfied: pydantic<3.0.0,>=2.7.4 in /usr/local/lib/python3.12/dist-packages (from langchain) (2.12.3) Requirement already satisfied: jsonpatch<2.0.0,>=1.33.0 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.1.0->langchain) (1.33) Requirement already satisfied: langsmith<1.0.0,>=0.3.45 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.1.0->langchain) (0.4.56) Requirement already satisfied: packaging<26.0.0,>=23.2.0 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.1.0->langchain) (25.0) Requirement already satisfied: pyyaml<7.0.0,>=5.3.0 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.1.0->langchain) (6.0.3) Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.1.0->langchain) (9.1.2) Requirement already satisfied: typing-extensions<5.0.0,>=4.7.0 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.1.0->langchain) (4.15.0) Requirement already satisfied: uuid-utils<1.0,>=0.12.0 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.1.0->langchain) (0.12.0) Requirement already satisfied: langgraph-checkpoint<4.0.0,>=2.1.0 in /usr/local/lib/python3.12/dist-packages (from langgraph<1.1.0,>=1.0.2->langchain) (3.0.1) Requirement already satisfied: langgraph-prebuilt<1.1.0,>=1.0.2 in /usr/local/lib/python3.12/dist-packages (from langgraph<1.1.0,>=1.0.2->langchain) (1.0.5) Requirement already satisfied: langgraph-sdk<0.3.0,>=0.2.2 in /usr/local/lib/python3.12/dist-packages (from langgraph<1.1.0,>=1.0.2->langchain) (0.2.14) Requirement already satisfied: xxhash>=3.5.0 in /usr/local/lib/python3.12/dist-packages (from langgraph<1.1.0,>=1.0.2->langchain) (3.6.0) Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.12/dist-packages (from pydantic<3.0.0,>=2.7.4->langchain) (0.7.0) Requirement already satisfied: pydantic-core==2.41.4 in /usr/local/lib/python3.12/dist-packages (from pydantic<3.0.0,>=2.7.4->langchain) (2.41.4) Requirement already satisfied: typing-inspection>=0.4.2 in /usr/local/lib/python3.12/dist-packages (from pydantic<3.0.0,>=2.7.4->langchain) (0.4.2) Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.12/dist-packages (from jsonpatch<2.0.0,>=1.33.0->langchain-core<2.0.0,>=1.1.0->langchain) (3.0.0) Requirement already satisfied: ormsgpack>=1.12.0 in /usr/local/lib/python3.12/dist-packages (from langgraph-checkpoint<4.0.0,>=2.1.0->langgraph<1.1.0,>=1.0.2->langchain) (1.12.0) Requirement already satisfied: httpx>=0.25.2 in /usr/local/lib/python3.12/dist-packages (from langgraph-sdk<0.3.0,>=0.2.2->langgraph<1.1.0,>=1.0.2->langchain) (0.28.1) Requirement already satisfied: orjson>=3.10.1 in /usr/local/lib/python3.12/dist-packages (from langgraph-sdk<0.3.0,>=0.2.2->langgraph<1.1.0,>=1.0.2->langchain) (3.11.5) Requirement already satisfied: requests-toolbelt>=1.0.0 in /usr/local/lib/python3.12/dist-packages (from langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=1.1.0->langchain) (1.0.0) Requirement already satisfied: requests>=2.0.0 in /usr/local/lib/python3.12/dist-packages (from langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=1.1.0->langchain) (2.32.4) Requirement already satisfied: zstandard>=0.23.0 in /usr/local/lib/python3.12/dist-packages (from langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=1.1.0->langchain) (0.25.0) Requirement already satisfied: anyio in /usr/local/lib/python3.12/dist-packages (from httpx>=0.25.2->langgraph-sdk<0.3.0,>=0.2.2->langgraph<1.1.0,>=1.0.2->langchain) (4.12.0) Requirement already satisfied: certifi in /usr/local/lib/python3.12/dist-packages (from httpx>=0.25.2->langgraph-sdk<0.3.0,>=0.2.2->langgraph<1.1.0,>=1.0.2->langchain) (2025.11.12) Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.12/dist-packages (from httpx>=0.25.2->langgraph-sdk<0.3.0,>=0.2.2->langgraph<1.1.0,>=1.0.2->langchain) (1.0.9) Requirement already satisfied: idna in /usr/local/lib/python3.12/dist-packages (from httpx>=0.25.2->langgraph-sdk<0.3.0,>=0.2.2->langgraph<1.1.0,>=1.0.2->langchain) (3.11) Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.12/dist-packages (from httpcore==1.*->httpx>=0.25.2->langgraph-sdk<0.3.0,>=0.2.2->langgraph<1.1.0,>=1.0.2->langchain) (0.16.0) Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (from requests>=2.0.0->langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=1.1.0->langchain) (3.4.4) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.12/dist-packages (from requests>=2.0.0->langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=1.1.0->langchain) (2.5.0)
!pip install langchain-community wikipedia
Collecting langchain-community Downloading langchain_community-0.4.1-py3-none-any.whl.metadata (3.0 kB) Collecting wikipedia Downloading wikipedia-1.4.0.tar.gz (27 kB) Preparing metadata (setup.py) ... done Requirement already satisfied: langchain-core<2.0.0,>=1.0.1 in /usr/local/lib/python3.12/dist-packages (from langchain-community) (1.1.2) Collecting langchain-classic<2.0.0,>=1.0.0 (from langchain-community) Downloading langchain_classic-1.0.0-py3-none-any.whl.metadata (3.9 kB) Requirement already satisfied: SQLAlchemy<3.0.0,>=1.4.0 in /usr/local/lib/python3.12/dist-packages (from langchain-community) (2.0.44) Collecting requests<3.0.0,>=2.32.5 (from langchain-community) Downloading requests-2.32.5-py3-none-any.whl.metadata (4.9 kB) Requirement already satisfied: PyYAML<7.0.0,>=5.3.0 in /usr/local/lib/python3.12/dist-packages (from langchain-community) (6.0.3) Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /usr/local/lib/python3.12/dist-packages (from langchain-community) (3.13.2) Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in /usr/local/lib/python3.12/dist-packages (from langchain-community) (9.1.2) Collecting dataclasses-json<0.7.0,>=0.6.7 (from langchain-community) Downloading dataclasses_json-0.6.7-py3-none-any.whl.metadata (25 kB) Requirement already satisfied: pydantic-settings<3.0.0,>=2.10.1 in /usr/local/lib/python3.12/dist-packages (from langchain-community) (2.12.0) Requirement already satisfied: langsmith<1.0.0,>=0.1.125 in /usr/local/lib/python3.12/dist-packages (from langchain-community) (0.4.56) Requirement already satisfied: httpx-sse<1.0.0,>=0.4.0 in /usr/local/lib/python3.12/dist-packages (from langchain-community) (0.4.3) Requirement already satisfied: numpy>=1.26.2 in /usr/local/lib/python3.12/dist-packages (from langchain-community) (2.0.2) Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.12/dist-packages (from wikipedia) (4.13.5) Requirement already satisfied: aiohappyeyeballs>=2.5.0 in /usr/local/lib/python3.12/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (2.6.1) Requirement already satisfied: aiosignal>=1.4.0 in /usr/local/lib/python3.12/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.4.0) Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.12/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (25.4.0) Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.12/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.8.0) Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.12/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (6.7.0) Requirement already satisfied: propcache>=0.2.0 in /usr/local/lib/python3.12/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (0.4.1) Requirement already satisfied: yarl<2.0,>=1.17.0 in /usr/local/lib/python3.12/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.22.0) Collecting marshmallow<4.0.0,>=3.18.0 (from dataclasses-json<0.7.0,>=0.6.7->langchain-community) Downloading marshmallow-3.26.1-py3-none-any.whl.metadata (7.3 kB) Collecting typing-inspect<1,>=0.4.0 (from dataclasses-json<0.7.0,>=0.6.7->langchain-community) Downloading typing_inspect-0.9.0-py3-none-any.whl.metadata (1.5 kB) Collecting langchain-text-splitters<2.0.0,>=1.0.0 (from langchain-classic<2.0.0,>=1.0.0->langchain-community) Downloading langchain_text_splitters-1.0.0-py3-none-any.whl.metadata (2.6 kB) Requirement already satisfied: pydantic<3.0.0,>=2.7.4 in /usr/local/lib/python3.12/dist-packages (from langchain-classic<2.0.0,>=1.0.0->langchain-community) (2.12.3) Requirement already satisfied: jsonpatch<2.0.0,>=1.33.0 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.0.1->langchain-community) (1.33) Requirement already satisfied: packaging<26.0.0,>=23.2.0 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.0.1->langchain-community) (25.0) Requirement already satisfied: typing-extensions<5.0.0,>=4.7.0 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.0.1->langchain-community) (4.15.0) Requirement already satisfied: uuid-utils<1.0,>=0.12.0 in /usr/local/lib/python3.12/dist-packages (from langchain-core<2.0.0,>=1.0.1->langchain-community) (0.12.0) Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.12/dist-packages (from langsmith<1.0.0,>=0.1.125->langchain-community) (0.28.1) Requirement already satisfied: orjson>=3.9.14 in /usr/local/lib/python3.12/dist-packages (from langsmith<1.0.0,>=0.1.125->langchain-community) (3.11.5) Requirement already satisfied: requests-toolbelt>=1.0.0 in /usr/local/lib/python3.12/dist-packages (from langsmith<1.0.0,>=0.1.125->langchain-community) (1.0.0) Requirement already satisfied: zstandard>=0.23.0 in /usr/local/lib/python3.12/dist-packages (from langsmith<1.0.0,>=0.1.125->langchain-community) (0.25.0) Requirement already satisfied: python-dotenv>=0.21.0 in /usr/local/lib/python3.12/dist-packages (from pydantic-settings<3.0.0,>=2.10.1->langchain-community) (1.2.1) Requirement already satisfied: typing-inspection>=0.4.0 in /usr/local/lib/python3.12/dist-packages (from pydantic-settings<3.0.0,>=2.10.1->langchain-community) (0.4.2) Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (from requests<3.0.0,>=2.32.5->langchain-community) (3.4.4) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.12/dist-packages (from requests<3.0.0,>=2.32.5->langchain-community) (3.11) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.12/dist-packages (from requests<3.0.0,>=2.32.5->langchain-community) (2.5.0) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.12/dist-packages (from requests<3.0.0,>=2.32.5->langchain-community) (2025.11.12) Requirement already satisfied: greenlet>=1 in /usr/local/lib/python3.12/dist-packages (from SQLAlchemy<3.0.0,>=1.4.0->langchain-community) (3.3.0) Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.12/dist-packages (from beautifulsoup4->wikipedia) (2.8) Requirement already satisfied: anyio in /usr/local/lib/python3.12/dist-packages (from httpx<1,>=0.23.0->langsmith<1.0.0,>=0.1.125->langchain-community) (4.12.0) Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.12/dist-packages (from httpx<1,>=0.23.0->langsmith<1.0.0,>=0.1.125->langchain-community) (1.0.9) Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.12/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith<1.0.0,>=0.1.125->langchain-community) (0.16.0) Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.12/dist-packages (from jsonpatch<2.0.0,>=1.33.0->langchain-core<2.0.0,>=1.0.1->langchain-community) (3.0.0) Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.12/dist-packages (from pydantic<3.0.0,>=2.7.4->langchain-classic<2.0.0,>=1.0.0->langchain-community) (0.7.0) Requirement already satisfied: pydantic-core==2.41.4 in /usr/local/lib/python3.12/dist-packages (from pydantic<3.0.0,>=2.7.4->langchain-classic<2.0.0,>=1.0.0->langchain-community) (2.41.4) Collecting mypy-extensions>=0.3.0 (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7.0,>=0.6.7->langchain-community) Downloading mypy_extensions-1.1.0-py3-none-any.whl.metadata (1.1 kB) Downloading langchain_community-0.4.1-py3-none-any.whl (2.5 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.5/2.5 MB 37.1 MB/s eta 0:00:00 Downloading dataclasses_json-0.6.7-py3-none-any.whl (28 kB) Downloading langchain_classic-1.0.0-py3-none-any.whl (1.0 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.0/1.0 MB 45.4 MB/s eta 0:00:00 Downloading requests-2.32.5-py3-none-any.whl (64 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 64.7/64.7 kB 4.0 MB/s eta 0:00:00 Downloading langchain_text_splitters-1.0.0-py3-none-any.whl (33 kB) Downloading marshmallow-3.26.1-py3-none-any.whl (50 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 50.9/50.9 kB 4.5 MB/s eta 0:00:00 Downloading typing_inspect-0.9.0-py3-none-any.whl (8.8 kB) Downloading mypy_extensions-1.1.0-py3-none-any.whl (5.0 kB) Building wheels for collected packages: wikipedia Building wheel for wikipedia (setup.py) ... done Created wheel for wikipedia: filename=wikipedia-1.4.0-py3-none-any.whl size=11678 sha256=b14ac4643ce9ab926bdb4baec6bdb61a287ae470fa8f0008aa65e87bdb0ac80c Stored in directory: /root/.cache/pip/wheels/63/47/7c/a9688349aa74d228ce0a9023229c6c0ac52ca2a40fe87679b8 Successfully built wikipedia Installing collected packages: requests, mypy-extensions, marshmallow, wikipedia, typing-inspect, dataclasses-json, langchain-text-splitters, langchain-classic, langchain-community Attempting uninstall: requests Found existing installation: requests 2.32.4 Uninstalling requests-2.32.4: Successfully uninstalled requests-2.32.4 ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. google-colab 1.0.0 requires requests==2.32.4, but you have requests 2.32.5 which is incompatible. Successfully installed dataclasses-json-0.6.7 langchain-classic-1.0.0 langchain-community-0.4.1 langchain-text-splitters-1.0.0 marshmallow-3.26.1 mypy-extensions-1.1.0 requests-2.32.5 typing-inspect-0.9.0 wikipedia-1.4.0
!pip install -qU langchain-openai
#ChatOpenAI
from langchain_openai import ChatOpenAI
from langchain_openai import OpenAIEmbeddings
#Wikipedia
from langchain_classic.retrievers import WikipediaRetriever
Pregunta directa a GPT 3.5-turbo
question = "Who are the current NBA champions?"
response = openai.chat.completions.create(model = "gpt-3.5-turbo",
messages = [{"role": "user",
"content": f'''{question}'''}],
temperature = 0.5)
answer = response.choices[0].message.content
print(answer)
The current NBA champions are the Milwaukee Bucks, who won the 2021 NBA Finals.
Como se puede observar, el modelo entregó información correcta pero de la temporada 2020-2021. Este hecho era esperable ya que el modelo GPT 3.5 no está actualizado al 2024.
Si se prueba con GPT-4o mini, se obtiene lo siguiente:
response = openai.chat.completions.create(model = "gpt-4o-mini",
messages = [{"role": "user",
"content": f'''{question}'''}],
temperature = 0.5)
answer = response.choices[0].message.content
print(answer)
As of October 2023, the current NBA champions are the Denver Nuggets. They won the championship in June 2023, defeating the Miami Heat in the NBA Finals.
A pesar de obtener una respuesta más actualizada, sigue siendo incorrecta.
Para resolver lo anterior, se puede trabajar con el enfoque Retrieval-augmented generation (RAG). En la siguiente imagen se presenta un esquema con su forma de operar.
En la siguiente celda se instancia el objeto retriever para obtener información desde wikipedia.
retriever_en = WikipediaRetriever(lang="en",
load_max_docs=5, #default son 100
load_all_available_meta=False)
Luego, se realizará una búsqueda en wikipedia usando NBA champions
contexto_en = retriever_en.invoke("NBA champions")
A continuación, se presenta el contexto obtenido. Esta respuesta está en inglés dado que el hiperparámetro entregado a WikipediaRetriever indicó este requerimiento.
contexto_en
[Document(metadata={'title': 'List of NBA champions', 'summary': "The NBA Finals is the annual championship series for the National Basketball Association (NBA) held at the conclusion of its postseason. All NBA Finals have been played in a best-of-seven format, and are contested between the winners of the Eastern Conference and the Western Conference (prior to 1971 it was played between division playoff winners), except in 1950 when the Eastern Division champion faced the winner between the Western and Central Division champions. From 1946 through 1949, when the league was known as the Basketball Association of America (BAA), the playoffs were a three-stage tournament where the two semifinal winners played each other in the finals. The winning team of the series receives the Larry O'Brien Championship Trophy, which has been awarded since 1977 (between 1947 and 1976 the winning team received the Walter A. Brown Trophy).", 'source': 'https://en.wikipedia.org/wiki/List_of_NBA_champions'}, page_content='The NBA Finals is the annual championship series for the National Basketball Association (NBA) held at the conclusion of its postseason. All NBA Finals have been played in a best-of-seven format, and are contested between the winners of the Eastern Conference and the Western Conference (prior to 1971 it was played between division playoff winners), except in 1950 when the Eastern Division champion faced the winner between the Western and Central Division champions. From 1946 through 1949, when the league was known as the Basketball Association of America (BAA), the playoffs were a three-stage tournament where the two semifinal winners played each other in the finals. The winning team of the series receives the Larry O\'Brien Championship Trophy, which has been awarded since 1977 (between 1947 and 1976 the winning team received the Walter A. Brown Trophy).\n\n\n== Champions ==\n\nThe most recent champions are the Oklahoma City Thunder, who won their first championship since relocating from Seattle in 2008, and their first since 1979 as the Seattle SuperSonics. The Boston Celtics have won the most championships of any team in the league at 18. As of 2025, the Eastern champions have a 41–38 advantage in NBA titles over the Western champions. The 1949–50 Minneapolis Lakers, who won the NBA Finals, are counted in the Eastern versus Western champions record as they played that sole season in the Central Division before returning to the Western Division.\n\nThe first parentheses in the Western champions and Eastern champions columns indicate the teams\' playoff seed. The second parentheses indicate the number of times that teams have appeared in an NBA Finals as well as each respective team\'s NBA Finals record to date.\n\n\n== Records by franchise ==\n\n\n== Consecutive championships ==\nEight consecutive\n\nBoston Celtics (1959–1966)\nThree consecutive\n\nMinneapolis Lakers (1952–1954)\nChicago Bulls (1991–1993)\nChicago Bulls (1996–1998)\nLos Angeles Lakers (2000–2002)\nTwo consecutive\n\nMinneapolis Lakers (1949, 1950)\nBoston Celtics (1968, 1969)\nLos Angeles Lakers (1987, 1988)\nDetroit Pistons (1989, 1990)\nHouston Rockets (1994, 1995)\nLos Angeles Lakers (2009, 2010)\nMiami Heat (2012, 2013)\nGolden State Warriors (2017, 2018)\n\n\n== Frequent matchups ==\n\n\n== See also ==\n\nNBA Finals Most Valuable Player Award\nList of NBA players with most championships\nList of NBA championship head coaches\nList of NBA franchise post-season droughts\nList of NBA franchise post-season streaks\nList of NBA longest winning streaks\nList of ABA champions\nList of National Basketball League (United States) champions\nList of NBA G League champions\nWNBA Finals\n\n\n== Notes ==\n\n\n== References ==\n\n\n== External links ==\n\n"NBA Season Recaps: A look back at every season since 1946". NBA. August 12, 2024. Archived from the original on August 15, 2024. Retrieved May 31, 2025.'),
Document(metadata={'title': 'National Basketball Association', 'summary': "The National Basketball Association (NBA) is a professional basketball league in North America composed of 30 teams (29 in the United States and 1 in Canada). The NBA is one of the major professional sports leagues in the United States and Canada and is considered the premier professional basketball league in the world. The league is headquartered in Midtown Manhattan.\nThe NBA was created on August 3, 1949, with the merger of the Basketball Association of America (BAA) and the National Basketball League (NBL). The league later adopted the BAA's history and considers its founding on June 6, 1946, as its own. In 1976, the NBA and the American Basketball Association (ABA) merged, adding four franchises to the NBA. The NBA's regular season runs from October to April, with each team playing 82 games. The league's playoff tournament extends into June, culminating with the NBA Finals championship series.\nThe NBA is an active member of USA Basketball (USAB), which is recognized by the International Basketball Federation (FIBA) as the governing body for basketball in the United States. The NBA is the second-wealthiest professional sports league in the world by revenue after the National Football League (NFL). As of 2020, NBA players are the world's highest paid athletes by average annual salary per player.\nThe Boston Celtics have the most NBA championships with 18, most recently winning in 2024. The Oklahoma City Thunder are the reigning league champions, having defeated the Indiana Pacers in the 2025 NBA Finals for their first title since moving to Oklahoma City in 2008, and their second ever, having won in 1979 as the Seattle SuperSonics.", 'source': 'https://en.wikipedia.org/wiki/National_Basketball_Association'}, page_content="The National Basketball Association (NBA) is a professional basketball league in North America composed of 30 teams (29 in the United States and 1 in Canada). The NBA is one of the major professional sports leagues in the United States and Canada and is considered the premier professional basketball league in the world. The league is headquartered in Midtown Manhattan.\nThe NBA was created on August 3, 1949, with the merger of the Basketball Association of America (BAA) and the National Basketball League (NBL). The league later adopted the BAA's history and considers its founding on June 6, 1946, as its own. In 1976, the NBA and the American Basketball Association (ABA) merged, adding four franchises to the NBA. The NBA's regular season runs from October to April, with each team playing 82 games. The league's playoff tournament extends into June, culminating with the NBA Finals championship series.\nThe NBA is an active member of USA Basketball (USAB), which is recognized by the International Basketball Federation (FIBA) as the governing body for basketball in the United States. The NBA is the second-wealthiest professional sports league in the world by revenue after the National Football League (NFL). As of 2020, NBA players are the world's highest paid athletes by average annual salary per player.\nThe Boston Celtics have the most NBA championships with 18, most recently winning in 2024. The Oklahoma City Thunder are the reigning league champions, having defeated the Indiana Pacers in the 2025 NBA Finals for their first title since moving to Oklahoma City in 2008, and their second ever, having won in 1979 as the Seattle SuperSonics.\n\n\n== History ==\n\n\n=== Creation and BAA–NBL merger (1946–1956) ===\n\nThe NBA traces its roots to the Basketball Association of America which was founded in 1946 by owners of the major ice hockey arenas in the Northeastern and Midwestern United States and Canada. On November 1, 1946, in Toronto, Ontario, Canada, the Toronto Huskies hosted the New York Knickerbockers at Maple Leaf Gardens, in a game the NBA now refers to as the first game played in NBA history. The first basket was made by Ossie Schectman of the Knickerbockers.\nAlthough there had been earlier attempts at professional basketball leagues, including the American Basketball League (ABL) and the National Basketball League (NBL), the BAA was the first league to attempt to play primarily in large arenas in major cities. During its early years, the quality of play in the BAA was not significantly better than in competing leagues or among leading independent clubs such as the Harlem Globetrotters. For instance, the 1947 ABL finalist Baltimore Bullets moved to the BAA and won that league's 1948 title, and the 1948 NBL champion Minneapolis Lakers won the 1949 BAA title.\nPrior to the 1948–49 season, the BAA lured away the Fort Wayne Pistons, Indianapolis Kautskys, Minneapolis Lakers, and Rochester Royals from the NBL with the prospect of playing in major venues such as Boston Garden and Madison Square Garden. The NBL hit back by outbidding the BAA for the services of several players, including Al Cervi, rookie Dolph Schayes and five stars from the University of Kentucky while also gaining the upper hand in Indianapolis with the creation of the Indianapolis Olympians while the Kautskys folded. With several teams facing financial difficulties, the BAA and the NBL agreed on a merger on August 3, 1949, to create the National Basketball Association. Maurice Podoloff, the president of BAA, became the president of the NBA while Ike Duffey, president of the NBL, became the chairman. The NBA later adopted the BAA's history and statistics as its own but did not do the same for NBL records and statistics.\n\nThe new league had seventeen franchises located in a mix of large and small cities, as well as large arenas and smaller gymnasiums and armories. In 1950, the NBA consolidated to eleven franchises, a process that continued until 1954–55, when the league reac"),
Document(metadata={'title': 'List of NBA G League champions', 'summary': "The NBA G League Finals is the championship game or series for the NBA G League and the conclusion of the league's postseason. The league was previously known as the National Basketball Development League (NBDL) from 2001 to 2005 and the National Basketball Association Development League (NBA D-League) from 2005 to 2017.\nSince the league's inception in 2001–02, a variety of formats has been used to determine the champion. From the inaugural postseason in 2002 until 2006, the four teams with the best records advanced to the postseason. In the first two seasons, both the semifinal round and the Finals were held in a best-of-three format. Then, between 2004 and 2007, the playoffs used a single-elimination tournament among the four teams, with two semifinal games and one winner-take-all championship match.\nIn 2007, the league expanded to twelve teams and was divided into Eastern and Western Conferences, comprising six teams each. All playoff rounds were one game each, ending with the Eastern Conference's Dakota Wizards winning the championship 129–121 in overtime against the Colorado 14ers. The best-of-three format for the Finals returned in 2008. With the league's continued expansion to fourteen teams in 2008 and sixteen teams in 2009, the two-conference format was replaced with a three-division format consisting of Western, Southwestern and Central Divisions. Both the 2008 and 2009 NBADL championship series were between teams representing the Western and Southwestern Divisions, with no Central teams making it to the finals. The 2009–10 season saw a reformatting to having Eastern and Western Conferences, with the 2012–13 season being the only one with a third conference named as \nthe Central and the 2013–14 season having just Divisions rather than conferences. Due to there being two more teams in the Western Conference (nine) than the Eastern Conference (seven), and because the top eight teams with the best regular season records qualified for the postseason irrespective of conference, the 2010 and 2012 NBADL Finals consisted of two Western Conference teams. Finally, in the 2014–15 season, the playoff structure was changed so the teams were separated by conference in seeding. The 2021 edition of the Finals was a one-game match, which equaled the number of games played for the other playoff rounds as played by the eight teams that were seeded solely by record. The league returned to the best-of-three format for the Finals in 2022.\nThe Rio Grande Valley Vipers lead the league in championship appearances (seven) and championships (four).", 'source': 'https://en.wikipedia.org/wiki/List_of_NBA_G_League_champions'}, page_content="The NBA G League Finals is the championship game or series for the NBA G League and the conclusion of the league's postseason. The league was previously known as the National Basketball Development League (NBDL) from 2001 to 2005 and the National Basketball Association Development League (NBA D-League) from 2005 to 2017.\nSince the league's inception in 2001–02, a variety of formats has been used to determine the champion. From the inaugural postseason in 2002 until 2006, the four teams with the best records advanced to the postseason. In the first two seasons, both the semifinal round and the Finals were held in a best-of-three format. Then, between 2004 and 2007, the playoffs used a single-elimination tournament among the four teams, with two semifinal games and one winner-take-all championship match.\nIn 2007, the league expanded to twelve teams and was divided into Eastern and Western Conferences, comprising six teams each. All playoff rounds were one game each, ending with the Eastern Conference's Dakota Wizards winning the championship 129–121 in overtime against the Colorado 14ers. The best-of-three format for the Finals returned in 2008. With the league's continued expansion to fourteen teams in 2008 and sixteen teams in 2009, the two-conference format was replaced with a three-division format consisting of Western, Southwestern and Central Divisions. Both the 2008 and 2009 NBADL championship series were between teams representing the Western and Southwestern Divisions, with no Central teams making it to the finals. The 2009–10 season saw a reformatting to having Eastern and Western Conferences, with the 2012–13 season being the only one with a third conference named as \nthe Central and the 2013–14 season having just Divisions rather than conferences. Due to there being two more teams in the Western Conference (nine) than the Eastern Conference (seven), and because the top eight teams with the best regular season records qualified for the postseason irrespective of conference, the 2010 and 2012 NBADL Finals consisted of two Western Conference teams. Finally, in the 2014–15 season, the playoff structure was changed so the teams were separated by conference in seeding. The 2021 edition of the Finals was a one-game match, which equaled the number of games played for the other playoff rounds as played by the eight teams that were seeded solely by record. The league returned to the best-of-three format for the Finals in 2022.\nThe Rio Grande Valley Vipers lead the league in championship appearances (seven) and championships (four).\n\n\n== Key ==\n\n\n== Champions ==\n\n\n== Results by teams ==\n\n\n== See also ==\nList of NBA champions\n Basketball portal\n\n\n== Notes ==\n\na The Asheville Altitude relocated and became the Tulsa 66ers in 2005–06.\nb The Columbus Riverdragons relocated and became the Austin Toros in 2005–06.\nc The Colorado 14ers went on hiatus in 2009–10 and returned as the Texas Legends in 2010–11 after a relocation.\nd The Huntsville Flight relocated and became the Albuquerque Thunderbirds in 2005–06, then the franchise renamed itself to the New Mexico Thunderbirds in 2010–11. The franchise was then purchased and relocated in 2011–12 and became known as the Canton Charge and then the Cleveland Charge in 2021–22.\ne After two years as the North Charleston Lowgators, the franchise became known as the Charleston Lowgators for one season prior to their relocation. The franchise was known as the Florida Flame for the 2004 through 2006 seasons.\nf The Dakota Wizards relocated and became the Santa Cruz Warriors in 2012–13.\ng The Fort Wayne Mad Ants relocated to Indianapolis in 2023–24, playing as the Indiana Mad Ants. After the 2024–25 season, the team relocated again, this time to the Indianapolis suburb of Noblesville, as the Noblesville Boom.\nh The Agua Caliente Clippers rebranded as the Ontario Clippers in 2022 without relocating. Starting in 2024–25, the team relocated to Oceanside, California as the San Diego Clippers.\n\n\n=")] Es importante notar que lo obtenido es una lista con metadata. Esto es importante ya que podría ser útil incorporar estos elementos para profundizar en algún punto particular.
Note que la salida de este retriever incluye 3 páginas. A continuación, se presenta cada una.
len(contexto_en)
3
Ver página 1
contexto_en[0].page_content
'The NBA Finals is the annual championship series for the National Basketball Association (NBA) held at the conclusion of its postseason. All NBA Finals have been played in a best-of-seven format, and are contested between the winners of the Eastern Conference and the Western Conference (prior to 1971 it was played between division playoff winners), except in 1950 when the Eastern Division champion faced the winner between the Western and Central Division champions. From 1946 through 1949, when the league was known as the Basketball Association of America (BAA), the playoffs were a three-stage tournament where the two semifinal winners played each other in the finals. The winning team of the series receives the Larry O\'Brien Championship Trophy, which has been awarded since 1977 (between 1947 and 1976 the winning team received the Walter A. Brown Trophy).\n\n\n== Champions ==\n\nThe most recent champions are the Oklahoma City Thunder, who won their first championship since relocating from Seattle in 2008, and their first since 1979 as the Seattle SuperSonics. The Boston Celtics have won the most championships of any team in the league at 18. As of 2025, the Eastern champions have a 41–38 advantage in NBA titles over the Western champions. The 1949–50 Minneapolis Lakers, who won the NBA Finals, are counted in the Eastern versus Western champions record as they played that sole season in the Central Division before returning to the Western Division.\n\nThe first parentheses in the Western champions and Eastern champions columns indicate the teams\' playoff seed. The second parentheses indicate the number of times that teams have appeared in an NBA Finals as well as each respective team\'s NBA Finals record to date.\n\n\n== Records by franchise ==\n\n\n== Consecutive championships ==\nEight consecutive\n\nBoston Celtics (1959–1966)\nThree consecutive\n\nMinneapolis Lakers (1952–1954)\nChicago Bulls (1991–1993)\nChicago Bulls (1996–1998)\nLos Angeles Lakers (2000–2002)\nTwo consecutive\n\nMinneapolis Lakers (1949, 1950)\nBoston Celtics (1968, 1969)\nLos Angeles Lakers (1987, 1988)\nDetroit Pistons (1989, 1990)\nHouston Rockets (1994, 1995)\nLos Angeles Lakers (2009, 2010)\nMiami Heat (2012, 2013)\nGolden State Warriors (2017, 2018)\n\n\n== Frequent matchups ==\n\n\n== See also ==\n\nNBA Finals Most Valuable Player Award\nList of NBA players with most championships\nList of NBA championship head coaches\nList of NBA franchise post-season droughts\nList of NBA franchise post-season streaks\nList of NBA longest winning streaks\nList of ABA champions\nList of National Basketball League (United States) champions\nList of NBA G League champions\nWNBA Finals\n\n\n== Notes ==\n\n\n== References ==\n\n\n== External links ==\n\n"NBA Season Recaps: A look back at every season since 1946". NBA. August 12, 2024. Archived from the original on August 15, 2024. Retrieved May 31, 2025.'
Ver página 2
contexto_en[1].page_content
"The National Basketball Association (NBA) is a professional basketball league in North America composed of 30 teams (29 in the United States and 1 in Canada). The NBA is one of the major professional sports leagues in the United States and Canada and is considered the premier professional basketball league in the world. The league is headquartered in Midtown Manhattan.\nThe NBA was created on August 3, 1949, with the merger of the Basketball Association of America (BAA) and the National Basketball League (NBL). The league later adopted the BAA's history and considers its founding on June 6, 1946, as its own. In 1976, the NBA and the American Basketball Association (ABA) merged, adding four franchises to the NBA. The NBA's regular season runs from October to April, with each team playing 82 games. The league's playoff tournament extends into June, culminating with the NBA Finals championship series.\nThe NBA is an active member of USA Basketball (USAB), which is recognized by the International Basketball Federation (FIBA) as the governing body for basketball in the United States. The NBA is the second-wealthiest professional sports league in the world by revenue after the National Football League (NFL). As of 2020, NBA players are the world's highest paid athletes by average annual salary per player.\nThe Boston Celtics have the most NBA championships with 18, most recently winning in 2024. The Oklahoma City Thunder are the reigning league champions, having defeated the Indiana Pacers in the 2025 NBA Finals for their first title since moving to Oklahoma City in 2008, and their second ever, having won in 1979 as the Seattle SuperSonics.\n\n\n== History ==\n\n\n=== Creation and BAA–NBL merger (1946–1956) ===\n\nThe NBA traces its roots to the Basketball Association of America which was founded in 1946 by owners of the major ice hockey arenas in the Northeastern and Midwestern United States and Canada. On November 1, 1946, in Toronto, Ontario, Canada, the Toronto Huskies hosted the New York Knickerbockers at Maple Leaf Gardens, in a game the NBA now refers to as the first game played in NBA history. The first basket was made by Ossie Schectman of the Knickerbockers.\nAlthough there had been earlier attempts at professional basketball leagues, including the American Basketball League (ABL) and the National Basketball League (NBL), the BAA was the first league to attempt to play primarily in large arenas in major cities. During its early years, the quality of play in the BAA was not significantly better than in competing leagues or among leading independent clubs such as the Harlem Globetrotters. For instance, the 1947 ABL finalist Baltimore Bullets moved to the BAA and won that league's 1948 title, and the 1948 NBL champion Minneapolis Lakers won the 1949 BAA title.\nPrior to the 1948–49 season, the BAA lured away the Fort Wayne Pistons, Indianapolis Kautskys, Minneapolis Lakers, and Rochester Royals from the NBL with the prospect of playing in major venues such as Boston Garden and Madison Square Garden. The NBL hit back by outbidding the BAA for the services of several players, including Al Cervi, rookie Dolph Schayes and five stars from the University of Kentucky while also gaining the upper hand in Indianapolis with the creation of the Indianapolis Olympians while the Kautskys folded. With several teams facing financial difficulties, the BAA and the NBL agreed on a merger on August 3, 1949, to create the National Basketball Association. Maurice Podoloff, the president of BAA, became the president of the NBA while Ike Duffey, president of the NBL, became the chairman. The NBA later adopted the BAA's history and statistics as its own but did not do the same for NBL records and statistics.\n\nThe new league had seventeen franchises located in a mix of large and small cities, as well as large arenas and smaller gymnasiums and armories. In 1950, the NBA consolidated to eleven franchises, a process that continued until 1954–55, when the league reac"
Ver página 3
contexto_en[2].page_content
"The NBA G League Finals is the championship game or series for the NBA G League and the conclusion of the league's postseason. The league was previously known as the National Basketball Development League (NBDL) from 2001 to 2005 and the National Basketball Association Development League (NBA D-League) from 2005 to 2017.\nSince the league's inception in 2001–02, a variety of formats has been used to determine the champion. From the inaugural postseason in 2002 until 2006, the four teams with the best records advanced to the postseason. In the first two seasons, both the semifinal round and the Finals were held in a best-of-three format. Then, between 2004 and 2007, the playoffs used a single-elimination tournament among the four teams, with two semifinal games and one winner-take-all championship match.\nIn 2007, the league expanded to twelve teams and was divided into Eastern and Western Conferences, comprising six teams each. All playoff rounds were one game each, ending with the Eastern Conference's Dakota Wizards winning the championship 129–121 in overtime against the Colorado 14ers. The best-of-three format for the Finals returned in 2008. With the league's continued expansion to fourteen teams in 2008 and sixteen teams in 2009, the two-conference format was replaced with a three-division format consisting of Western, Southwestern and Central Divisions. Both the 2008 and 2009 NBADL championship series were between teams representing the Western and Southwestern Divisions, with no Central teams making it to the finals. The 2009–10 season saw a reformatting to having Eastern and Western Conferences, with the 2012–13 season being the only one with a third conference named as \nthe Central and the 2013–14 season having just Divisions rather than conferences. Due to there being two more teams in the Western Conference (nine) than the Eastern Conference (seven), and because the top eight teams with the best regular season records qualified for the postseason irrespective of conference, the 2010 and 2012 NBADL Finals consisted of two Western Conference teams. Finally, in the 2014–15 season, the playoff structure was changed so the teams were separated by conference in seeding. The 2021 edition of the Finals was a one-game match, which equaled the number of games played for the other playoff rounds as played by the eight teams that were seeded solely by record. The league returned to the best-of-three format for the Finals in 2022.\nThe Rio Grande Valley Vipers lead the league in championship appearances (seven) and championships (four).\n\n\n== Key ==\n\n\n== Champions ==\n\n\n== Results by teams ==\n\n\n== See also ==\nList of NBA champions\n Basketball portal\n\n\n== Notes ==\n\na The Asheville Altitude relocated and became the Tulsa 66ers in 2005–06.\nb The Columbus Riverdragons relocated and became the Austin Toros in 2005–06.\nc The Colorado 14ers went on hiatus in 2009–10 and returned as the Texas Legends in 2010–11 after a relocation.\nd The Huntsville Flight relocated and became the Albuquerque Thunderbirds in 2005–06, then the franchise renamed itself to the New Mexico Thunderbirds in 2010–11. The franchise was then purchased and relocated in 2011–12 and became known as the Canton Charge and then the Cleveland Charge in 2021–22.\ne After two years as the North Charleston Lowgators, the franchise became known as the Charleston Lowgators for one season prior to their relocation. The franchise was known as the Florida Flame for the 2004 through 2006 seasons.\nf The Dakota Wizards relocated and became the Santa Cruz Warriors in 2012–13.\ng The Fort Wayne Mad Ants relocated to Indianapolis in 2023–24, playing as the Indiana Mad Ants. After the 2024–25 season, the team relocated again, this time to the Indianapolis suburb of Noblesville, as the Noblesville Boom.\nh The Agua Caliente Clippers rebranded as the Ontario Clippers in 2022 without relocating. Starting in 2024–25, the team relocated to Oceanside, California as the San Diego Clippers.\n\n\n="
Para evaluar la sensibilidad de este enfoque, se obtendrán tres salidas distintas, una por página.
A continuación, se define una función para incorporar el contexto. Note que la función requiere 3 variables:
- pregunta
- contexto
- temperature --> Por defecto inicia en 0.
def answer_context(pregunta, contexto, temperature=0.0):
response = openai.chat.completions.create(#openai.ChatCompletion.create(
model = "gpt-4o-mini",
messages = [{"role": "user",
"content": f'''Answer the question: {pregunta}, considering the following context: {contexto}'''}],
temperature = temperature
)
answer = response.choices[0].message.content
return answer
Respuesta con página 1
respuesta_0 = answer_context(pregunta="Who are the current NBA champions?",
contexto = contexto_en[0].page_content)
respuesta_0
'The current NBA champions are the Oklahoma City Thunder, who won their first championship since relocating from Seattle in 2008 and their first since 1979 as the Seattle SuperSonics.'
Respuesta con la página 2
respuesta_1 = answer_context(pregunta="Who are the current NBA champions?",
contexto = contexto_en[1].page_content)
respuesta_1
'The current NBA champions are the Oklahoma City Thunder, who won the title by defeating the Indiana Pacers in the 2025 NBA Finals. This victory marked their first championship since moving to Oklahoma City in 2008 and their second overall, having previously won in 1979 as the Seattle SuperSonics.'
Respuesta con la página 3
respuesta_2 = answer_context(pregunta="Who are the current NBA champions?",
contexto = contexto_en[2].page_content)
respuesta_2
'The context provided does not specify the current NBA champions. However, as of my last knowledge update in October 2023, the Denver Nuggets were the reigning NBA champions, having won the championship in June 2023. Please verify with the latest sources for the most current information.'
Notar que esta tercera respuesta menciona que el contexto entregado no es útil para responder la pregunta. Luego, indica la información asociada a los datos de entrenamiento.