-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Beta function to crete mind maps - Allows to ask, summarize and explain topics
- Loading branch information
Showing
11 changed files
with
154 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,4 +161,5 @@ cython_debug/ | |
|
||
# Other folders | ||
data/db/*.db | ||
data/reports/* | ||
data/reports/* | ||
data/mindmaps/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,57 @@ | ||
# aura/app/server/commands/ai_command.py | ||
|
||
from enum import Enum | ||
import os | ||
from datetime import datetime | ||
import discord | ||
from app.utils.cmdline import run_npx_command | ||
from app.server.commands.discord_commands import Commands | ||
from app.sources.llms.google_ai import GoogleAI | ||
|
||
|
||
class Model(Enum): | ||
GOOGLE_AI = "google_ai" | ||
OPEN_AI = "open_ai" | ||
from app.sources.generative.text_model import TextModel | ||
|
||
|
||
class AICommand(Commands): | ||
|
||
def __init__(self, model: Model = Model.GOOGLE_AI): | ||
super().__init__() | ||
self.model = model | ||
|
||
def execute(self, sub_command: str, prompt: str) -> discord.Embed: | ||
ai = self.get_model() | ||
|
||
if sub_command == "chat": | ||
return self.chat(ai, prompt) | ||
def execute(self, sub_command: str, *args) -> discord.Embed: | ||
ai = TextModel() | ||
|
||
if sub_command == "ask": | ||
return self.ask(ai, *args) | ||
elif sub_command == "explain": | ||
return self.explain(ai, *args) | ||
elif sub_command == "summarize": | ||
return self.summarize(ai, *args) | ||
elif sub_command == "mindmap": | ||
return self.generate_map(ai, *args) | ||
else: | ||
return self.get_help()["brief"] | ||
|
||
def chat(self, ai: GoogleAI, prompt: str) -> discord.Embed: | ||
response = ai.generate_text(prompt) | ||
def ask(self, ai: TextModel, prompt: str) -> discord.Embed: | ||
response = ai.ask(prompt) | ||
return self.create_embed(prompt, response, "") | ||
|
||
def get_model(self): | ||
if self.model == Model.GOOGLE_AI: | ||
return GoogleAI() | ||
else: | ||
return None | ||
def explain(self, ai: TextModel, topic: str) -> discord.Embed: | ||
response = ai.explain(topic) | ||
return self.create_embed(topic, response, "") | ||
|
||
def summarize(self, ai: TextModel, text: str, type: str = "brief") -> discord.Embed: | ||
response = ai.summarize(text, type) | ||
return self.create_embed(text, response, "") | ||
|
||
async def generate_map(self, ai: TextModel, text: str, user_id: str) -> None: | ||
user_folder = os.path.join("data", "mindmaps", str(user_id)) | ||
# Create the user folder if it does not exist | ||
os.makedirs(user_folder, exist_ok=True) | ||
response = ai.create_mindmap(text) | ||
filename = f"{user_id}_{datetime.utcnow().isoformat()}_mindmap.md" | ||
mindmap_path = os.path.join(user_folder, filename) | ||
with open(mindmap_path, "w", encoding="utf-8") as file: | ||
file.write(response) | ||
|
||
# Create the visual mind map | ||
run_npx_command("markmap-cli", "--no-open", "--no-toolbar", "--offline", "-o", mindmap_path, f"{mindmap_path}.html") | ||
|
||
def get_help(self): | ||
return { | ||
"brief": "Commands to interact with the AI", | ||
"search": "Generate text from the AI", | ||
"parameters": ["chat", "prompt"] | ||
"parameters": ["ask", "prompt"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# aura/app/sources/llms/google_ai.py | ||
|
||
from langchain_google_genai import ChatGoogleGenerativeAI | ||
|
||
|
||
class TextModel: | ||
|
||
def __init__(self, model_name: str = "gemini-pro"): | ||
self.llm = ChatGoogleGenerativeAI(model=model_name) | ||
|
||
def ask(self, question: str) -> str: | ||
prompt = f"Question: {question}\nAnswer: Be concise and clear." | ||
result = self.llm.invoke(prompt) | ||
return result.content | ||
|
||
def explain(self, topic: str) -> str: | ||
prompt = f"Explain: {topic}\nAnswer: Provide a step by step detailed explanation." | ||
result = self.llm.invoke(prompt) | ||
return result.content | ||
|
||
def summarize(self, text: str, type: str) -> str: | ||
prompt = f"Summarize: {text}\nAnswer: Provide a {type} summary." | ||
result = self.llm.invoke(prompt) | ||
return result.content | ||
|
||
def create_mindmap(self, text: str) -> str: | ||
prompt = f"Create a markdown mind map from the following text: {text} to use with markmap" | ||
result = self.llm.invoke(prompt) | ||
return result.content |
This file was deleted.
Oops, something went wrong.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# aura/app/utils/cmdline.py | ||
|
||
import subprocess | ||
|
||
|
||
def run_cli_command(command: str, *args) -> str: | ||
""" | ||
Run a command line command with the given arguments | ||
""" | ||
command_args = [command, *args] | ||
with subprocess.Popen(command_args, stdout=subprocess.PIPE) as process: | ||
output, _ = process.communicate() | ||
return output.decode("utf-8") | ||
|
||
|
||
def run_npx_command(command: str, *args) -> str: | ||
""" | ||
Run an npx command with the given arguments | ||
""" | ||
command_args = ["npx", command, *args] | ||
with subprocess.Popen(command_args, stdout=subprocess.PIPE) as process: | ||
output, _ = process.communicate() | ||
return output.decode("utf-8") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# aura/app/utils/parser.py | ||
|
||
import cairosvg | ||
|
||
|
||
def convert_html_to_svg(html: str, output_file: str) -> None: | ||
""" | ||
Convert HTML to SVG | ||
""" | ||
with open(html, "r", encoding="uft-8") as file: | ||
html_content = file.read() | ||
cairosvg.svg2svg(html_content, write_to=output_file) | ||
|
||
|
||
def convert_html_to_png(html: str, output_file: str) -> None: | ||
""" | ||
Convert HTML to PNG | ||
""" | ||
with open(html, "r", encoding="utf-8") as file: | ||
html_content = file.read() | ||
cairosvg.svg2png(html_content, write_to=output_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters