prompt.rs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. use reqwest::Client;
  2. use serde::Serialize;
  3. use serde_json::{json, Value};
  4. use crate::{
  5. http_error::HttpError,
  6. DEEPINFRA_TOKEN
  7. };
  8. #[derive(Serialize)]
  9. pub struct PromptMessage {
  10. pub role: PromptRole,
  11. pub content: String
  12. }
  13. #[derive(Serialize)]
  14. #[serde(rename_all = "lowercase")]
  15. pub enum PromptRole {
  16. User,
  17. System,
  18. Assistant
  19. }
  20. pub struct PromptResponse {
  21. pub message: String,
  22. pub input_tokens: i32,
  23. pub output_tokens: i32
  24. }
  25. pub async fn send_message(prompt: Vec<PromptMessage>) -> Result<PromptResponse, HttpError> {
  26. let client = Client::new();
  27. let key = DEEPINFRA_TOKEN.get().unwrap();
  28. let body = json!({
  29. "model": "Qwen/Qwen3-32B",
  30. "messages": prompt,
  31. "reasoning_effort": "none"
  32. });
  33. let response = client
  34. .post("https://api.deepinfra.com/v1/chat/completions")
  35. .header("Authorization", format!("Bearer {}", key))
  36. .header("Content-Type", "application/json")
  37. .json(&body)
  38. .send()
  39. .await
  40. .map_err(|e| HttpError::InternalError(e.to_string()))?;
  41. let data: Value = response
  42. .json()
  43. .await
  44. .map_err(|e| HttpError::InternalError(e.to_string()))?;
  45. Ok(PromptResponse {
  46. message: (&data["choices"][0]["message"]["content"]).to_string(),
  47. input_tokens: data["usage"]["prompt_tokens"].as_i64().unwrap_or(0) as i32,
  48. output_tokens: data["usage"]["completion_tokens"].as_i64().unwrap_or(0) as i32
  49. })
  50. }