|
| 1 | +use reqwest::Client; |
| 2 | +use serde_json::json; |
| 3 | +use std::{error::Error, time::Duration}; |
| 4 | +use tokio::time::sleep; |
| 5 | + |
| 6 | +pub async fn search_web(api_key: &str, query: &str) -> Result<String, Box<dyn Error>> { |
| 7 | + let client = Client::new(); |
| 8 | + let base = "https://api.openai.com/v1"; |
| 9 | + |
| 10 | + let assistant_res: serde_json::Value = client |
| 11 | + .post(&format!("{}/assistants", base)) |
| 12 | + .header("Authorization", format!("Bearer {}", api_key)) |
| 13 | + .header("OpenAI-Beta", "assistants=v1") |
| 14 | + .json(&json!({ |
| 15 | + "model": "gpt-4o", |
| 16 | + "instructions": "You are a web search assistant.", |
| 17 | + "tools": [{"type": "browser"}] |
| 18 | + })) |
| 19 | + .send() |
| 20 | + .await? |
| 21 | + .json() |
| 22 | + .await?; |
| 23 | + |
| 24 | + let assistant_id = assistant_res["id"] |
| 25 | + .as_str() |
| 26 | + .ok_or("missing assistant id")? |
| 27 | + .to_string(); |
| 28 | + |
| 29 | + let thread_res: serde_json::Value = client |
| 30 | + .post(&format!("{}/threads", base)) |
| 31 | + .header("Authorization", format!("Bearer {}", api_key)) |
| 32 | + .header("OpenAI-Beta", "assistants=v1") |
| 33 | + .send() |
| 34 | + .await? |
| 35 | + .json() |
| 36 | + .await?; |
| 37 | + |
| 38 | + let thread_id = thread_res["id"] |
| 39 | + .as_str() |
| 40 | + .ok_or("missing thread id")? |
| 41 | + .to_string(); |
| 42 | + |
| 43 | + client |
| 44 | + .post(&format!("{}/threads/{}/messages", base, thread_id)) |
| 45 | + .header("Authorization", format!("Bearer {}", api_key)) |
| 46 | + .header("OpenAI-Beta", "assistants=v1") |
| 47 | + .json(&json!({"role": "user", "content": query})) |
| 48 | + .send() |
| 49 | + .await?; |
| 50 | + |
| 51 | + let run_res: serde_json::Value = client |
| 52 | + .post(&format!("{}/threads/{}/runs", base, thread_id)) |
| 53 | + .header("Authorization", format!("Bearer {}", api_key)) |
| 54 | + .header("OpenAI-Beta", "assistants=v1") |
| 55 | + .json(&json!({"assistant_id": assistant_id})) |
| 56 | + .send() |
| 57 | + .await? |
| 58 | + .json() |
| 59 | + .await?; |
| 60 | + |
| 61 | + let run_id = run_res["id"].as_str().ok_or("missing run id")?.to_string(); |
| 62 | + |
| 63 | + loop { |
| 64 | + let run_status: serde_json::Value = client |
| 65 | + .get(&format!("{}/threads/{}/runs/{}", base, thread_id, run_id)) |
| 66 | + .header("Authorization", format!("Bearer {}", api_key)) |
| 67 | + .header("OpenAI-Beta", "assistants=v1") |
| 68 | + .send() |
| 69 | + .await? |
| 70 | + .json() |
| 71 | + .await?; |
| 72 | + |
| 73 | + match run_status["status"].as_str() { |
| 74 | + Some("completed") => break, |
| 75 | + Some("failed") | Some("expired") | Some("cancelled") => return Err("run failed".into()), |
| 76 | + _ => sleep(Duration::from_secs(1)).await, |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + let messages: serde_json::Value = client |
| 81 | + .get(&format!("{}/threads/{}/messages", base, thread_id)) |
| 82 | + .header("Authorization", format!("Bearer {}", api_key)) |
| 83 | + .header("OpenAI-Beta", "assistants=v1") |
| 84 | + .send() |
| 85 | + .await? |
| 86 | + .json() |
| 87 | + .await?; |
| 88 | + |
| 89 | + let answer = messages["data"][0]["content"][0]["text"]["value"] |
| 90 | + .as_str() |
| 91 | + .unwrap_or("") |
| 92 | + .to_string(); |
| 93 | + |
| 94 | + // cleanup |
| 95 | + let _ = client |
| 96 | + .delete(&format!("{}/assistants/{}", base, assistant_id)) |
| 97 | + .header("Authorization", format!("Bearer {}", api_key)) |
| 98 | + .header("OpenAI-Beta", "assistants=v1") |
| 99 | + .send() |
| 100 | + .await; |
| 101 | + let _ = client |
| 102 | + .delete(&format!("{}/threads/{}", base, thread_id)) |
| 103 | + .header("Authorization", format!("Bearer {}", api_key)) |
| 104 | + .header("OpenAI-Beta", "assistants=v1") |
| 105 | + .send() |
| 106 | + .await; |
| 107 | + |
| 108 | + Ok(answer) |
| 109 | +} |
0 commit comments