commit 85db3f71d44d015ac293f9645b58caa7e2481253 Author: binghuai <2586982002@qq.com> Date: Wed Feb 4 12:13:56 2026 +0800 初始化 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1ca1899 --- /dev/null +++ b/.gitignore @@ -0,0 +1,63 @@ +###################################################################### +# Build Tools + +.gradle +/build/ +!gradle/wrapper/gradle-wrapper.jar + +target/ +!.mvn/wrapper/maven-wrapper.jar + +###################################################################### +# IDE + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### JRebel ### +rebel.xml + +### NetBeans ### +nbproject/private/ +build/* +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ + +###################################################################### +# Others +*.log +*.xml.versionsBackup +*.swp + +!*/build/*.java +!*/build/*.html +!*/build/*.xml + +unpackage/* +.vscode/* +node_modules/* +BaiduSearchApp.exe.WebView2/* +BaiduSearchApp.exe.WebView2/ +cache/* +cache/ +Debug/* +Debug/ +dist/* +dist/ +dist_electron/ +dist_electron/* +unpackage/ +unpackage/* \ No newline at end of file diff --git a/BaiduSearch/BaiduSearchApp.sln b/BaiduSearch/BaiduSearchApp.sln new file mode 100644 index 0000000..eaf6728 --- /dev/null +++ b/BaiduSearch/BaiduSearchApp.sln @@ -0,0 +1,24 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.32014.148 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BaiduSearchApp", "BaiduSearchApp\BaiduSearchApp.csproj", "{3A9A5860-1D5E-4F4A-B0B9-010D75E91B3F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3A9A5860-1D5E-4F4A-B0B9-010D75E91B3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3A9A5860-1D5E-4F4A-B0B9-010D75E91B3F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3A9A5860-1D5E-4F4A-B0B9-010D75E91B3F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3A9A5860-1D5E-4F4A-B0B9-010D75E91B3F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B6D6F5A5-9F3E-4A0D-8B9C-1A4A6F7E8F90} + EndGlobalSection +EndGlobal \ No newline at end of file diff --git a/BaiduSearch/BaiduSearchApp/App.xaml b/BaiduSearch/BaiduSearchApp/App.xaml new file mode 100644 index 0000000..ede1228 --- /dev/null +++ b/BaiduSearch/BaiduSearchApp/App.xaml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/BaiduSearch/BaiduSearchApp/App.xaml.cs b/BaiduSearch/BaiduSearchApp/App.xaml.cs new file mode 100644 index 0000000..956ff19 --- /dev/null +++ b/BaiduSearch/BaiduSearchApp/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace BaiduSearchApp +{ + /// + /// App.xaml 的交互逻辑 + /// + public partial class App : Application + { + } +} \ No newline at end of file diff --git a/BaiduSearch/BaiduSearchApp/BaiduSearchApi.cs b/BaiduSearch/BaiduSearchApp/BaiduSearchApi.cs new file mode 100644 index 0000000..f9f142c --- /dev/null +++ b/BaiduSearch/BaiduSearchApp/BaiduSearchApi.cs @@ -0,0 +1,131 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace BaiduSearchApp +{ + public class BaiduSearchApi + { + private readonly string _apiKey; + private readonly HttpClient _httpClient; + private const string ApiUrl = "https://qianfan.baidubce.com/v2/ai_search/web_search"; + + public BaiduSearchApi(string apiKey) + { + _apiKey = apiKey; + _httpClient = new HttpClient(); + _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _apiKey); + _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + } + + public async Task SearchAsync(string query, int pageSize = 50) + { + if (string.IsNullOrEmpty(query)) + throw new ArgumentException("搜索内容不能为空", nameof(query)); + + var requestBody = new SearchRequest + { + Messages = new List + { + new Message { Content = query, Role = "user" } + }, + SearchSource = "baidu_search_v2", + ResourceTypeFilter = new List + { + new SearchResource { Type = "web", TopK = pageSize } + }, + SearchRecencyFilter = "year" + }; + + var jsonContent = JsonConvert.SerializeObject(requestBody); + var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); + + HttpResponseMessage response = await _httpClient.PostAsync(ApiUrl, content); + response.EnsureSuccessStatusCode(); + + string responseContent = await response.Content.ReadAsStringAsync(); + return JsonConvert.DeserializeObject(responseContent); + } + } + + // 请求相关类 + public class SearchRequest + { + [JsonProperty("messages")] + public List Messages { get; set; } + + [JsonProperty("search_source")] + public string SearchSource { get; set; } + + [JsonProperty("resource_type_filter")] + public List ResourceTypeFilter { get; set; } + + [JsonProperty("search_recency_filter")] + public string SearchRecencyFilter { get; set; } + + [JsonProperty("edition")] + public string Edition { get; set; } = "standard"; + } + + public class Message + { + [JsonProperty("content")] + public string Content { get; set; } + + [JsonProperty("role")] + public string Role { get; set; } + } + + public class SearchResource + { + [JsonProperty("type")] + public string Type { get; set; } + + [JsonProperty("top_k")] + public int TopK { get; set; } + } + + // 响应相关类 + public class SearchResponse + { + [JsonProperty("request_id")] + public string RequestId { get; set; } + + [JsonProperty("references")] + public List References { get; set; } + + [JsonProperty("code")] + public string Code { get; set; } + + [JsonProperty("message")] + public string ErrorMessage { get; set; } + } + + public class Reference + { + [JsonProperty("title")] + public string Title { get; set; } + + [JsonProperty("url")] + public string Url { get; set; } + + [JsonProperty("content")] + public string Content { get; set; } + + [JsonProperty("date")] + public string Date { get; set; } + + [JsonProperty("type")] + public string Type { get; set; } + + [JsonProperty("web_anchor")] + public string WebAnchor { get; set; } + + [JsonProperty("website")] + public string Website { get; set; } + } +} \ No newline at end of file diff --git a/BaiduSearch/BaiduSearchApp/BaiduSearchApp.csproj b/BaiduSearch/BaiduSearchApp/BaiduSearchApp.csproj new file mode 100644 index 0000000..bc480cf --- /dev/null +++ b/BaiduSearch/BaiduSearchApp/BaiduSearchApp.csproj @@ -0,0 +1,17 @@ + + + + WinExe + net7.0-windows + BaiduSearchApp + BaiduSearchApp + true + {3A9A5860-1D5E-4F4A-B0B9-010D75E91B3F} + 4 + + + + + + + \ No newline at end of file diff --git a/BaiduSearch/BaiduSearchApp/MainWindow.xaml b/BaiduSearch/BaiduSearchApp/MainWindow.xaml new file mode 100644 index 0000000..c9888b4 --- /dev/null +++ b/BaiduSearch/BaiduSearchApp/MainWindow.xaml @@ -0,0 +1,151 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +