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

Function call_llm

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

Source from the content-addressed store, hash-verified

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

Callers 3

ai_queryFunction · 0.85

Calls

no outgoing calls