Skip to content

0Darkn/AI-bot

Repository files navigation

English

BOT AI COMPLETO PARA OPENSIMULATOR EM PYTHON

🧠 VISÃO FINAL DO BOT

O bot será capaz de:

  • ✅ Entrar no OpenSimulator como avatar
  • ✅ Andar, virar, deslocar-se
  • ✅ Saber onde está e que objetos o rodeiam
  • ✅ Ouvir e responder no chat
  • ✅ Usar LLM + RAG local
  • ✅ Mostrar legendas explicativas (fontes + raciocínio)

📁 ESTRUTURA FINAL DO PROJETO

opensim_ai_bot/
│
├─ lib/
│  └─ OpenMetaverse.dll
│
├─ bot/
│  ├─ client.py        # ligação + eventos
│  ├─ movement.py      # andar, virar
│  ├─ perception.py    # posição, objetos
│
├─ ai/
│  ├─ llm.py           # LLM
│  ├─ rag.py           # RAG local
│  └─ prompts.py
│
├─ knowledge/
│  ├─ docs/
│  │  ├─ opensim.txt
│  │  ├─ mundo.md
│  │  └─ objetos.md
│
├─ subtitles/
│  └─ explain.py       # legendas explicativas
│
├─ main.py
└─ requirements.txt

1️⃣ MOVIMENTO DO BOT (andar, virar)

📄 bot/movement.py

import time
from OpenMetaverse import Vector3

class BotMovement:
    def __init__(self, client):
        self.client = client

    def walk_forward(self, meters=2.0):
        pos = self.client.Self.SimPosition
        direction = self.client.Self.Movement.BodyRotation.GetForwardVector()

        new_pos = Vector3(
            pos.X + direction.X * meters,
            pos.Y + direction.Y * meters,
            pos.Z
        )

        self.client.Self.AutoPilot(new_pos)
        time.sleep(2)

    def turn_left(self):
        self.client.Self.Movement.TurnLeft(True)
        time.sleep(1)
        self.client.Self.Movement.TurnLeft(False)

    def turn_right(self):
        self.client.Self.Movement.TurnRight(True)
        time.sleep(1)
        self.client.Self.Movement.TurnRight(False)

2️⃣ PERCEÇÃO DO MUNDO (posição + objetos)

📄 bot/perception.py

class BotPerception:
    def __init__(self, client):
        self.client = client

    def get_position(self):
        pos = self.client.Self.SimPosition
        return f"X={pos.X:.1f}, Y={pos.Y:.1f}, Z={pos.Z:.1f}"

    def list_nearby_objects(self, radius=10):
        objects = []
        for obj in self.client.Network.CurrentSim.ObjectsPrimitives.Values:
            if obj.Properties and obj.Properties.Name:
                objects.append(obj.Properties.Name)
        return objects[:10]

3️⃣ LIGAR LLM (local ou API)

Aqui faço abstração, para poderes trocar depois.

📄 ai/llm.py

class LLM:
    def generate(self, prompt):
        # Placeholder (mais tarde ligas a modelo real)
        return f"(Resposta gerada pelo LLM)\n{prompt[:200]}"

4️⃣ RAG LOCAL (simples e eficaz)

📄 ai/rag.py

import os

class RAG:
    def __init__(self, docs_path="knowledge/docs"):
        self.docs = {}
        self.load_docs(docs_path)

    def load_docs(self, path):
        for file in os.listdir(path):
            if file.endswith(".txt") or file.endswith(".md"):
                with open(os.path.join(path, file), "r", encoding="utf-8") as f:
                    self.docs[file] = f.read()

    def retrieve(self, query):
        for name, text in self.docs.items():
            if query.lower() in text.lower():
                return name, text[:500]
        return None, "Sem informação relevante."

5️⃣ LEGENDAS EXPLICATIVAS (RAG transparente)

📄 subtitles/explain.py

def explain(source, reason):
    return f"""
📚 Fonte:
{source}

🧠 Explicação:
{reason}
"""

🧩 CLIENTE PRINCIPAL DO BOT

📄 bot/client.py

import clr
import time

clr.AddReference("lib/OpenMetaverse")

from OpenMetaverse import GridClient
from bot.movement import BotMovement
from bot.perception import BotPerception
from ai.llm import LLM
from ai.rag import RAG
from subtitles.explain import explain

class OpenSimBot:
    def __init__(self, first, last, password, uri):
        self.client = GridClient()

        self.first = first
        self.last = last
        self.password = password
        self.uri = uri

        self.movement = BotMovement(self.client)
        self.perception = BotPerception(self.client)
        self.llm = LLM()
        self.rag = RAG()

        self.client.Self.ChatFromSimulator += self.on_chat

    def login(self):
        params = self.client.Network.DefaultLoginParams(
            self.first,
            self.last,
            self.password,
            "AI Bot",
            "Python"
        )
        params.URI = self.uri

        print("🔌 A ligar...")
        if not self.client.Network.Login(params):
            raise SystemExit("❌ Login falhou")

        print("✅ Bot ligado!")

    def on_chat(self, sender, args):
        msg = args.Message.lower()
        name = args.FromName

        print(f"💬 {name}: {msg}")

        if "anda" in msg:
            self.movement.walk_forward()
            self.say("🚶 A andar")

        elif "onde estás" in msg:
            pos = self.perception.get_position()
            self.say(f"📍 Estou em {pos}")

        elif "o que vês" in msg:
            objs = self.perception.list_nearby_objects()
            self.say("👀 Vejo: " + ", ".join(objs))

        else:
            source, context = self.rag.retrieve(msg)
            answer = self.llm.generate(context)

            legend = explain(
                source,
                "Usei documentação interna do mundo para responder."
            )

            self.say(answer)
            self.say(legend)

    def say(self, text):
        self.client.Self.Chat(
            text,
            0,
            self.client.Self.ChatType.Normal
        )

    def run(self):
        print("🤖 Bot ativo")
        while True:
            time.sleep(1)

🚀 MAIN

📄 main.py

from bot.client import OpenSimBot

bot = OpenSimBot(
    first="AI",
    last="Bot",
    password="password",
    uri="http://127.0.0.1:9000"
)

bot.login()
bot.run()

🧪 EXEMPLO NO OPENSIM

Utilizador:

onde estás?

Bot:

📍 Estou em X=128.0, Y=128.0, Z=22.0

Utilizador:

o que é este objeto?

Bot:

(Resposta gerada pelo LLM)
Este objeto faz parte do sistema...

📚 Fonte:
objetos.md



🏁 TEM AGORA

  • ✅ Bot AI completo
  • ✅ Movimento real
  • ✅ Perceção do mundo
  • ✅ RAG local
  • ✅ LLM pronto
  • ✅ Legendas explicativas
  • ✅ Estrutura profissional

🔥 PRÓXIMOS UPGRADES

  • FAISS / embeddings
  • Memória por utilizador
  • HUD com legendas
  • Comandos por menu
  • Multilinguagem
  • Logs XML / JSON
  • Modo professor / guia / segurança

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published