-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusing_structured_output_and_json_strict_mode_openai.py
65 lines (55 loc) · 1.64 KB
/
using_structured_output_and_json_strict_mode_openai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# pip install firecrawl openai
# set FIRECRAWL_API_KEY and OPENAI_API_KEY environment variables
from firecrawl import FirecrawlApp
from openai import OpenAI
import os
# Initialize the FirecrawlApp with your API key
firecrawl_app = FirecrawlApp(api_key=os.environ['FIRECRAWL_API_KEY'])
# Scrape data from mendable.ai
url = 'https://mendable.ai'
scraped_data = firecrawl_app.scrape_url(url)
# Initialize OpenAI client
client = OpenAI(
api_key=os.environ['OPENAI_API_KEY']
)
# Define the OpenAI API request
messages = [
{
"role": "system",
"content": "You are a helpful assistant that extracts structured data from web pages."
},
{
"role": "user",
"content": f"Extract the headline and description from the following HTML content: {scraped_data['content']}"
}
]
response_format = {
"type": "json_schema",
"json_schema": {
"name": "extracted_data",
"schema": {
"type": "object",
"properties": {
"headline": {
"type": "string"
},
"description": {
"type": "string"
}
},
"required": ["headline", "description"],
"additionalProperties": False
}
}
}
# Call the OpenAI API to extract structured data
chat_completion = client.chat.completions.create(
model="gpt-4o-mini-2024-07-18",
messages=messages,
response_format=response_format
)
# Extracted data
# Access the content of the first choice in the response
extracted_data = chat_completion.choices[0].message.content
# Print the extracted data
print(extracted_data)