MCPcopy Create free account
hub / github.com/CodeGraphContext/CodeGraphContext / call_llm

Function call_llm

src/codegraphcontext/viz/server.py:351–412  ·  view source on GitHub ↗
(system_prompt: str, user_prompt: str)

Source from the content-addressed store, hash-verified

349 raise HTTPException(status_code=500, detail=str(e))
350
351def call_llm(system_prompt: str, user_prompt: str) -> str:
352 gemini_key = os.getenv("GEMINI_API_KEY")
353 openai_key = os.getenv("OPENAI_API_KEY")
354 atlascloud_key = os.getenv("ATLASCLOUD_API_KEY") or os.getenv("ATLAS_CLOUD_API_KEY")
355
356 if not gemini_key and not openai_key and not atlascloud_key:
357 raise ValueError(
358 "GEMINI_API_KEY, OPENAI_API_KEY, or ATLASCLOUD_API_KEY environment variable is required."
359 )
360
361 if gemini_key:
362 url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={gemini_key}"
363 payload = {
364 "contents": [
365 {
366 "role": "user",
367 "parts": [{"text": f"{system_prompt}\n\nUser Prompt:\n{user_prompt}"}]
368 }
369 ]
370 }
371 res = requests.post(url, json=payload, timeout=30)
372 if res.status_code != 200:
373 raise RuntimeError(f"Gemini API returned error {res.status_code}: {res.text}")
374 data = res.json()
375 try:
376 return data["candidates"][0]["content"]["parts"][0]["text"]
377 except (KeyError, IndexError) as e:
378 raise RuntimeError(f"Failed to parse Gemini API response: {data}") from e
379 else:
380 if openai_key:
381 provider_name = "OpenAI"
382 api_key = openai_key
383 url = "https://api.openai.com/v1/chat/completions"
384 model = "gpt-4o-mini"
385 else:
386 provider_name = "Atlas Cloud"
387 api_key = atlascloud_key
388 base_url = os.getenv("ATLASCLOUD_API_BASE") or os.getenv("ATLAS_CLOUD_API_BASE")
389 base_url = (base_url or "https://api.atlascloud.ai/v1").rstrip("/")
390 url = f"{base_url}/chat/completions"
391 model = os.getenv("ATLASCLOUD_MODEL") or os.getenv("ATLAS_CLOUD_MODEL")
392 model = model or "deepseek-ai/deepseek-v4-pro"
393
394 headers = {
395 "Content-Type": "application/json",
396 "Authorization": f"Bearer {api_key}"
397 }
398 payload = {
399 "model": model,
400 "messages": [
401 {"role": "system", "content": system_prompt},
402 {"role": "user", "content": user_prompt}
403 ]
404 }
405 res = requests.post(url, json=payload, headers=headers, timeout=30)
406 if res.status_code != 200:
407 raise RuntimeError(f"{provider_name} API returned error {res.status_code}: {res.text}")
408 data = res.json()

Callers 3

ai_queryFunction · 0.85

Calls

no outgoing calls