Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
acu192 committed Jun 14, 2024
1 parent 350e646 commit c897555
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@
pip install -U lasagna-ai[openai,anthropic]
```

## Quickstart

TODO

## Debug Logging

This library logs using Python's builtin `logging` module. It logs mostly to `INFO`, so here's a snippet of code you can put in _your_ app to see those traces:

```python
import logging

logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
)

# ... now use Lasagna as you normally would, but you'll see extra log traces!
```

## License

`lasagna-ai` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
Expand Down
2 changes: 1 addition & 1 deletion src/lasagna/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async def in_memory_save_record(key: CacheKey, record: CacheRecord) -> None:


async def _hash_agent_runs(model: Model, runs: List[AgentRun]) -> CacheKey:
model_name = model.__class__.__name__
model_name = model.__class__.__name__ # TODO: ideally, this would be model.config_hash()
seed = f"__version__{__version__}__model__{model_name}"
def _do() -> CacheKey:
return recursive_hash(seed, runs, hashlib.md5())
Expand Down
53 changes: 51 additions & 2 deletions tests/test_caching.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import pytest
import hashlib
import random
from typing import List

from lasagna.caching import (
in_memory_cached_agent,
_hash_agent_runs,
)

from lasagna.types import AgentRun
from lasagna.types import AgentRun, Model, EventCallback, Message

from lasagna.agent_util import bind_model

from lasagna import __version__

Expand All @@ -29,9 +32,55 @@
]


def _make_agent():
# We need this factory so we can make as many separately-cached agents as we want.
@bind_model(MockProvider, 'some_model')
@in_memory_cached_agent
async def my_agent(
model: Model,
event_callback: EventCallback,
prev_runs: List[AgentRun],
) -> AgentRun:
assert prev_runs == _PREV_RUNS
messages: List[Message] = []
new_messages = await model.run(event_callback, messages, [])
return {
'type': 'messages',
'messages': [
*new_messages,
{
'role': 'human',
'text': f"Here is a random value {random.random()}.",
},
],
}

return my_agent


@pytest.mark.asyncio
async def test_in_memory_cached_agent():
pass # TODO
"""
We test the *in-memory* cache just as a concrete way to test the
underlying cache decorator.
"""
async def callback(event):
pass

agent1 = _make_agent()
result1 = await agent1(callback, _PREV_RUNS)

agent2 = _make_agent()
result2 = await agent2(callback, _PREV_RUNS)

assert result1 != result2 # because of the use of random.random() in the final message, these will be different (in the *vast* likelihood)

for _ in range(5):
result1_again = await agent1(callback, _PREV_RUNS)
result2_again = await agent2(callback, _PREV_RUNS)

assert result1 == result1_again # because it was cached
assert result2 == result2_again # because it was cached


@pytest.mark.asyncio
Expand Down

0 comments on commit c897555

Please sign in to comment.