forked from Dicklesworthstone/pi_agent_rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai_responses.rs
More file actions
347 lines (321 loc) · 11.4 KB
/
Copy pathopenai_responses.rs
File metadata and controls
347 lines (321 loc) · 11.4 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//! `OpenAI` Responses API provider streaming tests (VCR playback/recording).
use super::{
ProviderReplayCacheSpec, ScenarioExpectation, StreamExpectations, assert_error_translation,
assert_stream_expectations, assert_tool_schema_fidelity, cassette_root, collect_events,
log_summary, provider_request_schema_hash, record_provider_replay_cache_artifact,
record_stream_contract_artifact, user_text, vcr_mode, vcr_strict,
};
use crate::common::TestHarness;
use pi::http::client::Client;
use pi::model::{Message, StopReason};
use pi::provider::{Context, Provider, StreamOptions, ToolDef};
use pi::providers::openai_responses::OpenAIResponsesProvider;
use pi::vcr::{VcrMode, VcrRecorder};
use serde_json::json;
use std::env;
const SYSTEM_PROMPT: &str =
"You are a test harness model. Follow instructions precisely and deterministically.";
#[derive(Clone)]
struct ScenarioOptions {
max_tokens: u32,
temperature: Option<f32>,
}
impl Default for ScenarioOptions {
fn default() -> Self {
Self {
max_tokens: 256,
temperature: Some(0.0),
}
}
}
struct Scenario {
name: &'static str,
description: &'static str,
model: String,
messages: Vec<Message>,
tools: Vec<ToolDef>,
options: ScenarioOptions,
expectation: ScenarioExpectation,
}
fn openai_responses_model() -> String {
env::var("OPENAI_RESPONSES_TEST_MODEL").unwrap_or_else(|_| "gpt-4o-mini".to_string())
}
fn openai_responses_api_key(mode: VcrMode) -> String {
match mode {
VcrMode::Record => {
env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY required for VCR record mode")
}
_ => env::var("OPENAI_API_KEY").unwrap_or_else(|_| "test-key".to_string()),
}
}
fn build_context(scenario: &Scenario) -> Context<'static> {
Context {
system_prompt: Some(SYSTEM_PROMPT.to_string().into()),
messages: scenario.messages.clone().into(),
tools: scenario.tools.clone().into(),
}
}
fn build_options(scenario: &Scenario, api_key: String) -> StreamOptions {
StreamOptions {
api_key: Some(api_key),
max_tokens: Some(scenario.options.max_tokens),
temperature: scenario.options.temperature,
..Default::default()
}
}
async fn run_scenario(scenario: Scenario) {
let harness = TestHarness::new(format!("openai_responses_{}", scenario.name));
let cassette_dir = cassette_root();
let mode = vcr_mode();
let cassette_path = cassette_dir.join(format!("{}.json", scenario.name));
harness.record_artifact(format!("{}.json", scenario.name), &cassette_path);
if mode == VcrMode::Playback && !cassette_path.exists() {
let message = format!("Missing cassette {}", cassette_path.display());
if vcr_strict() {
panic!("{message}");
} else {
harness.log().warn("vcr", message);
return;
}
}
let request_schema_hash = provider_request_schema_hash(
&scenario.messages,
&scenario.tools,
&json!({
"systemPrompt": SYSTEM_PROMPT,
"maxTokens": scenario.options.max_tokens,
"temperature": scenario.options.temperature,
}),
);
record_provider_replay_cache_artifact(
&harness,
&ProviderReplayCacheSpec {
provider: "openai-responses",
route: "POST https://api.openai.com/v1/responses",
model: &scenario.model,
scenario: scenario.name,
cassette_path: &cassette_path,
request_schema_hash: &request_schema_hash,
mode,
},
);
let api_key = openai_responses_api_key(mode);
let recorder = VcrRecorder::new_with(scenario.name, mode, &cassette_dir);
let client = Client::new().with_vcr(recorder);
let provider = OpenAIResponsesProvider::new(scenario.model.clone()).with_client(client);
let context = build_context(&scenario);
let options = build_options(&scenario, api_key);
harness
.log()
.info_ctx("scenario", "OpenAI Responses scenario", |ctx| {
ctx.push(("name".into(), scenario.name.to_string()));
ctx.push(("description".into(), scenario.description.to_string()));
ctx.push(("mode".into(), format!("{mode:?}")));
ctx.push(("model".into(), scenario.model.clone()));
ctx.push(("max_tokens".into(), scenario.options.max_tokens.to_string()));
});
match scenario.expectation.clone() {
ScenarioExpectation::Stream(expectations) => {
let stream = provider
.stream(&context, &options)
.await
.expect("expected stream");
let outcome = collect_events(stream).await;
let summary = super::summarize_events(&outcome);
log_summary(&harness, scenario.name, &summary);
assert_stream_expectations(&harness, scenario.name, &summary, &expectations);
assert_tool_schema_fidelity(
&harness,
scenario.name,
&scenario.tools,
&summary.tool_calls,
);
record_stream_contract_artifact(
&harness,
"openai-responses",
scenario.name,
scenario.description,
&summary,
);
}
ScenarioExpectation::Error(expectation) => {
let Err(err) = provider.stream(&context, &options).await else {
panic!("expected error, got success");
};
let message = err.to_string();
assert_error_translation(
&harness,
"openai-responses",
scenario.name,
scenario.description,
&expectation,
&message,
);
}
}
}
fn tool_echo() -> ToolDef {
ToolDef {
name: "echo".to_string(),
description: "Echo the provided text.".to_string(),
parameters: json!({
"type": "object",
"properties": {
"text": { "type": "string" }
},
"required": ["text"]
}),
}
}
fn tool_add() -> ToolDef {
ToolDef {
name: "add".to_string(),
description: "Add two numbers.".to_string(),
parameters: json!({
"type": "object",
"properties": {
"a": { "type": "number" },
"b": { "type": "number" }
},
"required": ["a", "b"]
}),
}
}
fn tool_multiply() -> ToolDef {
ToolDef {
name: "multiply".to_string(),
description: "Multiply two numbers.".to_string(),
parameters: json!({
"type": "object",
"properties": {
"a": { "type": "number" },
"b": { "type": "number" }
},
"required": ["a", "b"]
}),
}
}
fn scenario_simple_text(model: &str) -> Scenario {
Scenario {
name: "openai_responses_simple_text",
description: "Simple text response",
model: model.to_string(),
messages: vec![user_text("Reply with the single word: pong.")],
tools: Vec::new(),
options: ScenarioOptions {
max_tokens: 64,
..ScenarioOptions::default()
},
expectation: ScenarioExpectation::Stream(StreamExpectations {
min_text_deltas: 1,
allowed_stop_reasons: Some(vec![StopReason::Stop]),
..StreamExpectations::default()
}),
}
}
fn scenario_multi_paragraph(model: &str) -> Scenario {
Scenario {
name: "openai_responses_multi_paragraph",
description: "Multi-paragraph response",
model: model.to_string(),
messages: vec![user_text(
"Reply with two paragraphs separated by a blank line. \
Paragraph one must be 'Paragraph 1.' and paragraph two must be 'Paragraph 2.'.",
)],
tools: Vec::new(),
options: ScenarioOptions::default(),
expectation: ScenarioExpectation::Stream(StreamExpectations {
min_text_deltas: 1,
require_blank_line: true,
..StreamExpectations::default()
}),
}
}
fn scenario_unicode(model: &str) -> Scenario {
Scenario {
name: "openai_responses_unicode_content",
description: "Unicode content response",
model: model.to_string(),
messages: vec![user_text("Reply with: 😀 你好 שלום")],
tools: Vec::new(),
options: ScenarioOptions::default(),
expectation: ScenarioExpectation::Stream(StreamExpectations {
min_text_deltas: 1,
require_unicode: true,
..StreamExpectations::default()
}),
}
}
fn scenario_tool_call_single(model: &str) -> Scenario {
Scenario {
name: "openai_responses_tool_call_single",
description: "Single tool call response",
model: model.to_string(),
messages: vec![user_text(
"Call the echo tool with text='hello'. Do not answer in text.",
)],
tools: vec![tool_echo()],
options: ScenarioOptions::default(),
expectation: ScenarioExpectation::Stream(StreamExpectations {
min_tool_calls: 1,
allowed_stop_reasons: Some(vec![StopReason::ToolUse]),
..StreamExpectations::default()
}),
}
}
fn scenario_tool_call_multiple(model: &str) -> Scenario {
Scenario {
name: "openai_responses_tool_call_multiple",
description: "Multiple tool calls response",
model: model.to_string(),
messages: vec![user_text(
"Call add with a=2 b=3, then call multiply with a=4 b=5. Do not answer in text.",
)],
tools: vec![tool_add(), tool_multiply()],
options: ScenarioOptions::default(),
expectation: ScenarioExpectation::Stream(StreamExpectations {
min_tool_calls: 2,
allowed_stop_reasons: Some(vec![StopReason::ToolUse]),
..StreamExpectations::default()
}),
}
}
fn scenario_long_response(model: &str) -> Scenario {
Scenario {
name: "openai_responses_very_long_response",
description: "Very long response hitting max tokens",
model: model.to_string(),
messages: vec![user_text(
"Write 50 short sentences about Rust without stopping early.",
)],
tools: Vec::new(),
options: ScenarioOptions {
max_tokens: 64,
..ScenarioOptions::default()
},
expectation: ScenarioExpectation::Stream(StreamExpectations {
min_text_deltas: 1,
allowed_stop_reasons: Some(vec![StopReason::Length, StopReason::Stop]),
..StreamExpectations::default()
}),
}
}
macro_rules! openai_responses_test {
($test_name:ident, $scenario_fn:ident) => {
#[test]
fn $test_name() {
asupersync::test_utils::run_test(|| async {
run_scenario($scenario_fn(&openai_responses_model())).await;
});
}
};
}
openai_responses_test!(openai_responses_simple_text, scenario_simple_text);
openai_responses_test!(openai_responses_multi_paragraph, scenario_multi_paragraph);
openai_responses_test!(openai_responses_unicode_content, scenario_unicode);
openai_responses_test!(openai_responses_tool_call_single, scenario_tool_call_single);
openai_responses_test!(
openai_responses_tool_call_multiple,
scenario_tool_call_multiple
);
openai_responses_test!(openai_responses_very_long_response, scenario_long_response);