
Setup
- Load your API key from an environment variable and define a helper that wraps the ChatCompletion API. Keep API keys out of source code and follow your organization’s secret management policies.
Store sensitive credentials (like OPENAI_API_KEY) in environment variables or
a secrets manager. Avoid hard-coding keys in notebooks.
- Separate context (source text) from instructions (the prompt). This makes prompts reusable and easier to test.
- Delimit large contexts (e.g., using triple backticks) so the model can clearly distinguish input data from the instruction.
- When requesting structured outputs (JSON, XML, CSV), be explicit about the required schema to minimize parsing errors.
When embedding large context into prompts, delimit it (for example with triple
backticks) so the model can clearly distinguish the source content from the
instruction.
- Keep the source text and the instruction separate. Here’s an excerpt from Steve Jobs’ 2005 Stanford commencement address. We ask for a 500-word summary, then show how to request a bullet-point summary for scannability.
- Steve Jobs delivered a commencement address at Stanford University in 2005 and shared three stories from his life.
- First story: connecting the dots — dropping out led him to learn calligraphy, which later influenced Macintosh design.
- Second story: love and loss — getting fired from Apple enabled him to start anew (NeXT, Pixar) and eventually return.
- Third story: death — facing mortality focused his priorities; follow your intuition and live authentically.
- Closing advice: “Stay Hungry. Stay Foolish.” — remain curious and brave in pursuing your work.
- Use the same structure: pass the text as context, then instruct the model how to label each item. This pattern is useful for generating labeled datasets for downstream model training or analysis.
- If you sometimes like to go to the movies to have fun, Wasabi is a good place to start. Sentiment: Positive
- An idealistic love story that brings out the latent 15-year-old romantic in everyone. Sentiment: Positive
- The story loses its bite in a last-minute happy ending that’s even less plausible than the rest of the picture. Sentiment: Negative
- LLMs can translate and preserve tone. Provide the poem as context and request a tone-preserving English rendering.
- Convert semi-structured plain text into structured formats for ingestion into pipelines and databases. Be explicit about the desired output schema (keys, types) to reduce ambiguity.
- What we covered:
- Summarization: fixed-length summaries and bullet-style output, emphasizing separation of context and prompt.
- Sentiment analysis: labeling text as Positive / Negative / Neutral for downstream use.
- Translation: preserving tone (poetic translation example).
- Format conversion: converting semi-structured text into JSON, XML, and JSONL for pipelines.
- OpenAI Chat API documentation: https://platform.openai.com/docs/api-reference/chat
- JSONL / NDJSON specification and best practices: https://jsonlines.org/
- Secrets management best practices: consider using environment variables or dedicated secret stores (Vault, AWS Secrets Manager, etc.)