In this hands-on module, we’ll apply the concepts you’ve learned to build and test real prompts using OpenAI’s ChatCompletion API (via the new OpenAI SDKopenai>=1.0.0). We will also look at several mini-projects and explore how to integrate them into real-world applications.
Setup
!pip install --upgrade openai
from openai import OpenAIimport osclient = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
1. Simple ChatBot Interaction
response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Who won the FIFA World Cup in 2022?"} ])print(response.choices[0].message.content)
2. Custom Assistant Role
response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a strict English grammar teacher."}, {"role": "user", "content": "Correct this: He don't like the cold."} ])print(response.choices[0].message.content)
3. Explore Parameters
Play with:
temperature: creativity
top_p: nucleus sampling
max_tokens: response length
stop: cut-off patterns
n: number of completions
response = client.chat.completions.create( model="gpt-3.5-turbo", temperature=0.9, top_p=0.85, max_tokens=100, messages=[ {"role": "user", "content": "Write a short story about a cat on Mars."} ])print(response.choices[0].message.content)