Last active
December 30, 2022 08:49
-
-
Save kkdai/7f099ee6613374805292f8b8e9ca1484 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
gpt3 "github.com/sashabaranov/go-gpt3" | |
) | |
var client *gpt3.Client | |
``` | |
//初始化 | |
apiKey := os.Getenv("ChatGptToken") | |
if apiKey != "" { | |
client = gpt3.NewClient(apiKey) | |
} | |
``` | |
func CompleteContext(ori string) (ret string) { | |
// 取得 context | |
ctx := context.Background() | |
// 主要 API Open AI Completion https://beta.openai.com/docs/guides/completion | |
req := gpt3.CompletionRequest{ | |
// Model: Davinci003 成果最好,但是也最慢。 | |
Model: gpt3.GPT3TextDavinci003, | |
// 最大輸出內容,可以調整一下。 | |
MaxTokens: 300, | |
// 輸入文字,也就是你平時在 ChatGPT 詢問他的問題。 | |
Prompt: ori, | |
} | |
resp, err := client.CreateCompletion(ctx, req) | |
if err != nil { | |
ret = fmt.Sprintf("Err: %v", err) | |
} else { | |
// 回來的成果中,拿精準度最高的為答案。 | |
ret = resp.Choices[0].Text | |
} | |
return ret | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment