Compete in HackAPrompt 2.0, the world's largest AI Red-Teaming competition!

Check it out →
Selamat Datang
😃Dasar
💼 Aplikasi Dasar
🧙‍♂️ Pelajaran Tingkat Menengah
🤖 Agen
⚖️ Keandalan
🖼️ Prompt untuk Menghasilkan Gambar
🔓 Prompt Hacking
🔨 Tooling
💪 Prompt Tuning
🎲 Serba aneka
Daftar Pustaka
📦 Prompted Products
🛸 Sumber Daya Tambahan
🔥 Hot Topics
✨ Credits

Kode sebagai Penalaran

🟦 This article is rated medium
Reading Time: 2 minutes
Last updated on August 7, 2024

Sander Schulhoff

Model Bahasa yang Didukung Program/Program-aided Language Models (PAL) adalah contoh lain dari sistem MRKL. Ketika diberikan pertanyaan, PAL dapat menulis kode yang menyelesaikan pertanyaan tersebut. Mereka mengirim kode ke runtime pemrograman untuk mendapatkan hasilnya. Mereka mengirimkan kode ke programmatic runtime untuk mendapatkan hasilnya. PAL bekerja berbeda dengan CoT; penalaran menengah PAL adalah kode, sedangkan CoT adalah bahasa alami.

Contoh PAL (Gao et al.)

Satu hal penting yang perlu dicatat adalah bahwa PAL sebenarnya memadukan bahasa alami (NL) dan kode. Dalam gambar di atas, berwarna biru adalah reasoning bahasa alami yang dihasilkan oleh PAL. Meskipun itu tidak ditampilkan dalam gambar, PAL sebenarnya menghasilkan '#' sebelum setiap baris penalaran NL, sehingga mereka diinterpretasikan sebagai komentar oleh runtime programmatic.

Contoh

Mari kita lihat contoh penyelesaian PAL dalam menjawab pertanyaan matematika. Saya menggunakan pertanyaan 3-shot, yang merupakan versi yang disederhanakan dari ini.

Saya akan menggunakan langchain, sebuah paket Python untuk menggabungkan fungsionalitas LLM untuk ini. Pertama, beberapa instalasi diperlukan:

!pip install langchain==0.0.26
!pip install openai
from langchain.llms import OpenAI
import os
os.environ["OPENAI_API_KEY"] = "sk-YOUR_KEY_HERE"

Kemudian, kita dapat membuat sebuah instansi GPT-3 davinci-002 (pemanggilan API terjadi ketika kita menggunakan objek ini)

llm = OpenAI(model_name='text-davinci-002', temperature=0)

Berikut adalah few shot prompt:

MATH_PROMPT = '''
Q: There were nine computers in the server room. Five more computers were installed each day, from monday to thursday. How many computers are now in the server room?

# solution in Python:
"""There were nine computers in the server room. Five more computers were installed each day, from monday to thursday. How many computers are now in the server room?"""
computers_initial = 9
computers_per_day = 5
num_days = 4  # 4 days between monday and thursday
computers_added = computers_per_day * num_days
computers_total = computers_initial + computers_added
result = computers_total
return result


Q: Shawn has five toys. For Christmas, he got two toys each from his mom and dad. How many toys does he have now?

# solution in Python:
"""Shawn has five toys. For Christmas, he got two toys each from his mom and dad. How many toys does he have now?"""
toys_initial = 5
mom_toys = 2
dad_toys = 2
total_received = mom_toys + dad_toys
total_toys = toys_initial + total_received
result = total_toys


Q: Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. How many lollipops did Jason give to Denny?

# solution in Python:
"""Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. How many lollipops did Jason give to Denny?"""
jason_lollipops_initial = 20
jason_lollipops_after = 12
denny_lollipops = jason_lollipops_initial - jason_lollipops_after
result = denny_lollipops

Q: {question}

# solution in Python:
'''

Sekarang kita bisa meneruskan prompt gabungan ke GPT-3.

llm_out = llm(MATH_PROMPT.format(question=question))
print(llm_out)

Hasilnya adalah:

Emma took a 60 minute plane ride to seattle. She then took a 2 hour train ride to portland, and then a 30 minute bus ride to vancouver. How long did it take her to get to vancouver?



plane_ride = 60


train_ride = 2 * 60 # 2 hours in minutes


bus_ride = 30


total_time = plane_ride + train_ride + bus_ride


result = total_time

Akhirnya, kita bisa meneruskan kode ini ke runtime Python untuk mendapatkan jawabannya:

exec(llm_out)
print(result)

Keluaran adalah 210, yang benar.

Lihat notebook Jupyter untuk contoh ini di sini.

Lebih Banyak

Lihat juga contoh kolaborasi PAL's .

Sander Schulhoff

Sander Schulhoff is the CEO of HackAPrompt and Learn Prompting. He created the first Prompt Engineering guide on the internet, two months before ChatGPT was released, which has taught 3 million people how to prompt ChatGPT. He also partnered with OpenAI to run the first AI Red Teaming competition, HackAPrompt, which was 2x larger than the White House's subsequent AI Red Teaming competition. Today, HackAPrompt partners with the Frontier AI labs to produce research that makes their models more secure. Sander's background is in Natural Language Processing and deep reinforcement learning. He recently led the team behind The Prompt Report, the most comprehensive study of prompt engineering ever done. This 76-page survey, co-authored with OpenAI, Microsoft, Google, Princeton, Stanford, and other leading institutions, analyzed 1,500+ academic papers and covered 200+ prompting techniques.

Footnotes

  1. Gao, L., Madaan, A., Zhou, S., Alon, U., Liu, P., Yang, Y., Callan, J., & Neubig, G. (2022). 2