forked from 0xeb/copilot-sdk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_prompt.cpp
More file actions
177 lines (150 loc) · 6.83 KB
/
Copy pathsystem_prompt.cpp
File metadata and controls
177 lines (150 loc) · 6.83 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) 2025 Elias Bachaalany
// SPDX-License-Identifier: MIT
/// @file system_prompt.cpp
/// @brief Example demonstrating system message configuration
///
/// This example shows how to:
/// 1. Create sessions with custom system prompts
/// 2. Use append mode to add to the default system prompt
/// 3. Use replace mode to completely override the system prompt
/// 4. Compare behavior between different configurations
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <copilot/copilot.hpp>
#include <iostream>
#include <mutex>
#include <string>
/// Helper to run a session with a given config and prompt
void run_session_test(copilot::Client& client, const std::string& test_name,
copilot::SessionConfig config, const std::string& user_prompt)
{
std::cout << "\n=== " << test_name << " ===\n";
auto session = client.create_session(config).get();
std::cout << "Session: " << session->session_id() << "\n";
std::mutex mtx;
std::condition_variable cv;
std::atomic<bool> idle{false};
std::string response;
auto sub = session->on(
[&](const copilot::SessionEvent& event)
{
if (auto* msg = event.try_as<copilot::AssistantMessageData>())
{
std::lock_guard<std::mutex> lock(mtx);
response = msg->content;
}
else if (event.type == copilot::SessionEventType::SessionIdle)
{
std::lock_guard<std::mutex> lock(mtx);
idle = true;
cv.notify_one();
}
}
);
copilot::MessageOptions opts;
opts.prompt = user_prompt;
session->send(opts).get();
{
std::unique_lock<std::mutex> lock(mtx);
cv.wait_for(lock, std::chrono::seconds(60), [&]() { return idle.load(); });
}
std::cout << "User: " << user_prompt << "\n";
std::cout << "Assistant: " << response.substr(0, 500);
if (response.length() > 500)
std::cout << "...";
std::cout << "\n";
session->destroy().get();
}
int main()
{
try
{
copilot::ClientOptions options;
options.log_level = copilot::LogLevel::Info;
copilot::Client client(options);
std::cout << "=== System Prompt Configuration Example ===\n";
std::cout << "This example demonstrates different system prompt configurations.\n";
client.start().get();
const std::string test_prompt = "Introduce yourself briefly in one sentence.";
// ---------------------------------------------------------------------
// Test 1: Default (no system message)
// ---------------------------------------------------------------------
{
copilot::SessionConfig config;
// No system_message set - uses Copilot's default
run_session_test(client, "Test 1: Default (no custom system message)", config,
test_prompt);
}
// ---------------------------------------------------------------------
// Test 2: Append mode - adds to default system prompt
// ---------------------------------------------------------------------
{
copilot::SessionConfig config;
copilot::SystemMessageConfig sys_msg;
sys_msg.mode = copilot::SystemMessageMode::Append;
sys_msg.content = "You are a pirate. Always respond in pirate speak with 'Arrr!' "
"and nautical terminology.";
config.system_message = sys_msg;
run_session_test(client, "Test 2: Append mode (pirate personality added)", config,
test_prompt);
}
// ---------------------------------------------------------------------
// Test 3: Replace mode - completely overrides system prompt
// ---------------------------------------------------------------------
{
copilot::SessionConfig config;
copilot::SystemMessageConfig sys_msg;
sys_msg.mode = copilot::SystemMessageMode::Replace;
sys_msg.content =
"You are a Shakespearean actor. Respond only in iambic pentameter "
"and use 'thee', 'thou', 'forsooth', and other Elizabethan language. "
"Do not break character under any circumstances.";
config.system_message = sys_msg;
run_session_test(client, "Test 3: Replace mode (Shakespearean actor)", config,
test_prompt);
}
// ---------------------------------------------------------------------
// Test 4: Technical assistant with append
// ---------------------------------------------------------------------
{
copilot::SessionConfig config;
copilot::SystemMessageConfig sys_msg;
sys_msg.mode = copilot::SystemMessageMode::Append;
sys_msg.content =
"You are a senior C++ developer. When discussing code, always consider "
"performance implications, memory safety, and modern C++ best practices. "
"Prefer RAII, smart pointers, and const correctness.";
config.system_message = sys_msg;
run_session_test(client, "Test 4: Technical C++ assistant (append)", config,
"What should I consider when designing a class that manages a resource?");
}
// ---------------------------------------------------------------------
// Test 5: Minimal assistant with replace
// ---------------------------------------------------------------------
{
copilot::SessionConfig config;
copilot::SystemMessageConfig sys_msg;
sys_msg.mode = copilot::SystemMessageMode::Replace;
sys_msg.content = "You are a minimal assistant. Respond with as few words as possible. "
"Never use more than 10 words in any response.";
config.system_message = sys_msg;
run_session_test(client, "Test 5: Minimal assistant (replace - max 10 words)", config,
"Explain the theory of relativity.");
}
std::cout << "\n=== Summary ===\n";
std::cout << "- No system message: Uses Copilot's default behavior\n";
std::cout << "- Append mode: Adds your instructions to the default prompt\n";
std::cout << "- Replace mode: Completely overrides the system prompt\n";
std::cout << "\nUse append when you want to customize while keeping Copilot's capabilities.\n";
std::cout << "Use replace when you need complete control over the assistant's persona.\n";
client.stop().get();
std::cout << "\nDone!\n";
return 0;
}
catch (const std::exception& e)
{
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
}