Skip to content

Commit

Permalink
fix: improve error handling and session management
Browse files Browse the repository at this point in the history
Co-Authored-By: Alex Reibman <meta.alex.r@gmail.com>
  • Loading branch information
devin-ai-integration[bot] and areibman committed Dec 13, 2024
1 parent f5b0b32 commit 5ee1751
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 102 deletions.
32 changes: 24 additions & 8 deletions examples/mistral/create_notebook.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import nbformat as nbf
import asyncio
import os
import nbformat as nbf
import sys
from pathlib import Path

# Add agentops to path if needed
if str(Path.home() / "repos/agentops") not in sys.path:
sys.path.append(str(Path.home() / "repos/agentops"))

from agentops.session import EndState # Import EndState at the top level
import agentops

nb = nbf.v4.new_notebook()

Expand Down Expand Up @@ -287,6 +295,7 @@ def analyze_costs(prompts):
),
nbf.v4.new_code_cell(
'''# Start a new analysis session
from agentops.session import EndState # Import EndState from correct module
session = agentops.start_session()
def comprehensive_analysis():
Expand All @@ -310,11 +319,18 @@ def comprehensive_analysis():
continue
with agent: # Use context manager for proper tracking
response = client.chat.complete(
model="mistral-small-latest",
messages=[{"role": "user", "content": prompt}]
)
results.append(response.choices[0].message.content)
try:
response = client.chat.complete(
model="mistral-small-latest",
messages=[{"role": "user", "content": prompt}]
)
results.append(response.choices[0].message.content)
except AttributeError:
print("Client not initialized. Using placeholder response.")
results.append("This is a placeholder response. Please set valid API keys.")
except Exception as e:
print(f"Error during completion: {str(e)}")
results.append(f"Error: {str(e)}")
# Analyze results
for i, (prompt, result) in enumerate(zip(prompts, results)):
Expand All @@ -334,7 +350,7 @@ def comprehensive_analysis():
# End the session with proper status
print("Ending AgentOps session...")
try:
session.end(agentops.EndState.COMPLETED)
session.end(EndState.COMPLETED) # Use imported EndState
except Exception as e:
print(f"Error ending session: {str(e)}")
print("Session ended successfully")'''
Expand Down
Loading

0 comments on commit 5ee1751

Please sign in to comment.