Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Zipstack/unstract into simple-promp…
Browse files Browse the repository at this point in the history
…t-studio-crud
  • Loading branch information
tahierhussain committed Jul 25, 2024
2 parents dc22d92 + e7bb9a2 commit a9be153
Show file tree
Hide file tree
Showing 32 changed files with 438 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from typing import Any, Optional

from django.core.exceptions import ObjectDoesNotExist
from prompt_studio.prompt_profile_manager.models import ProfileManager
from prompt_studio.prompt_studio.models import ToolStudioPrompt
from prompt_studio.prompt_studio_core.exceptions import (
Expand Down Expand Up @@ -144,3 +145,49 @@ def fetch_default_llm_profile(tool: CustomTool) -> ProfileManager:
return ProfileManager.get_default_llm_profile(tool=tool)
except DefaultProfileError:
raise DefaultProfileError("Default ProfileManager does not exist.")

@staticmethod
def fetch_default_output_response(
tool_studio_prompts: list[ToolStudioPrompt], document_manager_id: str
) -> dict[str, Any]:
"""Method to frame JSON responses for combined output for default for
default profile manager of the project.
Args:
tool_studio_prompts (list[ToolStudioPrompt])
document_manager_id (str)
Returns:
dict[str, Any]: Formatted JSON response for combined output.
"""
# Initialize the result dictionary
result: dict[str, Any] = {}
# Iterate over ToolStudioPrompt records
for tool_prompt in tool_studio_prompts:
if tool_prompt.prompt_type == PSOMKeys.NOTES:
continue
prompt_id = str(tool_prompt.prompt_id)
profile_manager_id = tool_prompt.profile_manager_id

# If profile_manager is not set, skip this record
if not profile_manager_id:
result[tool_prompt.prompt_key] = ""
continue

try:
queryset = PromptStudioOutputManager.objects.filter(
prompt_id=prompt_id,
profile_manager=profile_manager_id,
is_single_pass_extract=False,
document_manager_id=document_manager_id,
)

if not queryset.exists():
result[tool_prompt.prompt_key] = ""
continue

for output in queryset:
result[tool_prompt.prompt_key] = output.output
except ObjectDoesNotExist:
result[tool_prompt.prompt_key] = ""
return result
41 changes: 11 additions & 30 deletions backend/prompt_studio/prompt_studio_output_manager/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
PromptOutputManagerErrorMessage,
PromptStudioOutputManagerKeys,
)
from prompt_studio.prompt_studio_output_manager.output_manager_helper import (
OutputManagerHelper,
)
from prompt_studio.prompt_studio_output_manager.serializers import (
PromptStudioOutputSerializer,
)
Expand Down Expand Up @@ -68,38 +71,16 @@ def get_output_for_tool_default(self, request: HttpRequest) -> Response:

try:
# Fetch ToolStudioPrompt records based on tool_id
tool_studio_prompts = ToolStudioPrompt.objects.filter(tool_id=tool_id)
tool_studio_prompts = ToolStudioPrompt.objects.filter(
tool_id=tool_id
).order_by("sequence_number")
except ObjectDoesNotExist:
raise APIException(detail=tool_not_found, code=400)

# Initialize the result dictionary
result: dict[str, Any] = {}

# Iterate over ToolStudioPrompt records
for tool_prompt in tool_studio_prompts:
prompt_id = str(tool_prompt.prompt_id)
profile_manager_id = str(tool_prompt.profile_manager.profile_id)

# If profile_manager is not set, skip this record
if not profile_manager_id:
result[tool_prompt.prompt_key] = ""
continue

try:
queryset = PromptStudioOutputManager.objects.filter(
prompt_id=prompt_id,
profile_manager=profile_manager_id,
is_single_pass_extract=False,
document_manager_id=document_manager_id,
)

if not queryset.exists():
result[tool_prompt.prompt_key] = ""
continue

for output in queryset:
result[tool_prompt.prompt_key] = output.output
except ObjectDoesNotExist:
result[tool_prompt.prompt_key] = ""
# Invoke helper method to frame and fetch default response.
result: dict[str, Any] = OutputManagerHelper.fetch_default_output_response(
tool_studio_prompts=tool_studio_prompts,
document_manager_id=document_manager_id,
)

