Spaces:
Sleeping
Sleeping
Commit
Β·
8099e26
1
Parent(s):
e0a63a7
Fix OPIK configuration: make COMET_PROJECT optional and add OPIK init to main app
Browse files- app.py +8 -0
- src/second_brain_online/opik_utils.py +44 -53
app.py
CHANGED
|
@@ -15,6 +15,7 @@ sys.path.append('src')
|
|
| 15 |
|
| 16 |
from second_brain_online.application.agents import get_agent
|
| 17 |
from second_brain_online.application.ui import CustomGradioUI
|
|
|
|
| 18 |
|
| 19 |
def main():
|
| 20 |
"""Main function for Hugging Face Space deployment."""
|
|
@@ -31,6 +32,13 @@ def main():
|
|
| 31 |
print(f" OPENAI_API_KEY: {'SET' if os.getenv('OPENAI_API_KEY') else 'NOT SET'}")
|
| 32 |
print(f" MONGODB_URI: {'SET' if os.getenv('MONGODB_URI') else 'NOT SET'}")
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
# Initialize agent
|
| 36 |
agent = get_agent(retriever_config_path=Path(retriever_config_path))
|
|
|
|
| 15 |
|
| 16 |
from second_brain_online.application.agents import get_agent
|
| 17 |
from second_brain_online.application.ui import CustomGradioUI
|
| 18 |
+
from second_brain_online import opik_utils
|
| 19 |
|
| 20 |
def main():
|
| 21 |
"""Main function for Hugging Face Space deployment."""
|
|
|
|
| 32 |
print(f" OPENAI_API_KEY: {'SET' if os.getenv('OPENAI_API_KEY') else 'NOT SET'}")
|
| 33 |
print(f" MONGODB_URI: {'SET' if os.getenv('MONGODB_URI') else 'NOT SET'}")
|
| 34 |
|
| 35 |
+
# Configure OPIK (optional - will use default project if COMET_PROJECT not set)
|
| 36 |
+
try:
|
| 37 |
+
opik_utils.configure()
|
| 38 |
+
print("β
OPIK configured successfully")
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"β οΈ OPIK configuration failed: {e}")
|
| 41 |
+
|
| 42 |
try:
|
| 43 |
# Initialize agent
|
| 44 |
agent = get_agent(retriever_config_path=Path(retriever_config_path))
|
src/second_brain_online/opik_utils.py
CHANGED
|
@@ -1,60 +1,51 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
Direct implementation for HF Space deployment.
|
| 6 |
"""
|
| 7 |
|
| 8 |
import os
|
| 9 |
-
import
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
try:
|
| 43 |
-
# Initialize agent
|
| 44 |
-
agent = get_agent(retriever_config_path=Path(retriever_config_path))
|
| 45 |
-
|
| 46 |
-
# Get the actual agent from the wrapper
|
| 47 |
-
actual_agent = agent._AgentWrapper__agent
|
| 48 |
-
|
| 49 |
-
# Launch custom UI
|
| 50 |
-
CustomGradioUI(actual_agent).launch(
|
| 51 |
-
server_name="0.0.0.0",
|
| 52 |
-
server_port=7860,
|
| 53 |
-
share=False
|
| 54 |
)
|
| 55 |
-
except Exception as e:
|
| 56 |
-
print(f"β Error starting the application: {e}")
|
| 57 |
-
raise
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
OPIK utilities for Second Brain AI Assistant.
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import os
|
| 7 |
+
from loguru import logger
|
| 8 |
+
import opik
|
| 9 |
+
from opik import OpikConfigurator
|
| 10 |
+
|
| 11 |
+
from second_brain_online.config import settings
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def configure() -> None:
|
| 15 |
+
if settings.COMET_API_KEY:
|
| 16 |
+
try:
|
| 17 |
+
client = OpikConfigurator(api_key=settings.COMET_API_KEY)
|
| 18 |
+
default_workspace = client._get_default_workspace()
|
| 19 |
+
except Exception:
|
| 20 |
+
logger.warning(
|
| 21 |
+
"Default workspace not found. Setting workspace to None and enabling interactive mode."
|
| 22 |
+
)
|
| 23 |
+
default_workspace = None
|
| 24 |
+
|
| 25 |
+
os.environ["OPIK_PROJECT_NAME"] = settings.COMET_PROJECT or "second_brain_course"
|
| 26 |
+
|
| 27 |
+
opik.configure(
|
| 28 |
+
api_key=settings.COMET_API_KEY,
|
| 29 |
+
workspace=default_workspace,
|
| 30 |
+
use_local=False,
|
| 31 |
+
force=True,
|
| 32 |
+
)
|
| 33 |
+
logger.info(
|
| 34 |
+
f"Opik configured successfully using workspace '{default_workspace}'"
|
| 35 |
+
)
|
| 36 |
+
else:
|
| 37 |
+
logger.warning(
|
| 38 |
+
"COMET_API_KEY is not set. Set it to enable prompt monitoring with Opik (powered by Comet ML)."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
|
| 42 |
+
def get_or_create_dataset(name: str, prompts: list[str]) -> opik.Dataset | None:
|
| 43 |
+
client = opik.Opik()
|
| 44 |
+
try:
|
| 45 |
+
dataset = client.get_dataset(name)
|
| 46 |
+
if dataset is None:
|
| 47 |
+
dataset = client.create_dataset(name, prompts)
|
| 48 |
+
return dataset
|
| 49 |
+
except Exception as e:
|
| 50 |
+
logger.error(f"Error getting or creating dataset: {e}")
|
| 51 |
+
return None
|