| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package chat
- import (
- "github.com/gin-gonic/gin"
- "net/http"
- "time"
- )
- import chatgpt "github.com/chatgp/chatgpt-go"
- func FootApiChatAsk(c *gin.Context) { // /ping是路由
- //输出json结果给调用方
- token := ""
- cfValue := ""
- cookies := []*http.Cookie{
- {
- Name: "__Secure-next-auth.session-token",
- Value: token,
- },
- {
- Name: "cf_clearance",
- Value: cfValue,
- },
- }
- chatClient := chatgpt.NewClient(
- chatgpt.WithDebug(true),
- chatgpt.WithTimeout(60*time.Second),
- chatgpt.WithCookies(cookies),
- )
- question := "hello"
- stream, err := chatClient.GetChatStream(question)
- if err != nil {
- c.JSON(200, gin.H{
- "message": err.Error(),
- })
- return
- }
- var answer string
- for text := range stream.Stream {
- answer += text.Content
- }
- c.JSON(200, gin.H{
- "message": answer,
- })
- }
|