Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ jobs:
with:
tool: zola@0.19.2

- run: cargo build

- run: |
echo "DEEPINFRA_KEY=${{ secrets.DEEPINFRA_KEY }}" > .env

- run: cargo run

- run: zola build

- uses: cloudflare/wrangler-action@v3
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ _public/
**/tmp*
**/target/
**/Cargo.lock
.env
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "transformrs-org"
version = "0.1.0"
edition = "2021"

[dependencies]
regex = "1.11.1"
tempfile = "3"
transformrs = "0.2.0"
11 changes: 10 additions & 1 deletion content/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ For example, for DeepInfra, set `DEEPINFRA_KEY` in `.env`:
DEEPINFRA_KEY=<KEY>
```

and add the library to your `Cargo.toml`:

```toml
[dependencies]
transformrs = "0.2.0"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
```

Then, you can use the API as follows.

### Chat Completion
Expand All @@ -31,7 +39,8 @@ use transformrs::openai;
use transformrs::Message;
use transformrs::Provider;

fn main() {
#[tokio::main]
async fn main() {
let messages = vec![
Message {
role: "system".to_string(),
Expand Down
74 changes: 74 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use regex::Regex;
use std::fs;
use std::path::Path;
use std::process::Command;

fn code_blocks(content: &str, lang: &str) -> Vec<String> {
let re = Regex::new(&format!(r"```{lang}\n([\s\S]*?)```")).unwrap();

re.captures_iter(&content)
.filter_map(|cap| cap.get(1).map(|code| code.as_str().to_string()))
.collect()
}

fn create_project(dir: &Path, dependencies: &str, code: &str) {
let project = format!(
r#"
[package]
name = "transformrs-org-test"
version = "0.1.0"
edition = "2021"

{dependencies}
"#
);
fs::write(dir.join("Cargo.toml"), project).unwrap();

fs::create_dir_all(dir.join("src")).unwrap();
fs::write(dir.join("src/main.rs"), code).unwrap();
}

fn copy_env(dir: &Path) {
let env = fs::read_to_string(".env").unwrap();
fs::write(dir.join(".env"), env).unwrap();
}

fn run_project(dir: &Path) {
// list everything in dir
println!("Listing everything in dir: {:?}", dir);
let entries = fs::read_dir(dir).unwrap();
for entry in entries {
println!("Entry: {:?}", entry.unwrap().path());
}

let _output = Command::new("cargo")
.arg("build")
.current_dir(dir)
.output()
.expect("Failed to run cargo build");

let output = Command::new("cargo")
.arg("run")
.current_dir(dir)
.output()
.expect("Failed to run cargo run");

println!("Output: {:?}", String::from_utf8(output.stdout).unwrap());
}

fn main() {
// Read the markdown file
let content =
fs::read_to_string("content/_index.md").expect("Failed to read content/_index.md");

let dependencies = code_blocks(&content, "toml").first().unwrap().clone();
let code_blocks = code_blocks(&content, "rust");

let tmp_dir = tempfile::tempdir().unwrap();
let tmp_dir_path = tmp_dir.path();
println!("Tmp dir path: {:?}", tmp_dir_path);
create_project(tmp_dir_path, &dependencies, code_blocks.first().unwrap());
copy_env(tmp_dir_path);
run_project(tmp_dir_path);
drop(tmp_dir);
}