-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
402d2f0
commit 5380f2a
Showing
21 changed files
with
1,830 additions
and
731 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,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) |
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,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" | ||
) |
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,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() |
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
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,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 |
This file was deleted.
Oops, something went wrong.
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,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, | ||
}, | ||
} | ||
} |
Oops, something went wrong.