Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions crates/sprout-agent/src/llm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,14 @@ impl Llm {
let v = post(&self.http, &url, &body, |r| {
r.header("x-api-key", &cfg.api_key)
.header("anthropic-version", &cfg.anthropic_api_version)
.header("content-type", "application/json")
})
.await?;
parse_anthropic(v)
}
Provider::OpenAi => {
let body = openai_body(cfg, history, tools);
let url = format!("{}/chat/completions", cfg.base_url.trim_end_matches('/'));
let v = post(&self.http, &url, &body, |r| {
r.bearer_auth(&cfg.api_key)
.header("content-type", "application/json")
})
.await?;
let v = post(&self.http, &url, &body, |r| r.bearer_auth(&cfg.api_key)).await?;
parse_openai(v)
}
}
Expand Down Expand Up @@ -74,7 +69,6 @@ impl Llm {
let v = post(&self.http, &url, &body, |r| {
r.header("x-api-key", &cfg.api_key)
.header("anthropic-version", &cfg.anthropic_api_version)
.header("content-type", "application/json")
})
.await?;
Ok(parse_anthropic(v)?.text)
Expand All @@ -83,18 +77,14 @@ impl Llm {
let body = json!({
"model": cfg.model,
"stream": false,
"max_tokens": max_output_tokens,
"max_completion_tokens": max_output_tokens,
"messages": [
{ "role": "system", "content": system_prompt },
{ "role": "user", "content": user_prompt },
],
});
let url = format!("{}/chat/completions", cfg.base_url.trim_end_matches('/'));
let v = post(&self.http, &url, &body, |r| {
r.bearer_auth(&cfg.api_key)
.header("content-type", "application/json")
})
.await?;
let v = post(&self.http, &url, &body, |r| r.bearer_auth(&cfg.api_key)).await?;
Ok(parse_openai(v)?.text)
}
}
Expand Down Expand Up @@ -193,8 +183,8 @@ fn openai_body(cfg: &Config, history: &[HistoryItem], tools: &[ToolDef]) -> Valu
"parameters": t.input_schema } })
})
.collect();
let mut body = json!({ "model": cfg.model, "stream": false, "max_tokens": cfg.max_output_tokens,
"messages": messages });
let mut body = json!({ "model": cfg.model, "stream": false,
"max_completion_tokens": cfg.max_output_tokens, "messages": messages });
if !tools_json.is_empty() {
body["tools"] = Value::Array(tools_json);
body["tool_choice"] = json!("auto");
Expand Down Expand Up @@ -338,8 +328,17 @@ async fn post<F>(http: &Client, url: &str, body: &Value, apply: F) -> Result<Val
where
F: Fn(reqwest::RequestBuilder) -> reqwest::RequestBuilder,
{
let body_bytes =
serde_json::to_vec(body).map_err(|e| AgentError::Llm(format!("serialize: {e}")))?;
for attempt in 0..MAX_RETRIES {
let resp = match apply(http.post(url).json(body)).send().await {
let resp = match apply(
http.post(url)
.header("content-type", "application/json")
.body(body_bytes.clone()),
)
.send()
.await
{
Ok(r) => r,
Err(e) => {
if attempt + 1 < MAX_RETRIES && (e.is_timeout() || e.is_connect()) {
Expand Down
Loading