|
| 1 | +import BingSearchService |
| 2 | +import ChatContextCollector |
| 3 | +import Foundation |
| 4 | +import OpenAIService |
| 5 | +import Preferences |
| 6 | +import SuggestionModel |
| 7 | + |
| 8 | +public struct WebChatContextCollector: ChatContextCollector { |
| 9 | + public init() {} |
| 10 | + |
| 11 | + public func generateContext( |
| 12 | + history: [ChatMessage], |
| 13 | + scopes: Set<String>, |
| 14 | + content: String |
| 15 | + ) -> ChatContext? { |
| 16 | + guard scopes.contains("web") else { return nil } |
| 17 | + return .init( |
| 18 | + systemPrompt: "You prefer to answer questions with latest latest on the internet.", |
| 19 | + functions: [ |
| 20 | + SearchFunction(), |
| 21 | + ] |
| 22 | + ) |
| 23 | + } |
| 24 | + |
| 25 | + struct SearchFunction: ChatGPTFunction { |
| 26 | + static let dateFormatter = { |
| 27 | + let it = DateFormatter() |
| 28 | + it.dateFormat = "yyyy-MM-dd" |
| 29 | + return it |
| 30 | + }() |
| 31 | + |
| 32 | + struct Arguments: Codable { |
| 33 | + var query: String |
| 34 | + var freshness: String? |
| 35 | + } |
| 36 | + |
| 37 | + struct Result: ChatGPTFunctionResult { |
| 38 | + var result: BingSearchResult |
| 39 | + |
| 40 | + var botReadableContent: String { |
| 41 | + result.webPages.value.enumerated().map { |
| 42 | + let (index, page) = $0 |
| 43 | + return """ |
| 44 | + \(index + 1). \(page.name) \(page.url) |
| 45 | + \(page.snippet) |
| 46 | + """ |
| 47 | + }.joined(separator: "\n") |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + var name: String { |
| 52 | + "searchWeb" |
| 53 | + } |
| 54 | + |
| 55 | + var description: String { |
| 56 | + "Useful for when you need to answer questions about latest information." |
| 57 | + } |
| 58 | + |
| 59 | + var argumentSchema: JSONSchemaValue { |
| 60 | + let today = Self.dateFormatter.string(from: Date()) |
| 61 | + return [ |
| 62 | + .type: "object", |
| 63 | + .properties: [ |
| 64 | + "query": [ |
| 65 | + .type: "string", |
| 66 | + .description: "the search query", |
| 67 | + ], |
| 68 | + "freshness": [ |
| 69 | + .type: "string", |
| 70 | + .description: .string( |
| 71 | + "limit the search result to a specific range, use only when user ask the question about current events. Today is \(today). Format: yyyy-MM-dd..yyyy-MM-dd" |
| 72 | + ), |
| 73 | + .examples: ["1919-10-20..1988-10-20"], |
| 74 | + ], |
| 75 | + ], |
| 76 | + .required: ["query"], |
| 77 | + ] |
| 78 | + } |
| 79 | + |
| 80 | + func message(at phase: ChatGPTFunctionCallPhase) -> String { |
| 81 | + func parseArgument(_ string: String) throws -> Arguments { |
| 82 | + try JSONDecoder().decode(Arguments.self, from: string.data(using: .utf8) ?? Data()) |
| 83 | + } |
| 84 | + |
| 85 | + switch phase { |
| 86 | + case .detected: |
| 87 | + return "Searching.." |
| 88 | + case let .processing(argumentsJsonString): |
| 89 | + do { |
| 90 | + let arguments = try parseArgument(argumentsJsonString) |
| 91 | + return "Searching \(arguments.query)" |
| 92 | + } catch { |
| 93 | + return "Searching.." |
| 94 | + } |
| 95 | + case let .ended(argumentsJsonString, result): |
| 96 | + do { |
| 97 | + let arguments = try parseArgument(argumentsJsonString) |
| 98 | + if let result = result as? Result { |
| 99 | + return """ |
| 100 | + Finish searching \(arguments.query) |
| 101 | + \( |
| 102 | + result.result.webPages.value |
| 103 | + .map { "- [\($0.name)](\($0.url))" } |
| 104 | + .joined(separator: "\n") |
| 105 | + ) |
| 106 | + """ |
| 107 | + } |
| 108 | + return "Finish searching \(arguments.query)" |
| 109 | + } catch { |
| 110 | + return "Finish searching" |
| 111 | + } |
| 112 | + case let .error(argumentsJsonString, _): |
| 113 | + do { |
| 114 | + let arguments = try parseArgument(argumentsJsonString) |
| 115 | + return "Failed searching \(arguments.query)" |
| 116 | + } catch { |
| 117 | + return "Failed searching" |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + func call(arguments: Arguments) async throws -> Result { |
| 123 | + let bingSearch = BingSearchService( |
| 124 | + subscriptionKey: UserDefaults.shared.value(for: \.bingSearchSubscriptionKey), |
| 125 | + searchURL: UserDefaults.shared.value(for: \.bingSearchEndpoint) |
| 126 | + ) |
| 127 | + let result = try await bingSearch.search( |
| 128 | + query: arguments.query, |
| 129 | + numberOfResult: UserDefaults.shared.value(for: \.chatGPTMaxToken) > 5000 ? 5 : 3, |
| 130 | + freshness: arguments.freshness |
| 131 | + ) |
| 132 | + |
| 133 | + let content = result.webPages.value.enumerated().map { |
| 134 | + let (index, page) = $0 |
| 135 | + return """ |
| 136 | + \(index + 1). \(page.name) \(page.url) |
| 137 | + \(page.snippet) |
| 138 | + """ |
| 139 | + }.joined(separator: "\n") |
| 140 | + |
| 141 | + return .init(result: result) |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
0 commit comments