1- use std:: fmt:: Display ;
2-
31use serde:: { Deserialize , Serialize } ;
42use spin_world:: v2:: llm as wasi_llm;
53
64/// LLM model
75#[ derive( Serialize , Debug ) ]
86pub enum Model {
7+ #[ serde( rename = "gpt-5" ) ]
98 GPT5 ,
9+ #[ serde( rename = "gpt-5-mini" ) ]
1010 GPT5Mini ,
11+ #[ serde( rename = "gpt-5-nano" ) ]
1112 GPT5Nano ,
13+ #[ serde( rename = "gpt-5-chat" ) ]
1214 GPT5Chat ,
15+ #[ serde( rename = "gpt-4.5" ) ]
1316 GPT45 ,
17+ #[ serde( rename = "gpt-4.1" ) ]
1418 GPT41 ,
19+ #[ serde( rename = "gpt-4.1-mini" ) ]
1520 GPT41Mini ,
21+ #[ serde( rename = "gpt-4.1-nano" ) ]
1622 GPT41Nano ,
23+ #[ serde( rename = "gpt-4" ) ]
1724 GPT4 ,
25+ #[ serde( rename = "gpt-4o" ) ]
1826 GPT4o ,
27+ #[ serde( rename = "gpt-4o-mini" ) ]
1928 GPT4oMini ,
29+ #[ serde( rename = "o4-mini" ) ]
2030 O4Mini ,
31+ #[ serde( rename = "o3" ) ]
2132 O3 ,
33+ #[ serde( rename = "o1" ) ]
2234 O1 ,
2335}
2436
@@ -48,27 +60,6 @@ impl TryFrom<&str> for Model {
4860 }
4961}
5062
51- impl Display for Model {
52- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
53- match self {
54- Model :: GPT5 => write ! ( f, "gpt-5" ) ,
55- Model :: GPT5Mini => write ! ( f, "gpt-5-mini" ) ,
56- Model :: GPT5Nano => write ! ( f, "gpt-5-nano" ) ,
57- Model :: GPT5Chat => write ! ( f, "gpt-5-chat" ) ,
58- Model :: GPT45 => write ! ( f, "gpt-4.5" ) ,
59- Model :: GPT41 => write ! ( f, "gpt-4.1" ) ,
60- Model :: GPT41Mini => write ! ( f, "gpt-4.1-mini" ) ,
61- Model :: GPT41Nano => write ! ( f, "gpt-4.1-nano" ) ,
62- Model :: GPT4 => write ! ( f, "gpt-4" ) ,
63- Model :: GPT4o => write ! ( f, "gpt-4o" ) ,
64- Model :: GPT4oMini => write ! ( f, "gpt-4o-mini" ) ,
65- Model :: O4Mini => write ! ( f, "o4-mini" ) ,
66- Model :: O3 => write ! ( f, "o3" ) ,
67- Model :: O1 => write ! ( f, "o1" ) ,
68- }
69- }
70- }
71-
7263#[ derive( Serialize , Debug ) ]
7364pub struct Prompt {
7465 role : Role ,
@@ -83,23 +74,16 @@ impl Prompt {
8374
8475#[ derive( Serialize , Debug ) ]
8576pub enum Role {
77+ #[ serde( rename = "system" ) ]
8678 System ,
79+ #[ serde( rename = "user" ) ]
8780 User ,
81+ #[ serde( rename = "assistant" ) ]
8882 Assistant ,
83+ #[ serde( rename = "tool" ) ]
8984 Tool ,
9085}
9186
92- impl Display for Role {
93- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
94- match self {
95- Role :: System => write ! ( f, "system" ) ,
96- Role :: User => write ! ( f, "user" ) ,
97- Role :: Assistant => write ! ( f, "assistant" ) ,
98- Role :: Tool => write ! ( f, "tool" ) ,
99- }
100- }
101- }
102-
10387impl TryFrom < & str > for Role {
10488 type Error = wasi_llm:: Error ;
10589
@@ -118,19 +102,12 @@ impl TryFrom<&str> for Role {
118102
119103#[ derive( Serialize , Debug ) ]
120104pub enum EncodingFormat {
105+ #[ serde( rename = "float" ) ]
121106 Float ,
107+ #[ serde( rename = "base64" ) ]
122108 Base64 ,
123109}
124110
125- impl Display for EncodingFormat {
126- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
127- match self {
128- EncodingFormat :: Float => write ! ( f, "float" ) ,
129- EncodingFormat :: Base64 => write ! ( f, "base64" ) ,
130- }
131- }
132- }
133-
134111impl TryFrom < & str > for EncodingFormat {
135112 type Error = wasi_llm:: Error ;
136113
@@ -147,23 +124,15 @@ impl TryFrom<&str> for EncodingFormat {
147124
148125#[ derive( Serialize , Debug ) ]
149126pub enum EmbeddingModels {
127+ #[ serde( rename = "text-embedding-ada-002" ) ]
150128 TextEmbeddingAda002 ,
129+ #[ serde( rename = "text-embedding-3-small" ) ]
151130 TextEmbedding3Small ,
131+ #[ serde( rename = "text-embedding-3-large" ) ]
152132 TextEmbedding3Large ,
153133 Custom ( String ) ,
154134}
155135
156- impl Display for EmbeddingModels {
157- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
158- match self {
159- EmbeddingModels :: TextEmbeddingAda002 => write ! ( f, "text-embedding-ada-002" ) ,
160- EmbeddingModels :: TextEmbedding3Small => write ! ( f, "text-embedding-3-small" ) ,
161- EmbeddingModels :: TextEmbedding3Large => write ! ( f, "text-embedding-3-large" ) ,
162- EmbeddingModels :: Custom ( model) => write ! ( f, "{model}" ) ,
163- }
164- }
165- }
166-
167136impl TryFrom < & str > for EmbeddingModels {
168137 type Error = wasi_llm:: Error ;
169138
@@ -179,23 +148,16 @@ impl TryFrom<&str> for EmbeddingModels {
179148
180149#[ derive( Serialize , Debug ) ]
181150enum ReasoningEffort {
151+ #[ serde( rename = "minimal" ) ]
182152 Minimal ,
153+ #[ serde( rename = "low" ) ]
183154 Low ,
155+ #[ serde( rename = "medium" ) ]
184156 Medium ,
157+ #[ serde( rename = "high" ) ]
185158 High ,
186159}
187160
188- impl Display for ReasoningEffort {
189- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
190- match self {
191- ReasoningEffort :: Minimal => write ! ( f, "minimal" ) ,
192- ReasoningEffort :: Low => write ! ( f, "low" ) ,
193- ReasoningEffort :: Medium => write ! ( f, "medium" ) ,
194- ReasoningEffort :: High => write ! ( f, "high" ) ,
195- }
196- }
197- }
198-
199161impl TryFrom < & str > for ReasoningEffort {
200162 type Error = wasi_llm:: Error ;
201163
@@ -236,42 +198,68 @@ impl TryFrom<&str> for Verbosity {
236198
237199#[ derive( Deserialize ) ]
238200pub struct ChatCompletionChoice {
201+ #[ serde( rename = "index" ) ]
239202 /// The index of the choice in the list of choices
240203 _index : u32 ,
241204 pub message : ChatCompletionResponseMessage ,
242205 /// The reason the model stopped generating tokens. This will be `stop` if the model hit a
243206 /// natural stop point or a provided stop sequence,
207+ #[ serde( rename = "finish_reason" ) ]
244208 _finish_reason : String ,
245209 /// Log probability information for the choice.
210+ #[ serde( rename = "logprobs" ) ]
246211 _logprobs : Option < Logprobs > ,
247212}
248213
249214#[ derive( Deserialize ) ]
250215/// A chat completion message generated by the model.
251216pub struct ChatCompletionResponseMessage {
252217 /// The role of the author of this message
218+ #[ serde( rename = "role" ) ]
253219 _role : String ,
254220 /// The contents of the message
255221 pub content : String ,
256222 /// The refusal message generated by the model
223+ #[ serde( rename = "refusal" ) ]
257224 _refusal : Option < String > ,
258225}
259226
260227#[ derive( Deserialize ) ]
261228pub struct Logprobs {
262229 /// A list of message content tokens with log probability information.
230+ #[ serde( rename = "content" ) ]
263231 _content : Option < Vec < String > > ,
264232 /// A list of message refusal tokens with log probability information.
233+ #[ serde( rename = "refusal" ) ]
265234 _refusal : Option < Vec < String > > ,
266235}
267236
268237#[ derive( Deserialize ) ]
269238pub struct Embedding {
270239 /// The index of the embedding in the list of embeddings..
240+ #[ serde( rename = "index" ) ]
271241 _index : u32 ,
272242 /// The embedding vector, which is a list of floats. The length of vector depends on the model as
273243 /// listed in the [embedding guide](https://platform.openai.com/docs/guides/embeddings).
274244 pub embedding : Vec < f32 > ,
275245 /// The object type, which is always "embedding"
246+ #[ serde( rename = "object" ) ]
276247 _object : String ,
277248}
249+
250+ #[ derive( Deserialize , Default ) ]
251+ pub struct ResponseError {
252+ pub message : String ,
253+ #[ serde( rename = "type" ) ]
254+ _t : String ,
255+ #[ serde( rename = "param" ) ]
256+ _param : Option < String > ,
257+ #[ serde( rename = "code" ) ]
258+ _code : String ,
259+ }
260+
261+ impl From < ResponseError > for wasi_llm:: Error {
262+ fn from ( value : ResponseError ) -> Self {
263+ wasi_llm:: Error :: RuntimeError ( value. message )
264+ }
265+ }
0 commit comments