package main

import (
	"context"
	"fmt"
	"github.com/openai/openai-go"
	"github.com/openai/openai-go/option"
	"os"
)

func main() {
	baseUrl := "https://wishub.ctyun.cn:1443/v1" //api前缀地址
	//从环境变量获取API密钥，如果没有设置，也可以直接终端执行export XIRANG_app_key="xxx"
	appkey := os.Getenv("XIRANG_app_key")
	modelId := "xxxxxx" //模型id
	prompt := "你好啊"  //对话问题

	client := openai.NewClient(
		option.WithAPIKey(appkey),
		option.WithBaseURL(baseUrl),
	)

	stream := client.Chat.Completions.NewStreaming(context.Background(), openai.ChatCompletionNewParams{
		Messages: openai.F([]openai.ChatCompletionMessageParamUnion{
			openai.ChatCompletionMessage{Content: prompt, Role: "user"},
		}),
		Model: openai.F(modelId),
	})

	for stream.Next() {
		evt := stream.Current()
		if len(evt.Choices) > 0 {
			fmt.Print(evt.Choices[0].Delta.Content)
		}
	}

	if err := stream.Err(); err != nil {
		panic(err)
	}
}
