go gpt3
v1.2.0
ไคลเอนต์ OpenAI GPT-3 API ที่เปิดใช้งานโปรแกรม GO/Golang เพื่อโต้ตอบกับ GPT3 APIs
รองรับการใช้ APIs เสร็จสมบูรณ์โดยมีหรือไม่มีสตรีมมิ่ง
การใช้งานง่าย ๆ เพื่อเรียก 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
และ runn go run main.go
ฉันขอแนะนำให้ใช้โมดูล GO ซึ่งในกรณีนี้คุณจะต้องเรียกใช้ go mod init
ภายในการทดสอบของคุณ หรือคุณสามารถโคลน repo นี้และเรียกใช้สคริปต์ทดสอบด้วย 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 )
}