package main
import (
"context"
"errors"
"fmt"
"io"
"log"
"os"
"github.com/joho/godotenv"
openai "github.com/sashabaranov/go-openai"
)
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Some error occured. Err: %s", err)
}
OPENAI_API_KEY := os.Getenv("OPENAI_API_KEY")
OPENAI_BASE_URL := os.Getenv("OPENAI_BASE_URL")
config := openai.DefaultConfig(OPENAI_API_KEY)
config.BaseURL = OPENAI_BASE_URL
c := openai.NewClientWithConfig(config) // openai.NewClient(OPENAI_API_KEY)
ctx := context.Background()
req := openai.ChatCompletionRequest{
Model: openai.GPT4o,
// MaxTokens: 128*1024 - 1,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: "your question",
},
},
Stream: true,
}
stream, err := c.CreateChatCompletionStream(ctx, req)
if err != nil {
fmt.Printf("ChatCompletionStream error: %v\n", err)
return
}
defer stream.Close()
fmt.Printf("Stream response: ")
for {
response, err := stream.Recv()
if errors.Is(err, io.EOF) {
fmt.Println("\nStream finished")
return
}
if err != nil {
fmt.Printf("\nStream error: %v\n", err)
return
}
fmt.Printf(response.Choices[0].Delta.Content)
}
}
标签:err,示例,fmt,golang,OPENAI,KEY,Printf,openai
From: https://www.cnblogs.com/soarowl/p/18261213