Instructions to use amogaddy/GenerAI with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use amogaddy/GenerAI with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="amogaddy/GenerAI")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("amogaddy/GenerAI", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use amogaddy/GenerAI with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "amogaddy/GenerAI" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "amogaddy/GenerAI", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/amogaddy/GenerAI
- SGLang
How to use amogaddy/GenerAI with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "amogaddy/GenerAI" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "amogaddy/GenerAI", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "amogaddy/GenerAI" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "amogaddy/GenerAI", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use amogaddy/GenerAI with Docker Model Runner:
docker model run hf.co/amogaddy/GenerAI
Sync aggiornamento
Browse files
brain.py
CHANGED
|
@@ -119,15 +119,21 @@ class Brain:
|
|
| 119 |
|
| 120 |
log.info("Brain pronto. ModalitΓ : %s", "LLM+KB" if self._hf else "KB+Web")
|
| 121 |
|
| 122 |
-
async def ask(self, question: str) -> tuple[str, str]:
|
| 123 |
"""
|
| 124 |
Returns (answer, status).
|
| 125 |
-
|
|
|
|
| 126 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
self._last_doc_id = None
|
| 128 |
log.info("Domanda: %r", question)
|
| 129 |
|
| 130 |
# ββ 1. Ricerca KB locale βββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 131 |
try:
|
| 132 |
results = _rerank(self.kb.search(question, n_results=12), question)
|
| 133 |
except Exception as e:
|
|
@@ -141,27 +147,31 @@ class Brain:
|
|
| 141 |
best = results[0]
|
| 142 |
best_context = best["answer"]
|
| 143 |
best_source = best["metadata"].get("source", "")
|
|
|
|
| 144 |
log.info("Match KB locale (score=%.3f)", results[0]["score"])
|
| 145 |
|
| 146 |
-
# Se c'Γ¨ un LLM, usa il contesto KB per generare una risposta migliore
|
| 147 |
if self._hf:
|
|
|
|
| 148 |
return await self._llm_answer(question, best_context, best_source, status="llm")
|
| 149 |
|
| 150 |
answer = _extract_sentences(best_context, question)
|
| 151 |
if best_source and best_source != "grammatica_italiana":
|
| 152 |
answer += f"\n\n*Fonte: {best_source}*"
|
|
|
|
| 153 |
return answer, "local"
|
| 154 |
|
| 155 |
# ββ 2. Ricerca web βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 156 |
-
|
| 157 |
try:
|
| 158 |
web_results = await asyncio.to_thread(search_and_extract, question)
|
| 159 |
except Exception as e:
|
| 160 |
err = GenerAIError(ErrorCode.BRAIN_ASK_FAILED, f"Ricerca web fallita: {fmt_exc(e)}", cause=e)
|
| 161 |
err.log(log)
|
|
|
|
| 162 |
return err.user_message(), "error"
|
| 163 |
|
| 164 |
if not web_results:
|
|
|
|
| 165 |
log.warning("Nessun risultato web per: %r", question)
|
| 166 |
return (
|
| 167 |
"Non ho trovato informazioni su questo argomento nel mio database nΓ© sul web. "
|
|
@@ -169,18 +179,22 @@ class Brain:
|
|
| 169 |
"unknown",
|
| 170 |
)
|
| 171 |
|
|
|
|
| 172 |
combined = "\n\n---\n\n".join(f"[{r['title']}]\n{r['text']}" for r in web_results)
|
| 173 |
sources = ", ".join(r["url"] for r in web_results if r.get("url"))
|
| 174 |
|
| 175 |
-
# ββ 3. Genera risposta
|
| 176 |
if self._hf:
|
|
|
|
| 177 |
answer, status = await self._llm_answer(question, combined, sources, status="llm")
|
| 178 |
else:
|
|
|
|
| 179 |
answer = _extract_sentences(combined, question)
|
| 180 |
if sources:
|
| 181 |
answer += f"\n\n*Fonte: {sources}*"
|
| 182 |
status = "searched"
|
| 183 |
|
|
|
|
| 184 |
return answer, status
|
| 185 |
|
| 186 |
async def _llm_answer(
|
|
|
|
| 119 |
|
| 120 |
log.info("Brain pronto. ModalitΓ : %s", "LLM+KB" if self._hf else "KB+Web")
|
| 121 |
|
| 122 |
+
async def ask(self, question: str, on_status=None) -> tuple[str, str]:
|
| 123 |
"""
|
| 124 |
Returns (answer, status).
|
| 125 |
+
on_status: callable opzionale async(msg: str) per aggiornamenti in tempo reale.
|
| 126 |
+
status: "local" | "searched" | "unknown" | "error" | "llm"
|
| 127 |
"""
|
| 128 |
+
async def emit(msg: str):
|
| 129 |
+
if on_status:
|
| 130 |
+
await on_status(msg)
|
| 131 |
+
|
| 132 |
self._last_doc_id = None
|
| 133 |
log.info("Domanda: %r", question)
|
| 134 |
|
| 135 |
# ββ 1. Ricerca KB locale βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 136 |
+
await emit("π Cerco nella memoria locale...")
|
| 137 |
try:
|
| 138 |
results = _rerank(self.kb.search(question, n_results=12), question)
|
| 139 |
except Exception as e:
|
|
|
|
| 147 |
best = results[0]
|
| 148 |
best_context = best["answer"]
|
| 149 |
best_source = best["metadata"].get("source", "")
|
| 150 |
+
await emit(f"β
Trovato in memoria locale (score: {results[0]['score']:.2f})")
|
| 151 |
log.info("Match KB locale (score=%.3f)", results[0]["score"])
|
| 152 |
|
|
|
|
| 153 |
if self._hf:
|
| 154 |
+
await emit("π€ Genero risposta con LLM...")
|
| 155 |
return await self._llm_answer(question, best_context, best_source, status="llm")
|
| 156 |
|
| 157 |
answer = _extract_sentences(best_context, question)
|
| 158 |
if best_source and best_source != "grammatica_italiana":
|
| 159 |
answer += f"\n\n*Fonte: {best_source}*"
|
| 160 |
+
await emit("π¬ Risposta pronta!")
|
| 161 |
return answer, "local"
|
| 162 |
|
| 163 |
# ββ 2. Ricerca web βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 164 |
+
await emit("π Non trovato in memoria β ricerca sul web...")
|
| 165 |
try:
|
| 166 |
web_results = await asyncio.to_thread(search_and_extract, question)
|
| 167 |
except Exception as e:
|
| 168 |
err = GenerAIError(ErrorCode.BRAIN_ASK_FAILED, f"Ricerca web fallita: {fmt_exc(e)}", cause=e)
|
| 169 |
err.log(log)
|
| 170 |
+
await emit(f"β Errore ricerca web: {fmt_exc(e)}")
|
| 171 |
return err.user_message(), "error"
|
| 172 |
|
| 173 |
if not web_results:
|
| 174 |
+
await emit("β οΈ Nessun risultato trovato sul web")
|
| 175 |
log.warning("Nessun risultato web per: %r", question)
|
| 176 |
return (
|
| 177 |
"Non ho trovato informazioni su questo argomento nel mio database nΓ© sul web. "
|
|
|
|
| 179 |
"unknown",
|
| 180 |
)
|
| 181 |
|
| 182 |
+
await emit(f"π Trovati {len(web_results)} risultati β estraggo il testo...")
|
| 183 |
combined = "\n\n---\n\n".join(f"[{r['title']}]\n{r['text']}" for r in web_results)
|
| 184 |
sources = ", ".join(r["url"] for r in web_results if r.get("url"))
|
| 185 |
|
| 186 |
+
# ββ 3. Genera risposta βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 187 |
if self._hf:
|
| 188 |
+
await emit("π€ Genero risposta con LLM...")
|
| 189 |
answer, status = await self._llm_answer(question, combined, sources, status="llm")
|
| 190 |
else:
|
| 191 |
+
await emit("βοΈ Estraggo le frasi piΓΉ rilevanti...")
|
| 192 |
answer = _extract_sentences(combined, question)
|
| 193 |
if sources:
|
| 194 |
answer += f"\n\n*Fonte: {sources}*"
|
| 195 |
status = "searched"
|
| 196 |
|
| 197 |
+
await emit("π¬ Risposta pronta!")
|
| 198 |
return answer, status
|
| 199 |
|
| 200 |
async def _llm_answer(
|