From b04e69a68475061607fe7baa875e1900f4e003cf Mon Sep 17 00:00:00 2001 From: rauner <29773634+rauner@users.noreply.github.com> Date: Wed, 15 May 2024 14:16:14 +0200 Subject: [PATCH] migrate to api v2 and model gpt-4o --- .../{ => file_search}/help_articles.json | 0 rust_bot/instruction/instruction.txt | 35 ++++----------- rust_bot/src/assistant.rs | 44 +++++++++++-------- rust_bot/src/main.rs | 4 +- 4 files changed, 37 insertions(+), 46 deletions(-) rename rust_bot/context/{ => file_search}/help_articles.json (100%) diff --git a/rust_bot/context/help_articles.json b/rust_bot/context/file_search/help_articles.json similarity index 100% rename from rust_bot/context/help_articles.json rename to rust_bot/context/file_search/help_articles.json diff --git a/rust_bot/instruction/instruction.txt b/rust_bot/instruction/instruction.txt index f145596..affb491 100644 --- a/rust_bot/instruction/instruction.txt +++ b/rust_bot/instruction/instruction.txt @@ -93,6 +93,7 @@ https://buycycle.com/de-de/bike/[slug] Use this python code to filter out recommended bikes: +Do not use comments, when generating this code ```python import json @@ -100,31 +101,13 @@ import json with open("/mnt/data/{bikes.json}", "r") as file: bikes_data = json.load(file) - -# Filter bikes based on category, budget, rider height, and color -filtered_bikes = [] -for bike in bikes_data: - if bike["category"] == category \ - and bike["color"].lower() == color_preference.lower() \ - and budget_range_min <= bike["price"] <= budget_range_max \ - and bike["rider_height_min"] <= rider_height <= bike["rider_height_max"]: - filtered_bikes.append(bike) - -if not filtered_bikes: - for bike in bikes_data: - if bike["category"].lower() == category.lower() \ - and budget_range_min <= bike["price"] <= budget_range_max: - filtered_bikes.append(bike) - -# Sort filtered bikes by price, closest to the preferred budget -sorted_filtered_bikes = sorted(filtered_bikes, key=lambda x: abs(x["price"]-(budget_min+budget_max) -/2)) - -# Select the top 5 bikes based on the sorting -top_5_bikes = sorted_filtered_bikes[:5] - -# Generate URLs for the top 5 bikes -bike_urls = [f"https://buycycle.com/de-de/bike/{bike['slug']}" for bike in top_5_bikes] - +filtered_bikes = [bike for bike in bikes_data if bike["category"] == category and + bike["color"].lower() == color_preference.lower() and + budget_range_min <= bike["price"] <= budget_range_max and + bike["rider_height_min"] <= rider_height <= bike["rider_height_max"]] or \ + [bike for bike in bikes_data if bike["category"].lower() == category.lower() and + budget_range_min <= bike["price"] <= budget_range_max] +sorted_filtered_bikes = sorted(filtered_bikes, key=lambda x: abs(x["price"] - (budget_range_min + budget_range_max) / 2))[:5] +bike_urls = [f"https://buycycle.com/de-de/bike/{bike['slug']}" for bike in sorted_filtered_bikes] ``` diff --git a/rust_bot/src/assistant.rs b/rust_bot/src/assistant.rs index 19a6c34..92d9625 100644 --- a/rust_bot/src/assistant.rs +++ b/rust_bot/src/assistant.rs @@ -186,7 +186,11 @@ impl Ressources { let bikes_json_string = serde_json::to_string_pretty(&bikes) .map_err(|e| AssistantError::OpenAIError(e.to_string()))?; // Write the JSON data to a file in the specified folder_path - let file_path = PathBuf::from(&self.folder_path_file_search).join("bikes.json"); + let folder_path = PathBuf::from(&self.folder_path_code_interpreter); + if !folder_path.exists() { + fs::create_dir_all(&folder_path).expect("Failed to create folder_path_code_interpreter"); + }; + let file_path = PathBuf::from(&self.folder_path_code_interpreter).join("bikes.json"); let mut file = File::create(file_path).map_err(|e| AssistantError::DatabaseError(e.to_string()))?; file.write_all(bikes_json_string.as_bytes()) @@ -228,7 +232,7 @@ impl Ressources { let form = Form::new().part("file", part).text("purpose", "assistants"); let response = client .post("https://api.openai.com/v1/files") - .header("OpenAI-Beta", "assistants=v1") + .header("OpenAI-Beta", "assistants=v2") .bearer_auth(&api_key) .multipart(form) .send() @@ -297,7 +301,7 @@ impl Ressources { let form = Form::new().part("file", part).text("purpose", "assistants"); let response = client .post("https://api.openai.com/v1/files") - .header("OpenAI-Beta", "assistants=v1") + .header("OpenAI-Beta", "assistants=v2") .bearer_auth(&api_key) .multipart(form) .send() @@ -332,13 +336,17 @@ impl Ressources { Ok(()) } pub async fn create_vector_store(&mut self) -> Result<(), AssistantError> { - // Ensure files are uploaded before creating the vector store - self.upload_files_search().await?; // Extract file_ids from files_info_file_search let file_ids: Vec = self.files_info_file_search.iter().map(|info| info.file_id.clone()).collect(); // Prepare the JSON payload with file_ids let payload = json!({ - "file_ids": file_ids + "file_ids": file_ids, + "name": "assistant_vector_store", + "expires_after": { + "anchor": "last_active_at", + "days": 7 + }, + }); // Get the OpenAI API key from the environment let api_key = env::var("OPENAI_API_KEY") @@ -446,19 +454,19 @@ impl Assistant { {"type": "file_search"}, {"type": "code_interpreter"} ], - "tool_ressources": { - "file_search": { + "tool_resources": { + "code_interpreter": { "file_ids": file_ids_code_interpreter }, - "code_interpreter": { - "vector_store_id": vector_store_id + "file_search": { + "vector_store_ids": [vector_store_id] } }, "model": self.model, }); let response = client .post("https://api.openai.com/v1/assistants") - .header("OpenAI-Beta", "assistants=v1") + .header("OpenAI-Beta", "assistants=v2") .bearer_auth(&api_key) .json(&payload) .send() @@ -498,7 +506,7 @@ impl Assistant { let client = Client::new(); let response = client .delete(format!("https://api.openai.com/v1/assistants/{}", self.id)) - .header("OpenAI-Beta", "assistants=v1") + .header("OpenAI-Beta", "assistants=v2") .bearer_auth(&api_key) .send() .await; @@ -532,7 +540,7 @@ impl Assistant { let response = client .patch(&format!("https://api.openai.com/v1/assistants/{}", self.id)) .header("Content-Type", "application/json") - .header("OpenAI-Beta", "assistants=v1") + .header("OpenAI-Beta", "assistants=v2") .bearer_auth(&api_key) .json(&payload) .send() @@ -609,7 +617,7 @@ impl Chat { .post("https://api.openai.com/v1/threads") .header("Content-Type", "application/json") .bearer_auth(&api_key) - .header("OpenAI-Beta", "assistants=v1") + .header("OpenAI-Beta", "assistants=v2") .send() .await; match response { @@ -649,7 +657,7 @@ impl Chat { )) .header("Content-Type", "application/json") .bearer_auth(&api_key) - .header("OpenAI-Beta", "assistants=v1") + .header("OpenAI-Beta", "assistants=v2") .send() .await; match response { @@ -711,7 +719,7 @@ impl Chat { )) .header("Content-Type", "application/json") .bearer_auth(&api_key) - .header("OpenAI-Beta", "assistants=v1") + .header("OpenAI-Beta", "assistants=v2") .json(&payload) .send() .await; @@ -816,7 +824,7 @@ impl Run { )) .header("Content-Type", "application/json") .bearer_auth(&api_key) - .header("OpenAI-Beta", "assistants=v1") + .header("OpenAI-Beta", "assistants=v2") .json(&payload) .send() .await; @@ -851,7 +859,7 @@ impl Run { chat_id, self.id )) .header("Authorization", format!("Bearer {}", api_key)) - .header("OpenAI-Beta", "assistants=v1") + .header("OpenAI-Beta", "assistants=v2") .send() .await; match response { diff --git a/rust_bot/src/main.rs b/rust_bot/src/main.rs index 912aade..a1fe12e 100644 --- a/rust_bot/src/main.rs +++ b/rust_bot/src/main.rs @@ -45,7 +45,7 @@ async fn main() { let now = Utc::now(); let timestamp = now.format("%Y%m%d_%H%M%S").to_string(); let assistant_name = format!("Assistant_{}", timestamp); - let mut assistant = match create_assistant(&assistant_name, "gpt-4-1106-preview", ressources.clone()).await { + let mut assistant = match create_assistant(&assistant_name, "gpt-4o", ressources.clone()).await { Ok(assistant) => assistant, Err(e) => { log::error!("Failed to create assistant: {:?}", e); @@ -83,7 +83,7 @@ async fn main() { let assistant_name = format!("Assistant_{}", timestamp); match create_ressources("context/file_search","context/code_interpreter", Vec::new(), "instruction/instruction.txt").await { Ok(new_ressources) => { - match create_assistant(&assistant_name, "gpt-4-1106-preview", new_ressources.clone()) + match create_assistant(&assistant_name, "gpt-4o", new_ressources.clone()) .await { Ok(new_assistant) => {