การสร้างและอธิบายโค้ด

Prompt Engineering กับ OpenAI API

Fouad Trad

Machine Learning Engineer

การสร้างโค้ด

  • การสร้างโค้ดเพื่อแก้ปัญหาที่กำหนด
  • จำเป็นในทุกโดเมนที่ใช้ซอฟต์แวร์
  • ต้องเข้าใจโค้ดที่สร้างขึ้นเพื่อนำไปใช้ได้อย่างมีประสิทธิภาพ

ไอคอนแสดงโค้ดบนหน้าจอต่าง ๆ

Prompt Engineering กับ OpenAI API

พรอมต์สำหรับสร้างโค้ด

  • คำอธิบายปัญหา
  • ภาษาโปรแกรมมิง
  • รูปแบบ (สคริปต์ ฟังก์ชัน คลาส)
prompt = "Write a Python function that accepts a list of quarterly sales, and outputs the 
average sales per quarter."
print(get_response(prompt))
def calculate_average_sales(quarterly_sales):

total_sales = sum(quarterly_sales) average_sales = total_sales / len(quarterly_sales)
return average_sales
Prompt Engineering กับ OpenAI API

ตัวอย่าง input-output

  • ให้ตัวอย่าง input-output แก่โมเดล เพื่อสร้างโปรแกรมที่แมปข้อมูลเหล่านั้น
examples = """
Input: [150000, 180000, 200000, 170000] -> Output: 175000.0
Input: [10000, 25000, 30000, 15000] -> Output: 20000.0
Input: [50000, 75000, 60000, 45000] -> Output: 57500.0
"""

prompt = f"""You are provided with input-output examples delimited by triple backticks for a python program that receives a list of quarterly sales data. Write code for this program. ```{examples}```""" print(get_response(prompt))
Prompt Engineering กับ OpenAI API

ตัวอย่าง input-output

Based on the provided input-output examples, it seems like you want to calculate the average of 
quarterly sales data for each set of input values. 

Here's a Python program that does that: def calculate_average_quarterly_sales(sales_data): total_sales = sum(sales_data) num_quarters = len(sales_data) average_sales = total_sales / num_quarters return average_sales
print(calculate_average_quarterly_sales([150000, 180000, 200000, 170000])) # Output: 175000.0 print(calculate_average_quarterly_sales([10000, 25000, 30000, 15000])) # Output: 20000.0 print(calculate_average_quarterly_sales([50000, 75000, 60000, 45000])) # Output: 57500.0
Prompt Engineering กับ OpenAI API

การแก้ไขโค้ด

  • ขอให้โมเดลแก้ไขโค้ดตามข้อกำหนดที่ต้องการ
script = """
quarterly_sales = [150, 180, 200, 170]
total_sales = sum(quarterly_sales)
print("Total sales: ", total_sales)
"""

prompt = f"""Modify the script delimited by triple backticks to a function that we can call to compute the total sales given quarterly sales ```{script}```""" print(get_response(prompt))
Prompt Engineering กับ OpenAI API

การแก้ไขโค้ด

def compute_total_sales(quarterly_sales):
    total_sales = sum(quarterly_sales)
    return total_sales

quarterly_sales = [150, 180, 200, 170] total_sales = compute_total_sales(quarterly_sales) print("Total sales: ", total_sales)
Prompt Engineering กับ OpenAI API

การแก้ไขโค้ดหลายรายการ

script = """
quarterly_sales = [150, 180, 200, 170]
total_sales = sum(quarterly_sales)
print("Total sales: ", total_sales)
"""

prompt = f"""Modify the script delimited by triple backticks as follows: - Let user input parameters interactively ```{script}```""" print(get_response(prompt))
Prompt Engineering กับ OpenAI API

การแก้ไขโค้ดหลายรายการ

script = """
quarterly_sales = [150, 180, 200, 170]
total_sales = sum(quarterly_sales)
print("Total sales: ", total_sales)
"""

prompt = f"""Modify the script delimited by triple backticks as follows: - Let user input parameters interactively - Make sure to verify inputs are positive, otherwise, display a message for the user, and ask them to provide their input again. ```{script}```""" print(get_response(prompt))
Prompt Engineering กับ OpenAI API

การแก้ไขโค้ดหลายรายการ

quarterly_sales = []

for i in range(4):
    while True:
        sales = float(input("Enter quarterly sales for quarter {}: ".format(i+1)))
        if sales <= 0:
             print("Sales must be positive. Please try again.")
             continue
        quarterly_sales.append(sales)
        break

total_sales = sum(quarterly_sales)
print("Total sales: ", total_sales)
Prompt Engineering กับ OpenAI API

การอธิบายโค้ด

  • โค้ดบางครั้งอ่านยากและตีความลำบาก
  • LLM ช่วยอธิบายโค้ดได้

ภาพแสดงโค้ดที่ซับซ้อนบนหน้าจอ

Prompt Engineering กับ OpenAI API

ข้อกำหนดในการอธิบายโค้ด

code = """
def compute_average_sales_per_quarter(quarterly_sales):
    average_sales = sum(quarterly_sales) / len(quarterly_sales)
    return average_sales
"""
  • ระบุความยาวของคำอธิบาย
prompt = f"""Explain in one sentence what the code delimited by triple backticks does
             ```{code}```"""
print(get_response(prompt))
The code calculates the average sales per quarter by summing up the values in the 
"quarterly_sales" list and dividing the sum by the number of elements in the list.
Prompt Engineering กับ OpenAI API

การอธิบายโค้ดแบบละเอียด

code = """
def compute_average_sales_per_quarter(quarterly_sales):
    average_sales = sum(quarterly_sales) / len(quarterly_sales)
    return average_sales
"""
  • อธิบายแบบละเอียดด้วย chain-of-thought prompt
prompt = f"""Explain what the code delimited by triple backticks does. 
Let's think step by step.
```{code}```"""
print(get_response(prompt))
Prompt Engineering กับ OpenAI API

การอธิบายโค้ดแบบละเอียด

The code defines a function called `compute_average_sales_per_quarter` that takes in a list of `quarterly_sales` as a 
parameter.

Inside the function, it calculates the average sales by summing up all the values in the `quarterly_sales` list using the `sum()` function, and then dividing it by the length of the list using the `len()` function. The result is stored in the variable `average_sales`.
Finally, the function returns the calculated average sales value.
This code snippet can be used to calculate the average sales per quarter given a list of quarterly sales data.
Prompt Engineering กับ OpenAI API

มาฝึกกันเถอะ!

Prompt Engineering กับ OpenAI API

Preparing Video For Download...