From 8111e4566889874697ca450f110e4f80762b0aa6 Mon Sep 17 00:00:00 2001 From: Steph Herbers Date: Tue, 12 Nov 2024 17:05:13 -0500 Subject: [PATCH 1/3] make a version when creating a new experiment such that there is the working version and a v1 --- apps/chat/agent/tools.py | 3 +-- apps/experiments/tests/test_views.py | 28 ++++++++++++++++++++++++++++ apps/experiments/views/experiment.py | 1 + 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/apps/chat/agent/tools.py b/apps/chat/agent/tools.py index 70d469029..847267b9c 100644 --- a/apps/chat/agent/tools.py +++ b/apps/chat/agent/tools.py @@ -6,7 +6,6 @@ from langchain_community.utilities.openapi import OpenAPISpec from langchain_core.tools import BaseTool -from apps.assistants.models import OpenAiAssistant from apps.chat.agent import schemas from apps.chat.agent.openapi_tool import openapi_spec_op_to_function_def from apps.events.forms import ScheduledMessageConfigForm @@ -253,7 +252,7 @@ def get_assistant_tools(assistant) -> list[BaseTool]: return tools -def get_custom_action_tools(action_holder: Experiment | OpenAiAssistant) -> list[BaseTool]: +def get_custom_action_tools(action_holder) -> list[BaseTool]: operations = action_holder.custom_action_operations.select_related( "custom_action", "custom_action__auth_provider" ).all() diff --git a/apps/experiments/tests/test_views.py b/apps/experiments/tests/test_views.py index 968668de9..0f39d8ab2 100644 --- a/apps/experiments/tests/test_views.py +++ b/apps/experiments/tests/test_views.py @@ -74,6 +74,34 @@ def test_create_experiment_success(client, team_with_users): experiment.tools == [AgentTools.ONE_OFF_REMINDER] +@pytest.mark.django_db() +def test_create_experiment_creates_first_version(client, team_with_users): + user = team_with_users.members.first() + consent_form = ConsentFormFactory(team=team_with_users) + LlmProviderFactory(team=team_with_users) + client.force_login(user) + + post_data = { + "name": "some name", + "type": "llm", + "prompt_text": "You are a helpful assistant.", + "consent_form": consent_form.id, + "temperature": 0.7, + "llm_provider": LlmProviderFactory(team=team_with_users).id, + "llm_provider_model": LlmProviderModelFactory(team=team_with_users).id, + "max_token_limit": 100, + "voice_response_behaviour": VoiceResponseBehaviours.RECIPROCAL, + } + client.post(reverse("experiments:new", args=[team_with_users.slug]), data=post_data) + experiments = Experiment.objects.filter(owner=user).all() + assert len(experiments) == 2 + working_verison = experiments.filter(working_version=None).first() + versioned_exp = experiments.filter(version_number=1).first() + assert working_verison is not None + assert versioned_exp is not None + assert versioned_exp.is_default_version + + @override_flag("assistants", active=True) @pytest.mark.parametrize( ("with_assistant", "with_prompt", "with_llm_provider", "with_llm_model", "errors"), diff --git a/apps/experiments/views/experiment.py b/apps/experiments/views/experiment.py index 6c325997b..ff1dd0ccc 100644 --- a/apps/experiments/views/experiment.py +++ b/apps/experiments/views/experiment.py @@ -439,6 +439,7 @@ def form_valid(self, form, file_formset): if file_formset: files = file_formset.save(self.request) self.object.files.set(files) + self.object.create_new_version() return HttpResponseRedirect(self.get_success_url()) From 804fa190dbb5d96d46ae4fee87190d99e03e403d Mon Sep 17 00:00:00 2001 From: Steph Herbers Date: Tue, 12 Nov 2024 17:42:11 -0500 Subject: [PATCH 2/3] move logic to save --- apps/experiments/models.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/apps/experiments/models.py b/apps/experiments/models.py index 15a5bf372..140cba35b 100644 --- a/apps/experiments/models.py +++ b/apps/experiments/models.py @@ -613,9 +613,15 @@ def __str__(self): return f"{self.name} ({self.version_display})" def save(self, *args, **kwargs): - if self.working_version is None and self.is_default_version is True: - raise ValueError("A working experiment cannot be a default version") - return super().save(*args, **kwargs) + if not self.pk: + with transaction.atomic(): + super().save(*args, **kwargs) + if self.working_version is None and not self.versions.exists(): + self.create_new_version() + else: + if self.working_version is None and self.is_default_version is True: + raise ValueError("A working experiment cannot be a default version") + return super().save(*args, **kwargs) def get_absolute_url(self): return reverse("experiments:single_experiment_home", args=[self.team.slug, self.id]) From 1d7e1f29650cae498f86b54a6768a7835bd72225 Mon Sep 17 00:00:00 2001 From: Steph Herbers Date: Wed, 13 Nov 2024 15:30:15 -0500 Subject: [PATCH 3/3] Revert "move logic to save" This reverts commit 804fa190dbb5d96d46ae4fee87190d99e03e403d. --- apps/experiments/models.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/apps/experiments/models.py b/apps/experiments/models.py index 140cba35b..15a5bf372 100644 --- a/apps/experiments/models.py +++ b/apps/experiments/models.py @@ -613,15 +613,9 @@ def __str__(self): return f"{self.name} ({self.version_display})" def save(self, *args, **kwargs): - if not self.pk: - with transaction.atomic(): - super().save(*args, **kwargs) - if self.working_version is None and not self.versions.exists(): - self.create_new_version() - else: - if self.working_version is None and self.is_default_version is True: - raise ValueError("A working experiment cannot be a default version") - return super().save(*args, **kwargs) + if self.working_version is None and self.is_default_version is True: + raise ValueError("A working experiment cannot be a default version") + return super().save(*args, **kwargs) def get_absolute_url(self): return reverse("experiments:single_experiment_home", args=[self.team.slug, self.id])