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

Session bug (fix with context variables) #161

Merged
merged 11 commits into from
May 23, 2024
Merged

Session bug (fix with context variables) #161

merged 11 commits into from
May 23, 2024

Conversation

nqn
Copy link
Contributor

@nqn nqn commented May 21, 2024

No description provided.

Copy link
Contributor

@michaeljin michaeljin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice - haven't used contextvars before. Only change I'd be in favor of is to remove the default value for the contextvar. I think that's an easy way to get bugs that are difficult to reproduce.

log10/load.py Outdated
except Exception as e:
logging.warn(f"LOG10: failed to log: {e}. Skipping")
# Print the exception tracompletion_urlceback
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mangled comment

log10/load.py Outdated
if last_completion_response_var.get() is None:
return None
response = last_completion_response_var.get()
return url + "/app/" + response["organizationSlug"] + "/completions/" + response["completionID"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe write as fstring eg
f'{url}/app/{response["organizationSlug"]}/completions/{response["completionId"]}'

#
# Context variables
#
session_id_var = contextvars.ContextVar("session_id", default=get_session_id())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slight preference to not set a default here and force an explicit set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need this to keep the existing behavior - where llm calls in outer most "context" i.e. no context, still gets grouped together

import uuid


session_id_var = contextvars.ContextVar("session_id", default=str(uuid.uuid4()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here - slight preference to not set a default. Makes it more explicit and simpler to test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just for testing, if that's ok to keep

session_id_var.reset(self.token)


def simulated_llm_call():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you removed the default value - I'd expect to see an explicit set here (or in an outer scope) before get is possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note sure I follow. Is it to set uuids explicitly by having it as an input to the session object?

Comment on lines +56 to +73
async def test_nested_async_contexts():
print("")
before_outer_session = await simulated_llm_acall()
async with ExampleSession():
before_inner_session = await simulated_llm_acall()

async with ExampleSession():
inner_session = await simulated_llm_acall()

assert inner_session != before_inner_session
assert inner_session != before_outer_session

assert await simulated_llm_acall() == before_inner_session

assert await simulated_llm_acall() == before_outer_session


asyncio.run(test_nested_async_contexts())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding the @pytest.mark.asyncio before the def func, then we don't need to call asyncio.run here. We have setup that way in other tests (e.g openai tests)

await asyncio.gather(run_session(delay=0, run_time=3), run_session(delay=1, run_time=1))


asyncio.run(test_overlapping_async_contexts())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here as well

@nqn nqn merged commit 27b6a6e into main May 23, 2024
1 check passed
@nqn nqn deleted the nqn/session-bug branch May 23, 2024 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants