kefu/wechathook/service/chatgpt_test.go

119 lines
3.1 KiB
Go

package service
import (
"encoding/json"
"errors"
"io"
"log"
"testing"
)
func TestChatGPT3Dot5Turbo(t *testing.T) {
gpt := NewChatGptTool("", "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")
message := []Gpt3Dot5Message{
{
Role: "system",
Content: "你是一个精通开发的资深工程师,熟悉全栈技术,任何问题都难不倒你",
},
{
Role: "user",
Content: "帮我使用golang开发一个在线客服系统",
},
}
res, err := gpt.ChatGPT3Dot5Turbo(message, "")
log.Println(res, err)
}
func TestChatGPT3Dot5Stream(t *testing.T) {
gpt := NewChatGptTool("https://salary-ai.openai.azure.com/", "1492e7c37d97415293b7140b9a83cb33")
message := []Gpt3Dot5Message{
{
Role: "system",
Content: "你是一个精通开发的资深工程师,熟悉全栈技术,任何问题都难不倒你",
},
{
Role: "user",
Content: "帮我使用golang开发一个在线客服系统",
},
}
stream, err := gpt.ChatGPT3Dot5TurboStream(message, "")
if err != nil {
log.Println(err)
return
}
for {
response, err := stream.Recv()
if errors.Is(err, io.EOF) {
log.Println("\nStream finished")
break
} else if err != nil {
log.Printf("\nStream error: %v\n", err)
break
} else {
log.Printf("%s,%v", response.Choices[0].Delta.Content, err)
}
}
stream.Close()
}
func TestGetEmbedding(t *testing.T) {
gpt := NewChatGptTool("https://openai.api2d.net", "fk188528-JAPbwe87SKzXwGBroAIdcOLfSC1bAMVU")
response, err := gpt.GetEmbedding("测试", "text-embedding-ada-002")
if err != nil {
t.Errorf("Error GetEmbedding: %v", err)
}
var embeddingResponse EmbeddingResponse
json.Unmarshal([]byte(response), &embeddingResponse)
log.Println(embeddingResponse)
}
func TestGetEmbedding2(t *testing.T) {
gpt := NewChatGptTool("https://salary-ai.openai.azure.com/", "1492e7c37d97415293b7140b9a83cb33")
response, err := gpt.GetEmbedding("测试", "text-embedding-ada-002")
if err != nil {
t.Errorf("Error GetEmbedding: %v", err)
}
var embeddingResponse EmbeddingResponse
json.Unmarshal([]byte(response), &embeddingResponse)
log.Println(embeddingResponse)
}
func TestGetWhisper(t *testing.T) {
gpt := NewChatGptTool("https://api.xi-ai.cn", "sk-UYxyuvFKOuiojF8jA529Ca2766804148AcE2F540E8A3398c")
response, err := gpt.GetWhisper("D:\\software\\python\\wework\\BigModel\\1.wav")
if err != nil {
t.Errorf("Error : %v", err)
}
log.Println(response)
}
// golang http接口请求openai流式响应
func TestOpenaiHTTPStream(t *testing.T) {
gpt := NewChatGptTool("https://api.xi-ai.cn/", "sk-UYxyuvFKOuiojF8jA529Ca2766804148AcE2F540E8A3398c")
message := []Gpt3Dot5Message{
{
Role: "system",
Content: "你是一个精通开发的资深工程师,熟悉全栈技术,任何问题都难不倒你",
},
{
Role: "user",
Content: "帮我使用golang开发一个在线客服系统,不超过30字",
},
}
stream, err := gpt.ChatGPTHttpStream(message, "", "2")
if err != nil {
log.Println(err)
return
}
for {
response, err := stream.Recv()
if err != nil {
break
}
if len(response.Choices) < 1 {
continue
}
log.Println(response.Choices[0].Delta.Content)
}
stream.Close()
}