Skip to content

Commit

Permalink
chore(openai): add json_structure handling js browser events
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Oct 26, 2024
1 parent 9fa9d4d commit 692c7b0
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 27 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spider/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider"
version = "2.10.26"
version = "2.10.27"
authors = [
"j-mendez <[email protected]>"
]
Expand Down
50 changes: 35 additions & 15 deletions spider/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1974,23 +1974,43 @@ pub async fn openai_request_base(
let mut tokens_used = crate::features::openai_common::OpenAIUsage::default();
let json_mode = gpt_configs.extra_ai_data;

let response_format = match gpt_configs.json_schema {
Some(ref structure) => async_openai::types::ResponseFormat::JsonSchema {
json_schema: async_openai::types::ResponseFormatJsonSchema {
description: structure.description.clone(),
name: structure.name.clone(),
schema: serde_json::from_str(&structure.schema.clone().unwrap_or_default())
.unwrap_or_default(),
strict: structure.strict,
},
},
_ => {
if json_mode {
async_openai::types::ResponseFormat::JsonObject
} else {
async_openai::types::ResponseFormat::Text
let response_format = {
let mut mode = if json_mode {
async_openai::types::ResponseFormat::JsonObject
} else {
async_openai::types::ResponseFormat::Text
};

if let Some(ref structure) = gpt_configs.json_schema {
if let Some(ref schema) = structure.schema {
if let Ok(mut schema) = serde_json::from_str::<serde_json::Value>(&schema) {
if json_mode {
// Insert the "js" property into the schema's properties. Todo: capture if the js property exist and re-word prompt to match new js property with after removal.
if let Some(properties) = schema.get_mut("properties") {
if let Some(properties_map) = properties.as_object_mut() {
properties_map.insert(
"js".to_string(),
serde_json::json!({
"type": "string"
}),
);
}
}
}

mode = async_openai::types::ResponseFormat::JsonSchema {
json_schema: async_openai::types::ResponseFormatJsonSchema {
description: structure.description.clone(),
name: structure.name.clone(),
schema: if schema.is_null() { None } else { Some(schema) },
strict: structure.strict,
},
}
}
}
}

mode
};

match async_openai::types::ChatCompletionRequestAssistantMessageArgs::default()
Expand Down
2 changes: 1 addition & 1 deletion spider_chrome/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_chrome"
version = "2.10.26"
version = "2.10.27"
rust-version = "1.70"
authors = [
"j-mendez <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion spider_cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_cli"
version = "2.10.26"
version = "2.10.27"
authors = [
"j-mendez <[email protected]>"
]
Expand Down
2 changes: 1 addition & 1 deletion spider_transformations/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_transformations"
version = "2.10.26"
version = "2.10.27"
authors = [
"j-mendez <[email protected]>"
]
Expand Down
2 changes: 1 addition & 1 deletion spider_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_utils"
version = "2.10.26"
version = "2.10.27"
authors = [
"j-mendez <[email protected]>"
]
Expand Down
2 changes: 1 addition & 1 deletion spider_worker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_worker"
version = "2.10.26"
version = "2.10.27"
authors = [
"j-mendez <[email protected]>"
]
Expand Down

0 comments on commit 692c7b0

Please sign in to comment.