Ilyas KHIAT commited on
Commit
e516684
1 Parent(s): b8471e7

ecologits codecarbon

Browse files
Files changed (4) hide show
  1. app.py +27 -6
  2. chat_with_pps.py +62 -4
  3. emissions.csv +0 -0
  4. partie_prenante_carte.py +16 -1
app.py CHANGED
@@ -40,6 +40,15 @@ def main():
40
  tracker = EmissionsTracker()
41
  tracker.start()
42
  st.session_state["emission"] = tracker
 
 
 
 
 
 
 
 
 
43
 
44
  st.sidebar.title("OPEN DATA & IA au service de la RSE")
45
  section_principale = st.sidebar.radio(
@@ -137,17 +146,29 @@ def main():
137
  """,
138
  unsafe_allow_html=True
139
  )
140
- st.sidebar.markdown(f"<div style='text-align: center; margin-bottom: 10px;'><b>{st.session_state['emission'].stop()*1000:.3f}</b> g eq. CO2</div>", unsafe_allow_html=True)
141
- st.sidebar.markdown("(avec les outils code carbon, Ecologits)")
 
 
 
 
 
 
 
 
 
 
 
 
142
  c1,c2,c3 = st.sidebar.columns([1,1,1])
143
 
144
  c2.write("---")
145
 
146
- st.sidebar.markdown("**Votre équivalence:**")
147
  col1,col2,col3 = st.sidebar.columns([1,1,1])
148
- display_comparaison(col1,st.session_state['emission'].stop(),dict_comparaison_1kgCO2["eau en litre"][0]*1000,dict_comparaison_1kgCO2["eau en litre"][1],"ml")
149
- display_comparaison(col2,st.session_state['emission'].stop(),dict_comparaison_1kgCO2["tgv en km"][0],dict_comparaison_1kgCO2["tgv en km"][1],"km")
150
- display_comparaison(col3,st.session_state['emission'].stop(),dict_comparaison_1kgCO2["voiture en km"][0]*1000,dict_comparaison_1kgCO2["voiture en km"][1],"m")
151
  st.sidebar.markdown("\n")
152
  st.sidebar.markdown(
153
  f"""
 
40
  tracker = EmissionsTracker()
41
  tracker.start()
42
  st.session_state["emission"] = tracker
43
+
44
+ if "partial_emissions" not in st.session_state:
45
+ st.session_state["partial_emissions"] = {
46
+ "Scrapping": {"cc": 0, "el": 0}, #cc for codecarbon , el for ecologits
47
+ "extraction_pp": {"cc": 0, "el": 0},
48
+ "cartographie": {"cc": 0, "el": 0},
49
+ "chatbot": {"cc": 0, "el": 0},
50
+ "download_rapport": {"cc": 0, "el": 0},
51
+ }
52
 
53
  st.sidebar.title("OPEN DATA & IA au service de la RSE")
54
  section_principale = st.sidebar.radio(
 
146
  """,
147
  unsafe_allow_html=True
148
  )
149
+
150
+ serveur_emission = st.session_state['emission'].stop()
151
+ emission_api = sum([value["el"] for value in st.session_state["partial_emissions"].values()])
152
+
153
+ total_emission = serveur_emission + emission_api
154
+
155
+ pourcentage_api = emission_api / total_emission
156
+ pourcentage_serveur = serveur_emission / total_emission
157
+
158
+ st.sidebar.markdown(f"<div style='text-align: center; margin-bottom: 10px;'><b>{total_emission*1000:.3f}</b> g eq. CO2</div>", unsafe_allow_html=True)
159
+ st.sidebar.markdown("Dont :")
160
+ st.sidebar.markdown(f"- Empreinte serveur (via CodeCarbon) : **{serveur_emission*1000:.3f}** g eq. CO2 ({pourcentage_serveur:.2%})")
161
+ st.sidebar.markdown(f"- Empreinte serveur (via EcoLogits) : **{emission_api*1000:.3f}** g eq. CO2 ({pourcentage_api:.2%})")
162
+ # st.sidebar.markdown("(avec l'outil CodeCarbon)")
163
  c1,c2,c3 = st.sidebar.columns([1,1,1])
164
 
165
  c2.write("---")
166
 
167
+ st.sidebar.markdown("**Votre équivalence**")
168
  col1,col2,col3 = st.sidebar.columns([1,1,1])
169
+ display_comparaison(col1,total_emission,dict_comparaison_1kgCO2["eau en litre"][0]*1000,dict_comparaison_1kgCO2["eau en litre"][1],"ml")
170
+ display_comparaison(col2,total_emission,dict_comparaison_1kgCO2["tgv en km"][0],dict_comparaison_1kgCO2["tgv en km"][1],"km")
171
+ display_comparaison(col3,total_emission,dict_comparaison_1kgCO2["voiture en km"][0]*1000,dict_comparaison_1kgCO2["voiture en km"][1],"m")
172
  st.sidebar.markdown("\n")
173
  st.sidebar.markdown(
174
  f"""
chat_with_pps.py CHANGED
@@ -13,6 +13,9 @@ import random
13
  import pandas as pd
14
  from codecarbon import EmissionsTracker
15
 
 
 
 
16
 
17
 
18
 
@@ -105,6 +108,58 @@ def get_response(user_query, chat_history, context,llm=None,history_limit=5,stre
105
  "user_question": user_query,
106
  })
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  def display_chart():
109
  if "pp_grouped" not in st.session_state or st.session_state['pp_grouped'] is None or len(st.session_state['pp_grouped']) == 0:
110
  st.warning("Aucune partie prenante n'a été définie")
@@ -315,10 +370,10 @@ def display_chat():
315
  st.markdown(f"**{st.session_state.model}**")
316
  if st.session_state.model not in st.session_state.used_models:
317
  st.session_state.used_models.append(st.session_state.model)
318
- response = st.write_stream(get_response(last_message.content, st.session_state.chat_history,format_context(st.session_state['pp_grouped'],st.session_state['Nom de la marque']),st.session_state.model))
319
  st.session_state.chat_history.append(AIMessage(content=response))
320
  with st.spinner("Proposition de prompts..."):
321
- propositions_prompts = get_response(generated_prompt_question, st.session_state.chat_history,format_context(st.session_state['pp_grouped'],st.session_state['Nom de la marque']),st.session_state.model,history_limit=1,stream=False)
322
  extract_format_prompts_from_response(propositions_prompts)
323
 
324
 
@@ -343,10 +398,13 @@ def display_chat():
343
  format_pp_add_viz(partie_prenante)
344
  disp_carto_in_chat()
345
  else:
346
- response = st.write_stream(get_response(user_query, st.session_state.chat_history,format_context(st.session_state['pp_grouped'],st.session_state['Nom de la marque']),st.session_state.model))
 
 
347
  st.session_state.chat_history.append(AIMessage(content=response))
 
348
  with st.spinner("Proposition de prompts..."):
349
- propositions_prompts = get_response(generated_prompt_question, st.session_state.chat_history,format_context(st.session_state['pp_grouped'],st.session_state['Nom de la marque']),st.session_state.model,history_limit=1,stream=False)
350
  extract_format_prompts_from_response(propositions_prompts)
351
 
352
 
 
13
  import pandas as pd
14
  from codecarbon import EmissionsTracker
15
 
16
+ from ecologits.tracers.utils import compute_llm_impacts
17
+ import time
18
+ import itertools
19
 
20
 
21
 
 
108
  "user_question": user_query,
109
  })
