Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anthropic beta #497

Merged
merged 5 commits into from
Nov 10, 2024
Merged
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
177 changes: 101 additions & 76 deletions agentops/llms/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"""Handle responses for Anthropic"""
from anthropic import Stream, AsyncStream
from anthropic.resources import AsyncMessages
import anthropic.resources.beta.messages.messages as beta_messages

Check warning on line 32 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L32

Added line #L32 was not covered by tests
from anthropic.types import Message

llm_event = LLMEvent(init_timestamp=init_timestamp, params=kwargs)
Expand Down Expand Up @@ -155,6 +156,7 @@

def _override_completion(self):
from anthropic.resources import messages
import anthropic.resources.beta.messages.messages as beta_messages

Check warning on line 159 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L159

Added line #L159 was not covered by tests
from anthropic.types import (
Message,
RawContentBlockDeltaEvent,
Expand All @@ -167,54 +169,64 @@

# Store the original method
self.original_create = messages.Messages.create
self.original_create_beta = beta_messages.Messages.create

Check warning on line 172 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L172

Added line #L172 was not covered by tests

def patched_function(*args, **kwargs):
init_timestamp = get_ISO_time()
session = kwargs.get("session", None)
if "session" in kwargs.keys():
del kwargs["session"]
def create_patched_function(is_beta=False):
def patched_function(*args, **kwargs):
init_timestamp = get_ISO_time()
session = kwargs.get("session", None)

Check warning on line 177 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L174-L177

Added lines #L174 - L177 were not covered by tests
if "session" in kwargs.keys():
del kwargs["session"]

Check warning on line 179 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L179

Added line #L179 was not covered by tests

completion_override = fetch_completion_override_from_time_travel_cache(
kwargs
)
if completion_override:
result_model = None
pydantic_models = (
Message,
RawContentBlockDeltaEvent,
RawContentBlockStartEvent,
RawContentBlockStopEvent,
RawMessageDeltaEvent,
RawMessageStartEvent,
RawMessageStopEvent,
completion_override = fetch_completion_override_from_time_travel_cache(

Check warning on line 181 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L181

Added line #L181 was not covered by tests
kwargs
)
if completion_override:
result_model = None
pydantic_models = (

Check warning on line 186 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L185-L186

Added lines #L185 - L186 were not covered by tests
Message,
RawContentBlockDeltaEvent,
RawContentBlockStartEvent,
RawContentBlockStopEvent,
RawMessageDeltaEvent,
RawMessageStartEvent,
RawMessageStopEvent,
)

for pydantic_model in pydantic_models:
try:
result_model = pydantic_model.model_validate_json(
completion_override
for pydantic_model in pydantic_models:
try:
result_model = pydantic_model.model_validate_json(

Check warning on line 198 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L197-L198

Added lines #L197 - L198 were not covered by tests
completion_override
)
break
except Exception as e:
pass

Check warning on line 203 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L201-L203

Added lines #L201 - L203 were not covered by tests

if result_model is None:
logger.error(

Check warning on line 206 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L206

Added line #L206 was not covered by tests
f"Time Travel: Pydantic validation failed for {pydantic_models} \n"
f"Time Travel: Completion override was:\n"
f"{pprint.pformat(completion_override)}"
)
break
except Exception as e:
pass

if result_model is None:
logger.error(
f"Time Travel: Pydantic validation failed for {pydantic_models} \n"
f"Time Travel: Completion override was:\n"
f"{pprint.pformat(completion_override)}"
return None
return self.handle_response(

Check warning on line 212 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L211-L212

Added lines #L211 - L212 were not covered by tests
result_model, kwargs, init_timestamp, session=session
)
return None

# Call the original function with its original arguments
original_func = (

Check warning on line 217 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L217

Added line #L217 was not covered by tests
self.original_create_beta if is_beta else self.original_create
)
result = original_func(*args, **kwargs)

Check warning on line 220 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L220

Added line #L220 was not covered by tests
return self.handle_response(
result_model, kwargs, init_timestamp, session=session
result, kwargs, init_timestamp, session=session
)

# Call the original function with its original arguments
result = self.original_create(*args, **kwargs)
return self.handle_response(result, kwargs, init_timestamp, session=session)
return patched_function

Check warning on line 225 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L225

Added line #L225 was not covered by tests

# Override the original method with the patched one
messages.Messages.create = patched_function
# Override the original methods with the patched ones
messages.Messages.create = create_patched_function(is_beta=False)
beta_messages.Messages.create = create_patched_function(is_beta=True)

Check warning on line 229 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L228-L229

Added lines #L228 - L229 were not covered by tests

def _override_async_completion(self):
from anthropic.resources import messages
Expand All @@ -227,58 +239,71 @@
RawMessageStartEvent,
RawMessageStopEvent,
)
import anthropic.resources.beta.messages.messages as beta_messages

Check warning on line 242 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L242

Added line #L242 was not covered by tests

# Store the original method
self.original_create_async = messages.AsyncMessages.create
self.original_create_async_beta = beta_messages.AsyncMessages.create

Check warning on line 246 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L246

Added line #L246 was not covered by tests

async def patched_function(*args, **kwargs):
# Call the original function with its original arguments
init_timestamp = get_ISO_time()
session = kwargs.get("session", None)
if "session" in kwargs.keys():
del kwargs["session"]
def create_patched_async_function(is_beta=False):
async def patched_function(*args, **kwargs):
init_timestamp = get_ISO_time()
session = kwargs.get("session", None)

Check warning on line 251 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L248-L251

Added lines #L248 - L251 were not covered by tests
if "session" in kwargs.keys():
del kwargs["session"]

Check warning on line 253 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L253

Added line #L253 was not covered by tests

completion_override = fetch_completion_override_from_time_travel_cache(
kwargs
)
if completion_override:
result_model = None
pydantic_models = (
Message,
RawContentBlockDeltaEvent,
RawContentBlockStartEvent,
RawContentBlockStopEvent,
RawMessageDeltaEvent,
RawMessageStartEvent,
RawMessageStopEvent,
completion_override = fetch_completion_override_from_time_travel_cache(

Check warning on line 255 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L255

Added line #L255 was not covered by tests
kwargs
)
if completion_override:
result_model = None
pydantic_models = (

Check warning on line 260 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L259-L260

Added lines #L259 - L260 were not covered by tests
Message,
RawContentBlockDeltaEvent,
RawContentBlockStartEvent,
RawContentBlockStopEvent,
RawMessageDeltaEvent,
RawMessageStartEvent,
RawMessageStopEvent,
)

for pydantic_model in pydantic_models:
try:
result_model = pydantic_model.model_validate_json(
completion_override
for pydantic_model in pydantic_models:
try:
result_model = pydantic_model.model_validate_json(

Check warning on line 272 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L271-L272

Added lines #L271 - L272 were not covered by tests
completion_override
)
break
except Exception as e:
pass

Check warning on line 277 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L275-L277

Added lines #L275 - L277 were not covered by tests

if result_model is None:
logger.error(

Check warning on line 280 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L280

Added line #L280 was not covered by tests
f"Time Travel: Pydantic validation failed for {pydantic_models} \n"
f"Time Travel: Completion override was:\n"
f"{pprint.pformat(completion_override)}"
)
break
except Exception as e:
pass

if result_model is None:
logger.error(
f"Time Travel: Pydantic validation failed for {pydantic_models} \n"
f"Time Travel: Completion override was:\n"
f"{pprint.pformat(completion_override)}"
return None

Check warning on line 285 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L285

Added line #L285 was not covered by tests

return self.handle_response(

Check warning on line 287 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L287

Added line #L287 was not covered by tests
result_model, kwargs, init_timestamp, session=session
)
return None

# Call the original function with its original arguments
original_func = (

Check warning on line 292 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L292

Added line #L292 was not covered by tests
self.original_create_async_beta
if is_beta
else self.original_create_async
)
result = await original_func(*args, **kwargs)

Check warning on line 297 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L297

Added line #L297 was not covered by tests
return self.handle_response(
result_model, kwargs, init_timestamp, session=session
result, kwargs, init_timestamp, session=session
)

result = await self.original_create_async(*args, **kwargs)
return self.handle_response(result, kwargs, init_timestamp, session=session)
return patched_function

Check warning on line 302 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L302

Added line #L302 was not covered by tests

# Override the original method with the patched one
messages.AsyncMessages.create = patched_function
# Override the original methods with the patched ones
areibman marked this conversation as resolved.
Show resolved Hide resolved
messages.AsyncMessages.create = create_patched_async_function(is_beta=False)
beta_messages.AsyncMessages.create = create_patched_async_function(is_beta=True)

Check warning on line 306 in agentops/llms/anthropic.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/anthropic.py#L305-L306

Added lines #L305 - L306 were not covered by tests

def undo_override(self):
if self.original_create is not None and self.original_create_async is not None:
Expand Down
Loading
Loading