3030Add the following to your Cargo.toml file:
3131
3232``` toml
33- openai_api_rust = " 0.1.1 "
33+ openai_api_rust = " 0.1.3 "
3434```
3535
3636Export your API key into the environment variables
@@ -43,33 +43,39 @@ Then use the crate in your Rust code:
4343
4444``` rust
4545use openai_api_rust :: * ;
46- use openai_api_rust :: edits :: * ;
46+ use openai_api_rust :: chat :: * ;
47+ use openai_api_rust :: completions :: * ;
4748
4849fn main () {
4950 // Load API key from environment OPENAI_API_KEY.
5051 // You can also hadcode through `Auth::new(<your_api_key>)`, but it is not recommended.
5152 let auth = Auth :: from_env (). unwrap ();
5253 let openai = OpenAI :: new (auth , " https://api.openai.com/v1/" );
53- let body = EditsBody {
54- model : " text-davinci-edit-001" . to_string (),
55- temperature : None ,
56- top_p : None ,
54+ let body = ChatBody {
55+ model : " gpt-3.5-turbo" . to_string (),
56+ max_tokens : Some (7 ),
57+ temperature : Some (0_f32 ),
58+ top_p : Some (0_f32 ),
5759 n : Some (2 ),
58- instruction : " Fix the spelling mistakes" . to_string (),
59- input : Some (" What day of the wek is it?" . to_string ()),
60+ stream : Some (false ),
61+ stop : None ,
62+ presence_penalty : None ,
63+ frequency_penalty : None ,
64+ logit_bias : None ,
65+ user : None ,
66+ messages : vec! [Message {
67+ role : Some (" user" . to_string ()),
68+ content : Some (" Hello!" . to_string ()),
69+ }],
6070 };
61- let rs = openai . edit_create (& body ). unwrap ();
62- let choice = rs . choices. get (0 ). unwrap ();
63- println! (" choice: {:?}" , choice . text);
71+ let rs = openai . chat_completion_create (& body );
72+ let choice = rs . unwrap (). choices;
73+ let message = & choice [0 ]. message. as_ref (). unwrap ();
74+ let content = message . content. as_ref (). unwrap ();
75+ assert! (content . contains (" Hello" ));
6476}
6577```
6678
67- Output:
68-
69- ``` bash
70- choice: Some(" What day of the week is it?\n" )
71- ```
72-
7379### Use proxy
7480
7581Load proxy from env
0 commit comments