Spaces:
Runtime error
Runtime error
llllllllll
Browse files- my_tools.py +63 -14
my_tools.py
CHANGED
|
@@ -1,6 +1,14 @@
|
|
| 1 |
-
import os, math, wikipedia
|
| 2 |
import google.generativeai as genai
|
| 3 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from llama_index.core.agent import ReActAgent
|
| 5 |
from llama_index.core.tools import FunctionTool
|
| 6 |
from duckduckgo_search import DDGS
|
|
@@ -9,30 +17,71 @@ from duckduckgo_search import DDGS
|
|
| 9 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY", ""))
|
| 10 |
|
| 11 |
class GeminiLLM(LLM):
|
| 12 |
-
"""
|
|
|
|
| 13 |
def __init__(self, model: str = "models/gemini-1.5-flash-latest"):
|
| 14 |
-
self.
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
prompt = "\n".join(
|
| 18 |
-
[("User: " if m.role == "user" else "Assistant: ") + m.content
|
| 19 |
-
for m in messages]
|
| 20 |
) + "\nAssistant:"
|
| 21 |
-
resp = self.
|
| 22 |
return ChatResponse(
|
| 23 |
message=ChatMessage(role="assistant", content=resp.text),
|
| 24 |
raw=resp
|
| 25 |
)
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# ── Instanciar el LLM ──
|
| 28 |
llm = GeminiLLM()
|
| 29 |
|
| 30 |
# ── Herramientas auxiliares ──
|
| 31 |
def buscar_web(query: str) -> str:
|
| 32 |
with DDGS() as ddgs:
|
| 33 |
-
results = list(
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
if results:
|
| 37 |
return "\n".join([f"{r['title']}: {r['body']}" for r in results])
|
| 38 |
return "No se encontraron resultados."
|
|
@@ -40,7 +89,7 @@ def buscar_web(query: str) -> str:
|
|
| 40 |
search_tool = FunctionTool.from_defaults(
|
| 41 |
fn=buscar_web,
|
| 42 |
name="web_search",
|
| 43 |
-
description="Busca en la web utilizando DuckDuckGo."
|
| 44 |
)
|
| 45 |
|
| 46 |
def get_wikipedia_summary(query: str) -> str:
|
|
@@ -52,7 +101,7 @@ def get_wikipedia_summary(query: str) -> str:
|
|
| 52 |
wikipedia_tool = FunctionTool.from_defaults(
|
| 53 |
fn=get_wikipedia_summary,
|
| 54 |
name="wikipedia_lookup",
|
| 55 |
-
description="Obtiene un resumen breve de un tema en Wikipedia."
|
| 56 |
)
|
| 57 |
|
| 58 |
def calcular_expresion(expr: str) -> str:
|
|
@@ -65,7 +114,7 @@ def calcular_expresion(expr: str) -> str:
|
|
| 65 |
calculator_tool = FunctionTool.from_defaults(
|
| 66 |
fn=calcular_expresion,
|
| 67 |
name="calculadora",
|
| 68 |
-
description="Resuelve expresiones matemáticas (trigonometría, log, potencias, etc.)."
|
| 69 |
)
|
| 70 |
|
| 71 |
# ── Agente ReAct con herramientas ──
|
|
|
|
| 1 |
+
import os, math, wikipedia, asyncio
|
| 2 |
import google.generativeai as genai
|
| 3 |
+
from typing import List, Generator
|
| 4 |
+
|
| 5 |
+
from llama_index.core.llms import (
|
| 6 |
+
LLM,
|
| 7 |
+
ChatMessage,
|
| 8 |
+
ChatResponse,
|
| 9 |
+
CompletionResponse,
|
| 10 |
+
CompletionResponseGen,
|
| 11 |
+
)
|
| 12 |
from llama_index.core.agent import ReActAgent
|
| 13 |
from llama_index.core.tools import FunctionTool
|
| 14 |
from duckduckgo_search import DDGS
|
|
|
|
| 17 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY", ""))
|
| 18 |
|
| 19 |
class GeminiLLM(LLM):
|
| 20 |
+
"""Implementación mínima de LLM que satisface todos los métodos abstractos."""
|
| 21 |
+
|
| 22 |
def __init__(self, model: str = "models/gemini-1.5-flash-latest"):
|
| 23 |
+
self._model = genai.GenerativeModel(model)
|
| 24 |
+
self._model_name = model
|
| 25 |
|
| 26 |
+
# ---------- metadata ----------
|
| 27 |
+
@property
|
| 28 |
+
def metadata(self):
|
| 29 |
+
return {
|
| 30 |
+
"context_window": 32768,
|
| 31 |
+
"num_output": 2048,
|
| 32 |
+
"is_chat_model": True,
|
| 33 |
+
"model_name": self._model_name,
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# ---------- chat ----------
|
| 37 |
+
def chat(self, messages: List[ChatMessage], **kwargs) -> ChatResponse:
|
| 38 |
prompt = "\n".join(
|
| 39 |
+
[("User: " if m.role == "user" else "Assistant: ") + m.content for m in messages]
|
|
|
|
| 40 |
) + "\nAssistant:"
|
| 41 |
+
resp = self._model.generate_content(prompt)
|
| 42 |
return ChatResponse(
|
| 43 |
message=ChatMessage(role="assistant", content=resp.text),
|
| 44 |
raw=resp
|
| 45 |
)
|
| 46 |
|
| 47 |
+
def stream_chat(self, messages: List[ChatMessage], **kwargs) -> Generator[ChatResponse, None, None]:
|
| 48 |
+
yield self.chat(messages, **kwargs)
|
| 49 |
+
|
| 50 |
+
async def achat(self, messages: List[ChatMessage], **kwargs) -> ChatResponse:
|
| 51 |
+
return self.chat(messages, **kwargs)
|
| 52 |
+
|
| 53 |
+
async def astream_chat(self, messages: List[ChatMessage], **kwargs):
|
| 54 |
+
yield self.chat(messages, **kwargs)
|
| 55 |
+
|
| 56 |
+
# ---------- complete ----------
|
| 57 |
+
def complete(self, prompt: str, **kwargs) -> CompletionResponse:
|
| 58 |
+
resp = self._model.generate_content(prompt)
|
| 59 |
+
return CompletionResponse(text=resp.text, raw=resp)
|
| 60 |
+
|
| 61 |
+
def stream_complete(self, prompt: str, **kwargs) -> CompletionResponseGen:
|
| 62 |
+
yield self.complete(prompt, **kwargs)
|
| 63 |
+
|
| 64 |
+
async def acomplete(self, prompt: str, **kwargs) -> CompletionResponse:
|
| 65 |
+
return self.complete(prompt, **kwargs)
|
| 66 |
+
|
| 67 |
+
async def astream_complete(self, prompt: str, **kwargs):
|
| 68 |
+
yield self.complete(prompt, **kwargs)
|
| 69 |
+
|
| 70 |
# ── Instanciar el LLM ──
|
| 71 |
llm = GeminiLLM()
|
| 72 |
|
| 73 |
# ── Herramientas auxiliares ──
|
| 74 |
def buscar_web(query: str) -> str:
|
| 75 |
with DDGS() as ddgs:
|
| 76 |
+
results = list(
|
| 77 |
+
ddgs.text(
|
| 78 |
+
query,
|
| 79 |
+
region="es-es",
|
| 80 |
+
safesearch="moderate",
|
| 81 |
+
timelimit="y",
|
| 82 |
+
max_results=3,
|
| 83 |
+
)
|
| 84 |
+
)
|
| 85 |
if results:
|
| 86 |
return "\n".join([f"{r['title']}: {r['body']}" for r in results])
|
| 87 |
return "No se encontraron resultados."
|
|
|
|
| 89 |
search_tool = FunctionTool.from_defaults(
|
| 90 |
fn=buscar_web,
|
| 91 |
name="web_search",
|
| 92 |
+
description="Busca en la web utilizando DuckDuckGo.",
|
| 93 |
)
|
| 94 |
|
| 95 |
def get_wikipedia_summary(query: str) -> str:
|
|
|
|
| 101 |
wikipedia_tool = FunctionTool.from_defaults(
|
| 102 |
fn=get_wikipedia_summary,
|
| 103 |
name="wikipedia_lookup",
|
| 104 |
+
description="Obtiene un resumen breve de un tema en Wikipedia.",
|
| 105 |
)
|
| 106 |
|
| 107 |
def calcular_expresion(expr: str) -> str:
|
|
|
|
| 114 |
calculator_tool = FunctionTool.from_defaults(
|
| 115 |
fn=calcular_expresion,
|
| 116 |
name="calculadora",
|
| 117 |
+
description="Resuelve expresiones matemáticas (trigonometría, log, potencias, etc.).",
|
| 118 |
)
|
| 119 |
|
| 120 |
# ── Agente ReAct con herramientas ──
|