Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +14 -28
src/streamlit_app.py
CHANGED
|
@@ -18,6 +18,12 @@ def get_headers(api_key: str) -> Dict[str, str]:
|
|
| 18 |
def pretty(obj: Any) -> str:
|
| 19 |
return json.dumps(obj, ensure_ascii=False, indent=2)
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def make_client(base_url: str, api_key: str) -> OpenAI:
|
| 22 |
return OpenAI(base_url=base_url, api_key=api_key)
|
| 23 |
|
|
@@ -38,22 +44,22 @@ def provider_defaults() -> Dict[str, Dict[str, str]]:
|
|
| 38 |
return {
|
| 39 |
"OpenAI": {
|
| 40 |
"base_url": "https://api.openai.com/v1",
|
| 41 |
-
"env_key":
|
| 42 |
"notes": "Supporte Responses API (web_search, image) + Chat/Embeddings/Models.",
|
| 43 |
},
|
| 44 |
"Groq": {
|
| 45 |
"base_url": "https://api.groq.com/openai/v1",
|
| 46 |
-
"env_key":
|
| 47 |
"notes": "OpenAI-compatible pour Chat/Models/Embeddings (selon offre). Pas de web_search OpenAI.",
|
| 48 |
},
|
| 49 |
"Ollama": {
|
| 50 |
"base_url": "https://ollama.com/v1",
|
| 51 |
-
"env_key":
|
| 52 |
"notes": "OpenAI-compatible local. Models/Chat ok selon config. Embeddings selon modèles dispo.",
|
| 53 |
},
|
| 54 |
"Albert (Etalab)": {
|
| 55 |
"base_url": "https://albert.api.etalab.gouv.fr/v1",
|
| 56 |
-
"env_key":
|
| 57 |
"notes": "OpenAI-compatible (selon endpoints activés). Pas de web_search OpenAI.",
|
| 58 |
},
|
| 59 |
}
|
|
@@ -100,11 +106,11 @@ def pick_default(ids: List[str], preferred: List[str]) -> int:
|
|
| 100 |
# Streamlit UI
|
| 101 |
# ----------------------------
|
| 102 |
st.set_page_config(page_title="LLM API Playground (pédagogique)", layout="wide")
|
| 103 |
-
st.title("Mini-app de requêtes API sur LLMs")
|
| 104 |
-
st.caption("Choisir un provider")
|
| 105 |
|
| 106 |
with st.sidebar:
|
| 107 |
-
st.header("Configuration")
|
| 108 |
defaults = provider_defaults()
|
| 109 |
provider_name = st.selectbox("Fournisseur", list(defaults.keys()), index=0)
|
| 110 |
|
|
@@ -264,10 +270,6 @@ with tabs[1]:
|
|
| 264 |
except Exception as e:
|
| 265 |
st.error(f"Erreur: {e}")
|
| 266 |
|
| 267 |
-
#with colB:
|
| 268 |
-
#st.write("À retenir")
|
| 269 |
-
#st.markdown("- Le modèle sélectionné vient de `/models` quand c’est possible.\n- Sinon, saisis l’ID à la main.")
|
| 270 |
-
|
| 271 |
# ----------------------------
|
| 272 |
# 3) Chat completion
|
| 273 |
# ----------------------------
|
|
@@ -317,10 +319,6 @@ with tabs[2]:
|
|
| 317 |
except Exception as e:
|
| 318 |
st.error(f"Erreur: {e}")
|
| 319 |
|
| 320 |
-
#with colB:
|
| 321 |
-
# st.write("À retenir")
|
| 322 |
-
# st.markdown("- Même endpoint, providers différents.\n- Le dropdown aide à éviter les IDs de modèles invalides.")
|
| 323 |
-
|
| 324 |
# ----------------------------
|
| 325 |
# 4) JSON extraction
|
| 326 |
# ----------------------------
|
|
@@ -373,10 +371,6 @@ with tabs[3]:
|
|
| 373 |
st.error(f"Erreur: {e}")
|
| 374 |
st.info("Certains providers/modèles ne supportent pas `response_format`.")
|
| 375 |
|
| 376 |
-
#with colB:
|
| 377 |
-
# st.write("À retenir")
|
| 378 |
-
# st.markdown("- Dropdown = modèle valide (quand `/models` répond).\n- `response_format` peut rester non supporté selon provider.")
|
| 379 |
-
|
| 380 |
# ----------------------------
|
| 381 |
# 5) OpenAI web_search (Responses API)
|
| 382 |
# ----------------------------
|
|
@@ -399,11 +393,7 @@ with tabs[4]:
|
|
| 399 |
prompt = st.text_area(
|
| 400 |
"Prompt",
|
| 401 |
value=(
|
| 402 |
-
"
|
| 403 |
-
"- titre : L'enfant et la rivière\n"
|
| 404 |
-
"- auteur : Henri Bosco\n"
|
| 405 |
-
"- date : 2015\n\n"
|
| 406 |
-
"Fais une recherche dans le catalogue CCFr et trouve le nombre de localisations Sudoc."
|
| 407 |
),
|
| 408 |
height=180,
|
| 409 |
)
|
|
@@ -476,9 +466,5 @@ with tabs[5]:
|
|
| 476 |
except Exception as e:
|
| 477 |
st.error(f"Erreur: {e}")
|
| 478 |
|
| 479 |
-
#with colB:
|
| 480 |
-
# st.write("À retenir")
|
| 481 |
-
# st.markdown("- Multimodal OpenAI.\n- Dropdown = modèle valide quand la liste est accessible.")
|
| 482 |
-
|
| 483 |
st.markdown("---")
|
| 484 |
st.caption("Astuce : si `/models` ne marche pas chez un provider, la fallback text_input permet quand même de tester.")
|
|
|
|
| 18 |
def pretty(obj: Any) -> str:
|
| 19 |
return json.dumps(obj, ensure_ascii=False, indent=2)
|
| 20 |
|
| 21 |
+
|
| 22 |
+
def safe_get_env(name: str, default: str = "") -> str:
|
| 23 |
+
v = os.getenv(name)
|
| 24 |
+
return v if v is not None else default
|
| 25 |
+
|
| 26 |
+
|
| 27 |
def make_client(base_url: str, api_key: str) -> OpenAI:
|
| 28 |
return OpenAI(base_url=base_url, api_key=api_key)
|
| 29 |
|
|
|
|
| 44 |
return {
|
| 45 |
"OpenAI": {
|
| 46 |
"base_url": "https://api.openai.com/v1",
|
| 47 |
+
"env_key": "OPENAI_API_KEY",
|
| 48 |
"notes": "Supporte Responses API (web_search, image) + Chat/Embeddings/Models.",
|
| 49 |
},
|
| 50 |
"Groq": {
|
| 51 |
"base_url": "https://api.groq.com/openai/v1",
|
| 52 |
+
"env_key": "GROQ_API_KEY",
|
| 53 |
"notes": "OpenAI-compatible pour Chat/Models/Embeddings (selon offre). Pas de web_search OpenAI.",
|
| 54 |
},
|
| 55 |
"Ollama": {
|
| 56 |
"base_url": "https://ollama.com/v1",
|
| 57 |
+
"env_key": "OLLAMA_API_KEY",
|
| 58 |
"notes": "OpenAI-compatible local. Models/Chat ok selon config. Embeddings selon modèles dispo.",
|
| 59 |
},
|
| 60 |
"Albert (Etalab)": {
|
| 61 |
"base_url": "https://albert.api.etalab.gouv.fr/v1",
|
| 62 |
+
"env_key": "ALBERT_API_KEY",
|
| 63 |
"notes": "OpenAI-compatible (selon endpoints activés). Pas de web_search OpenAI.",
|
| 64 |
},
|
| 65 |
}
|
|
|
|
| 106 |
# Streamlit UI
|
| 107 |
# ----------------------------
|
| 108 |
st.set_page_config(page_title="LLM API Playground (pédagogique)", layout="wide")
|
| 109 |
+
st.title("🧪 Mini-app de requêtes API sur LLMs")
|
| 110 |
+
st.caption("Choisir un provider → on charge `/models` → chaque onglet propose un menu déroulant des modèles.")
|
| 111 |
|
| 112 |
with st.sidebar:
|
| 113 |
+
st.header("⚙️ Configuration")
|
| 114 |
defaults = provider_defaults()
|
| 115 |
provider_name = st.selectbox("Fournisseur", list(defaults.keys()), index=0)
|
| 116 |
|
|
|
|
| 270 |
except Exception as e:
|
| 271 |
st.error(f"Erreur: {e}")
|
| 272 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
# ----------------------------
|
| 274 |
# 3) Chat completion
|
| 275 |
# ----------------------------
|
|
|
|
| 319 |
except Exception as e:
|
| 320 |
st.error(f"Erreur: {e}")
|
| 321 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
# ----------------------------
|
| 323 |
# 4) JSON extraction
|
| 324 |
# ----------------------------
|
|
|
|
| 371 |
st.error(f"Erreur: {e}")
|
| 372 |
st.info("Certains providers/modèles ne supportent pas `response_format`.")
|
| 373 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
# ----------------------------
|
| 375 |
# 5) OpenAI web_search (Responses API)
|
| 376 |
# ----------------------------
|
|
|
|
| 393 |
prompt = st.text_area(
|
| 394 |
"Prompt",
|
| 395 |
value=(
|
| 396 |
+
"Trouve le ppn de l'autorité personne Albert Camus dans le référentiel français Idref."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
),
|
| 398 |
height=180,
|
| 399 |
)
|
|
|
|
| 466 |
except Exception as e:
|
| 467 |
st.error(f"Erreur: {e}")
|
| 468 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
st.markdown("---")
|
| 470 |
st.caption("Astuce : si `/models` ne marche pas chez un provider, la fallback text_input permet quand même de tester.")
|