zai_rs/tool/web_search/
data.rs1use crate::client::http::HttpClient;
2use crate::tool::web_search::{request::*, response::*};
3use async_trait::async_trait;
4
5pub struct WebSearchRequest {
7 pub key: String,
9 body: WebSearchBody,
11}
12
13impl WebSearchRequest {
14 pub fn new(key: String, search_query: String, search_engine: SearchEngine) -> Self {
21 Self {
22 key,
23 body: WebSearchBody::new(search_query, search_engine),
24 }
25 }
26
27 pub fn with_body(key: String, body: WebSearchBody) -> Self {
29 Self { key, body }
30 }
31
32 pub fn with_search_intent(mut self, enabled: bool) -> Self {
34 self.body = self.body.with_search_intent(enabled);
35 self
36 }
37
38 pub fn with_count(mut self, count: i32) -> Self {
40 self.body = self.body.with_count(count);
41 self
42 }
43
44 pub fn with_domain_filter(mut self, domain: String) -> Self {
46 self.body = self.body.with_domain_filter(domain);
47 self
48 }
49
50 pub fn with_recency_filter(mut self, filter: SearchRecencyFilter) -> Self {
52 self.body = self.body.with_recency_filter(filter);
53 self
54 }
55
56 pub fn with_content_size(mut self, size: ContentSize) -> Self {
58 self.body = self.body.with_content_size(size);
59 self
60 }
61
62 pub fn with_request_id(mut self, request_id: String) -> Self {
64 self.body = self.body.with_request_id(request_id);
65 self
66 }
67
68 pub fn with_user_id(mut self, user_id: String) -> Self {
70 self.body = self.body.with_user_id(user_id);
71 self
72 }
73
74 pub fn validate(&self) -> anyhow::Result<()> {
76 self.body.validate_constraints()
77 }
78
79 pub async fn send(&self) -> anyhow::Result<WebSearchResponse> {
81 self.validate()?;
82 let resp: reqwest::Response = self.post().await?;
83 let parsed = resp.json::<WebSearchResponse>().await?;
84 Ok(parsed)
85 }
86}
87
88#[async_trait]
89impl HttpClient for WebSearchRequest {
90 type Body = WebSearchBody;
91 type ApiUrl = &'static str;
92 type ApiKey = String;
93
94 fn api_url(&self) -> &Self::ApiUrl {
95 &"https://open.bigmodel.cn/api/paas/v4/web_search"
96 }
97
98 fn api_key(&self) -> &Self::ApiKey {
99 &self.key
100 }
101
102 fn body(&self) -> &Self::Body {
103 &self.body
104 }
105}