110
 
111
+ def get_response_with_impact(user_query, chat_history, context,llm=None,history_limit=5,stream=True):
112
+ model_vs_provider = {
113
+ "Mistral (FR)": ["mistral-large-latest","mistralai"],
114
+ "GPT-4o": ["gpt-4o","openai"]
115
+ }
116
+
117
+ start = time.perf_counter()
118
+ response_generator = get_response(user_query, chat_history, context,llm,history_limit,stream)
119
+ request_latency_invoke = time.perf_counter() - start
120
+ wrapped_response_generator, token_count_generator = itertools.tee(response_generator)
121
+
122
+ token_count = 0
123
+
124
+
125
+ # Stream the response while counting tokens
126
+ if stream:
127
+ try:
128
+ final_response = st.write_stream(wrapped_response_generator)
129
+
130
+ finally:
131
+ token_count = len(list(token_count_generator))
132
+ else:
133
+ final_response = response_generator
134
+ token_count = len(list(token_count_generator))
135
+
136
+ request_latency_stream = time.perf_counter() - start
137
+
138
+ nbre_out_tokens = token_count
139
+ model_name = model_vs_provider[st.session_state.model][0]
140
+ model_provider = model_vs_provider[st.session_state.model][1]
141
+
142
+ if not stream:
143
+ request_latency = request_latency_invoke
144
+ else:
145
+ request_latency = request_latency_stream
146
+
147
+ impact = compute_llm_impacts(
148
+ provider=model_provider,
149
+ model_name=model_name,
150
+ output_token_count=nbre_out_tokens,
151
+ request_latency=request_latency,
152
+ )
153
+
154
+ print(f"Request latency: {request_latency:.3f} s")
155
+ print(f"Output token count: {nbre_out_tokens}")
156
+ print(f"Impact: {impact.gwp.value} {impact.gwp.unit}")
157
+
158
+ st.session_state["partial_emissions"]["chatbot"]["el"] += impact.gwp.value
159
+
160
+ return final_response
161
+
162
+
163
  def display_chart():
