Skip to content

Commit 3af3104

Browse files
committed
use context and merge payload
1 parent 0ff37d5 commit 3af3104

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

src/llm/gemini.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use async_trait::async_trait;
22
use crate::llm::{LlmGenerationClient, LlmSpec, LlmGenerateRequest, LlmGenerateResponse, ToJsonSchemaOptions, OutputFormat};
3-
use anyhow::{Result, bail};
3+
use anyhow::{Result, bail, Context};
44
use serde_json::Value;
55
use crate::api_bail;
66
use urlencoding::encode;
@@ -55,17 +55,12 @@ impl LlmGenerationClient for Client {
5555
"parts": [{ "text": request.user_prompt }]
5656
})];
5757

58-
// Optionally add system prompt
59-
let system_instruction = request.system_prompt.map(|system|
60-
serde_json::json!({
61-
"parts": [ { "text": system } ]
62-
})
63-
);
64-
6558
// Prepare payload
6659
let mut payload = serde_json::json!({ "contents": contents });
67-
if let Some(system) = system_instruction {
68-
payload["systemInstruction"] = system;
60+
if let Some(system) = request.system_prompt {
61+
payload["systemInstruction"] = serde_json::json!({
62+
"parts": [ { "text": system } ]
63+
});
6964
}
7065

7166
// If structured output is requested, add schema and responseMimeType
@@ -84,18 +79,13 @@ impl LlmGenerationClient for Client {
8479
encode(&self.model), encode(api_key)
8580
);
8681

87-
let resp = match self.client.post(&url)
82+
let resp = self.client.post(&url)
8883
.json(&payload)
8984
.send()
90-
.await {
91-
Ok(resp) => resp,
92-
Err(e) => api_bail!("HTTP error: {e}"),
93-
};
85+
.await
86+
.context("HTTP error")?;
9487

95-
let resp_json: Value = match resp.json().await {
96-
Ok(json) => json,
97-
Err(e) => api_bail!("Invalid JSON: {e}"),
98-
};
88+
let resp_json: Value = resp.json().await.context("Invalid JSON")?;
9989

10090
if let Some(error) = resp_json.get("error") {
10191
bail!("Gemini API error: {:?}", error);

0 commit comments

Comments
 (0)