Efficient API interactions rely on well-structured prompts and parameter tuning. Key considerations include:
from openai import OpenAI
def extract_key_points(text):
# Initialize the client
# Note: In a production environment, you should use environment variables
# or a secure configuration method instead of hardcoding the API key
client = OpenAI(api_key="your-api-key-here")
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "Extract key points from the following text."},
{"role": "user", "content": text}
]
)
return response.choices[0].message.content
# Example usage
document = "The company saw a 20% revenue increase in Q4 due to higher sales."
print(extract_key_points(document))– The company had a 20% revenue increase in Q4. – The increase was due to higher sales.
This ensures AI efficiently extracts essential information, improving business intelligence.
OpenAI’s API supports function calling, allowing structured output generation. This is useful for:
import json
from openai import OpenAI
def get_weather(location):
# Initialize the client
# Note: In a production environment, you should use environment variables
# or a secure configuration method instead of hardcoding the API key
client = OpenAI(api_key="your-api-key-here")
# Create a prompt that instructs the model to return valid JSON
prompt = f"""Provide weather information for {location} in JSON format.
Return ONLY valid JSON with the following structure, with no additional text:
{{
"location": "city name",
"temperature": "temperature in celsius",
"condition": "weather condition (e.g., sunny, cloudy, rainy)",
"humidity": "humidity percentage",
"wind_speed": "wind speed in km/h"
}}"""
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "system", "content": "You return only valid JSON with no explanation."},
{"role": "user", "content": prompt}]
)
# Extract the JSON content from the response
json_content = response.choices[0].message.content
# Parse the JSON string into a Python dictionary
try:
weather_data = json.loads(json_content)
return weather_data
except json.JSONDecodeError:
# In case the response isn't valid JSON
return {"error": "Failed to get weather data in JSON format", "raw_response": json_content}
# Example usage
location = "New York"
print(get_weather(location)){‘location’: ‘New York’, ‘temperature’: ’10’, ‘condition’: ‘cloudy’, ‘humidity’: ‘80%’, ‘wind_speed’: ’10 km/h’}