forked from jonbrenner/bot-pages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai_adapter.go
49 lines (39 loc) · 960 Bytes
/
openai_adapter.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
package main
import (
"context"
"errors"
"fmt"
"io"
"github.com/sashabaranov/go-openai"
)
type OpenAIAdapter struct {
APIKey string
}
func CreateRequest(prompt string) openai.CompletionRequest {
return openai.CompletionRequest{
Model: openai.GPT3Dot5TurboInstruct,
MaxTokens: 1024,
Prompt: promptPrefix + prompt + promptStartText,
Stream: false,
Temperature: 0.05,
}
}
func (adapter *OpenAIAdapter) FetchCompletionStream(req openai.CompletionRequest, respCh chan<- string) error {
defer close(respCh)
client := openai.NewClient(adapter.APIKey)
ctx := context.Background()
stream, err := client.CreateCompletionStream(ctx, req)
if err != nil {
return fmt.Errorf("error creating completion stream: %v", err)
}
for {
response, err := stream.Recv()
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
return fmt.Errorf("stream error: %v", err)
}
respCh <- response.Choices[0].Text
}
}