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
use std::sync::Arc;
use tokio::runtime::{Handle, Runtime};
use crate::adapter::ModelAdapter;
use crate::error::{Error, Result};
use crate::headers::RequestOptions;
use crate::stream::{ModelStream, StreamEvent, next_event};
use crate::types::{ChatRequest, ChatResponse};
/// Blocking wrapper for an async [`ModelAdapter`].
///
/// Construction and calls fail when invoked from inside a Tokio runtime. Use
/// this wrapper only from synchronous runtimes or dedicated worker threads.
pub struct BlockingAdapter<A> {
inner: A,
runtime: Arc<Runtime>,
}
impl<A> BlockingAdapter<A>
where
A: ModelAdapter,
{
/// Creates a blocking adapter around `inner`, backed by a dedicated
/// single-threaded Tokio runtime.
///
/// # Errors
///
/// Returns an error when called from inside a Tokio runtime, or when the
/// blocking runtime cannot be created.
pub fn new(inner: A) -> Result<Self> {
ensure_sync_context()?;
let runtime = Runtime::new().map_err(|error| {
Error::InvalidRequest(format!("failed to create blocking runtime: {error}"))
})?;
Ok(Self {
inner,
runtime: Arc::new(runtime),
})
}
/// Borrows the wrapped async adapter.
pub fn inner(&self) -> &A {
&self.inner
}
/// Consumes the wrapper and returns the wrapped async adapter.
pub fn into_inner(self) -> A {
self.inner
}
/// Blocking version of [`ModelAdapter::complete`].
///
/// # Errors
///
/// Returns an error when called from inside a Tokio runtime, or the
/// underlying adapter error.
pub fn complete(&self, request: &ChatRequest) -> Result<ChatResponse> {
ensure_sync_context()?;
self.runtime.block_on(self.inner.complete(request))
}
/// Blocking version of [`ModelAdapter::complete_with`].
///
/// # Errors
///
/// Returns an error when called from inside a Tokio runtime, or the
/// underlying adapter error.
pub fn complete_with(
&self,
request: &ChatRequest,
options: &RequestOptions,
) -> Result<ChatResponse> {
ensure_sync_context()?;
self.runtime
.block_on(self.inner.complete_with(request, options))
}
/// Blocking version of [`ModelAdapter::stream`].
///
/// # Errors
///
/// Returns an error when called from inside a Tokio runtime, or the
/// underlying adapter error.
pub fn stream(&self, request: &ChatRequest) -> Result<BlockingStream> {
ensure_sync_context()?;
let stream = self.runtime.block_on(self.inner.stream(request))?;
Ok(BlockingStream {
runtime: self.runtime.clone(),
stream,
})
}
/// Blocking version of [`ModelAdapter::stream_with`].
///
/// # Errors
///
/// Returns an error when called from inside a Tokio runtime, or the
/// underlying adapter error.
pub fn stream_with(
&self,
request: &ChatRequest,
options: &RequestOptions,
) -> Result<BlockingStream> {
ensure_sync_context()?;
let stream = self
.runtime
.block_on(self.inner.stream_with(request, options))?;
Ok(BlockingStream {
runtime: self.runtime.clone(),
stream,
})
}
}
/// Blocking iterator over a model stream.
pub struct BlockingStream {
runtime: Arc<Runtime>,
stream: ModelStream,
}
impl BlockingStream {
/// Blocks until the next stream event is available, returning `None`
/// when the stream has ended.
///
/// Calling this from inside a Tokio runtime yields a single
/// `Err(Error::InvalidRequest)` item instead of panicking.
pub fn next_event(&mut self) -> Option<Result<StreamEvent>> {
if let Err(error) = ensure_sync_context() {
return Some(Err(error));
}
self.runtime.block_on(next_event(&mut self.stream))
}
}
impl Iterator for BlockingStream {
type Item = Result<StreamEvent>;
fn next(&mut self) -> Option<Self::Item> {
self.next_event()
}
}
fn ensure_sync_context() -> Result<()> {
if Handle::try_current().is_ok() {
Err(Error::InvalidRequest(
"blocking adapter cannot be called from an async Tokio runtime".to_string(),
))
} else {
Ok(())
}
}