chinmayjha commited on
Commit
8099e26
Β·
1 Parent(s): e0a63a7

Fix OPIK configuration: make COMET_PROJECT optional and add OPIK init to main app

Browse files
Files changed (2) hide show
  1. app.py +8 -0
  2. 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
- Hugging Face Space app for Second Brain AI Assistant.
4
-
5
- Direct implementation for HF Space deployment.
6
  """
7
 
8
  import os
9
- import sys
10
- from pathlib import Path
11
-
12
- # Add the current directory and src directory to Python path
13
- sys.path.append('.')
14
- 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
- from second_brain_online import opik_utils
19
-
20
- def main():
21
- """Main function for Hugging Face Space deployment."""
22
- # Set default values for HF Spaces
23
- retriever_config_path = os.getenv("RETRIEVER_CONFIG_PATH", "configs/compute_rag_vector_index_openai_contextual_simple.yaml")
24
-
25
- print("πŸš€ Starting Second Brain AI Assistant...")
26
- print(f"πŸ“ Using retriever config: {retriever_config_path}")
27
-
28
- # Debug: Check environment variables
29
- print("πŸ” Environment variables check:")
30
- print(f" COMET_API_KEY: {'SET' if os.getenv('COMET_API_KEY') else 'NOT SET'}")
31
- print(f" COMET_PROJECT: {os.getenv('COMET_PROJECT', 'NOT SET')}")
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))
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
- if __name__ == "__main__":
60
- main()
 
 
 
 
 
 
 
 
 
 
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