Spaces:
Runtime error
Runtime error
Quincy Hsieh commited on
Commit ·
929f2ac
1
Parent(s): b250e76
Add CO2 emission and token counts
Browse filesFiles changed (3)
- app.py+31-45
- llm.pyAdded+133
- requirements.txt+1
35 unmodified lines363738394041215 unmodified lines25725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930036 unmodified lines33733833934034134234334434534634734834935035135217 unmodified lines370371372373374375115 unmodified lines4914924934944954964974984995001 unmodified line50250350450550650750850929 unmodified lines53954054154254354454554654754854935 unmodified linesfrom langchain_text_splitters import RecursiveCharacterTextSplitterfrom pypdf import PdfReaderlogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)215 unmodified linesreturn contextsdef call_llm(prompt: str) -> dict:"""Make a call to the LLM via Azure Foundry (GPT-5).Calls the Azure OpenAI-compatible chat/completions endpoint.Endpoint URL is loaded from config.json; API key from AZURE_API_KEY env var.Returns a dict with 'content' (str) and 'total_tokens' (int)."""headers = {"api-key": AZURE_API_KEY,"Content-Type": "application/json",}payload = {"model": LLM_MODEL_NAME,"messages": [{"role": "user", "content": prompt}],"max_completion_tokens": LLM_MAX_TOKENS,"temperature": LLM_TEMPERATURE,"top_p": LLM_TOP_P,}try:resp = http_requests.post(LLM_ENDPOINT_URL, headers=headers, json=payload, timeout=None)resp.raise_for_status()data = resp.json()content = data["choices"][0]["message"]["content"].strip()total_tokens = data.get("usage", {}).get("total_tokens", 0)return {"content": content, "total_tokens": total_tokens}except http_requests.exceptions.HTTPError as e:logger.error(f"LLM API call failed: {e} — {resp.text}")raise HTTPException(status_code=503, detail=f"LLM service unavailable: {str(e)}")except (http_requests.exceptions.JSONDecodeError, ValueError) as e:logger.error(f"LLM API returned non-JSON response (status {resp.status_code}): {repr(resp.text)}")raise HTTPException(status_code=502, detail="LLM service returned an invalid response")except (KeyError, IndexError) as e:logger.error(f"Unexpected LLM response format: {e} — body: {resp.text}")raise HTTPException(status_code=502, detail="Unexpected response from LLM service")def build_rag_prompt(query: str, contexts: list[dict]) -> str:"""Construct the RAG prompt by combining retrieved context with the user question.36 unmodified lines"sources": [],"explanation": "No documents found in the vector store to retrieve context from.","total_token": 0,"run_time_in_ms": elapsed_ms,}# Step 2: Build the augmented promptprompt = build_rag_prompt(query, contexts)# Step 3: Generate answer from LLMllm_result = call_llm(prompt)raw_content = llm_result["content"]total_token = llm_result["total_tokens"]# Parse structured JSON response from LLM (handle markdown code fences)json_str = raw_content.strip()17 unmodified lines"sources": [{"source": ctx["source"], "score": ctx["similarity_score"], "ref_text": ctx["text"]} for ctx in contexts],"explanation": explanation,"total_token": total_token,"run_time_in_ms": elapsed_ms,}115 unmodified lines# Step 6: Gradio UI for Interactive Demo# ---------------------------------------------------------------------------def gradio_query(question: str) -> tuple[str, str, str, str]:"""Handle queries from the Gradio chat interface."""if not question.strip():return "Please enter a question.", "", "", ""result = rag_query(question)sources_text = "\n".join(f" - {s['source']} (relevance: {s['score']:.2f})" for s in result["sources"]1 unmodified lineanswer = f"{result['answer']}\n\n📚 Sources:\n{sources_text}" if result["sources"] else result["answer"]explanation = result.get("explanation", "")token_info = str(result.get("total_token", 0))run_time = f"{result.get('run_time_in_ms', 0)} ms"return answer, explanation, token_info, run_timedef gradio_ingest(text: str, source_name: str) -> str:29 unmodified linesquery_explanation = gr.Textbox(label="Explanation", lines=3, interactive=False)with gr.Row():query_tokens = gr.Textbox(label="Total Tokens", interactive=False)query_runtime = gr.Textbox(label="Run Time", interactive=False)query_button.click(fn=gradio_query,inputs=query_input,outputs=[query_output, query_explanation, query_tokens, query_runtime],)with gr.Tab("📄 Ingest Documents"):35 unmodified lines3637383940414243215 unmodified lines25926026126226326436 unmodified lines30130230330430530630730830931031131231331431531631731831932032132232332432532632732832933017 unmodified lines348349350351352353354355356357358115 unmodified lines4744754764774784794804814824831 unmodified line48548648748848949049149249349429 unmodified lines52452552652752852953053153253353453535 unmodified linesfrom langchain_text_splitters import RecursiveCharacterTextSplitterfrom pypdf import PdfReaderfrom llm import call_llm as call_llm_with_metricslogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)215 unmodified linesreturn contextsdef build_rag_prompt(query: str, contexts: list[dict]) -> str:"""Construct the RAG prompt by combining retrieved context with the user question.36 unmodified lines"sources": [],"explanation": "No documents found in the vector store to retrieve context from.","total_token": 0,"prompt_tokens": 0,"completion_tokens": 0,"cached_tokens": 0,"co2_grams": None,"energy_kwh": None,"run_time_in_ms": elapsed_ms,}# Step 2: Build the augmented promptprompt = build_rag_prompt(query, contexts)# Step 3: Generate answer from LLM (with token + CO2 metrics)llm_result = call_llm_with_metrics(prompt,endpoint_url=LLM_ENDPOINT_URL,api_key=AZURE_API_KEY,model=LLM_MODEL_NAME,max_completion_tokens=LLM_MAX_TOKENS,temperature=LLM_TEMPERATURE,top_p=LLM_TOP_P,)raw_content = llm_result["content"]tokens = llm_result["tokens"]total_token = tokens["total"]# Parse structured JSON response from LLM (handle markdown code fences)json_str = raw_content.strip()17 unmodified lines"sources": [{"source": ctx["source"], "score": ctx["similarity_score"], "ref_text": ctx["text"]} for ctx in contexts],"explanation": explanation,"total_token": total_token,"prompt_tokens": tokens["prompt"],"completion_tokens": tokens["completion"],"cached_tokens": tokens["cached"],"co2_grams": llm_result["co2_grams"],"energy_kwh": llm_result["energy_kwh"],"run_time_in_ms": elapsed_ms,}115 unmodified lines# Step 6: Gradio UI for Interactive Demo# ---------------------------------------------------------------------------def gradio_query(question: str) -> tuple[str, str, str, str, str]:"""Handle queries from the Gradio chat interface."""if not question.strip():return "Please enter a question.", "", "", "", ""result = rag_query(question)sources_text = "\n".join(f" - {s['source']} (relevance: {s['score']:.2f})" for s in result["sources"]1 unmodified lineanswer = f"{result['answer']}\n\n📚 Sources:\n{sources_text}" if result["sources"] else result["answer"]explanation = result.get("explanation", "")token_info = str(result.get("total_token", 0))co2_value = result.get("co2_grams")co2_info = f"{co2_value:.4f} g" if isinstance(co2_value, (int, float)) else "N/A"run_time = f"{result.get('run_time_in_ms', 0)} ms"return answer, explanation, token_info, co2_info, run_timedef gradio_ingest(text: str, source_name: str) -> str:29 unmodified linesquery_explanation = gr.Textbox(label="Explanation", lines=3, interactive=False)with gr.Row():query_tokens = gr.Textbox(label="Total Tokens", interactive=False)query_co2 = gr.Textbox(label="CO2 Emission", interactive=False)query_runtime = gr.Textbox(label="Run Time", interactive=False)query_button.click(fn=gradio_query,inputs=query_input,outputs=[query_output, query_explanation, query_tokens, query_co2, query_runtime],)with gr.Tab("📄 Ingest Documents"):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133"""LLM wrapper with token accounting and CO2 emission estimation.
Wraps an Azure OpenAI-compatible chat/completions call and returns:- content: the generated text- tokens: prompt / completion / cached / total- energy_kwh, co2_grams: environmental impact for the call
CO2 emissions are estimated with `ecologits`, which uses a model registry(parameter counts, hardware assumptions) plus output token count and requestlatency to compute global warming potential (kgCO2eq) and energy (kWh).We chose `ecologits` over `codecarbon` because the LLM runs on a remoteendpoint — `codecarbon` measures local process energy and would only countthe client overhead, not the inference itself."""
import loggingimport timefrom typing import Optional
import requestsfrom fastapi import HTTPException
logger = logging.getLogger(__name__)
try: from ecologits.tracers.utils import llm_impacts _ECOLOGITS_AVAILABLE = Trueexcept ImportError: _ECOLOGITS_AVAILABLE = False logger.warning("ecologits not installed — CO2 emission will be reported as None.")
def call_llm( prompt: str, *, endpoint_url: str, api_key: str, model: str, max_completion_tokens: int = 512, temperature: float = 0.7, top_p: float = 0.95, provider: str = "openai", timeout: Optional[float] = None,) -> dict: """ Call an Azure OpenAI-compatible chat/completions endpoint and return the response together with token counts and CO2 emission estimate.
Returns a dict: { "content": str, "tokens": { "prompt": int, "completion": int, "cached": int, "total": int, }, "energy_kwh": float | None, "co2_grams": float | None, "latency_s": float, } """ headers = { "api-key": api_key, "Content-Type": "application/json", } payload = { "model": model, "messages": [{"role": "user", "content": prompt}], "max_completion_tokens": max_completion_tokens, "temperature": temperature, "top_p": top_p, }
start = time.perf_counter() try: resp = requests.post(endpoint_url, headers=headers, json=payload, timeout=timeout) resp.raise_for_status() data = resp.json() except requests.exceptions.HTTPError as e: logger.error(f"LLM API call failed: {e} — {resp.text}") raise HTTPException(status_code=503, detail=f"LLM service unavailable: {str(e)}") except (requests.exceptions.JSONDecodeError, ValueError): logger.error( f"LLM API returned non-JSON response (status {resp.status_code}): {repr(resp.text)}" ) raise HTTPException(status_code=502, detail="LLM service returned an invalid response") latency_s = time.perf_counter() - start
try: content = data["choices"][0]["message"]["content"].strip() except (KeyError, IndexError) as e: logger.error(f"Unexpected LLM response format: {e} — body: {data!r}") raise HTTPException(status_code=502, detail="Unexpected response from LLM service")
usage = data.get("usage") or {} prompt_tokens = usage.get("prompt_tokens", 0) completion_tokens = usage.get("completion_tokens", 0) total_tokens = usage.get("total_tokens", prompt_tokens + completion_tokens)
prompt_details = usage.get("prompt_tokens_details") or {} cached_tokens = prompt_details.get("cached_tokens", usage.get("cached_tokens", 0))
energy_kwh: Optional[float] = None co2_grams: Optional[float] = None if _ECOLOGITS_AVAILABLE: try: impacts = llm_impacts( provider=provider, model_name=model, output_token_count=completion_tokens, request_latency=latency_s, ) if impacts is not None: energy_kwh = float(impacts.energy.value) # ecologits returns gwp in kgCO2eq; convert to grams co2_grams = float(impacts.gwp.value) * 1000.0 except Exception as e: logger.warning(f"ecologits impact calc failed for model={model}: {e}")
return { "content": content, "tokens": { "prompt": prompt_tokens, "completion": completion_tokens, "cached": cached_tokens, "total": total_tokens, }, "energy_kwh": energy_kwh, "co2_grams": co2_grams, "latency_s": latency_s, }11 unmodified lines12131411 unmodified linespython-multipart==0.0.9pydantic==2.9.0requests>=2.31.011 unmodified lines1213141511 unmodified linespython-multipart==0.0.9pydantic==2.9.0requests>=2.31.0ecologits>=0.5.0
app.py+31-45
llm.pyAdded+133
requirements.txt+1