Skip to content

Commit

Permalink
[python] New Hal9 App (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisGuillen03 authored Dec 10, 2024
1 parent 402d2f0 commit 5380f2a
Show file tree
Hide file tree
Showing 21 changed files with 1,830 additions and 731 deletions.
21 changes: 0 additions & 21 deletions apps/hal9/.vscode/launch.json

This file was deleted.

104 changes: 43 additions & 61 deletions apps/hal9/app.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,43 @@
from groq import Groq
import os
import hal9 as h9
import json
import openai

from tools.calculator import calculate
from tools.game import build_game
from tools.generic import generic_reply
from tools.hal9 import hal9_reply
from tools.website import build_website
from tools.streamlit import build_streamlit
from tools.image import create_image
from tools.document import document_reply
from tools.csv import csv_reply
from tools.image_analyzer import image_analyzer

MODEL = "llama3-groq-70b-8192-tool-use-preview"
def run(messages, tools):
return Groq().chat.completions.create(
model = MODEL,
messages = messages,
temperature = 0,
seed = 1,
tools=tools,
tool_choice = "required",)

prompt = input("")
h9.event('prompt', prompt)

messages = h9.load("messages", [])
if len(messages) <= 0:
messages.append({"role": "system", "content": "You are Hal9, a helpful and highly capable AI assistant. Your primary responsibility is to analyze user questions and select the most appropriate tool to provide precise, relevant, and actionable responses. Always prioritize using the right tool to ensure efficiency and clarity in your answers."})
messages.append({"role": "user", "content": prompt})
h9.save("messages", messages, hidden=True)

all_tools = [
calculate,
build_game,
generic_reply,
hal9_reply,
build_website,
build_streamlit,
create_image,
document_reply,
csv_reply,
image_analyzer
]

tools = h9.describe(all_tools, model = "llama")

try:
completion = run(messages, tools)
h9.complete(completion, messages = messages, tools = all_tools, show = False, model = "llama")
except Exception as e:
h9.event('error', str(e))
one_tool = h9.describe([generic_reply], model = "llama")
completion = run(messages, one_tool)
h9.complete(completion, messages = messages, tools = [generic_reply], show = False, model = "llama")

h9.save("messages", messages, hidden=True)
from utils import generate_response, load_messages, insert_message, execute_function, save_messages, insert_tool_message, is_url, download_file, generate_text_embeddings_parquet
from tools.calculator import solve_math_problem_description, solve_math_problem
from tools.generic import answer_generic_question_description, answer_generic_question
from tools.csv_agent import analyze_csv_description, analyze_csv
from tools.image_agent import images_management_system, images_management_system_description, add_images_descriptions
from tools.hal9 import answer_hal9_questions_description, answer_hal9_questions
from tools.text_agent import analyze_text_file_description, analyze_text_file
from tools.streamlit import streamlit_generator, streamlit_generator_description
from tools.website import website_generator, website_generator_description

# load messages
messages = load_messages()

# load tools
tools_descriptions = [solve_math_problem_description, answer_generic_question_description, analyze_csv_description, images_management_system_description, answer_hal9_questions_description, analyze_text_file_description, streamlit_generator_description, website_generator_description]
tools_functions = [solve_math_problem, answer_generic_question, analyze_csv, images_management_system, answer_hal9_questions, analyze_text_file, streamlit_generator, website_generator]

if len(messages) < 1:
messages = insert_message(messages, "system", "You are Hal9, a helpful and highly capable AI assistant. Your primary responsibility is to analyze user questions and select the most appropriate tool to provide precise, relevant, and actionable responses. Always prioritize using the right tool to ensure efficiency and clarity in your answers.")

user_input = input()
if is_url(user_input):
filename = user_input.split("/")[-1]
file_extension = filename.split(".")[-1] if "." in filename else "No extension"
download_file(user_input)
messages = insert_message(messages, "system", f"Consider using the file available at path: './.storage/.{filename}' for the following questions.")
messages = insert_message(messages, "assistant", f"Im ready to response questions about your file: {filename}")
if file_extension.lower() == "pdf":
generate_text_embeddings_parquet(user_input)
if file_extension.lower() in ['jpg', 'jpeg', 'png','webp']:
add_images_descriptions(f"./.storage/.{filename}")
print(f"Im ready to response questions about your file: {filename}")
else:
user_input = user_input.replace("\f", "\n")
messages = insert_message(messages, "user", user_input)

response = generate_response("openai", "gpt-4-turbo", messages, tools_descriptions, tool_choice = "required", parallel_tool_calls=False)

tool_result = execute_function(response, tools_functions)

insert_tool_message(messages, response, tool_result)

save_messages(messages)
15 changes: 15 additions & 0 deletions apps/hal9/clients.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from openai import AzureOpenAI, OpenAI
import os

# Azure - OpenAI (gpt-4)
azure_openai_client = AzureOpenAI(
azure_endpoint = 'https://openai-hal9.openai.azure.com/',
api_key = os.environ['OPENAI_AZURE'],
api_version = '2024-10-01-preview',
)

# o1 Client
openai_client = OpenAI(
base_url="https://api.hal9.com/proxy/server=https://api.openai.com/v1/",
api_key = "hal9"
)
13 changes: 13 additions & 0 deletions apps/hal9/data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os

def load_data():
data = {}
base_path = os.path.dirname(__file__)
for file_name in os.listdir(base_path):
if file_name.endswith(".txt"):
data_name = os.path.splitext(file_name)[0]
with open(os.path.join(base_path, file_name), 'r') as file:
data[data_name] = file.read()
return data

DATA = load_data()
4 changes: 1 addition & 3 deletions apps/hal9/tools/hal9.txt → apps/hal9/data/hal9.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
You can reply in the following ways:

- Reply with general knowledge answers
- If they ask about capabilities of this chat you can mention you support doing math, building games, answering questions about Hal9, building websited.
- For numerical problems that may involve calculation or files like Excel, indicate that you recomment them to ask instead the CSV builder chatbot, this chatbot can be found scrolling down in the Hal9 website.
- If the user wants to provide reference documents like PDF files, recomment using the PDF chatbot, this chatbot can be found scrolling down in the Hal9 website.
- If they ask about capabilities of this chat you can mention you support multiple tools as solve math problems, CSV Analytics, image generation, generic answers, websites creation, games generation, streamlit dashboars
- If the user is just curious about what Hal9 is, ask them questions about their work and creatievely explore how automating tasks with AI can help them find value. If they want custom demos or have questions that might require a team of experts to be involved encourage them to chat with our team using https://calendly.com/javierluraschi/meet

Some more information about Hal9:
Expand Down
6 changes: 3 additions & 3 deletions apps/hal9/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
replicate==1.0.3
groq==0.12.0
openai==1.55.3
httpx==0.27.2
typing-extensions>=4.11.0,<5.0.0
typing-extensions>=4.11.0,<5.0.0
kaleido==0.2.1
replicate==1.0.3
123 changes: 0 additions & 123 deletions apps/hal9/tools/analyst.py

This file was deleted.

40 changes: 32 additions & 8 deletions apps/hal9/tools/calculator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
def calculate(expression):
"""Use this tool to provide a solution to mathematical problems with a expression using Python code
Parameters:
'expression' = is the aritmetic operations to evaluate, needs conversion to proper Python syntax.
"""
result = eval(expression)
print(result)
return result
from utils import stream_print

def solve_math_problem(steps_explanation, code_solution):
stream_print("Steps:\n")
stream_print(steps_explanation)
stream_print("\n\nPython Code:\n")
exec(code_solution)
return f"Steps:\n{steps_explanation}\n\n\nPython Code: {code_solution}"

solve_math_problem_description = {
"type": "function",
"function": {
"name": "solve_math_problem",
"description": "This function provides solutions to mathematical problems by offering both a step-by-step breakdown of the problem-solving process and a Python code implementation. It ensures clarity by explaining relevant concepts, formulas, and logic, while also demonstrating how the solution can be executed programmatically.",
"strict": True,
"parameters": {
"type": "object",
"properties": {
"steps_explanation": {
"type": "string",
"description": "A comprehensive, step-by-step description of how to solve the specified mathematical problem, including relevant formulas and concepts.",
},
"code_solution": {
"type": "string",
"description": "A complete Python script with imports that executes the described solution, clearly demonstrating the implementation of each step and outputting the final answer with a print.",
},
},
"required": ["steps_explanation", "code_solution"],
"additionalProperties": False,
},
}
}
Loading

0 comments on commit 5380f2a

Please sign in to comment.