DEV Community

Astha Jhawar
Astha Jhawar

Posted on

We can automate the collection of user prompts and assistant responses to build a OrderBot.

Ever asked ChatGPT something and got back complete nonsense? Yeah, me too. Here's how to actually talk to AI without losing your sanity.

Local Setup (The Easy Part)

!pip install openai
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")

def get_completion(prompt, model="gpt-3.5-turbo"):
    messages = [{"role": "user", "content": prompt}]
    return openai.ChatCompletion.create(model=model, messages=messages, temperature=0)
Enter fullscreen mode Exit fullscreen mode

This get_completion function is your best friend - it wraps your prompt in a message format and sends it to OpenAI. Think of it as your AI translator.
For HTML outputs: from IPython.display import display, HTML then display(HTML(response)) boom, rendered HTML in Jupyter!

The Magic Chat Function Explained 🎭

def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=temperature  # 0 = robot mode, 1 = creative chaos
    )
    return response.choices[0].message["content"]

# Shakespeare bot example
messages = [  
    {'role':'system', 'content':'You are an assistant that speaks like Shakespeare.'},    
    {'role':'user', 'content':'tell me a joke'}
]
Enter fullscreen mode Exit fullscreen mode

Here's the breakdown:
messages: Array of conversation history with roles (system, user, assistant)
system: Sets the AI's personality (like "You're Shakespeare")
temperature: Controls randomness (0 = predictable, 1 = creative madness)
response.choices[0].message["content"]: Extracts just the text response

Smart Prompting Tricks 🧠

Condition Checking: Make AI verify stuff

prompt = "Check if this text contains harmful content: 'Hello world'"
Enter fullscreen mode Exit fullscreen mode

Force Deep Thinking:

prompt = "Solve 15 x 23 step-by-step before giving final answer"
Enter fullscreen mode Exit fullscreen mode

Multi-tasking Master:

prompt = "Do 3 things: 1) Translate to Spanish 2) Find sentiment 3) Extract emotions. Return JSON."
Enter fullscreen mode Exit fullscreen mode

Emotion Detective: Ask for {"emotions": ["happy", "excited"]}format
Translation Pro: Not just languages - tones too! "Convert slang to business English"

Watch Out for AI Brain Farts! 💨

Hallucinations: AI sometimes confidently makes stuff up. Always double-check important facts - it's like that friend who swears they know the shortcut but gets you lost.

OrderBot Magic 🍕

The course shows building pizza ordering bots that remember context, handle complex conversations, and output structured JSON orders. Basically, your AI employee that never needs coffee breaks!
Pro Tips: Use delimiters ("""text"""), ask for JSON structure, show examples, be specific about format, and iterate - your first prompt will probably suck.

Top comments (0)