0
0
Prompt Engineering / GenAIml~20 mins

First interaction with GenAI APIs - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - First interaction with GenAI APIs
Problem:You want to generate text using a GenAI API but the output is not as expected. The generated text is too short and lacks detail.
Current Metrics:Output length average: 20 words; Relevance score (manual check): 60%
Issue:The model output is too brief and not detailed enough, indicating underutilization of the API parameters.
Your Task
Increase the output length to at least 50 words and improve relevance score to above 80% by adjusting API parameters.
You can only change the API request parameters such as max tokens, temperature, and prompt wording.
You cannot change the underlying model or use a different API.
Hint 1
Hint 2
Hint 3
Solution
Prompt Engineering / GenAI
import requests

api_url = "https://api.genai.example/v1/generate"
headers = {"Authorization": "Bearer YOUR_API_KEY"}
prompt = "Write a detailed description about the benefits of daily exercise."

params = {
    "model": "genai-text-001",
    "prompt": prompt,
    "max_tokens": 100,
    "temperature": 0.7
}

response = requests.post(api_url, headers=headers, json=params)
output = response.json().get("text", "")
print(f"Generated Text:\n{output}")
Increased max_tokens from 30 to 100 to allow longer output.
Set temperature to 0.7 to balance creativity and coherence.
Made the prompt more specific by asking for a detailed description.
Results Interpretation

Before: Output length: 20 words, Relevance: 60%

After: Output length: 65 words, Relevance: 85%

Adjusting API parameters like max tokens and temperature, along with clear prompts, helps generate longer and more relevant text from GenAI APIs.
Bonus Experiment
Try using different temperature values (e.g., 0.3, 0.9) to see how creativity and detail in the output change.
💡 Hint
Lower temperature makes output more focused and deterministic; higher temperature increases creativity but may reduce coherence.