Discover how to integrate the new OpenAI o1-preview model into a Python script to enhance your AI projects. This script lets you interact with the OpenAI API using the o1-preview model, with the possibility to include web content in prompts thanks to a web scraping feature. Additionally, it properly handles LaTeX math expressions in the model’s responses, converting them into readable Unicode text in the terminal.
Introduction
On September 12, 2024, OpenAI launched its new series of AI models called OpenAI o1. These models are designed to perform deeper reasoning before providing an answer, enabling them to solve complex problems in science, coding, and mathematics. The o1-preview model particularly excels in these areas, outperforming previous models like gpt-4o.
Key points of the script:
- o1-preview model integration: The script uses the o1-preview model by default, providing advanced reasoning capabilities.
- Built-in web scraping: It can extract content from web pages to enrich the prompt context.
- LaTeX expression handling: Mathematical expressions in responses are converted to Unicode text for easy reading in the terminal.
- Customizable: The script allows choosing the OpenAI model and can be adapted to various use cases.
In this article, I will detail the script code, explain how it works, and run a series of complex prompts.
Prerequisites
Before starting, make sure you have the following:
- Python 3.x installed on your machine.
- An OpenAI API key. You can get one by signing up on OpenAI’s website.
- A Python virtual environment (recommended to isolate dependencies).
- The required Python modules.
Setting up the virtual environment
To isolate this project’s dependencies, it is recommended to use a virtual environment. Here is how to create one and install the required dependencies:
python3 -m venv env
source env/bin/activate # Sur Windows, utilisez env\Scripts\activate
pip install openai selenium webdriver-manager pylatexenc
Setting the OpenAI API key
Set your OpenAI API key as an environment variable:
export OPENAI_API_KEY='votre_clé_api_ici'
Replace 'votre_clé_api_ici' with your actual API key.
Complete script code
Here is the full Python script code:
#!/usr/bin/env python3
import os
import sys
import argparse
import re
from openai import OpenAI
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from pylatexenc.latex2text import LatexNodes2Text
def get_web_content(url):
if not url:
return ""
try:
# Configure les options de Chrome
chrome_options = Options()
# Ne pas utiliser le mode headless pour éviter les problèmes de vérification humaine
# Utilise ChromeDriverManager pour gérer l\'installation de ChromeDriver
driver = webdriver.Chrome(
service=Service(ChromeDriverManager().install()), options=chrome_options
)
# Charge la page web
driver.get(url)
# Récupère le contenu textuel de la page
web_content = driver.execute_script("return document.documentElement.innerText")
# Ferme le navigateur
driver.quit()
return web_content if web_content else None
except Exception as e:
return None
def convert_latex_to_text(latex_str):
# Convertit les expressions LaTeX en texte Unicode
return LatexNodes2Text().latex_to_text(latex_str)
def clean_output(content):
# Trouve toutes les expressions LaTeX dans le contenu et les convertit
patterns = [r"\\\\[.*?\\\\]", r"\\\(.*?\\\)", r"\$\$.*?\$\$", r"\$.*?\$"]
for pattern in patterns:
matches = re.findall(pattern, content, flags=re.DOTALL)
for match in matches:
plain_text = convert_latex_to_text(match)
content = content.replace(match, plain_text)
return content
def get_response(prompt, client, model="o1-preview"):
urls = re.findall(r"(https?://\S+)", prompt)
for url in urls:
web_content = get_web_content(url)
if web_content:
prompt = prompt.replace(url, web_content)
else:
return f"Erreur: Le contenu web pour {url} ne peut être récupéré."
try:
response = client.chat.completions.create(
model=model,
messages=[
{
"role": "user",
"content": prompt,
},
],
)
first_choice_message = response.choices[0].message
content = first_choice_message.content
# Convertit les expressions LaTeX en texte lisible
cleaned_content = clean_output(content)
return cleaned_content
except Exception as e:
return f"Une erreur est survenue : {e}"
def main():
parser = argparse.ArgumentParser()
parser.add_argument("prompt", nargs="?", help="Le prompt contenant des URLs")
parser.add_argument(
"--model",
default="o1-preview",
choices=["gpt-4o", "o1-preview", "o1-mini"],
help="Le modèle OpenAI à utiliser (par défaut o1-preview)",
)
args = parser.parse_args()
openai_api_key = os.getenv("OPENAI_API_KEY")
if not openai_api_key:
raise ValueError(
"La clé API OPENAI_API_KEY n'est pas définie dans les variables d\'environnement."
)
with OpenAI(api_key=openai_api_key) as client:
prompt = args.prompt or sys.stdin.read()
response = get_response(prompt, client, model=args.model)
print(response)
if __name__ == "__main__":
main()
Code explanation
Importing required modules
The script starts by importing the essential modules:
- os, sys, argparse, re: Standard modules to manage environment variables, command-line arguments, and regular expressions.
- openai: Module to interact with the OpenAI API.
- selenium and webdriver_manager: To perform web scraping.
- pylatexenc: To convert LaTeX expressions into readable Unicode text.
Function get_web_content
This function retrieves the textual content of a web page.
def get_web_content(url):
if not url:
return ""
try:
# Configure les options de Chrome
chrome_options = Options()
# Ne pas utiliser le mode headless pour éviter les problèmes de vérification humaine
# Utilise ChromeDriverManager pour gérer l\'installation de ChromeDriver
driver = webdriver.Chrome(
service=Service(ChromeDriverManager().install()), options=chrome_options
)
# Charge la page web
driver.get(url)
# Récupère le contenu textuel de la page
web_content = driver.execute_script("return document.documentElement.innerText")
# Ferme le navigateur
driver.quit()
return web_content if web_content else None
except Exception as e:
return None
Key points:
- Chrome options: The script does not use headless mode to avoid human-check challenges some pages impose on headless browsers.
- ChromeDriverManager: Automatically handles installation and updates of ChromeDriver.
- Content extraction: Uses JavaScript to extract the page’s full text.
- Exception handling: In case of an error, the function returns
None.
Function convert_latex_to_text
This function converts LaTeX expressions to Unicode text.
def convert_latex_to_text(latex_str):
# Convertit les expressions LaTeX en texte Unicode
return LatexNodes2Text().latex_to_text(latex_str)
Key points:
- Uses the
pylatexenclibrary to convert LaTeX expressions, making math formulas readable in the terminal.
Function clean_output
This function processes the model’s response to convert LaTeX expressions.
def clean_output(content):
# Trouve toutes les expressions LaTeX dans le contenu et les convertit
patterns = [r"\\\\[.*?\\\\]", r"\\\(.*?\\\)", r"\$\$.*?\$\$", r"\$.*?\$"]
for pattern in patterns:
matches = re.findall(pattern, content, flags=re.DOTALL)
for match in matches:
plain_text = convert_latex_to_text(match)
content = content.replace(match, plain_text)
return content
Key points:
- Finding LaTeX expressions: Uses regular expressions to identify formulas.
- Conversion: Each formula is converted to Unicode text.
- Replacement: LaTeX formulas are replaced by their readable equivalents.
Function get_response
Prepares the prompt, handles web scraping if necessary, calls the OpenAI API, and cleans the response.
def get_response(prompt, client, model="o1-preview"):
urls = re.findall(r"(https?://\S+)", prompt)
for url in urls:
web_content = get_web_content(url)
if web_content:
prompt = prompt.replace(url, web_content)
else:
return f"Erreur: Le contenu web pour {url} ne peut être récupéré."
try:
response = client.chat.completions.create(
model=model,
messages=[
{
"role": "user",
"content": prompt,
},
],
)
first_choice_message = response.choices[0].message
content = first_choice_message.content
# Convertit les expressions LaTeX en texte lisible
cleaned_content = clean_output(content)
return cleaned_content
except Exception as e:
return f"Une erreur est survenue : {e}"
Key points:
- URL handling: If the prompt contains URLs, the content is extracted and inserted.
- OpenAI API call: Sends the modified prompt to the specified model.
- Response cleaning: LaTeX expressions are converted for easy reading.
Function main
Handles command-line arguments and runs the script.
def main():
parser = argparse.ArgumentParser()
parser.add_argument("prompt", nargs="?", help="Le prompt contenant des URLs")
parser.add_argument(
"--model",
default="o1-preview",
choices=["gpt-4o", "o1-preview", "o1-mini"],
help="Le modèle OpenAI à utiliser (par défaut o1-preview)",
)
args = parser.parse_args()
openai_api_key = os.getenv("OPENAI_API_KEY")
if not openai_api_key:
raise ValueError(
"La clé API OPENAI_API_KEY n'est pas définie dans les variables d\'environnement."
)
with OpenAI(api_key=openai_api_key) as client:
prompt = args.prompt or sys.stdin.read()
response = get_response(prompt, client, model=args.model)
print(response)
Key points:
- Arguments: The script accepts a prompt and a model as arguments.
- API key: Checks that the API key is set.
- Execution: Calls
get_responseand prints the response.
Running the script
if __name__ == "__main__":
main()
Usage examples
Ask a question requiring reasoning
./openai_poc.py "Dans un triangle rectangle, si les côtés adjacents à l'angle droit mesurent 3 cm et 4 cm, calcule la longueur de l'hypoténuse."
To calculate the length of the hypotenuse in a right triangle where the legs adjacent to the right angle measure 3 cm and 4 cm, we use the Pythagorean theorem:
Replacing with the given values:
Taking the square root of both sides:
Therefore, the length of the hypotenuse is 5 cm.
The length of the hypotenuse is 5 cm.
Generate a summary of a web page
./openai_poc.py "Fais-moi un résumé de cette page : https://openai.com/index/introducing-openai-o1-preview/"
On September 12, 2024, OpenAI announced the introduction of a new series of AI models called OpenAI o1, designed to take more time to think before answering. These models are capable of reasoning through complex tasks and solving harder problems than previous models in science, coding, and mathematics.
The first model in this series, o1-preview, is now available on ChatGPT and via the OpenAI API. A lighter and more cost-effective version called OpenAI o1-mini is also offered, providing efficient coding capabilities at a reduced cost of 80% compared to o1-preview.
Solve a simple multiplication
./openai_poc.py "Quel est le résultat de 15 x 12 ?"
The result of is 180.
Solve a simple equation
./openai_poc.py "Résous l'équation 2x + 5 = 15."
To solve the equation , follow these steps:
- Subtract 5 from both sides to isolate the term with the variable:
- Divide both sides by 2 to solve for :
Solution: .
Calculate a percentage
./openai_poc.py "Quel est 20% de 250 ?"
of equals 50.
Calculation:
Calculate the area of a circle
./openai_poc.py "Calcule l'aire d'un cercle de rayon 5 cm."
To calculate the area of a circle with radius 5 cm, use the area formula:
where:
- is the area of the circle,
- is the radius of the circle.
Replacing with 5 cm:
So, the area of the circle is .
If you want an approximate value, using :
Thus, the area of the circle is approximately 78.54 cm².
Compute a simple integral
./openai_poc.py "Calcule l'intégrale de 0 à 1 de x^2 dx."
To compute the integral from 0 to 1 of the function , we integrate with respect to and evaluate the resulting expression between 0 and 1.
Steps:
- Integrate :
where C is the constant of integration.
- Evaluate between 0 and 1:
- Compute the boundary values:
- Subtract the values to get the final result:
Conclusion:
The value of the integral from 0 to 1 of is .
Calculate an average speed
./openai_poc.py "Si une voiture parcourt 150 km en 3 heures, quelle est sa vitesse moyenne ?"
To calculate the average speed of a car that travels 150 km in 3 hours, use the average speed formula:
Applying the given values:
The car’s average speed is therefore 50 km/h.
Classical mechanics
./openai_poc.py "Un bloc de 5 kg est tiré sur une surface sans frottement par une force de 20 N. Calcule son accélération."
To calculate the acceleration of the block, we use Newton’s second law, which states that the net force acting on an object equals the object’s mass multiplied by its acceleration ().
Given:
- Mass of the block,
- Applied force,
- Frictionless surface (so no opposing friction force)
Rearranging the formula to solve for acceleration ():
Plugging in the values:
Answer: The block’s acceleration is 4 meters per second squared ().
Solving differential equations
./openai_poc.py "Résous l'équation différentielle dy/dx = x^2 + y^2 avec la condition initiale y(0) = 1."
To solve the differential equation:
with the initial condition , note that this equation is nonlinear and cannot be solved by elementary methods such as separation of variables or standard integrating factors. However, we can try to transform the equation into a more solvable form.
Step 1: Recognize the equation type
The given equation is a Riccati differential equation of general form:
In our case:
Step 2: Transform into a second-order linear equation
To solve a Riccati equation, use the substitution:
where is a function to determine, and is its derivative with respect to .
Compute in terms of :
Applying the quotient rule:
Step 3: Substitute into the original equation
Substitute and into the original differential equation:
Simplify:
Multiply both sides by to eliminate denominators:
Simplify:
Divide by (assuming ):
Step 4: Solve the second-order linear equation
We obtain a homogeneous second-order linear differential equation:
This is a linear differential equation with variable coefficients. It is known in relation to Airy-type equations. The solutions are not expressible in elementary functions but are given in terms of special functions called Airy functions.
Solutions:
The general solutions of are:
where and are Airy functions of the first and second kind, and is the imaginary unit with .
Step 5: Return to
Recall:
Knowing , we can find by computing the derivative of and taking the quotient.
Step 6: Apply the initial condition
To determine the integration constants and , apply the initial condition . However, due to the complexity of Airy functions with complex arguments, the explicit calculation is delicate.
Conclusion
The solution of the differential equation with is expressed in terms of special functions (Airy functions) and cannot be written explicitly using elementary functions.
Final answer:
The solution of the differential equation with is given implicitly in terms of Airy functions and cannot be expressed explicitly with elementary functions.
Electromagnetism
./openai_poc.py "Calcule le champ électrique à une distance r d'une charge ponctuelle Q."
The electric field created by a point charge at a distance is given by Coulomb’s law. The formula to calculate the electric field is:
where:
- is the electric field (in volts per meter, V/m),
- is the point charge (in coulombs, C),
- is the distance to the charge (in meters, m),
- is the vacuum permittivity, approximately .
Example calculation:
Suppose you have a charge (2 microcoulombs) and you want to calculate the electric field at a distance .
- Compute the constant in front of the fraction:
- Apply the formula:
Conclusion:
The electric field at a distance from a point charge is .
Thus, to calculate the electric field at a given distance from a point charge, use the formula above, replacing and with the specific values for the problem.
Chemical equilibrium
./openai_poc.py "Explique comment le principe de Le Chatelier s'applique à l'équilibre suivant : N2 + 3H2 ⇌ 2NH3."
Le Chatelier’s principle states that when a system at equilibrium is subjected to an external change (stress), it responds in a way that minimizes that disturbance and establishes a new equilibrium. Applied to the following chemical equilibrium:
one can analyze how different changes influence the position of equilibrium.
1. Concentration changes: - Increase in reactant concentration (N₂ or H₂): If nitrogen (N₂) or hydrogen (H₂) is added to the system, the concentration of these reactants increases. According to Le Chatelier’s principle, the equilibrium will shift in the direction that consumes the added reactants, i.e., to the right, favoring the formation of ammonia (NH₃).
-
Decrease in product concentration (NH₃): If ammonia is removed from the system, the concentration of NH₃ decreases. The equilibrium will then shift to the right to compensate for this decrease, again favoring the formation of ammonia.
-
Increase in product concentration (NH₃): If ammonia is added to the system, the equilibrium will shift to the left to reduce the excess NH₃, favoring the formation of N₂ and H₂.
2. Change in pressure:
-
Increase in the total pressure of the system: For gases, an increase in pressure (by decreasing volume) favors the reaction that produces the fewer gaseous moles. In this reaction, we go from 4 moles of gas (1 N₂ + 3 H₂) to 2 moles of gas (2 NH₃). Therefore, increasing pressure shifts the equilibrium to the right, favoring the formation of ammonia.
-
Decrease in the total pressure of the system: Conversely, a decrease in pressure will favor the reaction producing the greater number of gaseous moles, shifting the equilibrium to the left and favoring the dissociation of NH₃ into N₂ and H₂.
3. Change in temperature:
-
Exothermic nature of the reaction: The synthesis of ammonia is an exothermic reaction (), meaning it releases heat.
-
Increase in temperature: By increasing temperature, heat is supplied to the system. According to Le Chatelier’s principle, the equilibrium will shift in the direction that absorbs this additional heat, therefore to the left (the endothermic reaction), favoring the dissociation of NH₃ into N₂ and H₂.
-
Decrease in temperature: By lowering the temperature, the system responds by producing heat to compensate for this loss. The equilibrium therefore shifts to the right (the exothermic reaction), favoring the formation of ammonia.
4. Presence of a catalyst:
- Effect of the catalyst: Adding a catalyst (such as finely divided iron with promoters) increases the rate at which equilibrium is reached, but does not change the position of the equilibrium. It accelerates both the forward and reverse reactions equally.
Summary:
Le Chatelier’s principle allows one to predict the direction in which a chemical equilibrium will shift in response to a change in the system’s conditions. For the synthesis of ammonia:
- High pressure favors the formation of NH₃ (fewer gaseous moles).
- Low temperature favors the formation of NH₃ (exothermic reaction).
- Increase in N₂ or H₂ concentrations favors the formation of NH₃.
- Removal of NH₃ from the system favors the production of more NH₃.
These principles are applied in the Haber-Bosch process to optimize industrial ammonia production.
Conclusion
This Python script offers a practical integration of the OpenAI o1-preview model, allowing you to leverage its advanced reasoning capabilities for a wide range of questions, from the simplest to the most complex. Whether you are a curious beginner or an expert looking to test the model’s limits, this script is a handy tool for your AI projects.
Resources
- OpenAI o1-preview model announcement
- Official OpenAI API documentation
- Selenium documentation
- pylatexenc documentation
- Guide to Python virtual environments
This document was translated from the fr version into the en language using the gpt-5-mini model. For more information on the translation process, see https://gitlab.com/jls42/ai-powered-markdown-translator