Repository navigation
Expand file tree
/
Copy pathpersistent_chat.cpp
More file actions
46 lines (38 loc) · 1.48 KB
/
Copy pathpersistent_chat.cpp
File metadata and controls
46 lines (38 loc) · 1.48 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#define LLM_CHAT_IMPLEMENTATION
#include "llm_chat.hpp"
#include <cstdlib>
#include <fstream>
#include <iostream>
static const char* HISTORY_FILE = "chat_history.jsonl";
int main() {
const char* key = std::getenv("OPENAI_API_KEY");
if (!key || !*key) { std::cerr << "Set OPENAI_API_KEY\n"; return 1; }
llm::ChatConfig cfg;
cfg.api_key = key;
cfg.model = "gpt-4o-mini";
cfg.system_prompt = "You are a helpful assistant who remembers our conversation.";
cfg.max_tokens = 256;
llm::Conversation conv(cfg);
// Try loading previous session
std::ifstream f(HISTORY_FILE);
if (f) {
std::string jsonl((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
conv.deserialize(jsonl);
std::cout << "Loaded " << conv.message_count()
<< " messages from " << HISTORY_FILE << "\n\n";
} else {
std::cout << "Starting fresh session (no history file found)\n\n";
}
// Send a message
std::string reply = conv.chat("What did we talk about previously, if anything?");
std::cout << "AI: " << reply << "\n\n";
reply = conv.chat("Good. Remember that my favorite color is blue.");
std::cout << "AI: " << reply << "\n\n";
// Save for next session
std::ofstream out(HISTORY_FILE);
out << conv.serialize();
std::cout << "Session saved to " << HISTORY_FILE
<< " (" << conv.message_count() << " messages)\n";
return 0;
}