Skip to content Skip to footer

Azure OpenAI Workshop

Your first prompt

Open a terminal and execute the following commands:

pip3 install -U pandas openpyxl openai python-dotenv requests pydantic markdownify

.env

AZURE_OPENAI_ENDPOINT = "[your endpoint]"
AZURE_OPENAI_KEY = "[your api key]"
CHAT_COMPLETION_NAME = "[name of your model]"

my-first-prompt.py

import os
from openai import AzureOpenAI
from dotenv import load_dotenv
load_dotenv()

client = AzureOpenAI(
  azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"), 
  api_key=os.getenv("AZURE_OPENAI_KEY"),  
  api_version="2024-08-01-preview"
)

# Select the model
model = os.getenv("CHAT_COMPLETION_NAME")

# Create your first prompt
text_prompt = "How far away is the moon from the earth?"

response = client.chat.completions.create(
  model=model,
  messages = [{"role":"system", "content":"You are a helpful assistant."},
              {"role":"user","content":text_prompt},])

print(response.choices[0].message.content)

Extracting information from an email

extracting_info_from_email.py

import os
from openai import AzureOpenAI
from dotenv import load_dotenv
from pydantic import BaseModel

load_dotenv()

client = AzureOpenAI(
  azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"), 
  api_key=os.getenv("AZURE_OPENAI_KEY"),  
  api_version="2024-08-01-preview"
)

# Select the General Purpose curie model for text
model = os.getenv("CHAT_COMPLETION_NAME")

email_content = """

Dear customer service,

I lost my invoice. Can you please resend it? My customer account number is 12567 and the invoice number is 3443.

Best regards,
Thomas

"""


class ExtractedEmail(BaseModel):
    customer_number: str
    invoice_number: str



completion = client.beta.chat.completions.parse(
    model=model,
    messages=[
        {
            "role": "system",
            "content": "You are a helpful assistant. You will receive an email and from this email you extract the customer number and the invoice number",
        },
        {
            "role": "user",
            "content": """
            Extract customer number and invoice number from this email
            
            
            """+email_content,
        },
    ],
    response_format=ExtractedEmail,
)

message = completion.choices[0].message

if message.parsed:
    print("Customer number: "+message.parsed.customer_number)
    print("Invoice number: "+message.parsed.invoice_number)

else:
    print(message.refusal)

Extracting information from Wikipedia

extracting_info_from_wikipedia.py

import os
from openai import AzureOpenAI
from dotenv import load_dotenv
from pydantic import BaseModel
import requests

load_dotenv()

client = AzureOpenAI(
  azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"), 
  api_key=os.getenv("AZURE_OPENAI_KEY"),  
  api_version="2024-08-01-preview"
)

# Select the General Purpose curie model for text
model = os.getenv("CHAT_COMPLETION_NAME")


url = "https://en.wikipedia.org/wiki/Benjamin_Franklin"



response = requests.get(url)



class Person(BaseModel):
    name: str
    occupation: str
    related_persons: list[str]




completion = client.beta.chat.completions.parse(
    model=model,
    messages=[
        {
            "role": "system",
            "content": "You are a helpful assistant. You extract information from the provided HTML page and provide the requested fields",
        },
        {
            "role": "user",
            "content": """
            Extract the name, occupation and related persons from this data
            
            
            """+response.text[:10000],
        },
    ],
    response_format=Person,
)

message = completion.choices[0].message

if message.parsed:
    print("Name: "+message.parsed.name)
    print("Occupation: "+message.parsed.occupation)

    print("== Related persons ==")

    for related_person in message.parsed.related_persons:
        print("- "+related_person)

else:
    print(message.refusal)

Web Scraping books

scraping_books.py

import os
from openai import AzureOpenAI
from dotenv import load_dotenv
from pydantic import BaseModel
import requests
from typing import Optional
import json
import pandas as pd

load_dotenv()

client = AzureOpenAI(
  azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"), 
  api_key=os.getenv("AZURE_OPENAI_KEY"),  
  api_version="2024-08-01-preview"
)

# Select the General Purpose curie model for text
model = os.getenv("CHAT_COMPLETION_NAME")


url = "https://books.toscrape.com/"



response = requests.get(url)


class BookDetails(BaseModel):
    title: str
    price: float
    availability: str
    rating: Optional[str]

class BookPage(BaseModel):
    books: list[BookDetails]
    category: Optional[str]
    next_page_url: Optional[str]




completion = client.beta.chat.completions.parse(
    model=model,
    messages=[
        {
            "role": "system",
            "content": "You are a helpful assistant. You extract books from a HTML webpage",
        },
        {
            "role": "user",
            "content": """
            Extract the requested data about these books
            
            
            """+response.text,
        },
    ],
    response_format=BookPage,
)

message = completion.choices[0].message

if message.parsed:

    json_output = json.dumps([book.model_dump() for book in message.parsed.books], indent=4)

    books = json.loads(json_output)

    all_books = []

    for book in books:
        all_books.append(book)



    df = pd.DataFrame(all_books)
    df.to_excel("books.xlsx",index=False)
    df.to_csv("books.csv",index=False)

else:
    print(message.refusal)

scraping_books_markdownify.py

import os
from openai import AzureOpenAI
from dotenv import load_dotenv
from pydantic import BaseModel
import requests
from typing import Optional
import json
import pandas as pd
import markdownify

load_dotenv()

client = AzureOpenAI(
  azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"), 
  api_key=os.getenv("AZURE_OPENAI_KEY"),  
  api_version="2024-08-01-preview"
)

# Select the General Purpose curie model for text
model = os.getenv("CHAT_COMPLETION_NAME")


url = "https://books.toscrape.com/"



response = requests.get(url)


class BookDetails(BaseModel):
    title: str
    price: float
    availability: str
    rating: Optional[str]

class BookPage(BaseModel):
    books: list[BookDetails]
    category: Optional[str]
    next_page_url: Optional[str]




completion = client.beta.chat.completions.parse(
    model=model,
    messages=[
        {
            "role": "system",
            "content": "You are a helpful assistant. You extract books from a HTML webpage",
        },
        {
            "role": "user",
            "content": """
            Extract the requested data about these books
            
            
            """+markdownify.markdownify(response.text),
        },
    ],
    response_format=BookPage,
)

message = completion.choices[0].message

if message.parsed:

    json_output = json.dumps([book.model_dump() for book in message.parsed.books], indent=4)

    books = json.loads(json_output)

    all_books = []

    for book in books:
        all_books.append(book)



    df = pd.DataFrame(all_books)
    df.to_excel("books.xlsx",index=False)
    df.to_csv("books.csv",index=False)

else:
    print(message.refusal)

Leave a comment

Receive my Python cheatsheet today!

Do you want to become a Python expert? I summarized all my expertise in a 3 pages cheatsheet, so you never have to Google again :)

Socials

Tom’s Tech Academy © 2024. All Rights Reserved.