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

Default profile creation for prompt studio project #353

Merged
merged 8 commits into from
Jun 10, 2024
60 changes: 60 additions & 0 deletions backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import json
import logging
import os
import uuid
from pathlib import Path
from typing import Any, Optional

from account.constants import Common
from account.models import User
from adapter_processor.constants import AdapterKeys
from adapter_processor.models import AdapterInstance
from django.conf import settings
from django.db.models.manager import BaseManager
from file_management.file_management_helper import FileManagerHelper
from prompt_studio.prompt_profile_manager.models import ProfileManager
from prompt_studio.prompt_studio.models import ToolStudioPrompt
Expand Down Expand Up @@ -48,6 +52,62 @@
class PromptStudioHelper:
"""Helper class for Custom tool operations."""

@staticmethod
def create_default_profile_manager(user: User, tool_id: uuid) -> None:
"""Create a default profile manager for a given user and tool.

Args:
user (User): The user for whom the default profile manager is
created.
tool_id (uuid): The ID of the tool for which the default profile
manager is created.

Raises:
AdapterInstance.DoesNotExist: If no suitable adapter instance is
found for creating the default profile manager.

Returns:
None
"""
try:
AdapterInstance.objects.get(
is_friction_less=True,
is_usable=True,
adapter_type=AdapterKeys.LLM,
)
chandrasekharan-zipstack marked this conversation as resolved.
Show resolved Hide resolved

default_adapters: BaseManager[AdapterInstance] = (
AdapterInstance.objects.filter(is_friction_less=True)
)

profile_manager = ProfileManager(
prompt_studio_tool=CustomTool.objects.get(pk=tool_id),
is_default=True,
created_by=user,
modified_by=user,
chunk_size=0,
johnyrahul marked this conversation as resolved.
Show resolved Hide resolved
profile_name="sample profile",
chunk_overlap=0,
section="Default",
retrieval_strategy="simple",
similarity_top_k=3,
)

for adapter in default_adapters:
if adapter.adapter_type == AdapterKeys.LLM:
profile_manager.llm = adapter
elif adapter.adapter_type == AdapterKeys.VECTOR_DB:
profile_manager.vector_store = adapter
elif adapter.adapter_type == AdapterKeys.X2TEXT:
profile_manager.x2text = adapter
elif adapter.adapter_type == AdapterKeys.EMBEDDING:
profile_manager.embedding_model = adapter

profile_manager.save()

except AdapterInstance.DoesNotExist:
logger.info("skipping default profile creation")
johnyrahul marked this conversation as resolved.
Show resolved Hide resolved

@staticmethod
def validate_adapter_status(
profile_manager: ProfileManager,
Expand Down
3 changes: 3 additions & 0 deletions backend/prompt_studio/prompt_studio_core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ def create(self, request: HttpRequest) -> Response:
f"{ToolStudioErrors.TOOL_NAME_EXISTS}, \
{ToolStudioErrors.DUPLICATE_API}"
)
PromptStudioHelper.create_default_profile_manager(
request.user, serializer.data["tool_id"]
)
return Response(serializer.data, status=status.HTTP_201_CREATED)

def destroy(
Expand Down
Loading