Skip to content

Commit

Permalink
Merge pull request #20 from kamikazebr/dev
Browse files Browse the repository at this point in the history
Merge Dev to Main
  • Loading branch information
kamikazebr authored Nov 6, 2023
2 parents 2bf16c8 + db19522 commit baa6465
Show file tree
Hide file tree
Showing 17 changed files with 298 additions and 64 deletions.
42 changes: 42 additions & 0 deletions packages/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Rules and targets in Makefile1

up-all:
@echo "Uping API, Vector and Discord"
# To execute the default target from Makefile2 in another folder, use $(MAKE) -C path/to/directory
$(MAKE) -f vector_server/Makefile up
@echo "Uping ML"
$(MAKE) -f ml/Makefile up
@echo "Uping Discord"
$(MAKE) -f discord/Makefile up
@echo "Uping Complete"

down-all:
@echo "Downing API, Vector and Discord"
# To execute the default target from Makefile2 in another folder, use $(MAKE) -C path/to/directory
$(MAKE) -f discord/Makefile down
@echo "Downing ML"
$(MAKE) -f ml/Makefile down
@echo "Downing Vector"
$(MAKE) -f vector_server/Makefile down
@echo "Downing Complete"


restart-all:
@echo "Restarting API, Vector and Discord"
$(MAKE) -f vector_server/Makefile restart
@echo "Restarting ML"
$(MAKE) -f ml/Makefile restart
@echo "Restarting Discord"
$(MAKE) -f discord/Makefile restart
@echo "Restarting Complete"

build-all:
@echo "Building API, Vector and Discord"
$(MAKE) -f vector_server/Makefile build
@echo "Building ML"
$(MAKE) -f ml/Makefile build
@echo "Building Discord"
$(MAKE) -f discord/Makefile build
@echo "Building Complete"

.PHONY: up-all down-all build-all
2 changes: 1 addition & 1 deletion packages/discord/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ endif
SERVICE = discord_bot_dev

# Check if the 'service' option is set
ifdef s
ifdef service
SERVICE = $(service)
endif

Expand Down
6 changes: 6 additions & 0 deletions packages/discord/docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
version: "3"
networks:
vector_server_default:
external: true
services:
discord_bot_dev:
build:
context: .
dockerfile: dev.Dockerfile
networks:
- vector_server_default
restart: unless-stopped
env_file:
- .env
volumes:
Expand Down
3 changes: 3 additions & 0 deletions packages/discord/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
version: "3"
networks:
vector_server_default:
external: true
services:
discord_bot:
# image: ghcr.io/kamikazebr/qabot:main-vector_server
Expand Down
21 changes: 16 additions & 5 deletions packages/discord/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class FinishAction(Exception):
pass


@bot.tree.command(name="ask")
@bot.tree.command(name=f"ask{'2' if os.environ.get('BOT', None) else ''}")
@app_commands.describe(question="Ask anything to Hivemind")
async def ask(inter: discord.Interaction, question: str):
start_time = time.monotonic()
Expand Down Expand Up @@ -79,7 +79,7 @@ async def ask(inter: discord.Interaction, question: str):
raise FinishAction()

except FinishAction:
logger.info(f"FinishException")
logger.info(f"FinishAction")
pass
except Exception as e:
logger.error(f"Error on ask {e}")
Expand All @@ -91,14 +91,25 @@ async def ask(inter: discord.Interaction, question: str):
await session.close()

elapsed_time = time.monotonic() - start_time

# if elapsed_time > 60:
# elapsed_time = elapsed_time / 60
# time_unit = "minutes"
# else:
# time_unit = "seconds"
#
# result_message = f"{last_message}\nAnswer took about {elapsed_time:.2f} {time_unit}!"

