You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

56 lines
1.3 KiB

package bochasearch_test
import (
"bytes"
"context"
"yunyan/sys/websearch/bochasearch"
"encoding/json"
"fmt"
"net/http"
"testing"
)
func Test_Handle(t *testing.T) {
apiKey := "sk-7c4dc288a92b4efb9e811098207a1c31"
endpoint := "https://api.bochaai.com/v1/web-search"
// 请求参数
requestBody, _ := json.Marshal(map[string]interface{}{
"query": "衡阳市和深圳市面积对比",
"showSummary": true,
"count": 1,
})
// 发起请求
req, _ := http.NewRequestWithContext(context.Background(), "POST", endpoint, bytes.NewBuffer(requestBody))
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error:", err)
return
}
defer resp.Body.Close()
// 解析响应
var response bochasearch.BochaResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
fmt.Println("Decode error:", err)
return
}
fmt.Println("Response:", response)
}
func Test_Sys(t *testing.T) {
if err := bochasearch.OnInit(nil,
bochasearch.SetApiKey("sk-7c4dc288a92b4efb9e811098207a1c31"),
); err != nil {
return
} else {
results, err := bochasearch.Search(context.TODO(), "今日热点新闻", "noLimit", 3)
fmt.Println(results, err)
}
}