-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from Deeptechia/main
Geppetto v0.2.4
- Loading branch information
Showing
14 changed files
with
232 additions
and
58 deletions.
There are no files selected for viewing
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
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,85 @@ | ||
import re | ||
import os | ||
import logging | ||
|
||
from .llm_api_handler import LLMHandler | ||
from anthropic import Anthropic | ||
from dotenv import load_dotenv | ||
from typing import List | ||
from typing import Dict | ||
|
||
load_dotenv(os.path.join("config", ".env")) | ||
|
||
ANTHROPIC_API_KEY = os.getenv("CLAUDE_API_KEY") | ||
CLAUDE_MODEL = os.getenv("CLAUDE_MODEL") | ||
|
||
VERSION = os.getenv("GEPPETTO_VERSION") | ||
|
||
def convert_claude_to_slack(text): | ||
""" | ||
Converts Claude markdown format to Slack markdown format. | ||
This function handles: | ||
change to claude format | ||
Args: | ||
text (str): The Claude markdown text to be converted. | ||
Returns: | ||
str: The markdown text formatted for Slack. | ||
""" | ||
if not isinstance(text, str): | ||
raise ValueError("Input must be a string.") | ||
|
||
formatted_text = text.replace("* ", "- ") | ||
formatted_text = formatted_text.replace("**", "*") | ||
formatted_text = formatted_text.replace("__", "_") | ||
formatted_text = formatted_text.replace("- ", "• ") | ||
formatted_text = re.sub(r"\[(.*?)\]\((.*?)\)", r"<\2|\1>", formatted_text) | ||
|
||
formatted_text += f"\n\n_(Geppetto v{VERSION} Source: Claude Model {CLAUDE_MODEL})_" | ||
|
||
return formatted_text | ||
|
||
|
||
class ClaudeHandler(LLMHandler): | ||
|
||
def __init__( | ||
self, | ||
personality, | ||
): | ||
super().__init__( | ||
'Claude', | ||
CLAUDE_MODEL, | ||
Anthropic(api_key=ANTHROPIC_API_KEY) | ||
) | ||
self.claude_model = CLAUDE_MODEL | ||
self.personality = personality | ||
self.system_role = "system" | ||
self.assistant_role = "assistant" | ||
self.user_role = "user" | ||
self.MAX_TOKENS = 1024 | ||
|
||
def llm_generate_content(self, user_prompt: List[Dict], status_callback=None, *status_callback_args): | ||
logging.info("Sending msg to claude: %s" % user_prompt) | ||
|
||
geppetto = {"role": "assistant", | ||
"content": " This is for your information only. Do not write this in your answer. Your name is Geppetto, a bot developed by DeepTechia. Answer only in the language the user spoke or asked you to do."} | ||
|
||
try: | ||
user_prompt.append(geppetto) | ||
response = self.client.messages.create( | ||
model = self.model, | ||
max_tokens = self.MAX_TOKENS, | ||
messages = user_prompt, | ||
) | ||
|
||
markdown_response = convert_claude_to_slack(str(response.content[0].text)) | ||
return markdown_response | ||
|
||
except Exception as e: | ||
logging.error(f"Error generating content: {e}") | ||
return "I'm sorry, I couldn't generate a response at this time. Try using another AI model." | ||
|
||
|
||
|
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
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 |
---|---|---|
|
@@ -14,7 +14,8 @@ authors = [ | |
"David Weil <[email protected]>", | ||
"Diego Kelyacoubian <[email protected]>", | ||
"Sebastian Wain <[email protected]>", | ||
"Carlos Sims <[email protected]>" | ||
"Carlos Sims <[email protected]>", | ||
"Camila Gallo Garcia <[email protected]>" | ||
] | ||
description = "Geppetto is a sophisticated Slack bot that facilitates seamless interaction with multiple AI models, including OpenAI's ChatGPT-4, DALL-E-3, and Google's Gemini model." | ||
readme = "README.md" | ||
|
@@ -37,6 +38,7 @@ Pillow = "^10.1.0" | |
google-generativeai = "^0.7.1" | ||
IPython = "^8.0.0" | ||
unittest-xml-reporting = "^3.2.0" | ||
anthropic = "^0.32.0" | ||
|
||
[tool.poetry.scripts] | ||
geppetto = "geppetto.main:main" | ||
|
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 |
---|---|---|
|
@@ -9,3 +9,4 @@ unittest-xml-reporting>=3.2.0 | |
pytest>=8.2.0 | ||
pytest-cov>=5.0.0 | ||
flake8>=7.0.0 | ||
anthropic>=0.32.0 |
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,17 @@ | ||
import logging | ||
import unittest | ||
|
||
|
||
class TestBase(unittest.TestCase): | ||
def setUp(self): | ||
logging.getLogger().setLevel(logging.CRITICAL) | ||
|
||
|
||
def OF(**kw): | ||
class OF: | ||
pass | ||
|
||
instance = OF() | ||
for k, v in kw.items(): | ||
setattr(instance, k, v) | ||
return instance |
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,59 @@ | ||
import os | ||
import sys | ||
import unittest | ||
from unittest.mock import Mock, patch | ||
|
||
from tests import TestBase | ||
|
||
script_dir = os.path.dirname(os.path.abspath(__file__)) | ||
parent_dir = os.path.dirname(script_dir) | ||
sys.path.append(parent_dir) | ||
|
||
from geppetto.claude_handler import ClaudeHandler | ||
import logging | ||
TEST_PERSONALITY = "Your AI assistant" | ||
|
||
|
||
class TestClaude(TestBase): | ||
@classmethod | ||
def setUpClass(cls): | ||
cls.patcher = patch("geppetto.claude_handler.Anthropic") | ||
cls.mock_claude = cls.patcher.start() | ||
cls.claude_handler = ClaudeHandler(personality=TEST_PERSONALITY) | ||
logging.getLogger().setLevel(logging.CRITICAL) | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
cls.patcher.stop() | ||
|
||
def test_personality(self): | ||
self.assertEqual(self.claude_handler.personality, TEST_PERSONALITY) | ||
|
||
def test_llm_generate_content(self): | ||
user_prompt = [{"role":"user", "content": "Hello, Claude!"}] | ||
|
||
mock_response = Mock() | ||
mock_response.content = [Mock(text="Mocked Claude response")] | ||
self.claude_handler.client.messages.create = Mock(return_value=mock_response) | ||
|
||
response = self.claude_handler.llm_generate_content(user_prompt).split('\n\n_(Geppetto', 1)[0].strip() | ||
|
||
self.assertEqual(response, "Mocked Claude response") | ||
|
||
|
||
def test_failed_to_llm_generate_content(self): | ||
|
||
failed_response = "I'm sorry, I couldn't generate a response at this time. Try using another AI model." | ||
|
||
mock_claude = Mock() | ||
mock_claude.content = [Mock(text=failed_response)] | ||
mock_claude.return_value = mock_claude | ||
|
||
self.claude_handler.client.messages.create = mock_claude | ||
response = self.claude_handler.llm_generate_content("") | ||
|
||
self.assertEqual(response, failed_response) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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
Oops, something went wrong.