if elapsed_time > 60:
elapsed_time = elapsed_time / 60
minutes = int(elapsed_time // 60)
seconds = int(elapsed_time % 60)
time_unit = "minutes"
result_message = f"{last_message}\nAnswer took about {minutes} minutes and {seconds} seconds!"
else:
time_unit = "seconds"
result_message = f"{last_message}\nAnswer took about {elapsed_time:.2f} {time_unit}!"

result_message = f"{last_message}\nAnswer took about {elapsed_time:.2f} {time_unit}!"

logger.debug(f"Msg sent: {result_message}")
await inter.edit_original_response(content=result_message)

token = os.environ.get("DISCORD_BOT_SECRET")
Expand Down
60 changes: 60 additions & 0 deletions packages/ml/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Makefile for running Docker Compose commands with conditional -f option

# Default Docker Compose file
COMPOSE_FILE = docker-compose.dev.yml

# Check if the 'prod' option is set
ifdef prod
COMPOSE_FILE = docker-compose.yml
endif

# Default service name
SERVICE = hivemind_api

# Check if the 'service' option is set
ifdef service
SERVICE = $(service)
endif

# Default target (when you run just "make" without specifying a target)
default: up

# Target to start the specified service using Docker Compose
up:
docker compose -f $(COMPOSE_FILE) up -d $(SERVICE)

# Target to view the logs of the specified service using Docker Compose
logs-full:
docker compose -f $(COMPOSE_FILE) logs $(SERVICE) -f

logs:
docker compose -f $(COMPOSE_FILE) logs $(SERVICE) -f -n 50 -t

# Target to stop and remove the specified service using Docker Compose
down:
docker compose -f $(COMPOSE_FILE) down $(SERVICE)

# Target to stop the specified service using Docker Compose (without removing it)
stop:
docker compose -f $(COMPOSE_FILE) stop $(SERVICE)

# Target to restart the specified service using Docker Compose
restart:
docker compose -f $(COMPOSE_FILE) restart $(SERVICE)

# Target to rebuild the specified service using Docker Compose
build:
docker compose -f $(COMPOSE_FILE) up -d --build $(SERVICE)

# Target to show help message (list available targets)
help:
@echo "Available targets:"
@echo " up - Start the specified service using Docker Compose"
@echo " logs - View the logs of the specified service using Docker Compose"
@echo " down - Stop and remove the specified service using Docker Compose"
@echo " stop - Stop the specified service using Docker Compose (without removing it)"
@echo " restart - Restart the specified service using Docker Compose"
@echo " build - Rebuild the specified service using Docker Compose"
@echo " help - Show this help message"

.PHONY: up logs down stop restart build help
39 changes: 27 additions & 12 deletions packages/ml/dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@ FROM python:3.10-slim-bullseye AS base

LABEL hivemind.env.dev=true

ENV ENV_FILE="docker"
# Set environment variables
ENV ENV_FILE="docker" \
PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
PYTHONHASHSEED=random

ENV PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
PYTHONHASHSEED=random
# Install PDM
RUN pip install -U pip setuptools wheel && \
pip install pdm

# install PDM
RUN pip install -U pip setuptools wheel
RUN pip install pdm
# Set the working directory
WORKDIR /project

# copy files
COPY pyproject.toml pdm.lock /project/
# Copy project files to the working directory
COPY . /project/

WORKDIR /project/
# Install dependencies and the project in the local directory
RUN mkdir __pypackages__ && pdm sync --prod --no-editable --fail-fast

RUN pdm export --format requirements --without-hashes > requirements.txt
RUN pip install -r requirements.txt
# Set the PYTHONPATH environment variable
ENV PYTHONPATH=/project/pkgs

RUN mkdir -p /project/pkgs

# Move files and executables from the dependencies directory
RUN mv __pypackages__/3.10/lib/* /project/pkgs && \
mv __pypackages__/3.10/bin/* /bin/

WORKDIR /project

# Default command to be executed when the container starts
CMD ["python", "src/api.py"]
12 changes: 6 additions & 6 deletions packages/ml/docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
version: "3"
networks:
vector_server_default:
external:
name: vector_server_default
external: true
services:
hivemind_api:
restart: unless-stopped
ports:
- "3333:3333"
networks:
- vector_server_default
build:
context: .
dockerfile: Dockerfile
dockerfile: dev.Dockerfile
env_file:
- .env
# volumes:
# - ./:/project/
# command: [ "python","/project/src/api.py" ]
volumes:
- ./src:/project/src
command: [ "python","/project/src/api.py" ]
3 changes: 1 addition & 2 deletions packages/ml/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
version: "3"
networks:
vector_server_default:
external:
name: vector_server_default
external: true
services:
hivemind_api:
image: ghcr.io/kamikazebr/qabot:main-ml
Expand Down
1 change: 1 addition & 0 deletions packages/ml/src/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ async def _reason(self, should_try_complete=False, should_summary=False):

full_prompt = " ".join([msg.content for msg in prompt_msg])

logger.debug(f"full_prompt: {full_prompt}")
# await self.ui.notify(title="REASONING PROMPT", message=full_prompt)
try:
enc = tiktoken.encoding_for_model(self.openaichat.model_name)
Expand Down
6 changes: 5 additions & 1 deletion packages/ml/src/memory/semantic_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from llm.extract_entity.prompt import get_chat_template
from llm.extract_entity.schema import JsonSchema as ENTITY_EXTRACTION_SCHEMA
from llm.json_output_parser import LLMJsonOutputParser, LLMJsonOutputParserException
from logger.hivemind_logger import logger
from ui.cui import CommandlineUserInterface
from utils.constants import DEFAULT_EMBEDDINGS
from utils.util import atimeit, timeit
Expand Down Expand Up @@ -60,6 +61,9 @@ async def extract_entity(self, text: str, question: str, task: str) -> dict:
.to_messages()
)

full_prompt = " ".join([msg.content for msg in prompt])

logger.debug(f"semantic->extract_entity->Prompt: {full_prompt}")
llm_result = await self.openaichat._agenerate(messages=prompt)
await self.ui.call_callback_info_llm_result(llm_result)
result = llm_result.generations[0].message.content
Expand All @@ -80,7 +84,7 @@ async def extract_entity(self, text: str, question: str, task: str) -> dict:
if len(result_json_obj) > 0:
await asyncio.create_task(self._embed_knowledge(result_json_obj))

except Exception as e:
except BaseException as e:
print(f"semantic->extract_entity->Text: {text}\n")
print(f"semantic->extract_entity->Result: {result}\n")
print(
Expand Down
1 change: 1 addition & 0 deletions packages/ml/src/utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ async def async_get_request(url):
data = await response.json()
return data
else:
logger.error(f"Error while making the request: {await response.text()}")
# Handle HTTP errors here, if necessary
return None
except aiohttp.ClientError as e:
Expand Down
60 changes: 60 additions & 0 deletions packages/vector_server/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Makefile for running Docker Compose commands with conditional -f option

# Default Docker Compose file
COMPOSE_FILE = docker-compose.dev.yml

# Check if the 'prod' option is set
ifdef prod
COMPOSE_FILE = docker-compose.yml
endif

# Default service name
SERVICE = vector_api_dev

# Check if the 'service' option is set
ifdef service
SERVICE = $(service)
endif

# Default target (when you run just "make" without specifying a target)
default: up

# Target to start the specified service using Docker Compose
up:
docker compose -f $(COMPOSE_FILE) up -d $(SERVICE)

# Target to view the logs of the specified service using Docker Compose
logs-full:
docker compose -f $(COMPOSE_FILE) logs $(SERVICE) -f

logs:
docker compose -f $(COMPOSE_FILE) logs $(SERVICE) -f -n 50 -t

# Target to stop and remove the specified service using Docker Compose
down:
docker compose -f $(COMPOSE_FILE) down $(SERVICE)

# Target to stop the specified service using Docker Compose (without removing it)
stop:
docker compose -f $(COMPOSE_FILE) stop $(SERVICE)

# Target to restart the specified service using Docker Compose
restart:
docker compose -f $(COMPOSE_FILE) restart $(SERVICE)

# Target to rebuild the specified service using Docker Compose
build:
docker compose -f $(COMPOSE_FILE) up -d --build $(SERVICE)

# Target to show help message (list available targets)
help:
@echo "Available targets:"
@echo " up - Start the specified service using Docker Compose"
@echo " logs - View the logs of the specified service using Docker Compose"
@echo " down - Stop and remove the specified service using Docker Compose"
@echo " stop - Stop the specified service using Docker Compose (without removing it)"
@echo " restart - Restart the specified service using Docker Compose"
@echo " build - Rebuild the specified service using Docker Compose"
@echo " help - Show this help message"

.PHONY: up logs down stop restart build help
Loading

0 comments on commit baa6465

Please sign in to comment.