164
  if "pp_grouped" not in st.session_state or st.session_state['pp_grouped'] is None or len(st.session_state['pp_grouped']) == 0:
165
  st.warning("Aucune partie prenante n'a été définie")
 
370
  st.markdown(f"**{st.session_state.model}**")
371
  if st.session_state.model not in st.session_state.used_models:
372
  st.session_state.used_models.append(st.session_state.model)
373
+ response = get_response_with_impact(last_message.content, st.session_state.chat_history,format_context(st.session_state['pp_grouped'],st.session_state['Nom de la marque']),st.session_state.model)
374
  st.session_state.chat_history.append(AIMessage(content=response))
375
  with st.spinner("Proposition de prompts..."):
376
+ propositions_prompts = get_response_with_impact(generated_prompt_question, st.session_state.chat_history,format_context(st.session_state['pp_grouped'],st.session_state['Nom de la marque']),st.session_state.model,history_limit=1,stream=False)
377
  extract_format_prompts_from_response(propositions_prompts)
378
 
379
 
 
398
  format_pp_add_viz(partie_prenante)
399
  disp_carto_in_chat()
400
  else:
401
+ #response = st.write_stream(get_response(user_query, st.session_state.chat_history,format_context(st.session_state['pp_grouped'],st.session_state['Nom de la marque']),st.session_state.model))
402
+ response = get_response_with_impact(user_query, st.session_state.chat_history,format_context(st.session_state['pp_grouped'],st.session_state['Nom de la marque']),st.session_state.model)
403
+
404
  st.session_state.chat_history.append(AIMessage(content=response))
405
+
406
  with st.spinner("Proposition de prompts..."):
407
+ propositions_prompts = get_response_with_impact(generated_prompt_question, st.session_state.chat_history,format_context(st.session_state['pp_grouped'],st.session_state['Nom de la marque']),st.session_state.model,history_limit=1,stream=False)
408
  extract_format_prompts_from_response(propositions_prompts)
409
 
410
 
emissions.csv CHANGED
The diff for this file is too large to render. See raw diff
 
partie_prenante_carte.py CHANGED
@@ -3,6 +3,7 @@ import pandas as pd
3
  import numpy as np
4
  import re
5
  import random
 
6
 
7
  import streamlit as st
8
  from dotenv import load_dotenv
@@ -17,7 +18,7 @@ from langchain_core.prompts.prompt import PromptTemplate
17
  import os
18
  from high_chart import test_chart
19
  from chat_with_pps import get_response
20
-
21
  from codecarbon import EmissionsTracker
22
 
23
  load_dotenv()
@@ -184,14 +185,28 @@ def extract_pp(docs,input_variables):
184
 
185
  question = fill_promptQ_template(input_variables, template_extraction_PP)
186
 
 
187
  response = chain.invoke(question)
188
 
 
189
  # version plus poussée a considérer
190
  # each item in the list is a list with the name of the brand and the similarity percentage
191
  # partie_prenante = text_to_list(response.content)
192
  if "ne sais pas" in response.content:
193
  return "444"
194
 
 
 
 
 
 
 
 
 
 
 
 
 
195
  #version simple
196
  partie_prenante = response.content.replace("- ","").split('\n')
197
  partie_prenante = [item.strip() for item in partie_prenante]
 
3
  import numpy as np
4
  import re
5
  import random
6
+ import time
7
 
8
  import streamlit as st
9
  from dotenv import load_dotenv
 
18
  import os
19
  from high_chart import test_chart
20
  from chat_with_pps import get_response
21
+ from ecologits.tracers.utils import compute_llm_impacts
22
  from codecarbon import EmissionsTracker
23
 
24
  load_dotenv()
 
185
 
186
  question = fill_promptQ_template(input_variables, template_extraction_PP)
187
 
188
+ start = time.perf_counter()
189
  response = chain.invoke(question)
190
 
191
+ response_latency = time.perf_counter() - start
192
  # version plus poussée a considérer
193
  # each item in the list is a list with the name of the brand and the similarity percentage
194
  # partie_prenante = text_to_list(response.content)
195
  if "ne sais pas" in response.content:
196
  return "444"
197
 
198
+ #calculate impact
199
+ nbre_out_tokens = response.response_metadata["token_usage"]["completion_tokens"]
200
+ provider = "openai"
201
+ model = "gpt-4o"
202
+ impact = compute_llm_impacts(
203
+ provider=provider,
204
+ model_name=model,
205
+ output_token_count=nbre_out_tokens,
206
+ request_latency=response_latency,
207
+ )
208
+
209
+ st.session_state["partial_emissions"]["extraction_pp"]["el"] += impact.gwp.value
210
  #version simple
211
  partie_prenante = response.content.replace("- ","").split('\n')
212
  partie_prenante = [item.strip() for item in partie_prenante]