Skip to content

Commit

Permalink
feat(openai): add openai flag dynamic js snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Mar 20, 2024
1 parent bc6a649 commit 2307080
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ napi-derive = "2.14.6"
num_cpus = "1.16.0"
serde = "1.0.193"
serde_json = "1.0.108"
spider = { version = "1.85.4", features = ["napi", "budget", "cron", "regex", "cookies", "socks", "chrome", "control", "chrome_intercept", "cache" ] }
spider = { version = "1.86.6", features = ["napi", "budget", "cron", "regex", "cookies", "socks", "chrome", "control", "chrome_intercept", "cache", "openai" ] }

[target.x86_64-unknown-linux-gnu.dependencies]
openssl-sys = { version = "0.9.96", features = ["vendored"] }
Expand Down
14 changes: 14 additions & 0 deletions book/src/website.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ const website = new Website("https://choosealicense.com")
.build();
```

### OpenAI

Use OpenAI to generate dynamic scripts to use with headless. Make sure to set the `OPENAI_API_KEY` env variable.

```ts
const website = new Website("https://google.com")
.withOpenAI({
model: "gpt-3.5-turbo",
prompt: "Search for movies",
maxTokens: 300
})
.build();
```

### Request Timeout

Add a request timeout per page in miliseconds. Example shows 30 seconds.
Expand Down
48 changes: 48 additions & 0 deletions src/website.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,54 @@ impl Website {
self
}

/// Use OpenAI to generate dynamic javascript snippets.
#[napi]
pub fn with_openai(&mut self, openai_configs: Option<Object>) -> &Self {
match openai_configs {
Some(obj) => {
let keys = Object::keys(&obj).unwrap_or_default();
let mut model = String::new();
let mut prompt = String::new();
let mut max_tokens = 1;

// todo: get the urlmap for explicit routes.
for key in keys.into_iter() {
if key == "model" {
let value = obj
.get::<String, String>(key)
.unwrap_or_default()
.unwrap_or_default();

model = value;
} else if key == "prompt" {
let value = obj
.get::<String, String>(key)
.unwrap_or_default()
.unwrap_or_default();

prompt = value;
} else if key == "maxTokens" {
let value = obj
.get::<String, u16>(key)
.unwrap_or_default()
.unwrap_or_default();

max_tokens = value;
}
}

let gpt_configs = spider::configuration::GPTConfigs::new(&model, &prompt, max_tokens);

self.inner.with_openai(Some(gpt_configs));
}
_ => {
self.inner.with_openai(None);
}
};

self
}

/// Delay between request as ms.
#[napi]
pub fn with_delay(&mut self, delay: u32) -> &Self {
Expand Down

0 comments on commit 2307080

Please sign in to comment.