-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.go
57 lines (48 loc) · 1.14 KB
/
prompt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2023 Explore.dev Unipessoal Lda. All Rights Reserved.
// Use of this source code is governed by a license that can be
// found in the LICENSE file
package openai
import (
"context"
"errors"
"fmt"
"time"
openai "github.com/sashabaranov/go-openai"
)
// Prompt prompts the OpenAI API with a list of messages using 0.000001 temperature and a retry mechanism.
func (c *OpenAIClient) Prompt(ctx context.Context, messages []openai.ChatCompletionMessage) (string, error) {
var err error
var resp openai.ChatCompletionResponse
for i := 0; i < 3; i++ {
resp, err = c.Client.CreateChatCompletion(
ctx,
openai.ChatCompletionRequest{
Model: c.Model,
Messages: messages,
Temperature: 0.000001,
},
)
if err == nil {
break
}
fmt.Printf("[debug] repeating request\n")
e := &openai.APIError{}
if errors.As(err, &e) {
switch e.HTTPStatusCode {
case 429:
delta := 10 * (i + 1)
time.Sleep(time.Duration(delta) * time.Second)
continue
case 500:
continue
default:
break
}
}
}
if err != nil {
return "", err
}
reply := resp.Choices[0].Message.Content
return reply, nil
}