LLM เช่น ChatGPT มีประสิทธิภาพมากในการสร้างโค้ด ในส่วนนี้ เราจะกล่าวถึงตัวอย่างมากมายของวิธีใช้ ChatGPT ในการสร้างโค้ด
Playground ของ OpenAI (โหมดแชท) และโมเดล gpt-3.5-turbo ใช้สำหรับตัวอย่างทั้งหมดด้านล่าง
เช่นเดียวกับรูปแบบการแชททั้งหมดจาก OpenAI คุณสามารถใช้ System Message เพื่อกำหนดพฤติกรรมและรูปแบบของการตอบกลับ เราจะใช้ข้อความระบบต่อไปนี้สำหรับตัวอย่างพร้อมท์:
You are a helpful code assistant that can teach a junior developer how to code. Your language of choice is Python. Don't explain the code, just generate the code block itself.
จากนั้น User Message จะเป็นข้อความแจ้งพร้อมคำแนะนำของโค้ดเฉพาะที่คุณต้องการให้โมเดลสร้าง ด้านล่างนี้คือภาพรวมของพรอมต์พื้นฐานแรกสำหรับการสร้างโค้ด โปรดทราบว่ารหัสสุดท้ายถูกสร้างขึ้นโดย Assistant
นี่คือข้อความแจ้งหากคุณต้องการลองด้วยตัวเอง:
Write code that asks the user for their name and say "Hello"
คุณยังสามารถใช้ความสามารถในการสร้างโค้ดของ LLM เหล่านี้เพื่อสร้างโค้ดจากความคิดเห็นเพียงอย่างเดียวได้ ลองดูตัวอย่างอื่นที่ส่งคำแนะนำเป็นบล็อกความคิดเห็น:
Prompt:
"""
1. Create a list of movies
2. Create a list of ratings for these movies
3. Combine them to make a json object of 10 movies with their ratings.
"""
Output:
movies = ["The Shawshank Redemption", "The Godfather", "The Dark Knight", "Schindler's List", "Pulp Fiction", "The Lord of the Rings: The Return of the King", "Forrest Gump", "Star Wars: Episode V - The Empire Strikes Back", "Inception", "The Silence of the Lambs"]
ratings = [9.3, 9.2, 9.0, 8.9, 8.9, 8.9, 8.8, 8.7, 8.7, 8.6]
movie_ratings = {}
for i in range(len(movies)):
movie_ratings[movies[i]] = ratings[i]
json_object = json.dumps(movie_ratings, indent=4)
print(json_object)
รหัสที่สร้างขึ้นดูเหมือนรหัสที่ใช้งานได้ อย่างไรก็ตาม ผลลัพธ์ไม่มีคำสั่ง import ที่สำคัญ เช่น import json แม้ว่าโมเดลเหล่านี้จะสร้างโค้ดที่ใช้งานได้ แต่คุณจะต้องใส่ใจกับรายละเอียดเล็กๆ น้อยๆ เช่นนี้ และอย่าลืมทดสอบโค้ดของคุณเสมอ
LLM เหล่านี้ยังรวมอยู่ในเครื่องมือเช่น GitHub Copilot ซึ่งทำให้มีประโยชน์สำหรับนักพัฒนา คุณสมบัติที่มีประโยชน์ประการหนึ่งคือความสามารถของโมเดลในการทำหน้าที่ให้สมบูรณ์
Prompt:
# function to multiply two numbers and add 75 to it
def multiply(
Output:
a, b):
result = a * b
result += 75
return result
นอกจากตัวอย่างการสร้างโค้ดพื้นฐานข้างต้นแล้ว คุณสามารถใช้โมเดลเพื่อสร้างโค้ดที่มีประโยชน์ซึ่งสามารถนำไปใช้ในด้านอื่นๆ ของการเขียนโปรแกรม เช่น การสร้างและทดสอบคิวรี MySQL
สมมติว่าคุณมีชุดข้อมูลที่มีข้อมูลบางอย่างซึ่งคุณสามารถรวมเป็นส่วนหนึ่งของพรอมต์ จากนั้นสั่งให้สร้างการสืบค้นเฉพาะ ตัวอย่างเช่น:
"""
Table departments, columns = [DepartmentId, DepartmentName]
Table students, columns = [DepartmentId, StudentId, StudentName]
Create a MySQL query for all students in the Computer Science department
"""
Output:
SELECT students.StudentId, students.StudentName
FROM students
INNER JOIN departments
ON students.DepartmentId = departments.DepartmentId
WHERE departments.DepartmentName = 'Computer Science';
โมเดลสร้างแบบสอบถาม! อย่างไรก็ตาม ในตัวอย่างนี้ เราได้สร้างฐานข้อมูลขึ้นมา ดังนั้นเราจึงไม่มีฐานข้อมูลสำหรับทดสอบแบบสอบถามที่สร้างขึ้น หากคุณกำลังทดสอบตัวอย่างนี้ การสร้างฐานข้อมูลนี้อาจมีประโยชน์ โชคดีที่คุณสามารถใช้ ChatGPT เพื่อสร้างสิ่งนี้ให้กับคุณได้เช่นกัน วิธีหนึ่งคือการใช้พรอมต์ด้านบนเวอร์ชันที่แก้ไขแล้วพร้อมคำแนะนำที่แตกต่างกันเล็กน้อยเพื่อสร้างสคีมาฐานข้อมูล:
Table departments, columns = [DepartmentId, DepartmentName]
Table students, columns = [DepartmentId, StudentId, StudentName]
Create a valid database schema with the above tables and columns
โมเดลสร้างสคีมาต่อไปนี้:
CREATE TABLE departments (
DepartmentId INT PRIMARY KEY,
DepartmentName VARCHAR(50)
);
CREATE TABLE students (
DepartmentId INT,
StudentId INT PRIMARY KEY,
StudentName VARCHAR(50),
FOREIGN KEY (DepartmentId) REFERENCES departments(DepartmentId)
);
คุณสามารถสร้างฐานข้อมูลชั่วคราวได้โดยใช้บางอย่างเช่น sqliteonline.com
คุณจะต้องมีข้อมูลจำลองเพื่อทดสอบว่าการสืบค้นต้นฉบับที่สร้างขึ้นนั้นถูกต้องหรือไม่ ลองใช้ ChatGPT สำหรับสิ่งนี้ด้วย! พรอมต์อาจมีลักษณะดังนี้:
CREATE TABLE departments (
DepartmentId INT PRIMARY KEY,
DepartmentName VARCHAR(50)
);
CREATE TABLE students (
DepartmentId INT,
StudentId INT PRIMARY KEY,
StudentName VARCHAR(50),
FOREIGN KEY (DepartmentId) REFERENCES departments(DepartmentId)
);
Given the database schema above, generate valid insert statements include 4 rows for each table.
ผลลัพธ์ที่มีคำสั่งแทรก:
INSERT INTO departments (DepartmentId, DepartmentName)
VALUES (1, 'Computer Science'), (2, 'Mathematics'), (3, 'Physics'), (4, 'Biology');
INSERT INTO students (DepartmentId, StudentId, StudentName)
VALUES (1, 101, 'John Doe'), (1, 102, 'Jane Doe'), (2, 201, 'Alice Smith'), (2, 202, 'Bob Johnson');
เมื่อคุณมีฐานข้อมูลและข้อมูลจำลองแล้ว คุณสามารถทดสอบคิวรีต้นฉบับได้ จากข้อมูลจำลอง ผลลัพธ์ควรเป็นสองแถวที่มี Jane Doe และ John Doe
หากคุณกำลังเรียนรู้การเขียนโปรแกรมในภาษาใดภาษาหนึ่ง การแจ้งให้โมเดลอธิบายโค้ดบางส่วนอาจเป็นประโยชน์ ลองใช้แบบสอบถามที่สร้างขึ้นด้านบนซ้ำแล้วขอให้แบบจำลองอธิบาย
หากคุณใช้ข้อความระบบเดียวกันกับเมื่อก่อน โปรดใช้ความระมัดระวังในการสอนโมเดล ตัวอย่างเช่น หากเราใช้ตัวอย่างต่อไปนี้ โมเดลจะบ่นว่าเป็นผู้ช่วยโค้ดใน Python แต่ยังคงให้คำอธิบาย บางครั้งโมเดลปฏิเสธที่จะสร้างสิ่งที่คุณกำลังสอนเนื่องจากได้รับการออกแบบให้เป็นไปตามข้อความของระบบ
Prompt:
SELECT students.StudentId, students.StudentName
FROM students
INNER JOIN departments
ON students.DepartmentId = departments.DepartmentId
WHERE departments.DepartmentName = 'Computer Science';
Explain the above SQL statement.
Output:
Sorry, as a code assistant in Python, I cannot generate SQL code. However, I can explain the purpose of the SQL statement provided.
This SQL statement selects the `StudentId` and `StudentName` columns from the `students` table, where the `DepartmentName` column in the `departments` table is equal to 'Computer Science'. To achieve this, the statement performs an inner join on the `DepartmentId` column between the `students` and `departments` tables.
In simpler terms, it is selecting the names and IDs of students who are enrolled in the Computer Science department.
Example coming soon!
ตัวอย่างจะมาเร็ว ๆ นี้!
Example coming soon!
ตัวอย่างจะมาเร็ว ๆ นี้!
Coming soon! เร็วๆ นี้!