Skip to content

Commit c4ef01c

Browse files
authored
Merge pull request #12 from Chamepp/main
feat: getting the api key by using dependency and leveraging the SearchOptions from the developer
2 parents 476556b + d2d67af commit c4ef01c

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

Sources/SearchMind/Internal/SearchEngine.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -474,14 +474,17 @@ struct PatternMatchAlgorithm: SearchAlgorithm {
474474
struct GPTSemanticSearchAlgorithm: SearchAlgorithm {
475475
func search(term: String, type _: SearchType, options: SearchOptions) async throws -> [SearchMind.SearchResult] {
476476
let searchTerm = options.caseSensitive ? term : term.lowercased()
477-
let termEmbedding = try await embed(text: searchTerm)
477+
guard let apiKey = options.apiKey else {
478+
throw SearchError.missingKey
479+
}
480+
let termEmbedding = try await embed(text: searchTerm, apiKey: apiKey)
478481

479482
let files = try await getFilesToSearch(options: options)
480483
var results: [SearchMind.SearchResult] = []
481484

482485
for fileURL in files {
483486
let content = try String(contentsOf: fileURL)
484-
let fileEmbedding = try await embed(text: content)
487+
let fileEmbedding = try await embed(text: content, apiKey: apiKey)
485488

486489
let similarity = cosineSimilarity(termEmbedding, fileEmbedding)
487490
if similarity > 0.4 {
@@ -499,10 +502,7 @@ struct GPTSemanticSearchAlgorithm: SearchAlgorithm {
499502
.map { $0 }
500503
}
501504

502-
private func embed(text: String) async throws -> [Double] {
503-
guard let apiKey = ProcessInfo.processInfo.environment["OPENAI_API_KEY"] else {
504-
throw SearchError.missingKey
505-
}
505+
private func embed(text: String, apiKey: String) async throws -> [Double] {
506506
let request = EmbeddingRequest(model: "text-embedding-ada-002", input: [text])
507507
let jsonData = try JSONEncoder().encode(request)
508508

Sources/SearchMind/SearchMind.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,21 @@ public struct SearchOptions: Sendable {
369369
/// ```
370370
public let caseSensitive: Bool
371371

372+
/// A required API key for enabling AI-powered search algorithms, such as GPT-based semantic search.
373+
///
374+
/// This key must be provided when using any search strategy that relies on external AI services.
375+
/// If the key is missing and an AI-based algorithm is selected (e.g., `GPTSemanticSearchAlgorithm`),
376+
/// an error will be thrown or the search will fail.
377+
///
378+
/// - Important: This is mandatory for semantic searches. Omitting it while using AI algorithms
379+
/// will result in runtime errors or degraded functionality.
380+
///
381+
/// - Example:
382+
/// ```swift
383+
/// let options = SearchOptions(apiKey: "sk-...")
384+
/// ```
385+
public let apiKey: String?
386+
372387
/// Enables approximate (fuzzy) matching for file name searches
373388
///
374389
/// When `true` (default), file name searches will use Levenshtein distance
@@ -464,6 +479,7 @@ public struct SearchOptions: Sendable {
464479
/// - Returns: A configured SearchOptions instance
465480
public init(
466481
caseSensitive: Bool = false,
482+
apiKey: String? = ProcessInfo.processInfo.environment["OPENAI_API_KEY"],
467483
fuzzyMatching: Bool = true,
468484
semantic: Bool = false,
469485
patternMatch: Bool = false,
@@ -473,6 +489,7 @@ public struct SearchOptions: Sendable {
473489
timeout: TimeInterval? = nil
474490
) {
475491
self.caseSensitive = caseSensitive
492+
self.apiKey = apiKey
476493
self.fuzzyMatching = fuzzyMatching
477494
self.semantic = semantic
478495
self.patternMatch = patternMatch

0 commit comments

Comments
 (0)