forked from 0xeb/copilot-sdk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyok.cpp
More file actions
187 lines (163 loc) · 6.11 KB
/
Copy pathbyok.cpp
File metadata and controls
187 lines (163 loc) · 6.11 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
178
179
180
181
182
183
184
185
186
187
// Copyright (c) 2025 Elias Bachaalany
// SPDX-License-Identifier: MIT
/// @file byok.cpp
/// @brief Bring Your Own Key (BYOK) example demonstrating custom provider configuration
#include <atomic>
#include <condition_variable>
#include <copilot/copilot.hpp>
#include <cstdlib>
#include <iostream>
#include <mutex>
#include <string>
int main(int argc, char* argv[])
{
// Check for required environment variables or command-line args
std::string api_key;
std::string base_url = "https://api.openai.com/v1"; // Default to OpenAI
std::string model = "gpt-4";
// Try environment variables first
if (const char* env_key = std::getenv("OPENAI_API_KEY"))
api_key = env_key;
if (const char* env_url = std::getenv("OPENAI_BASE_URL"))
base_url = env_url;
if (const char* env_model = std::getenv("OPENAI_MODEL"))
model = env_model;
// Command-line overrides
for (int i = 1; i < argc; i++)
{
std::string arg = argv[i];
if (arg == "--api-key" && i + 1 < argc)
{
api_key = argv[++i];
}
else if (arg == "--base-url" && i + 1 < argc)
{
base_url = argv[++i];
}
else if (arg == "--model" && i + 1 < argc)
{
model = argv[++i];
}
else if (arg == "--help" || arg == "-h")
{
std::cout << "Usage: " << argv[0] << " [options]\n\n";
std::cout << "Options:\n";
std::cout << " --api-key KEY API key for the provider\n";
std::cout
<< " --base-url URL Base URL for the API (default: https://api.openai.com/v1)\n";
std::cout << " --model MODEL Model to use (default: gpt-4)\n";
std::cout << "\nEnvironment variables:\n";
std::cout << " OPENAI_API_KEY API key (overridden by --api-key)\n";
std::cout << " OPENAI_BASE_URL Base URL (overridden by --base-url)\n";
std::cout << " OPENAI_MODEL Model (overridden by --model)\n";
return 0;
}
}
if (api_key.empty())
{
std::cerr << "Error: API key required. Set OPENAI_API_KEY or use --api-key\n";
return 1;
}
try
{
std::cout << "=== Copilot SDK BYOK Example ===\n\n";
std::cout << "Provider configuration:\n";
std::cout << " Base URL: " << base_url << "\n";
std::cout << " Model: " << model << "\n";
std::cout << " API Key: " << std::string(api_key.length() - 4, '*')
<< api_key.substr(api_key.length() - 4) << "\n\n";
// Create client
copilot::ClientOptions options;
options.log_level = copilot::LogLevel::Info;
copilot::Client client(options);
std::cout << "Starting Copilot client...\n";
client.start().get();
// Create session with custom provider
copilot::SessionConfig session_config;
session_config.model = model;
// Configure custom provider
copilot::ProviderConfig provider;
provider.base_url = base_url;
provider.api_key = api_key;
provider.type = "openai"; // Provider type
session_config.provider = provider;
auto session = client.create_session(session_config).get();
std::cout << "Session created with custom provider\n\n";
// Synchronization
std::mutex mtx;
std::condition_variable cv;
std::atomic<bool> idle{false};
// Subscribe to events
auto subscription = session->on(
[&](const copilot::SessionEvent& event)
{
if (auto* msg = event.try_as<copilot::AssistantMessageData>())
{
std::cout << "\nAssistant: " << msg->content << "\n";
}
else if (auto* delta = event.try_as<copilot::AssistantMessageDeltaData>())
{
std::cout << delta->delta_content << std::flush;
}
else if (auto* usage = event.try_as<copilot::AssistantUsageData>())
{
std::cout << "\n\n[Usage: ";
if (usage->input_tokens)
std::cout << "in=" << static_cast<int>(*usage->input_tokens);
if (usage->output_tokens)
std::cout << " out=" << static_cast<int>(*usage->output_tokens);
if (usage->cost)
std::cout << " cost=$" << *usage->cost;
std::cout << "]\n";
}
else if (auto* error = event.try_as<copilot::SessionErrorData>())
{
std::cerr << "\nError: " << error->message << "\n";
std::lock_guard<std::mutex> lock(mtx);
idle = true;
cv.notify_one();
}
else if (event.type == copilot::SessionEventType::SessionIdle)
{
std::lock_guard<std::mutex> lock(mtx);
idle = true;
cv.notify_one();
}
}
);
// Interactive chat
std::cout << "Chat with " << model << " using your API key.\n";
std::cout << "Type 'quit' to exit.\n\n> ";
std::string line;
while (std::getline(std::cin, line))
{
if (line == "quit" || line == "exit")
break;
if (line.empty())
{
std::cout << "> ";
continue;
}
idle = false;
copilot::MessageOptions msg_opts;
msg_opts.prompt = line;
session->send(msg_opts).get();
// Wait for response
{
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [&idle]() { return idle.load(); });
}
std::cout << "\n> ";
}
// Cleanup
std::cout << "\nCleaning up...\n";
session->destroy().get();
client.stop().get();
return 0;
}
catch (const std::exception& e)
{
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
}