Skip to content

Commit d265353

Browse files
Merge pull request #108 from hand-e-fr/dev
Dev
2 parents b67eeb6 + 96f5678 commit d265353

15 files changed

Lines changed: 285 additions & 249 deletions

File tree

CHAGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ All significant changes to this project will be documented in this file.
44

55
---
66

7+
## **v1.1rc3**
8+
9+
- **Fixes**
10+
- `emulate` now works when emulated function is called inside another one
11+
- `emulate` works now with `lru_cache` decorator from `functools` module
12+
13+
- **Internal**
14+
- Added custom Exception classes for request and frame errors.
15+
- Added a loop to find the frame in `_extend_scope`
16+
- Added a Makefile for cleaning and packaging and tests
17+
---
18+
719
## **v1.1** 13/09/2024
820

921
- **Fixes**
@@ -39,6 +51,11 @@ All significant changes to this project will be documented in this file.
3951
- **Internal**
4052
- Functional tests have been added for each of the library's features
4153

54+
- **Doc**
55+
- Documentation now integrates Google Cobal link
56+
57+
---
58+
4259
## **v1.0** 29/08/2024:
4360

4461
- **Features**

Makefile

Lines changed: 96 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,117 @@
1-
PACKAGE_NAME = OpenHosta
2-
SRC_DIR = src/OpenHosta
3-
TEST_DIR = tests
4-
DOC_DIR = doc
1+
PACKAGE_NAME := OpenHosta
2+
SRC_DIR := src/OpenHosta
3+
TEST_DIR := tests
4+
DOC_DIR := doc
5+
API_KEY =
6+
TAG := "-==[OpenHosta]==- \>\>\>"
7+
8+
ifeq ($(OS),Windows_NT)
9+
DETECTED_OS := Windows
10+
NULL_DEVICE := NUL
11+
PYTHON := python
12+
PIP := pip
13+
L_SHELL := powershell -Command
14+
PYTEST := & C:\\Users\\Merlin\\AppData\\Roaming\\Python\\Python312\\Scripts\\pytest.exe
15+
ENV := $$env:
16+
WRITE := "Write-Host
17+
COLOR := -ForegroundColor DarkMagenta"
18+
FIND := "Get-ChildItem -Path . -Recurse -Directory -Filter
19+
RM := Remove-Item -Recurse -Force"
20+
CLEAR := "clear"
21+
CLEAR_DATA = type NUL > $(REDIRECT)
22+
MKDIR := New-Item -ItemType Directory -Force -Path
23+
else
24+
DETECTED_OS := Linux
25+
NULL_DEVICE := /dev/null
26+
PYTHON := python3
27+
PIP := pip3
28+
L_SHELL :=
29+
PYTEST := pytest
30+
ENV := export
31+
WRITE := echo
32+
COLOR :=
33+
FIND := find
34+
RM := rm -rf
35+
CLEAR := clear
36+
CLEAR_DATA = > $(REDIRECT)
37+
MKDIR := mkdir
38+
endif
539

640
all: help
741

842
help:
943
@echo Usage:
1044
@echo make help Show this help message
1145
@echo make package Package the project for pip installation
12-
@echo make tests Run the tests
46+
@echo make *tests Run the tests
1347
@echo make format Format the code using black
1448
@echo make lint Check code quality using flake8
1549
@echo make clean Clean not necessary files
1650

