Lesson 13 of 23 · Part 1: API Basics

Challenge: Restaurant Recommender

Combine all Part 1 skills to build a restaurant recommender.

Last time: Example user/assistant pairs (few-shot) teach the AI a task and its answer style.
Today: We combine all of Part 1 to build a restaurant recommender that replies in JSON.
Setup
System
Prompt
JSON mode
OpenAI
Raw text
json.loads
Cards
OpenAI
json
client
Source Code
1# 5_challenge_restaurant_recommender.py
2from openai import OpenAI
3import json
4
5client = OpenAI()
6
7print("Calling the Restaurant Recommender Bot (Bangalore)...")
8
9system_prompt = "You are a helpful restaurant recommender. You will be given a cuisine and location, and must reply in valid JSON format."
10
11user_prompt = """
12Find 3 great South Indian restaurants in Bangalore, India.
13The JSON output should be a list called "recommendations".
14Each item in the list should be an object with two keys: "name" and "reason".
15"""
16
17response = client.chat.completions.create(
18    model="gpt-4o-mini",
19    messages=[
20        {"role": "system", "content": system_prompt},
21        {"role": "user", "content": user_prompt}
22    ],
23    response_format={ "type": "json_object" }
24)
25
26print("\n--- AI's Raw JSON Response ---")
27raw_json = response.choices[0].message.content
28print(raw_json)
29
30print("\n--- AI's 'Pretty' JSON Response ---")
31try:
32    parsed_json = json.loads(raw_json)
33    pretty_json = json.dumps(parsed_json, indent=2)
34    print(pretty_json)
35except json.JSONDecodeError:
36    print("AI did not return valid JSON.")

No output yet...

← Prev
1/0
Next: Simple Agent →