return Response(result, status=status.HTTP_200_OK)
4 changes: 2 additions & 2 deletions backend/sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ PROMPT_STUDIO_FILE_PATH=/app/prompt-studio-data

# Structure Tool Image (Runs prompt studio exported tools)
# https://hub.docker.com/r/unstract/tool-structure
STRUCTURE_TOOL_IMAGE_URL="docker:unstract/tool-structure:0.0.31"
STRUCTURE_TOOL_IMAGE_URL="docker:unstract/tool-structure:0.0.32"
STRUCTURE_TOOL_IMAGE_NAME="unstract/tool-structure"
STRUCTURE_TOOL_IMAGE_TAG="0.0.31"
STRUCTURE_TOOL_IMAGE_TAG="0.0.32"

# Feature Flags
EVALUATION_SERVER_IP=unstract-flipt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "prismjs/plugins/line-numbers/prism-line-numbers.css";
import "prismjs/plugins/line-numbers/prism-line-numbers.js";
import "prismjs/themes/prism.css";
import { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import PropTypes from "prop-types";

import {
Expand All @@ -18,7 +19,6 @@ import { SpinnerLoader } from "../../widgets/spinner-loader/SpinnerLoader";
import "./CombinedOutput.css";
import { useExceptionHandler } from "../../../hooks/useExceptionHandler";
import { JsonView } from "./JsonView";
import { useParams } from "react-router-dom";

let TableView;
let promptOutputApiSps;
Expand All @@ -32,11 +32,14 @@ try {
}
let publicOutputsDocApi;
let publicAdapterApi;
let publicDefaultOutputApi;
try {
publicOutputsDocApi =
require("../../../plugins/prompt-studio-public-share/helpers/PublicShareAPIs").publicOutputsDocApi;
publicAdapterApi =
require("../../../plugins/prompt-studio-public-share/helpers/PublicShareAPIs").publicAdapterApi;
publicDefaultOutputApi =
require("../../../plugins/prompt-studio-public-share/helpers/PublicShareAPIs").publicDefaultOutputApi;
} catch {
// The component will remain null of it is not available
}
Expand Down Expand Up @@ -68,6 +71,9 @@ function CombinedOutput({ docId, setFilledFields }) {
if (!docId || isSinglePassExtractLoading) {
return;
}
if (singlePassExtractMode && activeKey === "0") {
setActiveKey("1");
}

let filledFields = 0;
setIsOutputLoading(true);
Expand Down Expand Up @@ -146,6 +152,9 @@ function CombinedOutput({ docId, setFilledFields }) {
selectedProfile || defaultLlmProfile,
singlePassExtractMode
);
if (activeKey === "0") {
url = publicDefaultOutputApi(id, docId);
}
} else {
url = `/api/v1/unstract/${
sessionDetails?.orgId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ function JsonView({
)}
{adapterData.map((adapter, index) => (
<TabPane
tab={<span>{adapter.llm_model}</span>}
tab={<span>{adapter?.llm_model || adapter?.profile_name}</span>}
key={(index + 1)?.toString()}
/>
))}
</Tabs>
<div className="combined-op-segment"></div>
</div>
<div className="combined-op-divider" />
<ProfileInfoBar profileId={selectedProfile} profiles={llmProfiles} />
{activeKey !== "0" && (
<ProfileInfoBar profileId={selectedProfile} profiles={llmProfiles} />
)}
<div className="combined-op-body code-snippet">
{combinedOutput && (
<pre className="line-numbers width-100">
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/components/custom-tools/footer/Footer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { PlusOutlined } from "@ant-design/icons";
import "./Footer.css";
import { FooterLayout } from "../footer-layout/FooterLayout";
import { promptType } from "../../../helpers/GetStaticData";
import { useCustomToolStore } from "../../../store/custom-tool-store";

function Footer({ activeKey, addPromptInstance }) {
const { isPublicSource } = useCustomToolStore();
if (activeKey === "1") {
return (
<FooterLayout>
Expand All @@ -16,6 +18,7 @@ function Footer({ activeKey, addPromptInstance }) {
type="link"
icon={<PlusOutlined />}
onClick={() => addPromptInstance(promptType.notes)}
disabled={isPublicSource}
>
Notes
</Button>
Expand All @@ -25,6 +28,7 @@ function Footer({ activeKey, addPromptInstance }) {
type="link"
icon={<PlusOutlined />}
onClick={() => addPromptInstance(promptType.prompt)}
disabled={isPublicSource}
>
Prompt
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ function HeaderTitle() {
<ArrowLeftOutlined />
</Button>
</div>
<div className="custom-tools-name">
<Typography.Text strong>{details?.tool_name}</Typography.Text>
<div>
<Typography.Text className="custom-tools-name" strong>
{details?.tool_name}
</Typography.Text>
<Button size="small" type="text" disabled>
<EditOutlined />
</Button>
Expand Down
33 changes: 17 additions & 16 deletions frontend/src/components/custom-tools/header/Header.css
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
/* Styles for Header */

.custom-tools-header-layout {
padding: 8px;
background-color: #F5F7F9;
display: flex;
align-items: center;
padding: 8px;
background-color: #f5f7f9;
display: flex;
align-items: center;
}

.custom-tools-header-btns {
display: flex;
justify-content: space-between;
margin-left: auto;
display: flex;
justify-content: space-between;
margin-left: auto;
}

.custom-tools-header-btns > div {
padding: 0px 5px;
padding: 0px 5px;
}

.custom-tools-name {
padding: 0px 8px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding: 0px 8px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
font-size: 16px;
}

.custom-tools-header-v-divider {
border-right: 1px #D9D9D9 solid;
padding: 0;
margin: 0px 5px;
border-right: 1px #d9d9d9 solid;
padding: 0;
margin: 0px 5px;
}

.select-llm-pro-header {
padding-top: 12px;
padding-top: 12px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from "antd";
import PropTypes from "prop-types";
import { useEffect, useState } from "react";
import { useParams } from "react-router-dom";

import { useAxiosPrivate } from "../../../hooks/useAxiosPrivate";
import { useExceptionHandler } from "../../../hooks/useExceptionHandler";
Expand All @@ -41,6 +42,13 @@ try {
// The component will remain null if it is not available
}

let publicIndexApi = null;
try {
publicIndexApi =
require("../../../plugins/prompt-studio-public-share/helpers/PublicShareAPIs").publicIndexApi;
} catch {
// The component will remain null if it is not available
}
const indexTypes = {
raw: "RAW",
summarize: "Summarize",
Expand All @@ -63,6 +71,7 @@ function ManageDocsModal({
const [lastMessagesUpdate, setLastMessagesUpdate] = useState("");
const { sessionDetails } = useSessionStore();
const { setAlertDetails } = useAlertStore();
const { id } = useParams();
const {
selectedDoc,
listOfDocs,
Expand Down Expand Up @@ -138,7 +147,7 @@ function ManageDocsModal({
}, [defaultLlmProfile, details]);

useEffect(() => {
if (!open || isPublicSource) {
if (!open) {
return;
}
handleGetIndexStatus(rawLlmProfile, indexTypes.raw);
Expand Down Expand Up @@ -243,10 +252,13 @@ function ManageDocsModal({
handleIndexStatus(indexType, []);
return;
}

let url = `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/document-index/?profile_manager=${llmProfileId}`;
if (isPublicSource) {
url = publicIndexApi(id, llmProfileId);
}
const requestOptions = {
method: "GET",
url: `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/document-index/?profile_manager=${llmProfileId}`,
url,
};

handleLoading(indexType, true);
Expand Down Expand Up @@ -425,8 +437,7 @@ function ManageDocsModal({
disabled={
disableLlmOrDocChange?.length > 0 ||
isSinglePassExtractLoading ||
indexDocs.includes(item?.document_id) ||
isPublicSource
indexDocs.includes(item?.document_id)
}
/>
),
Expand Down Expand Up @@ -592,7 +603,8 @@ function ManageDocsModal({
disabled={
!defaultLlmProfile ||
disableLlmOrDocChange?.length > 0 ||
isSinglePassExtractLoading
isSinglePassExtractLoading ||
isPublicSource
}
>
Upload New File
Expand Down
Loading

0 comments on commit a9be153

Please sign in to comment.