Skip to content

Commit 2f12b7e

Browse files
feat: Add optional prompt processing progress streaming
- Add return_progress parameter to slot_params (default: false) - Extend server_task_result_cmpl_partial with progress fields - Implement send_progress_response() function with batch completion logic - Add progress response in prompt processing loop - Update JSON response to include prompt_processing field when requested - Add comprehensive documentation to README.md - Add C++ test suite for progress feature validation - Ensure full backward compatibility with existing clients - Fix chat completions endpoint progress support Closes #14685
1 parent d31c734 commit 2f12b7e

File tree

3 files changed

+345
-32
lines changed

3 files changed

+345
-32
lines changed

tests/test-progress-feature.cpp

Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
#include <thread>
5+
#include <chrono>
6+
#include <cstdlib>
7+
#include <cstring>
8+
#include <cassert>
9+
#include <fstream>
10+
#include <sstream>
11+
#include <functional>
12+
13+
#ifdef _WIN32
14+
#include <winsock2.h>
15+
#include <ws2tcpip.h>
16+
#pragma comment(lib, "ws2_32.lib")
17+
#else
18+
#include <sys/socket.h>
19+
#include <netinet/in.h>
20+
#include <arpa/inet.h>
21+
#include <unistd.h>
22+
#include <netdb.h>
23+
#endif
24+
25+
class ProgressFeatureTest {
26+
private:
27+
std::string server_url;
28+
int server_port;
29+
30+
std::string create_long_prompt() {
31+
return R"(Please provide a comprehensive analysis of artificial intelligence and machine learning, including but not limited to:
32+
33+
1. Historical Development: Trace the evolution of AI from its early beginnings in the 1950s through the various AI winters and recent breakthroughs. Discuss key milestones such as the Dartmouth Conference, expert systems, neural networks, and deep learning.
34+
35+
2. Machine Learning Fundamentals: Explain the core concepts of supervised learning, unsupervised learning, and reinforcement learning. Describe different types of algorithms including decision trees, support vector machines, neural networks, and ensemble methods.
36+
37+
3. Deep Learning Revolution: Detail the resurgence of neural networks through deep learning, including convolutional neural networks (CNNs) for computer vision, recurrent neural networks (RNNs) and transformers for natural language processing, and generative adversarial networks (GANs).
38+
39+
4. Natural Language Processing: Discuss the evolution from rule-based systems to statistical methods to neural approaches. Cover topics like word embeddings, sequence-to-sequence models, attention mechanisms, and large language models like GPT, BERT, and their successors.
40+
41+
5. Computer Vision: Explore the development of computer vision from traditional image processing to deep learning approaches. Discuss object detection, image segmentation, face recognition, and recent advances in vision transformers.
42+
43+
6. Applications and Impact: Analyze how AI is transforming various industries including healthcare, finance, transportation, education, and entertainment. Discuss both the benefits and potential risks of AI deployment.
44+
45+
7. Ethical Considerations: Address important ethical issues such as bias in AI systems, privacy concerns, job displacement, and the need for responsible AI development and deployment.
46+
47+
8. Future Directions: Speculate on emerging trends in AI research, including multimodal AI, few-shot learning, explainable AI, and the pursuit of artificial general intelligence (AGI).
48+
49+
Please provide detailed explanations with specific examples and technical details where appropriate. This should be a thorough, academic-level analysis suitable for someone with a background in computer science or related fields.)";
50+
}
51+
52+
std::string create_completion_request(const std::string& prompt, bool return_progress = true) {
53+
std::ostringstream oss;
54+
oss << "POST /completion HTTP/1.1\r\n";
55+
oss << "Host: localhost:" << server_port << "\r\n";
56+
oss << "Content-Type: application/json\r\n";
57+
oss << "Connection: close\r\n";
58+
59+
std::string json_body = "{"
60+
"\"prompt\": \"" + escape_json_string(prompt) + "\","
61+
"\"stream\": true,"
62+
"\"return_progress\": " + (return_progress ? "true" : "false") + ","
63+
"\"max_tokens\": 20,"
64+
"\"temperature\": 0.7"
65+
"}";
66+
67+
oss << "Content-Length: " << json_body.length() << "\r\n";
68+
oss << "\r\n";
69+
oss << json_body;
70+
71+
return oss.str();
72+
}
73+
74+
std::string create_chat_completion_request(const std::string& prompt, bool return_progress = true) {
75+
std::ostringstream oss;
76+
oss << "POST /v1/chat/completions HTTP/1.1\r\n";
77+
oss << "Host: localhost:" << server_port << "\r\n";
78+
oss << "Content-Type: application/json\r\n";
79+
oss << "Connection: close\r\n";
80+
81+
std::string json_body = "{"
82+
"\"model\": \"test\","
83+
"\"messages\": [{\"role\": \"user\", \"content\": \"" + escape_json_string(prompt) + "\"}],"
84+
"\"stream\": true,"
85+
"\"return_progress\": " + (return_progress ? "true" : "false") + ","
86+
"\"max_tokens\": 20,"
87+
"\"temperature\": 0.7"
88+
"}";
89+
90+
oss << "Content-Length: " << json_body.length() << "\r\n";
91+
oss << "\r\n";
92+
oss << json_body;
93+
94+
return oss.str();
95+
}
96+
97+
std::string escape_json_string(const std::string& str) {
98+
std::string result;
99+
for (char c : str) {
100+
if (c == '"' || c == '\\' || c == '\n' || c == '\r' || c == '\t') {
101+
result += '\\';
102+
switch (c) {
103+
case '"': result += '"'; break;
104+
case '\\': result += '\\'; break;
105+
case '\n': result += 'n'; break;
106+
case '\r': result += 'r'; break;
107+
case '\t': result += 't'; break;
108+
}
109+
} else {
110+
result += c;
111+
}
112+
}
113+
return result;
114+
}
115+
116+
bool send_http_request(const std::string& request, std::string& response) {
117+
int sock = socket(AF_INET, SOCK_STREAM, 0);
118+
if (sock < 0) {
119+
std::cerr << "Failed to create socket" << std::endl;
120+
return false;
121+
}
122+
123+
struct sockaddr_in server_addr;
124+
server_addr.sin_family = AF_INET;
125+
server_addr.sin_port = htons(server_port);
126+
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
127+
128+
if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
129+
std::cerr << "Failed to connect to server" << std::endl;
130+
close(sock);
131+
return false;
132+
}
133+
134+
if (send(sock, request.c_str(), request.length(), 0) < 0) {
135+
std::cerr << "Failed to send request" << std::endl;
136+
close(sock);
137+
return false;
138+
}
139+
140+
char buffer[4096];
141+
response.clear();
142+
143+
while (true) {
144+
int bytes_received = recv(sock, buffer, sizeof(buffer) - 1, 0);
145+
if (bytes_received <= 0) break;
146+
147+
buffer[bytes_received] = '\0';
148+
response += buffer;
149+
}
150+
151+
close(sock);
152+
return true;
153+
}
154+
155+
bool parse_progress_responses(const std::string& response, std::vector<std::string>& progress_responses, std::vector<std::string>& content_responses) {
156+
std::istringstream iss(response);
157+
std::string line;
158+
159+
while (std::getline(iss, line)) {
160+
if (line.substr(0, 6) == "data: ") {
161+
std::string data = line.substr(6);
162+
if (data.find("\"n_prompt_tokens_processed\"") != std::string::npos || data.find("\"progress\"") != std::string::npos) {
163+
progress_responses.push_back(data);
164+
} else if (data.find("\"content\"") != std::string::npos || data.find("\"choices\"") != std::string::npos) {
165+
content_responses.push_back(data);
166+
}
167+
}
168+
}
169+
170+
return true;
171+
}
172+
173+
bool check_progress_completion(const std::vector<std::string>& progress_responses) {
174+
if (progress_responses.empty()) {
175+
return false;
176+
}
177+
178+
// Check if the last progress response shows 100% completion
179+
std::string last_response = progress_responses.back();
180+
return last_response.find("\"progress\":1.0") != std::string::npos;
181+
}
182+
183+
public:
184+
ProgressFeatureTest(int port = 8081) : server_port(port) {}
185+
186+
bool test_completion_endpoint_progress() {
187+
std::cout << "\n=== Testing /completion endpoint progress ===" << std::endl;
188+
189+
std::string prompt = create_long_prompt();
190+
std::string request = create_completion_request(prompt, true);
191+
std::string response;
192+
193+
if (!send_http_request(request, response)) {
194+
std::cout << "Failed to send request" << std::endl;
195+
return false;
196+
}
197+
198+
std::vector<std::string> progress_responses, content_responses;
199+
parse_progress_responses(response, progress_responses, content_responses);
200+
201+
std::cout << "Received " << progress_responses.size() << " progress responses" << std::endl;
202+
std::cout << "Received " << content_responses.size() << " content responses" << std::endl;
203+
204+
if (check_progress_completion(progress_responses)) {
205+
std::cout << "Progress reached 100% as expected" << std::endl;
206+
return true;
207+
} else {
208+
std::cout << "Progress did not reach 100%" << std::endl;
209+
return false;
210+
}
211+
}
212+
213+
bool test_chat_completion_endpoint_progress() {
214+
std::cout << "\n=== Testing /v1/chat/completions endpoint progress ===" << std::endl;
215+
216+
std::string prompt = create_long_prompt();
217+
std::string request = create_chat_completion_request(prompt, true);
218+
std::string response;
219+
220+
if (!send_http_request(request, response)) {
221+
std::cout << "Failed to send request" << std::endl;
222+
return false;
223+
}
224+
225+
std::vector<std::string> progress_responses, content_responses;
226+
parse_progress_responses(response, progress_responses, content_responses);
227+
228+
std::cout << "Received " << progress_responses.size() << " progress responses" << std::endl;
229+
std::cout << "Received " << content_responses.size() << " content responses" << std::endl;
230+
231+
if (check_progress_completion(progress_responses)) {
232+
std::cout << "Progress reached 100% as expected" << std::endl;
233+
return true;
234+
} else {
235+
std::cout << "Progress did not reach 100%" << std::endl;
236+
return false;
237+
}
238+
}
239+
240+
bool test_progress_disabled() {
241+
std::cout << "\n=== Testing progress disabled ===" << std::endl;
242+
243+
std::string prompt = create_long_prompt();
244+
std::string request = create_completion_request(prompt, false);
245+
std::string response;
246+
247+
if (!send_http_request(request, response)) {
248+
std::cout << "Failed to send request" << std::endl;
249+
return false;
250+
}
251+
252+
std::vector<std::string> progress_responses, content_responses;
253+
parse_progress_responses(response, progress_responses, content_responses);
254+
255+
std::cout << "Received " << progress_responses.size() << " progress responses (should be 0)" << std::endl;
256+
std::cout << "Received " << content_responses.size() << " content responses" << std::endl;
257+
258+
if (progress_responses.empty()) {
259+
std::cout << "No progress responses when disabled, as expected" << std::endl;
260+
return true;
261+
} else {
262+
std::cout << "Progress responses received when disabled" << std::endl;
263+
return false;
264+
}
265+
}
266+
267+
bool run_all_tests() {
268+
std::cout << "Starting Progress Feature Tests" << std::endl;
269+
std::cout << "==================================================" << std::endl;
270+
271+
std::vector<std::pair<std::string, std::function<bool()>>> tests = {
272+
{"Completion endpoint progress", [this]() { return test_completion_endpoint_progress(); }},
273+
{"Chat completion endpoint progress", [this]() { return test_chat_completion_endpoint_progress(); }},
274+
{"Progress disabled", [this]() { return test_progress_disabled(); }},
275+
};
276+
277+
int passed = 0;
278+
int total = tests.size();
279+
280+
for (const auto& test : tests) {
281+
std::cout << "\n==================== " << test.first << " ====================" << std::endl;
282+
if (test.second()) {
283+
std::cout << "PASSED" << std::endl;
284+
passed++;
285+
} else {
286+
std::cout << "FAILED" << std::endl;
287+
}
288+
}
289+
290+
std::cout << "\n==================================================" << std::endl;
291+
std::cout << "Test Results: " << passed << "/" << total << " tests passed" << std::endl;
292+
293+
return passed == total;
294+
}
295+
};
296+
297+
int main(int argc, char* argv[]) {
298+
if (argc != 1) {
299+
std::cout << "Usage: " << argv[0] << std::endl;
300+
std::cout << "Make sure the server is running on localhost:8081" << std::endl;
301+
return 1;
302+
}
303+
304+
ProgressFeatureTest tester;
305+
bool success = tester.run_all_tests();
306+
307+
if (success) {
308+
std::cout << "\nAll tests passed!" << std::endl;
309+
return 0;
310+
} else {
311+
std::cout << "\nSome tests failed!" << std::endl;
312+
return 1;
313+
}
314+
}

tools/server/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ By default, this value is set to `0`, meaning no tokens are kept. Use `-1` to re
428428

429429
`stream`: Allows receiving each predicted token in real-time instead of waiting for the completion to finish (uses a different response format). To enable this, set to `true`.
430430

431-
`include_prompt_progress`: When `stream` is enabled, this option allows receiving prompt processing progress information before the text generation begins. The progress responses contain a `prompt_processing` field with details about the number of tokens processed and overall progress. This is useful for long prompts where users want to see evaluation progress instead of waiting silently. Default: `false` (only applies when `stream` is `true`).
431+
`return_progress`: When `stream` is enabled, this option allows receiving prompt processing progress information before the text generation begins. The progress responses contain a `prompt_processing` field with details about the number of tokens processed and overall progress. This is useful for long prompts where users want to see evaluation progress instead of waiting silently. Default: `false` (only applies when `stream` is `true`).
432432

433433
`stop`: Specify a JSON array of stopping strings.
434434
These words will not be included in the completion, so make sure to add them to the prompt for the next iteration. Default: `[]`

0 commit comments

Comments
 (0)