-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcache.cpp
More file actions
26 lines (22 loc) · 898 Bytes
/
Copy pathcache.cpp
File metadata and controls
26 lines (22 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#define LLM_CACHE_IMPLEMENTATION
#include "llm_cache.hpp"
#include <cstdio>
int main() {
llm::CacheConfig cfg;
cfg.max_entries = 2; // tiny, to show LRU eviction
llm::ResponseCache cache(cfg);
int api_calls = 0;
auto ask = [&](const std::string& prompt) {
return cache.get_or_compute(prompt, [&] {
++api_calls; // your real model call goes here
return "answer #" + std::to_string(api_calls);
});
};
for (const char* p : {"What is RAII?", "what is raii?",
"Explain move semantics", "What is SFINAE?",
"What is RAII?"})
std::printf("%-24s -> %s\n", p, ask(p).c_str());
auto s = cache.stats();
std::printf("\napi calls %d | hits %zu | misses %zu | evictions %zu\n",
api_calls, s.hits, s.misses, s.evictions);
}