Parameter n dan echo adalah dua parameter yang umum digunakan.
Parameter n
berguna untuk menentukan berapa completion yang kita harapkan. Nilai defaultnya adalah 1.
Hal yang perlu diperhatikan, jika Anda menggunakan nilai lebih dari 1, maka akan digunakan token yang lebih banyak, yang mana akan mempengaruhi biaya.
Ketika menggunakan parameter n, jika menggunakan max_tokens, maka max_tokens merujuk untuk masing-masing result text (masing-masing completion), bukan total token secara keseluruhan.
Berikut contoh penggunaan parameter n.
import openai from dotenv import dotenv_values config = dotenv_values(".env") openai.api_key = config["OPENAI_KEY"] openai.Completion.create( model = "text-davinci-003", prompt = "tell me a joke about death", max_tokens= 100, n = 3 )
Code diatas akan mengembalikan response:
<OpenAIObject text_completion id=cmpl-7KPIm3c90HTiOQ811qfZbPKjnvh65 at 0x2679610bd10> JSON: {
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"text": "\n\nQ: What did the skeleton say to the bartender?\nA: \"Give me a beer, and a mop.\""
},
{
"finish_reason": "stop",
"index": 1,
"logprobs": null,
"text": "\n\nQ: Why don't skeletons ever go out on the town? \nA: Because they don't have any body to go with."
},
{
"finish_reason": "stop",
"index": 2,
"logprobs": null,
"text": "\n\nQ. Why did the ghost go to the bar?\nA. To get a beer-ie-poltergeist!"
}
],
"created": 1685098176,
"id": "cmpl-7KPIm3c90HTiOQ811qfZbPKjnvh65",
"model": "text-davinci-003",
"object": "text_completion",
...
"completion_tokens": 84,
"prompt_tokens": 6,
"total_tokens": 90
}
}
Dapat kita lihat, terdapat 3 output text yang dihasilkan dengan masing-masing indexnya. Hal ini akan memudahkan dalam memproses output.
Tentu Anda bisa berargumen, kenapa tidak menggunakan prompt yang meminta 3 jokes. Betul, hal ini dapat dilakukan, namun Anda perlu ekstra kerja untuk membuat skrip yang memisahkan ketiga jokes yang dibundle dalam satu output text. (lihat contoh dibawah dimana prompt meminta 3 jokes melalui prompt.
<OpenAIObject text_completion id=cmpl-7KPOeopKMxGeb1yCfqEgZCI0QpDXA at 0x26796139720> JSON: {
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"text": "\n\n1. What did the zombie say to his friend after dying? \"It's been a grave experience!\" \n2. What did the headstone say to the grave? \"Don't worry, I've got your back!\" \n3. What did the lawyer name his daughter? Sue-cide!"
}
],
"created": 1685098540,
"id": "cmpl-7KPOeopKMxGeb1yCfqEgZCI0QpDXA",
"model": "text-davinci-003",
"object": "text_completion",
"usage": {
"completion_tokens": 65,
"prompt_tokens": 6,
"total_tokens": 71
}
}
Parameter echo
Berguna untuk menambahkan prompt pada completion text. Nilai defaultnya adalah false.
Berikut contoh penggunaan echo pada completion request.
openai.Completion.create( model = "text-davinci-003", prompt = "Q: What is the tallest building in the world?", max_tokens= 100, echo = True )
Code diatas akan menghasilkan result text yang berisi prompt text.
<OpenAIObject text_completion id=cmpl-7KPXiGf2YTTgqOqi6D4PUPaxm9RCA at 0x2679610bc20> JSON: {
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"text": "Q: What is the tallest building in the world?\n\nA: The tallest building in the world is the Burj Khalifa in Dubai, United Arab Emirates, standing at 828 metres (2,717 ft)."
}
],
"created": 1685099102,
"id": "cmpl-7KPXiGf2YTTgqOqi6D4PUPaxm9RCA",
"model": "text-davinci-003",
"object": "text_completion",
"usage": {
"completion_tokens": 35,
"prompt_tokens": 11,
"total_tokens": 46
}
}
Anda juga tidak perlu khawatir mengenai biaya karena, completion tokens tidak akan memperhitungkan prompt text yang ditambahkan. Completion tokens hanya akan dihitung berdasarkan output yang digenerate oleh model.
Anda bisa saja manual menambahkan prompt text tanpa perlu menggunakan parameter echo. Penggunaan parameter echo lebih praktis dan mudah.