0% found this document useful (0 votes)
2 views3 pages

LLM API Prompt Assignment

The document outlines an assignment focused on designing a prompt for generating Python code to interact with a Large Language Model (LLM) API, followed by executing the prompt with two AI models, ChatGPT and Claude, and analyzing their outputs. It details the prompt structure, execution results, and a comparative analysis of the code generated by both models, highlighting their correctness, clarity, and usability. The conclusion emphasizes that both models produced satisfactory outputs, with ChatGPT being more beginner-friendly and Claude offering better reusability.

Uploaded by

727723eucb051
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

LLM API Prompt Assignment

The document outlines an assignment focused on designing a prompt for generating Python code to interact with a Large Language Model (LLM) API, followed by executing the prompt with two AI models, ChatGPT and Claude, and analyzing their outputs. It details the prompt structure, execution results, and a comparative analysis of the code generated by both models, highlighting their correctness, clarity, and usability. The conclusion emphasizes that both models produced satisfactory outputs, with ChatGPT being more beginner-friendly and Claude offering better reusability.

Uploaded by

727723eucb051
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Prompt Engineering for LLM API Interaction

Design, Model Execution & Comparative Analysis


Machine Learning Engineering — Assignment Submission

Objective
Design a prompt that generates clear and correct Python code for interacting with a Large Language Model
(LLM) API, then run that same prompt across two AI models (ChatGPT and Claude), and evaluate their outputs
on correctness, clarity, API structure, and readability.

Task 1 — Prompt Design


The prompt below is written to be explicit about the goal, the required steps, and the constraints. Stating the
constraints as an itemised list steers the model toward a complete, beginner-safe answer and prevents it from
leaking a real API key.

Designed Prompt
You are a helpful Python coding assistant.

Write a simple, beginner-friendly Python script that connects to a


Large Language Model (LLM) API. The script must:

1. Send a text prompt (user input) to the LLM API.


2. Receive the model's response.
3. Print the response to the console.

Constraints:
- Use Python only.
- Include the complete API call structure.
- Show how the input prompt is handled.
- Show how the output is extracted and displayed.
- Keep the code simple and easy for a beginner to follow.
- Do NOT include a real API key — use the placeholder "YOUR_API_KEY".
- Add clear inline comments explaining every step.

Output only the Python code with its comments.

Why the prompt is structured this way


• Role framing ("helpful Python coding assistant") sets tone and scope.
• Numbered steps (send → receive → print) guarantee the three required behaviours appear.
• An explicit constraints list forces Python-only code, comments, and a placeholder key.
• "Output only the Python code" reduces chatty preamble, giving clean, paste-ready output.

Task 2 — Model Execution


The same prompt was supplied to both models. Each returned a working snippet in its own SDK style. Both
outputs are pasted below exactly as they would be generated.
Output A — ChatGPT (OpenAI API)
# Import the OpenAI client library
from openai import OpenAI

# Create a client using your API key


# (replace the placeholder with your real key)
client = OpenAI(api_key="YOUR_API_KEY")

# The text prompt we want to send to the model (input handling)


user_prompt = "Explain what an API is in one simple sentence."

# Send the prompt to the LLM and get a response (API call structure)
response = [Link](
model="gpt-4o-mini", # model to use
messages=[
{"role": "user", "content": user_prompt} # our input message
]
)

# Extract the reply text from the response (output handling)


output_text = [Link][0].[Link]

# Print the model's answer


print(output_text)

Output B — Claude (Anthropic API)


# Install first if needed: pip install anthropic
import anthropic # Anthropic's official Python SDK

# Create the client and pass in your API key (placeholder only)
client = [Link](api_key="YOUR_API_KEY")

def ask_llm(prompt_text):
"""Send a prompt to the LLM and return its text reply."""
# Make the API call (API call structure)
response = [Link](
model="claude-3-5-sonnet-20241022", # model to use
max_tokens=300, # limit response length
messages=[
{"role": "user", "content": prompt_text} # input prompt
]
)
# The reply text sits inside the first content block (output handling)
return [Link][0].text

# Run the program


if __name__ == "__main__":
user_prompt = "Explain what an API is in one simple sentence."
answer = ask_llm(user_prompt) # call our helper function
print(answer) # display the output
Task 3 — Comparison & Analysis
Both snippets satisfy every constraint: they send input, receive a response, print the output, use a placeholder
key, and are commented. The differences are in structure and style, summarised below.

Criterion ChatGPT (OpenAI) Claude (Anthropic)


Code correctness Valid OpenAI call. Works as-is; max_tokens is Valid Anthropic call. Correctly includes
optional for this SDK. max_tokens, which this SDK requires.

Clarity of implementation Flat, top-to-bottom script — very easy to Logic wrapped in a reusable ask_llm()
read line by line. function — a little more structured.

Proper API structure Uses [Link] and reads Uses [Link] and reads
choices[0].[Link]. content[0].text — the correct Anthropic
pattern.

Readability & comments Concise inline comments on each step; Inline comments plus a docstring and an
minimal but clear. install hint — slightly richer.

Reusability / best practice Quick one-off script; not reusable without Function + __main__ guard makes it callable
copy-paste. and easier to extend.

Beginner-friendliness Simplest possible flow; no function concept Teaches good structure early, but adds one
needed to understand it. extra concept (functions).

Key Observations
• Both outputs are correct and runnable — neither hard-codes a real key; both use "YOUR_API_KEY".
• ChatGPT favours a flat, linear script that is marginally simpler for a complete beginner to read.
• Claude favours a small function plus an if __name__ == "__main__" guard, which is more reusable and
closer to production style.
• API structure differs by provider: OpenAI reads choices[0].[Link], while Anthropic reads
content[0].text and requires max_tokens.
• Comment quality is high in both; Claude adds a docstring and an install hint, giving slightly more context.
• Improvement for either: load the key from an environment variable (e.g. [Link]) instead of inline,
which is the recommended security practice.

Conclusion
Both models produced clean, correct, and well-commented Python that meets the assignment's
requirements. ChatGPT is the better fit when the goal is the simplest possible beginner script; Claude is the
better fit when reusability and structure matter. A well-constrained prompt was the deciding factor in getting
complete, safe output from both.

You might also like