MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / call_openai

Method call_openai

atomic-repository/src/ai/mod.rs:370–430  ·  view source on GitHub ↗
(&self, context: &str, query: &str)

Source from the content-addressed store, hash-verified

368 }
369
370 async fn call_openai(&self, context: &str, query: &str) -> Result<LlmResponse, AiError> {
371 let url = format!("{}/chat/completions", self.base_url);
372 let body = serde_json::json!({
373 "model": self.model,
374 "max_tokens": 1024,
375 "messages": [
376 {
377 "role": "system",
378 "content": "You are a code assistant for a repository tracked by Atomic VCS (not git). \
379 The context includes: entities (functions, structs, traits extracted by AST), \
380 files, changes (these ARE the commit history — each has a date, sequence number, and message), \
381 views (like branches), goals (development sessions), and intents (work items). \
382 Change nodes show the project's history — use their dates and messages to answer questions about \
383 recent modifications. Answer using only the provided context. Be concise and precise."
384 },
385 {
386 "role": "user",
387 "content": format!("{}\n\nQuestion: {}", context, query)
388 }
389 ]
390 });
391
392 let client = reqwest::Client::new();
393 let resp = client
394 .post(&url)
395 .header("Authorization", format!("Bearer {}", self.api_key))
396 .header("content-type", "application/json")
397 .json(&body)
398 .send()
399 .await
400 .map_err(|e| AiError::Http(e.to_string()))?;
401
402 if !resp.status().is_success() {
403 let status = resp.status();
404 let text = resp.text().await.unwrap_or_default();
405 return Err(AiError::ApiError {
406 status: status.as_u16(),
407 body: text,
408 });
409 }
410
411 let json: serde_json::Value = resp
412 .json()
413 .await
414 .map_err(|e| AiError::Http(e.to_string()))?;
415
416 let text = json["choices"]
417 .as_array()
418 .and_then(|arr| arr.first())
419 .and_then(|c| c["message"]["content"].as_str())
420 .unwrap_or("")
421 .to_string();
422
423 let tokens = json["usage"]["completion_tokens"].as_u64();
424
425 Ok(LlmResponse {
426 answer: text,
427 model: self.model.clone(),

Callers 1

answerMethod · 0.80

Calls 9

postMethod · 0.80
textMethod · 0.80
as_u64Method · 0.80
jsonMethod · 0.45
headerMethod · 0.45
is_successMethod · 0.45
statusMethod · 0.45
as_strMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected