Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: version 0.9.3 #42

Merged
merged 9 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ pip install nerif
from nerif.core import nerif
from nerif.model import SimpleChatModel

agent = SimpleChatModel()
model = SimpleChatModel()

# Use nerif judge "natural language statement"
if nerif("the sky is blue"):
print("True")
else:
# Call a simple agent
# Call a simple model
print("No", end=", ")
print(agent.chat("what is the color of the sky?"))
print(model.chat("what is the color of the sky?"))
```

## Documentation
Expand Down
13 changes: 10 additions & 3 deletions nerif/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ def convert(self, val: Any):
return str(val)

def verify(self, val: Any):
return super().verify(val)
if isinstance(val, int):
return super().verify(val)
else:
return False

def simple_fit(self, val: Any):
return int(super().simple_fit(val))
Expand Down Expand Up @@ -292,8 +295,12 @@ def embedding_mode(self, text: str):
question = "<question>\n" + text + "</question>\n"
user_prompt = self.prompt.replace("<question>", question)
response = self.agent.chat(user_prompt, max_tokens=10)
if self.verification.verify(response):
return response
try:
direct_result = int(response)
if self.verification.verify(direct_result):
return direct_result
except:
pass
simple_fit = self.verification.simple_fit(response)
if simple_fit is not None:
return simple_fit
Expand Down
4 changes: 2 additions & 2 deletions nerif/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def chat(self, message: str, append: bool = False, max_tokens: None | int = None
elif self.model.startswith("sllm"):
result = get_sllm_response(self.messages, **kwargs)
else:
raise ValueError(f"Model {self.model} not supported")
result = get_litellm_response(self.messages, **kwargs)

text_result = result.choices[0].message.content
if append:
Expand Down Expand Up @@ -248,7 +248,7 @@ def __init__(
def append_message(self, message_type: MessageType, content: str):
if message_type == MessageType.IMAGE_PATH:
content = f"data:image/jpeg;base64,{base64.b64encode(open(content, 'rb').read()).decode('utf-8')}"
self.content_cache.append({"type": "image_url", "image_url": {"url": content}})
self.content_cache.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{content}"}})
elif message_type == MessageType.IMAGE_URL:
self.content_cache.append({"type": "image_url", "image_url": {"url": content}})
elif message_type == MessageType.IMAGE_BASE64:
Expand Down
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ build-backend = "hatchling.build"

[project]
name = "nerif"
version = "0.9.1"
version = "0.9.3"

description = "LLM powered Python"
readme = "README.md"
authors = [{ name = "Chivier Humber" }]
Expand Down Expand Up @@ -36,7 +37,8 @@ dev = ["bumpver", "ruff", "pip-tools", "pytest", "ipython", "isort"]
Homepage = "https://github.com/Nerif-AI/nerif"

[tool.bumpver]
current_version = "0.9.1"
current_version = "0.9.3"

version_pattern = "MAJOR.MINOR.PATCH"
commit_message = "Bump version {old_version} -> {new_version}"
commit = true
Expand Down
3 changes: 2 additions & 1 deletion scripts/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

isort .
ruff check . --select I --fix
ruff format .
ruff format .

2 changes: 1 addition & 1 deletion test/nerif_format_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from nerif.core import (
from nerif.utils import (
FormatVerifierFloat,
FormatVerifierHumanReadableList,
FormatVerifierInt,
Expand Down
14 changes: 0 additions & 14 deletions test/nerif_agent_test.py → test/nerif_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,6 @@ def test_logits_agent(self):
print(result)
self.assertIsNotNone(result)

def test_ollama_agent(self):
ollama_agent = SimpleChatModel(model="ollama/llama3.1")
result = ollama_agent.chat("Hello, how are you?")
self.assertIsNotNone(result)
self.assertIsInstance(result, str)
self.assertGreater(len(result), 0)

def test_ollama_embed_agent(self):
ollama_agent = SimpleEmbeddingModel(model="ollama/mxbai-embed-large")
result = ollama_agent.embed("Hello, how are you?")
self.assertIsNotNone(result)
self.assertIsInstance(result, list[float])
self.assertGreater(len(result), 0)


if __name__ == "__main__":
unittest.main()
32 changes: 32 additions & 0 deletions test/nerif_model_test_ollama.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import sys
import unittest
import warnings

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from nerif.model import SimpleChatModel, SimpleEmbeddingModel


class TestNerifAgent(unittest.TestCase):
@classmethod
def setUpClass(cls):
warnings.filterwarnings("ignore")

def test_ollama_agent(self):
ollama_agent = SimpleChatModel(model="ollama/llama3.1")
result = ollama_agent.chat("Hello, how are you?")
self.assertIsNotNone(result)
self.assertIsInstance(result, str)
self.assertGreater(len(result), 0)

def test_ollama_embed_agent(self):
ollama_agent = SimpleEmbeddingModel(model="ollama/mxbai-embed-large")
result = ollama_agent.embed("Hello, how are you?")
self.assertIsNotNone(result)
self.assertIsInstance(result, list[float])
self.assertGreater(len(result), 0)


if __name__ == "__main__":
unittest.main()
27 changes: 8 additions & 19 deletions test/nerif_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,22 @@

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from nerif.core import core, nerif_match, nerif_match_string
from nerif.core import nerif, nerif_match_string


class MyTestCase(unittest.TestCase):
def test_judge(self):
judge = core("the sky is blue")
judge = nerif("the sky is blue")
self.assertEqual(True, judge) # add assertion here
judge = core("Do you know who I am?")
judge = nerif("Do you know who I am?")
self.assertEqual(False, judge)

def test_match(self):
test_selection = {
"1": "the sky is blue",
"2": "the sky is green",
"3": "the sky is red",
}
match_result = nerif_match_string("the truth", test_selection)

self.assertEqual("1", match_result)
test_selection = {
"reboot": "restart the server, it may takes a few minutes",
"adduser": "add a new user on my server",
"reservation": "make a reservation for the server",
}
match_result = nerif_match("I wanna use the server for AI training tonight", test_selection)

self.assertEqual("reservation", match_result)
selections = ["iPhone 5", "iPhone 6", "iPhone 12"]

best_choice = nerif_match_string(selections=selections, text="Which iPhone is the most powerful one?")

self.assertEqual(2, best_choice)


if __name__ == "__main__":
Expand Down
5 changes: 3 additions & 2 deletions test/nerif_token_counter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

from litellm import completion, embedding

from nerif.core import nerif
from nerif.model import NerifTokenCounter, SimpleChatModel, SimpleEmbeddingModel
from nerif.core import Nerif, nerif
from nerif.model import SimpleChatModel, SimpleEmbeddingModel
from nerif.utils import NerifTokenCounter

pretty_printer = pprint.PrettyPrinter()

Expand Down
Loading