Login Sign Up

Prompt Engineering for API-Based Systems

Structuring API Calls for Optimal Performance

Efficient API interactions rely on well-structured prompts and parameter tuning. Key considerations include:

  • Optimizing Token Usage: Keeping prompts concise to reduce API costs.
  • Using System Messages: Setting clear expectations for responses.
  • Batch Processing Requests: Sending multiple requests efficiently to save time and resources.

Example: Optimized API Call for Data Extraction

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))

Output:

– 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.

Function Calling with AI Models

OpenAI’s API supports function calling, allowing structured output generation. This is useful for:

  • Database Queries: AI interprets user intent and retrieves relevant data.
  • Automation Workflows: AI triggers API calls based on user input.
  • Data Transformation: AI formats responses for easier integration with applications.

Example: Function Calling for Weather Information

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))

Output:

{‘location’: ‘New York’, ‘temperature’: ’10’, ‘condition’: ‘cloudy’, ‘humidity’: ‘80%’, ‘wind_speed’: ’10 km/h’}