51+
install: all
52+
@$(L_SHELL) $(WRITE) '$(TAG) [INSTALL]' $(COLOR)
53+
@$(L_SHELL) $(WRITE) '$(TAG) Installing dependencies...' $(COLOR)
54+
@$(PIP) install requirements.txt > $(NULL_DEVICE)
55+
@$(PIP) install requirements-dev.txt > $(NULL_DEVICE)
56+
@$(L_SHELL) $(WRITE) '$(TAG) Installing package: $(PACKAGE_NAME)...' $(COLOR)
57+
@$(PIP) install . > $(NULL_DEVICE)
58+
@$(L_SHELL) $(WRITE) '$(TAG) Succesfully installed $(PACKAGE_NAME) !' $(COLOR)
59+
1760
build:
18-
python -m build
19-
python -m twine check dist/*
61+
$(PYTHON) -m build
62+
$(PYTHON) -m twine check dist/*
2063

21-
upload:
22-
python -m twine upload dist/*
64+
upload: build
65+
$(PYTHON) -m twine upload dist/*
2366

24-
tests:
25-
pip install .
26-
pytest .\tests\functionnalTests\test_mandatory.py -v
27-
pip uninstall -y OpenHosta
67+
ftests: clean
68+
@$(L_SHELL) $(WRITE) '$(TAG) Installing package: $(PACKAGE_NAME)...' $(COLOR)
69+
@$(PIP) install . > $(NULL_DEVICE)
70+
@$(L_SHELL) $(WRITE) '$(TAG) Running functionnal tests...' $(COLOR)
71+
@$(L_SHELL) $(SET_ENV)
72+
@-$(L_SHELL) "$(PYTEST) .\\tests\\functionnalTests\\test_mandatory.py $(ARGS)"
73+
@$(L_SHELL) $(UNSET_ENV)
74+
@$(L_SHELL) $(WRITE) '$(TAG) Uninstalling package: $(PACKAGE_NAME)...' $(COLOR)
75+
@$(PIP) uninstall -y OpenHosta > $(NULL_DEVICE)
76+
@$(L_SHELL) $(WRITE) '$(TAG) Succesfully ran functionnal tests !' $(COLOR)
2877

29-
format:
30-
black $(SRC_DIR)
78+
utests: clean
79+
@$(L_SHELL) $(WRITE) '$(TAG) Installing package: $(PACKAGE_NAME)...' $(COLOR)
80+
@$(PIP) install . > $(NULL_DEVICE)
81+
@$(L_SHELL) $(WRITE) '$(TAG) Running unit tests...' $(COLOR)
82+
@$(L_SHELL) $(SET_ENV)
83+
@-$(L_SHELL) "$(PYTEST) .\\tests\\unitTests\\test_exec.py $(ARGS)"
84+
@$(L_SHELL) $(UNSET_ENV)
85+
@$(L_SHELL) $(WRITE) '$(TAG) Uninstalling package: $(PACKAGE_NAME)...' $(COLOR)
86+
@$(PIP) uninstall -y OpenHosta > $(NULL_DEVICE)
87+
@$(L_SHELL) $(WRITE) '$(TAG) Succesfully ran unit tests !' $(COLOR)
3188

32-
lint:
33-
flake8 $(SRC_DIR) $(TEST_DIR)
3489

3590
clean:
36-
rm -rf build dist *.egg-info
37-
find . -name "__pycache__" -exec rm -rf {} +
38-
find . -name "__hostacache__" -exec rm -rf {} +
91+
@$(L_SHELL) $(WRITE) '$(TAG) [CLEAN]' $(COLOR)
92+
@$(L_SHELL) $(WRITE) '$(TAG) Cleaning repository...' $(COLOR)
93+
@$(L_SHELL) $(FIND) '__pycache__' | $(RM)
94+
@$(L_SHELL) $(FIND) '__hostacache__' | $(RM)
95+
@$(L_SHELL) $(FIND) 'build' | $(RM)
96+
@$(L_SHELL) $(FIND) 'dist' | $(RM)
97+
@$(L_SHELL) $(FIND) 'OpenHosta.egg-info' | $(RM)
98+
@$(L_SHELL) $(FIND) '.pytest_cache' | $(RM)
99+
@$(L_SHELL) $(WRITE) '$(TAG) Uninstalling package: $(PACKAGE_NAME)...' $(COLOR)
100+
@$(PIP) uninstall -y OpenHosta > $(NULL_DEVICE)
101+
@$(L_SHELL) $(CLEAR)
102+
@$(L_SHELL) $(WRITE) '$(TAG) Succesfully cleaned repository !' $(COLOR)
103+
104+
format: clean
105+
@black $(SRC_DIR) $(ARGS)
39106

40-
fclean: clean
41-
rm -rf .pytest_cache .mypy_cache
107+
lint: format
108+
@flake8 $(SRC_DIR) $(ARGS)
42109

43-
make re:
44-
pip uninstall -y OpenHosta
45-
pip install ..
110+
re: clean
111+
@$(L_SHELL) $(WRITE) '$(TAG) Uninstalling package: $(PACKAGE_NAME)...' $(COLOR)
112+
@$(PIP) uninstall -y OpenHosta > $(NULL_DEVICE)
113+
@$(L_SHELL) $(WRITE) '$(TAG) Installing package: $(PACKAGE_NAME)...' $(COLOR)
114+
@$(PIP) install . > $(NULL_DEVICE)
115+
@$(L_SHELL) $(WRITE) '$(TAG) Succesfully updated package !' $(COLOR)
46116

47-
.PHONY: all help build tests format lint clean fclean re
117+
.PHONY: all help build upload ftests utests format lint clean re install

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "OpenHosta"
7-
version = "1.1.0-rc2"
7+
version = "1.1.0-rc3"
88
description = "Open-Source programming project IA integretion in developement environnement"
99
keywords = ["AI", "GPT", "Natural language", "Autommatic", "Easy"]
1010
authors = [

requirements-dev.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

requirements.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/OpenHosta/config.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from pydantic import BaseModel
77
from typing import get_origin, get_args
88

9+
from .errors import ApiKeyError
10+
911

1012
def is_valid_url(url):
1113
regex = re.compile(
@@ -57,9 +59,9 @@ def __str__(self) -> str:
5759

5860
def api_call(
5961
self, sys_prompt: str, user_prompt: str, creativity: float, diversity: float
60-
):
62+
)->requests.models.Response:
6163
if self.api_key is None or not self.api_key:
62-
raise AttributeError("Empty API key.")
64+
raise ApiKeyError("Empty API key.")
6365

6466
l_body = {
6567
"model": self.model,
@@ -86,11 +88,12 @@ def api_call(
8688
response = requests.post(self.base_url, json=l_body, headers=headers)
8789
response.raise_for_status()
8890
except requests.exceptions.RequestException as e:
89-
sys.stderr.write(f"[CALL_ERROR] Request failed: {e}\n")
91+
sys.stderr.write(f"\n[CALL_ERROR] Request failed:\n{e}\n\n")
9092
if response.status_code != 200:
91-
sys.stderr.write(
92-
f"[CALL_ERROR] API call the request was unsuccessful. Status code: {response.status_code}:\n{response.text}"
93-
)
93+
if "invalid_api_key" in str(response.text):
94+
raise ApiKeyError("Incorrect API key.")
95+
else:
96+
sys.stderr.write(f"[CALL_ERROR] API call was unsuccessful.\nStatus code: {response.status_code}:\n{response.text}")
9497
return response
9598

9699
def request_handler(self, response, return_type, return_caller):

src/OpenHosta/emulate.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
import inspect
23

34
from .prompt import PromptMananger
45
from .config import Model, DefaultManager
@@ -11,12 +12,12 @@
1112
l_default = DefaultManager.get_default_model()
1213

1314

14-
def build_user_prompt(_function_infos: dict = None):
15-
function_doc = _function_infos["function_def"]
16-
function_call = _function_infos["function_call"]
17-
function_return = _function_infos["return_type"]
18-
function_example = _function_infos["ho_example"]
19-
function_locals = _function_infos["function_locals"]
15+
def build_user_prompt(_infos: dict = None):
16+
function_doc = _infos["function_def"]
17+
function_call = _infos["function_call"]
18+
function_return = _infos["return_type"]
19+
function_example = _infos["ho_example"]
20+
function_locals = _infos["function_locals"]
2021

2122
user_prompt = (
2223
"Here's the function definition:\n"
@@ -50,16 +51,16 @@ def build_user_prompt(_function_infos: dict = None):
5051

5152

5253
def _exec_emulate(
53-
_function_infos: dict = None,
54-
_function_obj: object = None,
54+
_infos: dict = None,
55+
_obj: object = None,
5556
model: Model = None,
5657
l_creativity: float = None,
5758
l_diversity: float = None,
5859
):
5960
global _emulator_pre_prompt
6061

61-
_function_return_caller = _function_infos["return_caller"]
62-
_function_return = _function_infos["return_type"]
62+
_function_return_caller = _infos["return_caller"]
63+
_function_return = _infos["return_type"]
6364

6465
if model is None:
6566
model = DefaultManager.get_default_model()
@@ -75,7 +76,7 @@ def _exec_emulate(
7576
sys.stderr.write(f"[EMULATE_ERROR]: {v}")
7677
return None
7778

78-
l_user_prompt = build_user_prompt(_function_infos)
79+
l_user_prompt = build_user_prompt(_infos)
7980

8081
response = model.api_call(
8182
sys_prompt=_emulator_pre_prompt,
@@ -84,8 +85,8 @@ def _exec_emulate(
8485
diversity=l_diversity,
8586
)
8687

87-
if _function_obj is not None and "bound method" not in str(_function_obj):
88-
setattr(_function_obj, "_last_response", response.json())
88+
if _obj is not None and inspect.isfunction(_obj):
89+
setattr(_obj, "_last_response", response.json())
8990

9091

9192
if response.status_code == 200:
@@ -97,7 +98,7 @@ def _exec_emulate(
9798
sys.stderr.write(f"Error {response.status_code}: {response.text}")
9899
l_ret = None
99100

100-
if _function_obj is not None and "bound method" not in str(_function_obj):
101-
setattr(_function_obj, "_last_request", f"{_emulator_pre_prompt}\n{l_user_prompt}")
101+
if _obj is not None and inspect.isfunction(_obj):
102+
setattr(_obj, "_last_request", f"{_emulator_pre_prompt}\n{l_user_prompt}")
102103

103104
return l_ret

src/OpenHosta/errors.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1-
class OhCustomError(Exception):
2-
"""Base class for other customs exceptions"""
1+
from typing import Literal
32

4-
pass
3+
__all__ = (
4+
"RequestError",
5+
"ApiKeyError",
6+
"FrameError"
7+
)
58

9+
OhErrorCodes = Literal[
10+
""
11+
]
612

7-
class RequestError(OhCustomError):
8-
"""Raised when a request to a llm went wrong"""
13+
class OhErrorMixin(Exception):
14+
""" Base class for other customs exceptions """
15+
def __init__(self, message:str):
16+
super().__init__(message)
17+
self.message = message
18+
19+
def __str__(self)->str:
20+
return f"{self.message}\n"
21+
22+
class RequestError(OhErrorMixin):
23+
""" Raised when a request to a llm went wrong """
924

10-
pass
25+
class ApiKeyError(RequestError):
26+
""" Raised when API key is missing or incorrect """
27+
28+
class FrameError(OhErrorMixin):
29+
""" Raised when the frame inspection fail """
30+

0 commit comments

Comments
 (0)