Skip to content
Open
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
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Python
__pycache__/
*.py[cod]
*.so
.Python
build/
dist/
*.egg-info/
.pytest_cache/
.coverage
htmlcov/

# Environments
.env
.venv
env/
venv/

# OS specific
.DS_Store
Thumbs.db

# Editors
.idea/
.vscode/
*.swp
*~

# Project specific
.hypothesis/
1 change: 1 addition & 0 deletions src/ember/core/app_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def _initialize_api_keys_from_env(config_manager: ConfigManager) -> None:
"OPENAI_API_KEY": "openai",
"ANTHROPIC_API_KEY": "anthropic",
"GOOGLE_API_KEY": "google",
"HUGGINGFACE_API_KEY": "huggingface",
}

# Set API keys from environment if available
Expand Down
9 changes: 9 additions & 0 deletions src/ember/core/registry/model/base/registry/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
)
from ember.core.registry.model.providers.openai.openai_discovery import OpenAIDiscovery

from ember.core.registry.model.providers.huggingface.huggingface_discovery import (
HuggingFaceDiscovery,
)

logger: logging.Logger = logging.getLogger(__name__)
# Set default log level to WARNING to reduce verbosity
logger.setLevel(logging.WARNING)
Expand Down Expand Up @@ -80,6 +84,11 @@ def _initialize_providers(self) -> List[BaseDiscoveryProvider]:
"GOOGLE_API_KEY",
lambda: {"api_key": os.environ.get("GOOGLE_API_KEY", "")},
),
(
HuggingFaceDiscovery,
"HUGGINGFACE_API_KEY", #Now searches for Hugging Face API key
lambda: {"api_key": os.environ.get("HUGGINGFACE_API_KEY", "")},
),
]

# Initializing providers with available credentials
Expand Down
162 changes: 162 additions & 0 deletions src/ember/core/registry/model/examples/mistral_7b_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
"""Mistral-7B-Instruct-v0.2 usage example using Hugging Face Inference API.

This module demonstrates how to use the Mistral-7B-Instruct-v0.2 model
through the Ember framework, showcasing instruction following capabilities
and chat-oriented generation.
"""

import logging
import os
from typing import Any, Dict, Optional, Union, cast

from ember.core.registry.model.base.schemas.chat_schemas import ChatResponse
from ember.core.registry.model.base.schemas.model_info import ModelInfo, ProviderInfo
from ember.core.registry.model.base.services.model_service import ModelService
from ember.core.registry.model.initialization import initialize_registry
from ember.core.registry.model.base.schemas.cost import ModelCost, RateLimit

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def get_model_service() -> ModelService:
"""Get the model service with Mistral-7B-Instruct-v0.2 registered.

Returns:
A ModelService instance with Mistral-7B-Instruct-v0.2 registered
"""
# Initialize the registry
registry = initialize_registry(auto_discover=True)
service = ModelService(registry=registry)

# Register Mistral-7B-Instruct-v0.2 if not already registered
try:
# Try to get the model - will raise an exception if not found
service.get_model("huggingface:mistralai/Mistral-7B-Instruct-v0.2")
logger.info("Mistral model already registered")
except Exception:
# Model not found, register it
logger.info("Registering Mistral model")

# Get API key from environment variable or use a default for testing
api_key = os.environ.get("HUGGINGFACE_API_KEY", "your_api_key_here")

# Create model info
model_info = ModelInfo(
id="huggingface:mistralai/Mistral-7B-Instruct-v0.2",
name="mistralai/Mistral-7B-Instruct-v0.2",
provider=ProviderInfo(name="HuggingFace", default_api_key=api_key),
cost=ModelCost(
input_cost_per_thousand=0.0,
output_cost_per_thousand=0.0,
),
)

# Register the model
registry.register_model(model_info)

return service


def basic_instruction_example(service: ModelService) -> None:
"""Basic instruction following with Mistral-7B-Instruct-v0.2.

This example shows how to provide instructions and get responses.

Args:
service: The model service with Mistral-7B-Instruct-v0.2 registered
"""
try:
# Get the model
mistral_model = service.get_model("huggingface:mistralai/Mistral-7B-Instruct-v0.2")

# Generate a response to an instruction
response = mistral_model(
"Explain what artificial intelligence is to a 10-year old child.",
temperature=0.7,
max_tokens=150
)

