Spaces:
Sleeping
Sleeping
Commit ·
bef40c6
1
Parent(s): 6698302
First Implementation with LangChain
Browse files- .gitignore +4 -0
- .vscode/settings.json +3 -0
- README.md +47 -1
- agents/assistant.py +39 -0
- main.py +8 -0
- requirements.txt +10 -0
- tools/calc.py +9 -0
.gitignore
CHANGED
|
@@ -132,6 +132,7 @@ celerybeat.pid
|
|
| 132 |
.venv
|
| 133 |
env/
|
| 134 |
venv/
|
|
|
|
| 135 |
ENV/
|
| 136 |
env.bak/
|
| 137 |
venv.bak/
|
|
@@ -172,3 +173,6 @@ cython_debug/
|
|
| 172 |
|
| 173 |
# PyPI configuration file
|
| 174 |
.pypirc
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
.venv
|
| 133 |
env/
|
| 134 |
venv/
|
| 135 |
+
jarvis-env/
|
| 136 |
ENV/
|
| 137 |
env.bak/
|
| 138 |
venv.bak/
|
|
|
|
| 173 |
|
| 174 |
# PyPI configuration file
|
| 175 |
.pypirc
|
| 176 |
+
|
| 177 |
+
# Others
|
| 178 |
+
*.DS_Store
|
.vscode/settings.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"python.pythonPath": "jarvis-env/Scripts/python.exe"
|
| 3 |
+
}
|
README.md
CHANGED
|
@@ -1,2 +1,48 @@
|
|
| 1 |
-
#
|
|
|
|
|
|
|
| 2 |
Hello, sir. How can I assist you today?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Proyecto Jarvis MVP (IA Personal con Langchain)
|
| 2 |
+
|
| 3 |
+
Este es un MVP de una IA estilo "Jarvis" usando Langchain.
|
| 4 |
Hello, sir. How can I assist you today?
|
| 5 |
+
|
| 6 |
+
## Requisitos
|
| 7 |
+
- Python 3.10+
|
| 8 |
+
- Clave API de OpenAI (opcional)
|
| 9 |
+
|
| 10 |
+
## Instalación
|
| 11 |
+
```bash
|
| 12 |
+
python -m venv jarvis-env
|
| 13 |
+
source jarvis-env/bin/activate # o .\jarvis-env\Scripts\activate en Windows
|
| 14 |
+
pip install -r requirements.txt
|
| 15 |
+
```
|
| 16 |
+
|
| 17 |
+
## Configuración
|
| 18 |
+
1. Copia `.env.example` como `.env`
|
| 19 |
+
2. Pon tu clave de OpenAI:
|
| 20 |
+
```
|
| 21 |
+
OPENAI_API_KEY=sk-...
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
## Uso
|
| 25 |
+
```bash
|
| 26 |
+
python main.py
|
| 27 |
+
```
|
| 28 |
+
|
| 29 |
+
## Estructura
|
| 30 |
+
```
|
| 31 |
+
project/
|
| 32 |
+
├── agents/
|
| 33 |
+
│ └── assistant.py
|
| 34 |
+
├── tools/
|
| 35 |
+
│ └── calc.py
|
| 36 |
+
├── main.py
|
| 37 |
+
├── .env.example
|
| 38 |
+
├── requirements.txt
|
| 39 |
+
├── .gitignore
|
| 40 |
+
└── README.md
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
## Roadmap
|
| 44 |
+
- [ ] Soporte para LLM local con `llama-cpp` o `transformers`
|
| 45 |
+
- [ ] Tool para leer PDFs o archivos
|
| 46 |
+
- [ ] Integración con micrófono y voz
|
| 47 |
+
- [ ] Resumen de audios de WhatsApp
|
| 48 |
+
- [ ] Versión para Raspberry Pi
|
agents/assistant.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from langchain.agents import initialize_agent, AgentType, Tool
|
| 3 |
+
from langchain_ollama import ChatOllama
|
| 4 |
+
from tools.calc import calculate
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
llm = ChatOllama(model="mistral")
|
| 10 |
+
|
| 11 |
+
prefix = """Eres un asistente inteligente llamado Jarvis, con la actitud propia de un mayordomo.
|
| 12 |
+
Para cada pregunta, piensa paso a paso y usa las herramientas disponibles.
|
| 13 |
+
Adapta el idioma de tu respuesta a aquel en el que te han hablado en la última interacción (por defecto, español).
|
| 14 |
+
Puede referirse al usuario por su nombre si es necesario. En caso de no saberlo, por defecto entenderá que se llama "Javi".
|
| 15 |
+
Cuando vayas a usar una herramienta, responde exactamente en este formato:
|
| 16 |
+
Action: <tool_name>(input: "<your input>")
|
| 17 |
+
Y cuando termines, responde con:
|
| 18 |
+
Final Answer: <your answer>
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
tools = [
|
| 22 |
+
Tool.from_function(
|
| 23 |
+
func=calculate,
|
| 24 |
+
name="Calculator",
|
| 25 |
+
description="Realiza cálculos matemáticos simples dados en lenguaje natural o expresiones."
|
| 26 |
+
)
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
agent = initialize_agent(
|
| 30 |
+
tools=tools,
|
| 31 |
+
llm=llm,
|
| 32 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 33 |
+
verbose=True,
|
| 34 |
+
handle_parsing_errors=True,
|
| 35 |
+
agent_kwargs={"prefix": prefix}
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
def ask_jarvis(question: str):
|
| 39 |
+
return agent.invoke({"input": question})['output']
|
main.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from agents.assistant import ask_jarvis
|
| 2 |
+
|
| 3 |
+
while True:
|
| 4 |
+
query = input("Hola, señor. ¿Con qué puedo ayudarle hoy? ")
|
| 5 |
+
if query.lower() in ["salir", "exit", "quit"]:
|
| 6 |
+
break
|
| 7 |
+
response = ask_jarvis(query)
|
| 8 |
+
print("Jarvis:", response)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain>=0.1.0
|
| 2 |
+
openai>=1.0.0
|
| 3 |
+
tiktoken
|
| 4 |
+
python-dotenv
|
| 5 |
+
duckduckgo-search
|
| 6 |
+
llama-cpp-python
|
| 7 |
+
langchain-community
|
| 8 |
+
langchain-openai
|
| 9 |
+
langchain-anthropic
|
| 10 |
+
langgraph
|
tools/calc.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.tools import tool
|
| 2 |
+
|
| 3 |
+
@tool
|
| 4 |
+
def calculate(expression: str) -> str:
|
| 5 |
+
"""Evalúa una expresión matemática en Python."""
|
| 6 |
+
try:
|
| 7 |
+
return str(eval(expression))
|
| 8 |
+
except Exception as e:
|
| 9 |
+
return f"Error: {e}"
|