Skip to content

Commit da37576

Browse files
committed
feat: add auto_generate macro and minimal_llm_function
1 parent 2a30bb1 commit da37576

File tree

3 files changed

+26
-38
lines changed

3 files changed

+26
-38
lines changed

examples/over_process.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use auto_rust::auto_generate;
2+
3+
fn main() {
4+
let a = auto_generate!("a random number between 1 and 10");
5+
6+
print_type_of(&a);
7+
println!("{}", a);
8+
}
9+
10+
fn print_type_of<T>(_: &T) {
11+
println!("{}", std::any::type_name::<T>())
12+
}

src/generator.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,11 @@ pub fn generate_body_function_from_head(
5454

5555
Ok(implementation)
5656
}
57+
58+
pub fn minimal_llm_function(input: String) -> String {
59+
let system_message = "Your task is respond with a string with double quote".to_string();
60+
61+
let res = open_ai_chat_completions(system_message, input).unwrap();
62+
63+
res.choices.first().unwrap().to_owned().message.content
64+
}

src/lib.rs

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,29 @@ use dotenv::dotenv;
77
use proc_macro::TokenStream;
88
use syn::{ItemFn, __private::ToTokens};
99

10-
use crate::generator::generate_body_function_from_head;
10+
use crate::generator::{generate_body_function_from_head, minimal_llm_function};
1111

12+
/// This macro gets an input like "String, "This is a llm generated function"" and returns a function that returns a String
1213
#[proc_macro]
13-
pub fn implement(_item: TokenStream) -> TokenStream {
14-
// TODO: Evaluate the use of dotenv in this crate
14+
pub fn auto_generate(item: TokenStream) -> TokenStream {
1515
dotenv().ok();
1616

17-
let implemented_fn = generate_body_function_from_head(_item.to_string(), None).unwrap();
17+
let res = minimal_llm_function(item.to_string());
18+
// println!("{:?}", res);
1819

19-
// println!("{}", implemented_fn);
20-
21-
implemented_fn.parse().unwrap()
20+
res.parse().unwrap()
2221
}
2322

2423
#[proc_macro_attribute]
2524
pub fn auto_implement(args: TokenStream, input: TokenStream) -> TokenStream {
26-
// env::vars()
27-
// .map(|v| format!("{}: {}\n", v.0, v.1))
28-
// .for_each(|v| print!("{}", v));
29-
30-
// let this_file = file!();
31-
// column!();
32-
33-
// println!("This file: {}", this_file);
34-
3525
let ast: ItemFn = syn::parse(input).expect("Failed to parse input as a function");
3626

3727
let context = args.to_string();
3828

39-
// println!("Context: {}", context);
40-
4129
let mut prompt_input = String::new();
4230

4331
let fn_header = ast.sig.to_token_stream().to_string();
4432

45-
// println!("Function header: {}", fn_header);
46-
4733
for attr in ast.attrs {
4834
let data = attr.to_token_stream().to_string();
4935

@@ -53,27 +39,9 @@ pub fn auto_implement(args: TokenStream, input: TokenStream) -> TokenStream {
5339

5440
prompt_input.push_str(&fn_header);
5541

56-
// println!("Information extracted: {:?}", prompt_input);
57-
5842
dotenv().ok();
5943

6044
let implemented_fn = generate_body_function_from_head(prompt_input, Some(context)).unwrap();
6145

62-
// println!("\n{}\n", implemented_fn);
63-
64-
// #[allow(long_running_const_eval)]
65-
66-
// loop {
67-
// let mut line = String::new();
68-
// let b1 = std::io::stdin().read_line(&mut line).unwrap();
69-
// println!("no of bytes read , {}", b1);
70-
// }
71-
72-
// static line = String::new();
73-
// println!("Enter your name :");
74-
//
75-
// println!("Hello , {}", line);
76-
// println!("no of bytes read , {}", b1);
77-
7846
implemented_fn.parse().unwrap()
7947
}

0 commit comments

Comments
 (0)