Skip to content

Commit

Permalink
Added support for image uploads and modularized the application
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-zuev committed Aug 22, 2024
1 parent 91c62bb commit a333b74
Show file tree
Hide file tree
Showing 19 changed files with 344 additions and 111 deletions.
5 changes: 4 additions & 1 deletion .chainlit/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ auto_tag_thread = true
# Authorize users to spontaneously upload files with messages
[features.spontaneous_file_upload]
enabled = true
accept = ["*/*"]
accept = ["image/jpeg", "image/png", "image/gif", "image/webp"] # Aligned with Anthropic docs
max_files = 20
max_size_mb = 100 #5Mb x 20 files == 100Mb which is aligned with Anthropic documentation.

Expand All @@ -55,6 +55,9 @@ edit_message = true
# Name of the assistant.
name = "Claude Assistant"

# Disable the creation of the chainlit.md file (default: true)
create_chainlit_md = false

# Description of the assistant. This is used for HTML tags.
# description = ""

Expand Down
12 changes: 7 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -686,10 +686,12 @@ cython_debug/

# Local chainlit translations
/.chainlit/translations

# Local testing files
/local_tests/
/.files
chainlit.md
/new_features
/tutorials

# Local files
/local_folders/local_tests/
/local_folders/new_features
/local_folders/tutorials
/local_folders
/local_folders/todo.md
File renamed without changes.
44 changes: 8 additions & 36 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import chainlit as cl
from anthropic_service.claude_assistant import ClaudeAssistant
from dotenv import load_dotenv

load_dotenv()
from src.config import load_config
from src.logger import get_logger
from src.handlers.message_handler import handle_message
from src.assistants.claude_assistant import ClaudeAssistant

config = load_config()
logger = get_logger(__name__)
anthropic_service = None


@cl.on_chat_start
async def start():
global anthropic_service
anthropic_service = ClaudeAssistant()
logger.info("Chat started, ClaudeAssistant initialized")

welcome_message = (
"👋 Welcome to the Claude 3.5 Sonnet with prompt caching enabled.\n"
Expand All @@ -19,37 +21,7 @@ async def start():
)
await cl.Message(content=welcome_message).send()


@cl.on_message
async def main(message: cl.Message):
global anthropic_service

try:
response_message = cl.Message(content="")
async for item in anthropic_service.generate_response(message.content):
if item["type"] == "chunk":
await response_message.stream_token(item["content"])
elif item["type"] == "tool_use":
# Create a step for tool use
with cl.Step(name=f"Using tool: {item['name']}") as step:
step.input = item['input']
step.output = f"URLs parsed: {item['result']['urls_parsed']}"
await step.send()
elif item["type"] == "final":
await response_message.send()

metrics = item["metrics"]
metrics_message = (
"📊 *Performance Metrics*:\n"
f"⏱️ - Time taken: {metrics['time_taken']:.2f} seconds\n"
f"📥 - User input tokens: {metrics['input_tokens']}\n"
f"📤 - Output tokens: {metrics['output_tokens']}\n"
f"💾 - Input tokens (cache read): {metrics['input_tokens_cache_read']}\n"
f"🆕 - Input tokens (cache create): {metrics['input_tokens_cache_create']}\n"
f"📈 - {metrics['percentage_cached']:.1f}% of input prompt cached ({metrics['total_input_tokens']} tokens)"
)
await cl.Message(content=metrics_message).send()

except Exception as e:
error_message = f"An error occurred: {str(e)}"
await cl.Message(content=error_message).send()
await handle_message(message, anthropic_service)
99 changes: 98 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ python-dotenv = "^1.0.1"
langchain = "^0.2.12"
firecrawl = "^0.0.20"
slugify = "^0.0.1"
pillow = "^10.4.0"


[build-system]
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit a333b74

Please sign in to comment.