Skip to content

Commit 0945ba9

Browse files
committed
Add settings for bing search
1 parent 13f4cbd commit 0945ba9

File tree

5 files changed

+93
-9
lines changed

5 files changed

+93
-9
lines changed

Core/Sources/ChatPlugins/SearchChatPlugin/SearchQuery.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import BingSearchService
22
import Foundation
33
import LangChain
4-
import PythonHelper
5-
import PythonKit
64

75
enum SearchEvent {
86
case startAction(String)
@@ -13,8 +11,8 @@ enum SearchEvent {
1311

1412
func search(_ query: String) async throws -> AsyncThrowingStream<SearchEvent, Error> {
1513
let bingSearch = BingSearchService(
16-
subscriptionKey: "",
17-
searchURL: ""
14+
subscriptionKey: UserDefaults.shared.value(for: \.bingSearchSubscriptionKey),
15+
searchURL: UserDefaults.shared.value(for: \.bingSearchEndpoint)
1816
)
1917

2018
final class LinkStorage {
@@ -30,7 +28,7 @@ func search(_ query: String) async throws -> AsyncThrowingStream<SearchEvent, Er
3028
run: {
3129
linkStorage.links = []
3230
let result = try await bingSearch.search(query: $0, numberOfResult: 5)
33-
guard let websites = result.webPages?.value else { return "No search results." }
31+
let websites = result.webPages.value
3432

3533
var string = ""
3634
for (index, website) in websites.enumerated() {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import AppKit
2+
import Client
3+
import OpenAIService
4+
import Preferences
5+
import SuggestionModel
6+
import SwiftUI
7+
8+
final class BingSearchViewSettings: ObservableObject {
9+
@AppStorage(\.bingSearchSubscriptionKey) var bingSearchSubscriptionKey: String
10+
@AppStorage(\.bingSearchEndpoint) var bingSearchEndpoint: String
11+
init() {}
12+
}
13+
14+
struct BingSearchView: View {
15+
@Environment(\.openURL) var openURL
16+
@StateObject var settings = BingSearchViewSettings()
17+
18+
var body: some View {
19+
Form {
20+
Button(action: {
21+
let url = URL(string: "https://www.microsoft.com/bing/apis/bing-web-search-api")!
22+
openURL(url)
23+
}) {
24+
Text("Apply for Subscription Key for Free")
25+
}
26+
27+
SecureField(text: $settings.bingSearchSubscriptionKey, prompt: Text("")) {
28+
Text("Bing Search Subscription Key")
29+
}
30+
.textFieldStyle(.roundedBorder)
31+
32+
TextField(
33+
text: $settings.bingSearchEndpoint,
34+
prompt: Text("https://api.bing.microsoft.com/***")
35+
) {
36+
Text("Bing Search Endpoint")
37+
}.textFieldStyle(.roundedBorder)
38+
}
39+
}
40+
}
41+
42+
struct BingSearchView_Previews: PreviewProvider {
43+
static var previews: some View {
44+
VStack(alignment: .leading, spacing: 8) {
45+
BingSearchView()
46+
}
47+
.frame(height: 800)
48+
.padding(.all, 8)
49+
}
50+
}
51+

Core/Sources/HostApp/ServiceView.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ struct ServiceView: View {
3939
subtitle: "Chat, Prompt to Code",
4040
image: "globe"
4141
)
42+
43+
ScrollView {
44+
BingSearchView().padding()
45+
}.sidebarItem(
46+
tag: 4,
47+
title: "Bing Search",
48+
subtitle: "Search Chat Plugin",
49+
image: "globe"
50+
)
4251
}
4352
}
4453
}

Tool/Sources/BingSearchService/BingSearchService.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22

33
public struct BingSearchResult: Codable {
4-
public var webPages: WebPages?
4+
public var webPages: WebPages
55

66
public struct WebPages: Codable {
77
public var webSearchUrl: String
@@ -18,6 +18,16 @@ public struct BingSearchResult: Codable {
1818
}
1919
}
2020

21+
struct BingSearchResponseError: Codable, Error, LocalizedError {
22+
struct E: Codable {
23+
var code: String?
24+
var message: String?
25+
}
26+
27+
var error: E
28+
var errorDescription: String? { error.message }
29+
}
30+
2131
enum BingSearchError: Error, LocalizedError {
2232
case searchURLFormatIncorrect(String)
2333

@@ -52,9 +62,13 @@ public struct BingSearchService {
5262
request.addValue(subscriptionKey, forHTTPHeaderField: "Ocp-Apim-Subscription-Key")
5363

5464
let (data, _) = try await URLSession.shared.data(for: request)
55-
let decoder = JSONDecoder()
56-
let result = try decoder.decode(BingSearchResult.self, from: data)
57-
return result
65+
do {
66+
let result = try JSONDecoder().decode(BingSearchResult.self, from: data)
67+
return result
68+
} catch {
69+
let e = try JSONDecoder().decode(BingSearchResponseError.self, from: data)
70+
throw e
71+
}
5872
}
5973
}
6074

Tool/Sources/Preferences/Keys.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,18 @@ public extension UserDefaultPreferenceKeys {
247247
}
248248
}
249249

250+
// MARK: - Bing Search
251+
252+
public extension UserDefaultPreferenceKeys {
253+
var bingSearchSubscriptionKey: PreferenceKey<String> {
254+
.init(defaultValue: "", key: "BingSearchSubscriptionKey")
255+
}
256+
257+
var bingSearchEndpoint: PreferenceKey<String> {
258+
.init(defaultValue: "https://api.bing.microsoft.com/v7.0/search/", key: "BingSearchEndpoint")
259+
}
260+
}
261+
250262
// MARK: - Custom Commands
251263

252264
public extension UserDefaultPreferenceKeys {

0 commit comments

Comments
 (0)