Skip to content

Commit

Permalink
migrate to api v2 and model gpt-4o
Browse files Browse the repository at this point in the history
  • Loading branch information
rauner committed May 15, 2024
1 parent dfa7ffb commit b04e69a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 46 deletions.
File renamed without changes.
35 changes: 9 additions & 26 deletions rust_bot/instruction/instruction.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,38 +93,21 @@ 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


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]
```

44 changes: 26 additions & 18 deletions rust_bot/src/assistant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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<String> = 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")
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions rust_bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit b04e69a

Please sign in to comment.