|
| 1 | +import AIModel |
| 2 | +import Foundation |
| 3 | +import Logger |
| 4 | + |
| 5 | +struct OllamaEmbeddingService: EmbeddingAPI { |
| 6 | + struct EmbeddingRequestBody: Encodable { |
| 7 | + var prompt: String |
| 8 | + var model: String |
| 9 | + } |
| 10 | + |
| 11 | + struct ResponseBody: Decodable { |
| 12 | + var embedding: [Float] |
| 13 | + } |
| 14 | + |
| 15 | + let model: EmbeddingModel |
| 16 | + let endpoint: String |
| 17 | + |
| 18 | + public func embed(text: String) async throws -> EmbeddingResponse { |
| 19 | + guard let url = URL(string: endpoint) else { throw ChatGPTServiceError.endpointIncorrect } |
| 20 | + var request = URLRequest(url: url) |
| 21 | + request.httpMethod = "POST" |
| 22 | + let encoder = JSONEncoder() |
| 23 | + request.httpBody = try encoder.encode(EmbeddingRequestBody( |
| 24 | + prompt: text, |
| 25 | + model: model.info.modelName |
| 26 | + )) |
| 27 | + request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
| 28 | + |
| 29 | + let (result, response) = try await URLSession.shared.data(for: request) |
| 30 | + guard let response = response as? HTTPURLResponse else { |
| 31 | + throw ChatGPTServiceError.responseInvalid |
| 32 | + } |
| 33 | + |
| 34 | + guard response.statusCode == 200 else { |
| 35 | + let error = try? JSONDecoder().decode( |
| 36 | + OpenAIChatCompletionsService.CompletionAPIError.self, |
| 37 | + from: result |
| 38 | + ) |
| 39 | + throw error ?? ChatGPTServiceError |
| 40 | + .otherError(String(data: result, encoding: .utf8) ?? "Unknown Error") |
| 41 | + } |
| 42 | + |
| 43 | + let embeddingResponse = try JSONDecoder().decode(ResponseBody.self, from: result) |
| 44 | + #if DEBUG |
| 45 | + Logger.service.info(""" |
| 46 | + Embedding usage |
| 47 | + - number of strings: \(text.count) |
| 48 | + - prompt tokens: N/A |
| 49 | + - total tokens: \(embeddingResponse.embedding.count) |
| 50 | +
|
| 51 | + """) |
| 52 | + #endif |
| 53 | + return .init( |
| 54 | + data: [.init( |
| 55 | + embedding: embeddingResponse.embedding, |
| 56 | + index: 0, |
| 57 | + object: model.info.modelName |
| 58 | + )], |
| 59 | + model: model.info.modelName, |
| 60 | + usage: .init(prompt_tokens: 0, total_tokens: embeddingResponse.embedding.count) |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + public func embed(texts: [String]) async throws -> EmbeddingResponse { |
| 65 | + try await withThrowingTaskGroup(of: EmbeddingResponse.self) { group in |
| 66 | + for text in texts { |
| 67 | + _ = group.addTaskUnlessCancelled { |
| 68 | + try await self.embed(text: text) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + var result = EmbeddingResponse( |
| 73 | + data: [], |
| 74 | + model: model.info.modelName, |
| 75 | + usage: .init(prompt_tokens: 0, total_tokens: 0) |
| 76 | + ) |
| 77 | + |
| 78 | + for try await response in group { |
| 79 | + result.data.append(contentsOf: response.data) |
| 80 | + result.usage.prompt_tokens += response.usage.prompt_tokens |
| 81 | + result.usage.total_tokens += response.usage.total_tokens |
| 82 | + } |
| 83 | + |
| 84 | + return result |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public func embed(tokens: [[Int]]) async throws -> EmbeddingResponse { |
| 89 | + throw CancellationError() |
| 90 | + } |
| 91 | +} |
| 92 | + |
0 commit comments