This repository was archived by the owner on Sep 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai_session.rs
More file actions
99 lines (86 loc) · 3.16 KB
/
Copy pathopenai_session.rs
File metadata and controls
99 lines (86 loc) · 3.16 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
use std::sync::Arc;
use async_trait::async_trait;
use openai_agents::{
Agent, AgentsError, InputItem, Model, ModelProvider, ModelRequest, ModelResponse,
OpenAIConversationsSession, OutputItem, Result as AgentsResult, Runner, Session, Usage,
};
#[derive(Clone, Default)]
struct OpenAISessionModel;
#[async_trait]
impl Model for OpenAISessionModel {
async fn generate(&self, request: ModelRequest) -> AgentsResult<ModelResponse> {
let latest = request
.input
.iter()
.rev()
.find_map(InputItem::as_text)
.unwrap_or_default()
.to_lowercase();
let text = if latest.contains("population") {
"California has about 39 million people.".to_owned()
} else if latest.contains("what state") {
"It is in California.".to_owned()
} else if latest.contains("golden gate bridge") {
"San Francisco.".to_owned()
} else {
"I can answer using the OpenAI conversation session.".to_owned()
};
Ok(ModelResponse {
model: request.model,
output: vec![OutputItem::Text { text }],
usage: Usage {
input_tokens: 8,
output_tokens: 5,
},
response_id: Some(format!(
"resp_{}",
request.previous_response_id.as_deref().unwrap_or("start")
)),
request_id: None,
})
}
}
#[derive(Clone, Default)]
struct OpenAISessionProvider {
model: Arc<OpenAISessionModel>,
}
impl ModelProvider for OpenAISessionProvider {
fn resolve(&self, _model: Option<&str>) -> Arc<dyn Model> {
self.model.clone()
}
}
#[tokio::main]
async fn main() -> Result<(), AgentsError> {
let agent = Agent::builder("Assistant")
.instructions("Reply very concisely.")
.build();
let session = OpenAIConversationsSession::new("conversation_123");
let runner = Runner::new().with_model_provider(Arc::new(OpenAISessionProvider::default()));
println!("=== OpenAI Conversation Session Example ===");
println!("conversation_id={}", session.conversation_id().await);
let first = runner
.run_with_session(&agent, "What city is the Golden Gate Bridge in?", &session)
.await?;
println!("assistant_1={}", first.final_output.unwrap_or_default());
println!(
"last_response_id={}",
session.last_response_id().await.unwrap_or_default()
);
let second = runner
.run_with_session(&agent, "What state is it in?", &session)
.await?;
println!("assistant_2={}", second.final_output.unwrap_or_default());
println!(
"last_response_id={}",
session.last_response_id().await.unwrap_or_default()
);
let third = runner
.run_with_session(&agent, "What's the population of that state?", &session)
.await?;
println!("assistant_3={}", third.final_output.unwrap_or_default());
let latest_items = session.get_items_with_limit(Some(2)).await?;
println!("latest_items={}", latest_items.len());
let all_items = session.get_items().await?;
println!("total_items={}", all_items.len());
Ok(())
}