| #include "ctxstream.hpp" |
|
|
| #include <atomic> |
| #include <chrono> |
| #include <cstdio> |
| #include <mutex> |
| #include <thread> |
| #include <vector> |
|
|
| namespace ctxstream { |
|
|
| |
| |
| |
| static const char* kExtractSystem = |
| "You extract structured data from one fragment of a larger corpus.\n" |
| "Rules:\n" |
| "1. Output ONLY lines of the form <key><TAB><number>. Nothing else.\n" |
| "2. No preamble, no explanation, no markdown, no totals.\n" |
| "3. Count only what is present in THIS fragment. Do not guess about the rest.\n" |
| "4. If nothing matches, output exactly: NONE"; |
|
|
| std::vector<SegmentResult> stream(const std::string& text, |
| const std::vector<Segment>& segments, |
| const std::string& extract_prompt, |
| const BackendOptions& backend, |
| const PipelineOptions& pipe) { |
| std::vector<SegmentResult> results(segments.size()); |
| if (segments.empty()) return results; |
|
|
| std::atomic<std::size_t> next{0}; |
| std::atomic<int> done{0}, failed{0}; |
| std::mutex log_mu; |
| const auto t0 = std::chrono::steady_clock::now(); |
|
|
| |
| const int workers = |
| std::max(1, std::min<int>(pipe.concurrency, static_cast<int>(segments.size()))); |
|
|
| auto worker = [&] { |
| for (;;) { |
| const std::size_t i = next.fetch_add(1); |
| if (i >= segments.size()) return; |
|
|
| const Segment& seg = segments[i]; |
| const std::string body = segment_text(text, seg); |
| const std::string prompt = |
| extract_prompt + "\n\n--- FRAGMENT " + std::to_string(seg.index + 1) + |
| " of " + std::to_string(segments.size()) + " ---\n" + body; |
|
|
| SegmentResult r; |
| r.index = seg.index; |
| const auto s0 = std::chrono::steady_clock::now(); |
|
|
| for (int attempt = 0; attempt <= pipe.max_retries; ++attempt) { |
| const Completion c = complete(backend, kExtractSystem, prompt); |
| if (!c.ok) { r.error = c.error; continue; } |
| r.prompt_tokens = c.prompt_tokens; |
| r.output_tokens = c.output_tokens; |
| |
| |
| if (looks_truncated(prompt.size(), c.prompt_tokens)) { |
| r.truncated = true; |
| r.error = "server processed " + std::to_string(c.prompt_tokens) + |
| " tokens of a " + std::to_string(prompt.size()) + |
| "-char prompt"; |
| break; |
| } |
| r.raw = c.text; |
| r.ok = true; |
| r.error.clear(); |
| break; |
| } |
|
|
| r.seconds = std::chrono::duration<double>( |
| std::chrono::steady_clock::now() - s0).count(); |
| results[i] = r; |
|
|
| const int d = ++done; |
| if (!r.ok) ++failed; |
| if (pipe.verbose) { |
| std::lock_guard<std::mutex> lk(log_mu); |
| const double elapsed = std::chrono::duration<double>( |
| std::chrono::steady_clock::now() - t0).count(); |
| const double eta = d ? elapsed / d * (segments.size() - d) : 0.0; |
| |
| |
| std::fprintf(stderr, |
| "[ctxstream] failed=%d %d/%zu seg=%d %.1fs eta=%.1fm\n", |
| failed.load(), d, segments.size(), r.index, r.seconds, |
| eta / 60.0); |
| } |
| } |
| }; |
|
|
| std::vector<std::thread> pool; |
| pool.reserve(workers); |
| for (int i = 0; i < workers; ++i) pool.emplace_back(worker); |
| for (auto& t : pool) t.join(); |
|
|
| if (pipe.verbose && failed) { |
| std::fprintf(stderr, "[ctxstream] WARNING: %d/%zu segments failed\n", |
| failed.load(), segments.size()); |
| } |
| return results; |
| } |
|
|
| } |
|
|