Step 1: Say hello: send a prompt, get a reply
A language model answers messages. Your program sends it a list of messages; the model sends back one new message. That round trip is the whole interface, and every AI feature you will ever build starts with it.
Open ai.py. The top of the file is already done:
from openai import OpenAIloads the client library. Most hosted models, not only OpenAI's, accept requests in this format.CLIENT = OpenAI()creates the connection. The lab already holds the address and the key, so it needs no arguments here.MODELis the name of the model you will talk to: Llama 3.1 8B, a small open model.
Each function takes client=None and starts with client = client or CLIENT.
That line means "use the real connection unless someone hands me another
one". The checker hands in a stand-in that records what you sent, so it can
tell you exactly what was wrong.
1. Write ask(prompt). Replace the TODO with two statements:
response = client.chat.completions.create(
model=MODEL,
messages=[{"role": "user", "content": prompt}],
)
return response.choices[0].message.content
messages is a Python list (square brackets) holding one dictionary
(curly braces). The dictionary says who is speaking ("user", that is,
you) and what they said. The reply comes back inside response: a list
called choices (you asked for one answer, so take the first, [0]), its
message, and that message's content.
2. Click Run. It calls ask() with a real question and prints the
answer. Run it twice. The wording will probably change.
3. Click Check. The checker sends ask() a question it can verify: it
asks the real model a question with one right answer.
ai.py, the file you edit50 lines
"""Your first AI calls, from Python.
The lab's environment already knows where the model lives and holds the key,
so `OpenAI()` needs no arguments here. On your own machine you would set
OPENAI_API_KEY (and OPENAI_BASE_URL for a non-OpenAI provider) first.
"""
import time
import openai
from openai import OpenAI
CLIENT = OpenAI()
MODEL = "meta/llama-3.1-8b-instruct"
def ask(prompt, client=None):
"""Send one question to the model and return its reply as a string."""
client = client or CLIENT
# TODO (Step 1): ask the model and return its reply text.
# 1. response = client.chat.completions.create(model=MODEL, messages=[...])
# messages is a list with ONE dict: {"role": "user", "content": prompt}
# 2. return response.choices[0].message.content
raise NotImplementedError("Step 1: write ask()")
def ask_with_stats(prompt, client=None):
"""Ask, and also report what the call cost in tokens and time."""
client = client or CLIENT
raise NotImplementedError("ask_with_stats() arrives in Step 2")
def sample(prompt, temperature, n=5, client=None):
"""Ask the same question n times at one temperature; return the n replies."""
client = client or CLIENT
raise NotImplementedError("sample() arrives in Step 3")
def ask_with_role(system, prompt, client=None):
"""Give the model standing instructions (a system message), then ask."""
client = client or CLIENT
raise NotImplementedError("ask_with_role() arrives in Step 4")
FALLBACK = "Sorry, the assistant is unavailable right now. Please try again in a minute."
def safe_ask(prompt, client=None, model=None):
"""Like ask(), but a failed call returns FALLBACK instead of crashing."""
client = client or CLIENT
raise NotImplementedError("safe_ask() arrives in Step 5")try_it.py