Files

94 lines
4.4 KiB
Rust

//! Responses over real gRPC: streaming, usage, length, tool calls and stop markers.
use crate::harness::{Runtime, Stack, assert_one_terminal, body, ready, sse_events, text_of, usage};
use leleka_backend_api::FinishReason;
use leleka_backend_api::scripted::Script;
use serde_json::{Value, json};
async fn streamed_text_arrives_in_order(runtime: &Runtime, stack: &Stack) {
runtime.engine.push(Script::stop_with(&["hel", "lo ", "жук"], usage(11, 3)));
let (status, text) = stack.post("/v1/responses", &body(json!({"stream": true}))).await;
assert_eq!(status, 200, "{text}");
let events = sse_events(&text);
assert_one_terminal(&events);
assert_eq!(events.last().unwrap()["type"], "response.completed");
let deltas: String = events
.iter()
.filter(|e| e["type"] == "response.output_text.delta")
.map(|e| e["delta"].as_str().unwrap())
.collect();
assert_eq!(deltas, "hello жук");
}
#[tokio::test(flavor = "multi_thread")]
async fn responses_over_real_grpc_stream_and_complete_with_the_engines_usage() {
let (runtime, stack) = ready("chain-basic").await;
streamed_text_arrives_in_order(&runtime, &stack).await;
runtime.engine.push(Script::stop_with(&["done"], usage(11, 1)));
let (status, text) = stack.post("/v1/responses", &body(json!({}))).await;
assert_eq!(status, 200, "{text}");
let response: Value = serde_json::from_str(&text).unwrap();
assert_eq!(text_of(&response), "done");
assert_eq!(response["usage"]["input_tokens"], 11);
// The prompt the engine got through gRPC is the rendered template, and the sampling defaults
// stayed unset.
let seen = runtime.engine.requests();
assert_eq!(seen[0].prompt, "<|im_start|>user\nhi<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n");
assert_eq!(seen[0].max_output_tokens, 4096);
stack.finish().await;
runtime.stop().await;
}
async fn length_finish_is_incomplete(runtime: &Runtime, stack: &Stack) {
runtime.engine.push(Script::finishing(&["cut"], FinishReason::Length, usage(5, 1)));
let (_, text) = stack.post("/v1/responses", &body(json!({"stream": true, "max_output_tokens": 1}))).await;
let events = sse_events(&text);
assert_one_terminal(&events);
assert_eq!(events.last().unwrap()["type"], "response.incomplete");
}
/// The markup of one tool call, cut into 9-character pieces as an engine streams it.
fn tool_call_pieces() -> Vec<String> {
let markup = "<tool_call>\n<function=lookup>\n<parameter=key>\nvalue\n</parameter>\n</function>\n</tool_call>";
markup.chars().collect::<Vec<_>>().chunks(9).map(|c| c.iter().collect()).collect()
}
fn lookup_tool_declaration() -> Value {
let parameters = json!({"type": "object", "properties": {"key": {"type": "string"}},
"required": ["key"], "additionalProperties": false});
json!({"tools": [{"type": "function", "name": "lookup", "parameters": parameters}]})
}
async fn tool_call_markup_becomes_a_function_call(runtime: &Runtime, stack: &Stack) {
let pieces = tool_call_pieces();
let refs: Vec<&str> = pieces.iter().map(String::as_str).collect();
runtime.engine.push(Script::stop_with(&refs, usage(30, 20)));
let (status, text) = stack.post("/v1/responses", &body(lookup_tool_declaration())).await;
assert_eq!(status, 200, "{text}");
let response: Value = serde_json::from_str(&text).unwrap();
let call = response["output"].as_array().unwrap().iter().find(|item| item["type"] == "function_call").unwrap();
assert_eq!(call["name"], "lookup");
assert_eq!(serde_json::from_str::<Value>(call["arguments"].as_str().unwrap()).unwrap(), json!({"key": "value"}));
}
#[tokio::test(flavor = "multi_thread")]
async fn length_tool_call_and_stop_marker_over_real_grpc() {
let (runtime, stack) = ready("chain-finish").await;
length_finish_is_incomplete(&runtime, &stack).await;
tool_call_markup_becomes_a_function_call(&runtime, &stack).await;
// The model ends its turn with a marker; the engine would keep going, the marker stops it.
runtime.engine.push(Script::hanging_after(&["ok", "<|im_end|>", "junk"]));
let (status, text) = stack.post("/v1/responses", &body(json!({}))).await;
assert_eq!(status, 200, "{text}");
assert_eq!(text_of(&serde_json::from_str(&text).unwrap()), "ok", "{text}");
let ids = runtime.engine.cancels();
assert_eq!(ids.len(), 1, "the marker cancelled the runtime generation");
stack.finish().await;
runtime.stop().await;
}