Skip to content

Commit

Permalink
[apps/hal9] Run generic replies in parallel follow up optimizations (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisGuillen03 authored Jan 13, 2025
1 parent 0ac6738 commit 5baa10b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 69 deletions.
21 changes: 11 additions & 10 deletions apps/hal9/app.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
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_wrapper_description, answer_generic_wrapper, answer_generic_question_submit
from tools.generic import answer_generic_question, answer_generic_question_description
from tools.generic import answer_generic_question, answer_generic_question_description, answer_generic_future
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
from concurrent.futures import ThreadPoolExecutor, Future
from concurrent.futures import ThreadPoolExecutor

# load messages
messages = load_messages()

# load tools
tools_descriptions = [answer_generic_wrapper_description, solve_math_problem_description, analyze_csv_description, images_management_system_description, answer_hal9_questions_description, analyze_text_file_description, streamlit_generator_description, website_generator_description]
tools_functions = [answer_generic_wrapper, solve_math_problem, analyze_csv, images_management_system, answer_hal9_questions, analyze_text_file, streamlit_generator, website_generator]
tools_descriptions = [answer_generic_question_description, solve_math_problem_description, analyze_csv_description, images_management_system_description, answer_hal9_questions_description, analyze_text_file_description, streamlit_generator_description, website_generator_description]
tools_functions = [answer_generic_question, solve_math_problem, 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.")
Expand All @@ -34,16 +33,18 @@
print(f"I'm ready to answer questions about your file: {filename}")
else:
user_input = user_input.replace("\f", "\n")
messages = insert_message(messages, "user", user_input)

with ThreadPoolExecutor() as executor:
answer_generic_question_submit(executor, user_input)
answer_generic_future = executor.submit(answer_generic_question, user_input)

messages = insert_message(messages, "user", user_input)
response = executor.submit(generate_response, "openai", "gpt-4-turbo", messages, tools_descriptions, tool_choice="required", parallel_tool_calls=False)

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

tool_result = execute_function(response, tools_functions)
tool_result = execute_function(response_result, tools_functions)

insert_tool_message(messages, response, tool_result)
if tool_result is not None:
insert_tool_message(messages, response_result, tool_result)

save_messages(messages)
65 changes: 24 additions & 41 deletions apps/hal9/tools/generic.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
from groq import Groq
from utils import stream_print, load_messages, insert_message, save_messages
from utils import load_messages, insert_message, save_messages
from concurrent.futures import ThreadPoolExecutor

def answer_generic_question(user_input, show = True):
# load messages
answer_generic_future = None

def answer_generic_question(user_input):
global answer_generic_future
if answer_generic_future is not None:
text_response=answer_generic_future.result()
print(text_response)
return text_response
else:
with ThreadPoolExecutor() as executor:
answer_generic_future = executor.submit(process_request, user_input)
text_response = answer_generic_future.result()
return text_response

def process_request(user_input):
messages = load_messages(file_path="./.storage/.generic_agent_messages.json")
messages = insert_message(messages, "user", user_input)
stream = Groq().chat.completions.create(
model = "llama3-70b-8192",
messages = messages,
temperature = 0,
seed = 1,
stream = True)

text_response = stream_print(stream, show)
response = Groq().chat.completions.create(
model="llama3-70b-8192",
messages=messages,
temperature=0,
seed=1
)
text_response = response.choices[0].message.content
messages = insert_message(messages, "assistant", text_response)
save_messages(messages, file_path="./.storage/.generic_agent_messages.json")

return text_response

answer_generic_question_description = {
Expand All @@ -36,33 +48,4 @@ def answer_generic_question(user_input, show = True):
"additionalProperties": False,
},
}
}

answer_generic_future = None
def answer_generic_wrapper(user_input):
result = answer_generic_future.result()
print(result)

def answer_generic_question_submit(executor, user_input):
global answer_generic_future
answer_generic_future = executor.submit(answer_generic_question, user_input, False)

answer_generic_wrapper_description = {
"type": "function",
"function": {
"name": "answer_generic_wrapper",
"description": "Handles general questions or queries provided by the user by taking their input and generating a meaningful response.",
"strict": True,
"parameters": {
"type": "object",
"properties": {
"user_input": {
"type": "string",
"description": "Take the user input and pass the same string to the function",
},
},
"required": ["user_input"],
"additionalProperties": False,
},
}
}
7 changes: 3 additions & 4 deletions apps/hal9/tools/hal9.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
from utils import stream_print

def answer_hal9_questions(user_input):
stream = Groq().chat.completions.create(
response = Groq().chat.completions.create(
model = "llama3-70b-8192",
messages = [{"role": "system", "content": DATA["hal9"]},{"role": "user", "content": user_input}],
temperature = 0,
seed = 1,
stream = True)
seed = 1,)

text_response = stream_print(stream)
text_response = print(response.choices[0].message.content)
return text_response

answer_hal9_questions_description = {
Expand Down
15 changes: 1 addition & 14 deletions apps/hal9/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import urllib.parse
import urllib.request
import requests
import time
from typing import Literal, List, Dict, Any, Union, Optional
from clients import openai_client, azure_openai_client
from groq import Groq
Expand Down Expand Up @@ -90,7 +89,7 @@ def generate_response(
"n": n
}

if tools is not None and not client_type == "groq":
if tools is not None:
payload["parallel_tool_calls"] = parallel_tool_calls

# Generate the response using the client's completion API.
Expand All @@ -105,9 +104,6 @@ def load_messages(file_path="./.storage/.messages.json") -> List[Dict[str, Any]]
Returns:
List[Dict[str, Any]]: A list of messages if the file exists and is valid.
"""
# Create the .storage directory if it doesn't exist
if not os.path.exists("./.storage"):
os.makedirs("./.storage")
if not os.path.exists(file_path):
return []
else :
Expand All @@ -124,9 +120,6 @@ def save_messages(messages: List[Dict[str, Any]], file_path="./.storage/.message
messages (List[Dict[str, Any]]): A list of messages to be saved.
"""

# Create the .storage directory if it doesn't exist
if not os.path.exists("./.storage"):
os.makedirs("./.storage")
with open(file_path, "w", encoding="utf-8") as file:
json.dump(messages, file, ensure_ascii=False, indent=4)

Expand Down Expand Up @@ -219,9 +212,6 @@ def is_url(prompt):
return all([result.scheme, result.netloc])

def download_file(url):
# Create the .storage directory if it doesn't exist
if not os.path.exists("./.storage"):
os.makedirs("./.storage")
filename = url.split("/")[-1]
modified_filename = f"./.storage/.{filename}"

Expand Down Expand Up @@ -283,9 +273,6 @@ def process_chunk(chunk_info):
}

def generate_text_embeddings_parquet(url, model="text-embedding-3-small", client_type="azure", n_words=300, overlap=0, max_threads=8):
# Create the .storage directory if it doesn't exist
if not os.path.exists("./.storage"):
os.makedirs("./.storage")
# Download and read the PDF
response = requests.get(url)
pdf_document = fitz.open(stream=BytesIO(response.content))
Expand Down

0 comments on commit 5baa10b

Please sign in to comment.