go gpt3
v1.2.0
OpenAI GPT-3 API客戶端,使GO/Golang程序可以與GPT3 API進行交互。
支持使用或沒有流媒體的完成API。
簡單用法調用主要GPT-3 API,完成:
client := gpt3 . NewClient ( apiKey )
resp , err := client . Completion ( ctx , gpt3. CompletionRequest {
Prompt : [] string { "2, 3, 5, 7, 11," },
})
fmt . Print ( resp . Choices [ 0 ]. Text )
// prints " 13, 17, 19, 23, 29, 31", etc 請查看GO文檔,以獲取有關提供的類型和方法的更詳細的文檔:https://pkg.go.dev/github.com/pullrequestinc/go-gpt3
嘗試將內容放在main.go中,然後運行go run main.go嘗試一下這些示例。我建議使用GO模塊,在這種情況下,您還需要在測試存儲庫中運行go mod init 。另外,您可以克隆此存儲庫並使用go run cmd/test/main.go運行測試腳本。
您還需要有一個看起來像這樣的.env文件才能使用以下示例:
API_KEY=<openAI API Key>
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/PullRequestInc/go-gpt3"
"github.com/joho/godotenv"
)
func main () {
godotenv . Load ()
apiKey := os . Getenv ( "API_KEY" )
if apiKey == "" {
log . Fatalln ( "Missing API KEY" )
}
ctx := context . Background ()
client := gpt3 . NewClient ( apiKey )
resp , err := client . Completion ( ctx , gpt3. CompletionRequest {
Prompt : [] string { "The first thing you should know about javascript is" },
MaxTokens : gpt3 . IntPtr ( 30 ),
Stop : [] string { "." },
Echo : true ,
})
if err != nil {
log . Fatalln ( err )
}
fmt . Println ( resp . Choices [ 0 ]. Text )
}