print("\n=== Basic Instruction ===")
print(f"Response: {response.data}")
print(f"Used {response.usage.total_tokens} tokens")

except Exception as error:
logger.exception("Error during basic instruction: %s", error)


def creative_writing_example(service: ModelService) -> None:
"""Creative writing example with Mistral-7B-Instruct-v0.2.

This example demonstrates using the model for creative writing tasks.

Args:
service: The model service with Mistral-7B-Instruct-v0.2 registered
"""
try:
# Get the model
mistral_model = service.get_model("huggingface:mistralai/Mistral-7B-Instruct-v0.2")

# Create a short story prompt
response = mistral_model(
"Write a short story about a robot discovering emotions for the first time. Make it touching and meaningful.",
temperature=0.8, # Higher temperature for more creativity
max_tokens=300
)

print("\n=== Creative Writing ===")
print(f"Response: {response.data}")
print(f"Used {response.usage.total_tokens} tokens")

except Exception as error:
logger.exception("Error during creative writing: %s", error)


def factual_qa_example(service: ModelService) -> None:
"""Factual Q&A example with Mistral-7B-Instruct-v0.2.

This example tests the model's ability to answer factual questions.

Args:
service: The model service with Mistral-7B-Instruct-v0.2 registered
"""
try:
# Get the model
mistral_model = service.get_model("huggingface:mistralai/Mistral-7B-Instruct-v0.2")

# Ask a factual question
response = mistral_model(
"What are the main components of the solar system? List the planets in order from the sun.",
temperature=0.3, # Lower temperature for more factual responses
max_tokens=200
)

print("\n=== Factual Q&A ===")
print(f"Response: {response.data}")
print(f"Used {response.usage.total_tokens} tokens")

except Exception as error:
logger.exception("Error during factual Q&A: %s", error)


def main() -> None:
"""Run all Mistral-7B-Instruct-v0.2 examples."""
print("Mistral-7B-Instruct-v0.2 Usage Examples")
print("========================================")

try:
# Get the model service with Mistral registered
service = get_model_service()

# Run examples
basic_instruction_example(service)
creative_writing_example(service)
factual_qa_example(service)

except Exception as error:
logger.exception("Error during examples: %s", error)


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#TODO Just a placeholder implementation, need to implement discovery by author later
"""Discovery mechanism for Hugging Face models.

This module provides functionality to discover and register Hugging Face models
available through the Hugging Face Hub.
"""

import logging
from typing import Dict, List, Optional, Set

from ember.core.registry.model.base.discovery.model_discovery import ModelDiscoveryBase
from ember.core.registry.model.base.schemas.model_info import ModelInfo, ProviderInfo

logger = logging.getLogger(__name__)


class HuggingFaceDiscovery(ModelDiscoveryBase):
"""Discovery implementation for Hugging Face models.

This class provides methods to discover models available through the
Hugging Face Hub and register them with the Ember model registry.
"""

PROVIDER_NAME = "HuggingFace"

def discover_models(self) -> List[ModelInfo]:
"""Discover available Hugging Face models.

Returns:
List[ModelInfo]: A list of model information objects for discovered models.
"""
logger.info("Discovering Hugging Face models...")

# This is a simplified implementation
# In a real implementation, you might query the Hugging Face API
# to get a list of popular or featured models

# For now, just return a predefined list of popular models
models = [
# Prioritize the Mistral Instruct model
self._create_model_info("mistralai/Mistral-7B-Instruct-v0.2"),
self._create_model_info("meta-llama/Llama-2-7b-chat-hf"),
# Keep the base model for comparison
self._create_model_info("mistralai/Mistral-7B-v0.3"),
self._create_model_info("google/gemma-7b-it"),
]

logger.info("Discovered %d Hugging Face models", len(models))
return models

def _create_model_info(self, model_name: str) -> ModelInfo:
"""Create model information for a Hugging Face model.

Args:
model_name: The name of the model on the Hugging Face Hub.

Returns:
ModelInfo: The model information object.
"""
return ModelInfo(
id=f"huggingface:{model_name}",
name=model_name,
provider=ProviderInfo(
name=self.PROVIDER_NAME,
# API key will be filled in from environment or config
default_api_key=None,
),
)
Loading