智普ai的并发调用-----go编写
//https://github.com/zhangbo2008/Concurrency_zhipuAI_call
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"sync"
"time"
)
var zhipuapikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" //===========input your key for 智普ai
func main() {
allmes := []string{"你好1", "今天天气如何", "你会不会死", "你是谁啊", "我是谁", "你好1", "今天天气如何", "你会不会死", "你是谁啊", "我是谁"}
var wg sync.WaitGroup
wg.Add(len(allmes)) // 添加两个子协程
t := time.Now()
aa := func(a11 string) string { //go不支持函数嵌套, 但是可以写匿名函数,然后在外面给他命名aa即可.go语法很弱智.
defer wg.Done()
type yitiao struct {
Role string `json:"role"`
Content string `json:"content"`
}
type yici struct {
Model string `json:"model"`
Messages []yitiao `json:"messages"`
}
a := yici{
Model: "glm-4-0520",
Messages: []yitiao{
{Role: "user", Content: a11},
},
}
a1, _ := json.Marshal(a)
print(a1)
targetUrl := "https://open.bigmodel.cn/api/paas/v4/chat/completions"
client := &http.Client{}
req, _ := http.NewRequest("POST", targetUrl, bytes.NewReader(a1))
req.Header.Add("Authorization", "Bearer "+zhipuapikey)
req.Header.Add("Content-Type", "application/json")
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
return string(body)
}
for _, x := range allmes {
go aa(x)
}
wg.Wait() // 等待所有子协程完成
t2 := time.Now()
fmt.Println("实用了多长时间", t2.Sub(t))
}
效果展示: 9条数据一起问用了4s多. 比串行快太多了.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"reflect"
"sync"
"time"
"github.com/gin-gonic/gin"
)
var zhipuapikey = "xxxxxxxxxxxxxxxxxxxxx" //===========input your key for 智普ai
func main3(allmes []string) []string {
// allmes := []string{"你好1", "今天天气如何", "你会不会死", "你是谁啊", "我是谁", "你好1", "今天天气如何", "你会不会死", "你是谁啊", "我是谁"}
var wg sync.WaitGroup
wg.Add(len(allmes)) // 添加两个子协程
t := time.Now()
result := make([]string, len(allmes))
aa := func(dex int, a11 string) string { //go不支持函数嵌套, 但是可以写匿名函数,然后在外面给他命名aa即可.go语法很弱智.
defer wg.Done()
type yitiao struct {
Role string `json:"role"`
Content string `json:"content"`
}
type yici struct {
Model string `json:"model"`
Messages []yitiao `json:"messages"`
}
a := yici{
Model: "glm-4-0520",
Messages: []yitiao{
{Role: "user", Content: a11},
},
}
a1, _ := json.Marshal(a)
print(a1)
targetUrl := "https://open.bigmodel.cn/api/paas/v4/chat/completions"
client := &http.Client{}
req, _ := http.NewRequest("POST", targetUrl, bytes.NewReader(a1))
req.Header.Add("Authorization", "Bearer "+zhipuapikey)
req.Header.Add("Content-Type", "application/json")
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
// fmt.Println(string(body))
result[dex] = string(body)
return string(body)
}
for dex, x := range allmes {
go aa(dex, x)
}
wg.Wait() // 等待所有子协程完成
t2 := time.Now()
fmt.Println("实用了多长时间", t2.Sub(t))
print("打印最终结果")
for _, x := range result {
println(x)
}
return result
}
func tosting(actual interface{}) []string {
var res []string
value := reflect.ValueOf(actual)
if value.Kind() != reflect.Slice && value.Kind() != reflect.Array {
return nil
}
for i := 0; i < value.Len(); i++ {
res = append(res, value.Index(i).Interface().(string))
}
return res
}
func main() {
//传统的Web服务写法
//http.HandleFunc("/hello", sayHello)
//err := http.ListenAndServe(":9090", nil)
//if err != nil {
// fmt.Printf("http server faile,err:%v\n", err)
// return
//}
//fmt.Println("项目启动成功")
//利用Gin框架的web写法,来源于gin官网
r := gin.Default()
r.POST("/bingfa", func(c *gin.Context) {
//获取参数
json := make(map[string]interface{}) //注意该结构接受的内容
c.BindJSON(&json)
log.Printf("%v", &json)
print(json["data"])
name := c.PostForm("name")
a111 := tosting(json["data"])
println(111111111111111111)
print(a111)
// var paramSlice []string
// for _, param := range json["data"] {
// paramSlice = append(paramSlice, param.(string))
// }
a3 := main3(a111)
println(a3)
print(name)
c.JSON(200, gin.H{
"message": a3,
})
})
_ = r.Run() // listen and serve on 0.0.0.0:8080
panic(r.Run())
}