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