Skip to content
Merged
5 changes: 4 additions & 1 deletion backend/dataset/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import resource
try:
import resource
except ImportError:
resource = None
from django.contrib import admin
from import_export.admin import ImportExportActionModelAdmin
from .resources import *
Expand Down
8 changes: 6 additions & 2 deletions backend/functions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,18 @@ def chat_log(request):
@permission_classes([AllowAny])
@api_view(["POST"])
def chat_output(request):
DEFAULT_SYSTEM_PROMPT = (
"We will be rendering your response on a frontend. So, please add spaces or indentation or nextline chars or "
"bullet or numberings etc. suitably for code or the text, wherever required."
)
prompt = request.data.get("message")
history = request.data.get("history", "")
model = request.data.get("model", "GPT3.5")
system_prompt = (request.data.get("system_prompt", "") or "").strip() or DEFAULT_SYSTEM_PROMPT
return Response(
{
"message": get_model_output(
"We will be rendering your response on a frontend. so please add spaces or indentation or nextline chars or "
"bullet or numberings etc. suitably for code or the text. wherever required.",
system_prompt,
prompt,
history,
model,
Expand Down
2 changes: 2 additions & 0 deletions backend/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ class Project(models.Model):
),
)



def clear_expired_lock(self):
self.lock.filter(expires_at__lt=now()).delete()

Expand Down
82 changes: 50 additions & 32 deletions backend/tasks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1812,7 +1812,7 @@ def partial_update(self, request, pk=None):
annotation_obj.task,
annotation_obj,
annotation_obj.task.project_id.metadata_json,
task.data["model"]
task.data.get("model", [])
)
if output_result == -1:
ret_dict = {
Expand Down Expand Up @@ -2074,7 +2074,7 @@ def partial_update(self, request, pk=None):
annotation_obj.task,
annotation_obj,
annotation_obj.task.project_id.metadata_json,
task.data["model"]
task.data.get("model", [])
)
if output_result == -1:
ret_dict = {
Expand Down Expand Up @@ -2405,7 +2405,7 @@ def partial_update(self, request, pk=None):
annotation_obj.task,
annotation_obj,
annotation_obj.task.project_id.metadata_json,
task.data["model"]
task.data.get("model", [])
)
if output_result == -1:
ret_dict = {
Expand Down Expand Up @@ -2938,16 +2938,12 @@ def get_llm_output(prompt, task, annotation, project_metadata_json):
intent = task.data["meta_info_intent"]
domain = task.data["meta_info_domain"]
lang_type = task.data["meta_info_language"]
ann_result = (
json.loads(annotation.result)
if isinstance(annotation.result, str)
else annotation.result
)
project_metadata = (
json.loads(project_metadata_json)
if isinstance(project_metadata_json, str)
else project_metadata_json
)
ann_result = annotation.result
if isinstance(ann_result, str):
ann_result = json.loads(ann_result) if ann_result.strip() else []
project_metadata = project_metadata_json
if isinstance(project_metadata, str):
project_metadata = json.loads(project_metadata) if project_metadata.strip() else {}
if isinstance(project_metadata, dict) and project_metadata.get("blank_response") == True:
return ""
if prompt in [None, "Null", 0, "None", "", " "]:
Expand Down Expand Up @@ -2980,11 +2976,21 @@ def get_llm_output(prompt, task, annotation, project_metadata_json):
dup_check = duplicate_check(ann_result, prompt)

# GET MODEL OUTPUT
history = ann_result
DEFAULT_SYSTEM_PROMPT = (
"We will be rendering your response on a frontend. So, please add spaces or indentation or nextline chars or "
"bullet or numberings etc. suitably for code or the text, wherever required."
)
sys_prompt_data = project_metadata.get("system_prompt", {}) if isinstance(project_metadata, dict) else {}
model = task.data["model"]

if isinstance(sys_prompt_data, dict):
system_prompt = sys_prompt_data.get(model) or sys_prompt_data.get("default") or DEFAULT_SYSTEM_PROMPT
else:
system_prompt = sys_prompt_data.strip() if sys_prompt_data.strip() else DEFAULT_SYSTEM_PROMPT

history = ann_result
model_output = get_model_output(
"We will be rendering your response on a frontend. so please add spaces or indentation or nextline chars or "
"bullet or numberings etc. suitably for code or the text. wherever required.",
system_prompt,
prompt,
history,
model,
Expand All @@ -2995,20 +3001,23 @@ def get_llm_output(prompt, task, annotation, project_metadata_json):
return res

def get_all_llm_output(prompt, task, annotation, project_metadata_json, models_to_run):
project_metadata = project_metadata_json
if isinstance(project_metadata, str):
project_metadata = json.loads(project_metadata) if project_metadata.strip() else {}

if not models_to_run:
models_to_run = project_metadata.get("models_set", []) if isinstance(project_metadata, dict) else []

if not models_to_run:
return Response({"message": "No models are configured for this task. Please configure models for this project."}, status=status.HTTP_400_BAD_REQUEST)

# CHECKS
intent = task.data["meta_info_intent"]
domain = task.data["meta_info_domain"]
lang_type = task.data["meta_info_language"]
ann_result = (
json.loads(annotation.result)
if isinstance(annotation.result, str)
else annotation.result
)
project_metadata = (
json.loads(project_metadata_json)
if isinstance(project_metadata_json, str)
else project_metadata_json
)
intent = task.data.get("meta_info_intent")
domain = task.data.get("meta_info_domain")
lang_type = task.data.get("meta_info_language")
ann_result = annotation.result
if isinstance(ann_result, str):
ann_result = json.loads(ann_result) if ann_result.strip() else []
if prompt in [None, "Null", 0, "None", "", " "]:
return -1
intentDomain_test, lang_test, duplicate_test = False, False, False
Expand Down Expand Up @@ -3039,15 +3048,24 @@ def get_all_llm_output(prompt, task, annotation, project_metadata_json, models_t
dup_check = duplicate_check(ann_result, prompt)

# GET MODEL OUTPUT
DEFAULT_SYSTEM_PROMPT = (
"We will be rendering your response on a frontend. So, please add spaces or indentation or nextline chars or "
"bullet or numberings etc. suitably for code or the text, wherever required."
)
sys_prompt_data = project_metadata.get("system_prompt", {}) if isinstance(project_metadata, dict) else {}

if not isinstance(sys_prompt_data, dict):
sys_prompt_data = {"default": sys_prompt_data.strip() if sys_prompt_data.strip() else DEFAULT_SYSTEM_PROMPT}

history = ann_result[0]


model_output = get_all_model_output(
"We will be rendering your response on a frontend. so please add spaces or indentation or nextline chars or "
"bullet or numberings etc. suitably for code or the text. wherever required.",
sys_prompt_data,
prompt,
history,
models_to_run
models_to_run,
DEFAULT_SYSTEM_PROMPT
)

return model_output
Expand Down
19 changes: 15 additions & 4 deletions backend/utils/llm_interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,16 @@ def get_llama2_output(system_prompt, conv_history, user_prompt):
"max_new_tokens": 500,
"top_p": 1,
}
s = requests.Session()
result = s.post(url, headers={"Authorization": f"Bearer {token}"}, json=body)
return result.json()["choices"][0]["message"]["content"].strip()
try:
s = requests.Session()
result = s.post(url, headers={"Authorization": f"Bearer {token}"}, json=body)
result.raise_for_status()
return result.json()["choices"][0]["message"]["content"].strip()
except Exception as e:
err_msg = str(e)
message = f"An error occurred while interacting with Llama2 API: {err_msg}"
st = status.HTTP_500_INTERNAL_SERVER_ERROR
return Response({"message": message}, status=st)

def get_sarvam_m_output(system_prompt, conv_history, user_prompt):
api_base = os.getenv("SARVAM_M_API_BASE")
Expand Down Expand Up @@ -256,10 +263,11 @@ def get_model_output(system_prompt, user_prompt, history, model=GPT4OMini):
out = get_deepinfra_output(system_prompt, user_prompt, history, model)
return out

def get_all_model_output(system_prompt, user_prompt, history, models_to_run):
def get_all_model_output(system_prompt_data, user_prompt, history, models_to_run, default_system_prompt=""):
results = {}

for model in models_to_run:
system_prompt = system_prompt_data.get(model) or system_prompt_data.get("default") or default_system_prompt if isinstance(system_prompt_data, dict) else system_prompt_data
# print("history:", history)
# model_history = next(
# (entry["interaction_json"] for entry in history if entry.get("model_name") == model),
Expand All @@ -284,4 +292,7 @@ def get_all_model_output(system_prompt, user_prompt, history, models_to_run):
else:
results[model] = get_deepinfra_output(system_prompt, user_prompt, model_history, model)

if isinstance(results[model], Response):
return results[model]

return results
Loading