Skip to content

Commit

Permalink
feat: Enhance Voyage AI provider with async support and improved erro…
Browse files Browse the repository at this point in the history
…r handling

- Add async embedding support
- Improve error handling and logging
- Add token counting from usage data
- Enhance type hints
- Maintain Python version warning

Fixes #461

Co-Authored-By: Alex Reibman <meta.alex.r@gmail.com>
  • Loading branch information
devin-ai-integration[bot] and areibman committed Dec 12, 2024
1 parent bd25d3b commit 2f6a5f6
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions agentops/llms/providers/voyage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import json
from typing import Optional, Callable
import pprint
from typing import Optional, Callable, Dict, Any

from agentops.llms.providers.instrumented_provider import InstrumentedProvider
from agentops.event import LLMEvent, ErrorEvent
Expand Down Expand Up @@ -44,8 +45,8 @@ def _check_python_version(self) -> bool:
return sys.version_info >= (3, 9)

def handle_response(
self, response: dict, kwargs: dict, init_timestamp: str, session: Optional[Session] = None
) -> dict:
self, response: Dict[str, Any], kwargs: Dict[str, Any], init_timestamp: str, session: Optional[Session] = None
) -> Dict[str, Any]:
"""Handle responses for Voyage AI embeddings.
Args:
Expand All @@ -66,33 +67,54 @@ def handle_response(
llm_event.model = kwargs.get("model")
llm_event.prompt = kwargs.get("input")
llm_event.agent_id = check_call_stack_for_agent_id()

Check warning on line 69 in agentops/llms/providers/voyage.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/providers/voyage.py#L65-L69

Added lines #L65 - L69 were not covered by tests
llm_event.end_timestamp = get_ISO_time()

# Extract token counts if available
if usage := response.get("usage"):
llm_event.prompt_tokens = usage.get("prompt_tokens")
llm_event.completion_tokens = usage.get("completion_tokens")

Check warning on line 74 in agentops/llms/providers/voyage.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/providers/voyage.py#L73-L74

Added lines #L73 - L74 were not covered by tests

llm_event.end_timestamp = get_ISO_time()
self._safe_record(session, llm_event)
except Exception as e:
self._safe_record(session, ErrorEvent(trigger_event=llm_event, exception=e))
logger.warning("Unable to parse response for Voyage call. Skipping upload to AgentOps\n")
kwargs_str = pprint.pformat(kwargs)
response_str = pprint.pformat(response)
logger.warning(

Check warning on line 82 in agentops/llms/providers/voyage.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/providers/voyage.py#L76-L82

Added lines #L76 - L82 were not covered by tests
f"Unable to parse response for Voyage call. Skipping upload to AgentOps\n"
f"response:\n {response_str}\n"
f"kwargs:\n {kwargs_str}\n"
)

return response

Check warning on line 88 in agentops/llms/providers/voyage.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/providers/voyage.py#L88

Added line #L88 was not covered by tests

def override(self):
"""Override Voyage AI SDK methods with instrumented versions."""
import voyageai

Check warning on line 92 in agentops/llms/providers/voyage.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/providers/voyage.py#L92

Added line #L92 was not covered by tests

# Store original methods
self.original_embed = voyageai.Client.embed
self.original_embed_async = voyageai.Client.aembed

Check warning on line 96 in agentops/llms/providers/voyage.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/providers/voyage.py#L95-L96

Added lines #L95 - L96 were not covered by tests

def patched_function(self, *args, **kwargs):
def patched_embed(self, *args, **kwargs):
init_timestamp = get_ISO_time()
session = kwargs.pop("session", None)

result = self.original_embed(*args, **kwargs)
return self.handle_response(result, kwargs, init_timestamp, session=session)

Check warning on line 102 in agentops/llms/providers/voyage.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/providers/voyage.py#L98-L102

Added lines #L98 - L102 were not covered by tests

voyageai.Client.embed = patched_function
async def patched_embed_async(self, *args, **kwargs):
init_timestamp = get_ISO_time()
session = kwargs.pop("session", None)
result = await self.original_embed_async(*args, **kwargs)
return self.handle_response(result, kwargs, init_timestamp, session=session)

Check warning on line 108 in agentops/llms/providers/voyage.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/providers/voyage.py#L104-L108

Added lines #L104 - L108 were not covered by tests

voyageai.Client.embed = patched_embed
voyageai.Client.aembed = patched_embed_async

Check warning on line 111 in agentops/llms/providers/voyage.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/providers/voyage.py#L110-L111

Added lines #L110 - L111 were not covered by tests

def undo_override(self):
"""Restore original Voyage AI SDK methods."""
import voyageai

Check warning on line 115 in agentops/llms/providers/voyage.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/providers/voyage.py#L115

Added line #L115 was not covered by tests

if self.original_embed:
voyageai.Client.embed = self.original_embed

Check warning on line 118 in agentops/llms/providers/voyage.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/providers/voyage.py#L118

Added line #L118 was not covered by tests
if self.original_embed_async:
voyageai.Client.aembed = self.original_embed_async

Check warning on line 120 in agentops/llms/providers/voyage.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/providers/voyage.py#L120

Added line #L120 was not covered by tests

0 comments on commit 2f6a5f6

Please sign in to comment.