|
|
@@ -0,0 +1,60 @@
|
|
|
+use reqwest::Client;
|
|
|
+use serde::Serialize;
|
|
|
+use serde_json::{json, Value};
|
|
|
+use crate::{
|
|
|
+ http_error::HttpError,
|
|
|
+ DEEPINFRA_TOKEN
|
|
|
+};
|
|
|
+
|
|
|
+#[derive(Serialize)]
|
|
|
+pub struct PromptMessage {
|
|
|
+ role: PromptRole,
|
|
|
+ content: String
|
|
|
+}
|
|
|
+
|
|
|
+#[derive(Serialize)]
|
|
|
+#[serde(rename_all = "lowercase")]
|
|
|
+pub enum PromptRole {
|
|
|
+ User,
|
|
|
+ System,
|
|
|
+ Assistant
|
|
|
+}
|
|
|
+
|
|
|
+impl PromptRole {
|
|
|
+ fn to_string(&self) -> String {
|
|
|
+ match self {
|
|
|
+ PromptRole::System => "system".to_string(),
|
|
|
+ PromptRole::User => "user".to_string(),
|
|
|
+ PromptRole::Assistant => "assistant".to_string()
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+pub async fn send_message(prompt: Vec<PromptMessage>) -> Result<String, HttpError> {
|
|
|
+ let client = Client::new();
|
|
|
+ let key = DEEPINFRA_TOKEN.get().unwrap();
|
|
|
+
|
|
|
+ let body = json!({
|
|
|
+ "model": "Qwen/Qwen3-32B",
|
|
|
+ "messages": prompt
|
|
|
+ });
|
|
|
+
|
|
|
+ let response = client
|
|
|
+ .post("https://api.deepinfra.com/v1/openai/chat/completions")
|
|
|
+ .header("Authorization", format!("Bearer {}", key))
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
+ .json(&body)
|
|
|
+ .send()
|
|
|
+ .await
|
|
|
+ .map_err(|e| HttpError::InternalError(e.to_string()))?;
|
|
|
+
|
|
|
+ let data: Value = response
|
|
|
+ .json()
|
|
|
+ .await
|
|
|
+ .map_err(|e| HttpError::InternalError(e.to_string()))?;
|
|
|
+
|
|
|
+ let content = &data["choices"][0]["message"]["content"];
|
|
|
+ println!("{}", content);
|
|
|
+
|
|
|
+ Ok(content.to_string())
|
|
|
+}
|