Completion Parameter – Top P

Top P mirip dengan Temparature, berguna mengatur “kreatifitas” dan tingakat random dari output. Default value adalah 1.

Top P akan mengontrol set dari kata yang dapat dipilih oleh model. Top P membatasi kata-kata kandidat ke set terkecil yang probabilitas kumulatifnya lebih besar dari atau sama dengan ambang batas “p” yang diberikan. Jadi seperti windowing.

Top P tidak mengubah probabilitas dari kata, tetapi mengatur ukuran window kumpulan kata yang akan dipilih.

Berikut penjelsana sederhananya, misalkan terdapat list words dari favorite food dengan probabilitas seperti berikut:

  • Pizza : 14%
  • Hamburger: 6%
  • French Fries : 4%
  • Pancake : 2%
  • Hotdog : 1%

Jika nilai Top P adalah 0.2 maka, word yang akan dipilih adalah Pizza dan Hamburger, karena total kumulatif probabilitasnya adalah 20% atau 0.2.

Berikut contoh demo code untuk menunjukan completion dari top-p yang berbeda dengan prompt yang sama.

import openai
from dotenv import dotenv_values
import textwrap

config = dotenv_values(".env")
openai.api_key = config["OPENAI_KEY"]

def nice_print(dictionary):
    for key, value in dictionary.items():
        wrapped_text = "\n".join(textwrap.wrap(value, width=120))
        print(f"{key}:")
        print(wrapped_text)
        print("-"*100)

nice_print({
    f"Top P {top_p}": openai.Completion.create(model="text-davinci-003", prompt="""My favorite food is""".strip(), max_tokens =75, top_p=top_p, echo=True)
    .choices[0]["text"]
    .strip()
    for top_p in [1, 0.5, 0]
})
Top P 1:
My favorite food is Chinese food. I love the wide variety of dishes, from crunchy vegetable stir-fries to hearty noodle
soups. Some of my favorites include spicy Sichuan Kung Pao chicken, delicate dim sum dumplings, and savory noodle soups.
There's something for everyone, which is what I like the most about Chinese food.
----------------------------------------------------------------------------------------------------
Top P 0.5:
My favorite food is pizza. I love the combination of the doughy crust, the melted cheese, and the variety of toppings.
Pizza is also a great food to share with friends and family. Whether it's a thin crust, deep dish, or a stuffed crust,
there's something for everyone. I also love that pizza can be customized to suit individual tastes.
----------------------------------------------------------------------------------------------------
Top P 0:
My favorite food is pizza. I love the combination of the doughy crust, the tangy tomato sauce, and the melty cheese. I
also like to add different toppings like pepperoni, mushrooms, and olives. Pizza is a great meal for any occasion,
whether it's a casual night in or a special celebration.
----------------------------------------------------------------------------------------------------

Setelah melakukan beberapa kali request, kami bisa mendapatkan variasi untuk Top p = 1. Pada contoh diatas favorite food untuk top_p = 1 adalah Chinese Food.

Jadi semakin tinggi nilai top_p, semakin random response yang diberikan. Walaupun pada kasus ini, perlu dilakukan beberapa kali query.

Dokumentasi OpenAI menyarankan untuk menggunakan salah satu saja, top_p atau temperature.

Sharing is caring: