Manipulation de l’API d’Openai¶
Dans ce TP vous utiliserez l’API d’OpenAI afin de créer des agents personaliser.
Voici une liste de commandes les plus utiles pour intéragir avec l’environement Jupyter:
- « Maj » + « Enter » = Exécuter une cellule
- « a » = créer une cellule en haut
- « b » = créer une cellule en bas
- « dd » = supprimer une cellule
- « echap » = sortir d’une cellule
- « m » = Transformer une cellule de code en cellule de texte.
1. Exécutez la cellule ci-dessous pour installer l’API d’OpenAI¶
In [2]:
# Installation de la bibliothèque OpenAi ==> Version 0.28.0
!pip install openai==0.28.0
Collecting openai==0.28.0 Downloading openai-0.28.0-py3-none-any.whl (76 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 76.5/76.5 kB 2.0 MB/s eta 0:00:00 Requirement already satisfied: requests>=2.20 in /usr/local/lib/python3.10/dist-packages (from openai==0.28.0) (2.31.0) Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from openai==0.28.0) (4.66.2) Requirement already satisfied: aiohttp in /usr/local/lib/python3.10/dist-packages (from openai==0.28.0) (3.9.3) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai==0.28.0) (3.3.2) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai==0.28.0) (3.6) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai==0.28.0) (2.0.7) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai==0.28.0) (2024.2.2) Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai==0.28.0) (1.3.1) Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai==0.28.0) (23.2.0) Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai==0.28.0) (1.4.1) Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai==0.28.0) (6.0.5) Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai==0.28.0) (1.9.4) Requirement already satisfied: async-timeout<5.0,>=4.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai==0.28.0) (4.0.3) Installing collected packages: openai Successfully installed openai-0.28.0
2. Exécutez la cellule ci-dessous pour importer les librairies¶
In [3]:
# Import de la bibliothèque :
import openai
In [5]:
a = 5
In [6]:
# Observation des variables en mémoire : %whos
%whos
Variable Type Data/Info ------------------------------ a int 5 openai module <module 'openai' from '/u<...>ages/openai/__init__.py'>
3. Exécutez la cellule ci-dessous pour utiliser l’API d’OpenAI¶
In [7]:
import openai
# Exécutez la commande suivant
def create_image(prompt:str='rat in Paris') -> str:
openai.api_key = "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"
image = openai.Image.create(
model="dall-e-3",
prompt=prompt,
size="1024x1024",
quality="standard",
n=1,
)
return image['data'][0]['url']
In [ ]:
%whos
Variable Type Data/Info ------------------------------------ create_image function <function create_image at 0x7c0917b04b80> openai module <module 'openai' from '/u<...>ages/openai/__init__.py'>
In [ ]:
# Création d'une image : un rat à paris
create_image(prompt='ninja in space')
Out[ ]:
'https://oaidalleapiprodscus.blob.core.windows.net/private/org-7z5FszmIViA0Ipk6dzVjcojw/user-2RzcPuAc8lRGA7glVJ1sTtJM/img-RlkmtxFpWBVaYXlnRjZvY3BM.png?st=2024-03-08T11%3A27%3A20Z&se=2024-03-08T13%3A27%3A20Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-03-08T09%3A22%3A45Z&ske=2024-03-09T09%3A22%3A45Z&sks=b&skv=2021-08-06&sig=XLANlZLyCbEk72BmX9qiZ%2Bhw5i37LDsTYvfsQw36vdE%3D'
4. Créez une image personalisée¶
In [ ]:
# Créez une représentation visuelle d'un monde futuriste puis générez une vidéo via l'outil GEN-2
In [ ]:
jupyter nbconvert --to html /PATH/TO/YOUR/NOTEBOOKFILE.ipynb
In [ ]:
In [ ]:
Entraînement d’un modèle¶
In [8]:
import pandas as pd
In [10]:
df = pd.read_csv('data_clean.csv')
In [14]:
df.Age.mean()
Out[14]:
43.53608402100525
In [13]:
df[(df.Gender == 1) & (df.Age > 50)]
Out[13]:
Gender | Ever_Married | Age | Graduated | Profession | Work_Experience | Spending_Score | Family_Size | Segmentation | |
---|---|---|---|---|---|---|---|---|---|
1 | 1 | 0 | 67 | 0 | 4 | 1.0 | 0 | 1.0 | 3 |
6 | 1 | 0 | 61 | 0 | 4 | 0.0 | 0 | 3.0 | 0 |
7 | 1 | 0 | 55 | 0 | 0 | 1.0 | 1 | 4.0 | 1 |
10 | 1 | 0 | 58 | 1 | 3 | 0.0 | 0 | 1.0 | 3 |
15 | 1 | 0 | 79 | 0 | 0 | 0.0 | 2 | 1.0 | 1 |
… | … | … | … | … | … | … | … | … | … |
6618 | 1 | 0 | 84 | 0 | 0 | 0.0 | 2 | 2.0 | 3 |
6621 | 1 | 0 | 70 | 0 | 6 | 1.0 | 2 | 2.0 | 1 |
6628 | 1 | 0 | 56 | 0 | 0 | 1.0 | 1 | 5.0 | 1 |
6636 | 1 | 0 | 81 | 0 | 6 | 1.0 | 2 | 3.0 | 3 |
6645 | 1 | 0 | 52 | 1 | 3 | 3.0 | 1 | 2.0 | 2 |
796 rows × 9 columns
In [25]:
# Identification de la target Y
y = df.Ever_Married
# Selection des features
X = df.drop(['Ever_Married'], axis=1)
In [28]:
# Séparation du jeu de données en jeu d'entraînement et je de test
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
In [29]:
# Import du modèle depuis sklearn
from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier()
# Entrainement du modèle
clf.fit(X_train, y_train)
Out[29]:
DecisionTreeClassifier()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
DecisionTreeClassifier()
In [30]:
# Prédiction du modèle à partir des données de test
y_pred = clf.predict(X_test)
In [31]:
# Evaluation du modèle du modèle à partir de la prédiction et de la target test
from sklearn.metrics import accuracy_score
accuracy_score(y_test, y_pred)
Out[31]:
0.8537134283570893
In [ ]:
jupyter nbconvert --to html /PATH/TO/YOUR/NOTEBOOKFILE.ipynb