Implementasi asli Golang untuk berinteraksi dengan mudah dengan OpenAI API.
https://beta.openai.com/docs/api-reference/
Anda dapat menggunakan variabel lingkungan untuk menyimpan kunci rahasia API
export OPENAI_KEY=[YOUR_KEY]
Untuk menginisialisasi mesin, gunakan ini:
e := openai . New ( os . Getenv ( "OPENAI_KEY" ))
Jika Anda ingin menggunakan model paling canggih untuk menghasilkan keluaran teks, pastikan Anda menggunakan "text-davinci-003". Model ini didefinisikan sebagai openai.ModelTextDavinci003
konstan.
Anda dapat menggunakan bundel Penyelesaian+Edit untuk membuat ulang respons berdasarkan konteks terakhir.
e := openai . New ( os . Getenv ( "OPENAI_KEY" ))
ctx := context . Background ()
completionResp , err := e . Completion ( ctx , & openai. CompletionOptions {
// Choose model, you can see list of available models in models.go file
Model : openai . ModelTextDavinci001 ,
// Number of completion tokens to generate response. By default - 1024
MaxTokens : 1200 ,
// Text to completion
Prompt : [] string { "Write a little bit of Wikipedia. What is that?" },
})
editResp , err := e . Edit ( ctx , & EditOptions {
Model : ModelTextDavinci001 ,
Input : completionResp . Choices [ 0 ],
Instruction : "Please rewrite a bit more and add more information about Wikipedia in different aspects. Please build based on that for 4 topics" ,
})
Jika diberi perintah, model akan menampilkan satu atau beberapa prediksi penyelesaian.
Catatan : jumlah token penyelesaian default adalah 1024, jika Anda ingin menambah atau mengurangi batas ini, Anda harus mengubah parameter MaxTokens
.
e := openai . New ( os . Getenv ( "OPENAI_KEY" ))
r , err := e . Completion ( context . Background (), & openai. CompletionOptions {
// Choose model, you can see list of available models in models.go file
Model : openai . ModelTextDavinci001 ,
// Number of completion tokens to generate response. By default - 1024
MaxTokens : 1200 ,
// Text to completion
Prompt : [] string { "Write a little bit of Wikipedia. What is that?" },
})
Anda akan mendapatkan output berikutnya:
{
"id" : " cmpl-6SrcYDLCVT7xyHKVNuSLNuhRvwOJ1 " ,
"object" : " text_completion " ,
"created" : 1672337322 ,
"model" : " text-davinci-001 " ,
"choices" : [
{
"text" : " nn Wikipedia is a free online encyclopedia, created and edited by volunteers. " ,
"index" : 0 ,
"finish_reason" : " stop "
}
],
"usage" : {
"prompt_tokens" : 11 ,
"completion_tokens" : 15 ,
"total_tokens" : 26
}
}
Untuk mendapatkan teks saja, Anda harus menggunakan kode berikutnya:
fmt . Println ( r . Choices [ 0 ]. Text )
Jadi, kode lengkapnya adalah:
package main
import (
"context"
"encoding/json"
"log"
"os"
"testing"
"github.com/0x9ef/openai-go"
)
func main () {
e := openai . New ( os . Getenv ( "OPENAI_KEY" ))
r , err := e . Completion ( context . Background (), & openai. CompletionOptions {
// Choose model, you can see list of available models in models.go file
Model : openai . ModelTextDavinci001 ,
// Text to completion
Prompt : [] string { "Write a little bit of Wikipedia. What is that?" },
})
if b , err := json . MarshalIndent ( r , "" , " " ); err != nil {
panic ( err )
} else {
fmt . Println ( string ( b ))
}
// Wikipedia is a free online encyclopedia, created and edited by volunteers.
fmt . Println ( "What is the Wikipedia?" , r . Choices [ 0 ]. Text )
}
Mencantumkan model yang tersedia saat ini, dan memberikan informasi dasar tentang masing-masing model seperti pemilik dan ketersediaan.
e := openai . New ( os . Getenv ( "OPENAI_KEY" ))
r , err := e . ListModels ( context . Background ())
if err != nil {
log . Fatal ( err )
}
Anda akan mendapatkan output berikutnya:
{
"data" : [
{
"id" : " babbage " ,
"object" : " model " ,
"owned_by" : " openai "
},
{
"id" : " ada " ,
"object" : " model " ,
"owned_by" : " openai "
},
{
"id" : " text-davinci-002 " ,
"object" : " model " ,
"owned_by" : " openai "
},
{
"id" : " davinci " ,
"object" : " model " ,
"owned_by" : " openai "
},
...
]
}
Untuk mengambil informasi tentang model tertentu dan bukan semua model, Anda dapat melakukan ini:
e := openai . New ( os . Getenv ( "OPENAI_KEY" ))
r , err := e . RetrieveModel ( context . Background (), & openai. RetrieveModelOptions {
ID : openai . ModelDavinci ,
})
if err != nil {
log . Fatal ( err )
}
MIT