Skip to content

Commit

Permalink
Refactor - revamp use of prompt revisions in playground
Browse files Browse the repository at this point in the history
  • Loading branch information
aybruhm committed Feb 14, 2024
1 parent aaa1d0d commit f46153e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 75 deletions.
12 changes: 4 additions & 8 deletions agenta-web/src/components/Playground/ViewNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import {useAppsData} from "@/contexts/app.context"
import {isDemo} from "@/lib/helpers/utils"

interface Props {
interface PruseVariantops {
variant: Variant
handlePersistVariant: (variantName: string) => void
environments: Environment[]
Expand Down Expand Up @@ -60,12 +60,11 @@ const ViewNavigation: React.FC<Props> = ({
saveOptParams,
isLoading,
isChatVariant,
promptRevisions,
historyStatus,
setPromptOptParams,
setPromptRevisions,
setHistoryStatus,
} = useVariant(appId, variant)

const [retrying, setRetrying] = useState(false)
const [isParamsCollapsed, setIsParamsCollapsed] = useState("1")
const [containerURI, setContainerURI] = useState("")
Expand Down Expand Up @@ -244,7 +243,8 @@ const ViewNavigation: React.FC<Props> = ({
tabID={tabID}
setHistoryStatus={setHistoryStatus}
setIsDrawerOpen={setIsDrawerOpen}
setPromptRevisions={setPromptRevisions}
isDrawerOpen={isDrawerOpen}
historyStatus={historyStatus}
/>
</Col>
</Row>
Expand All @@ -259,12 +259,8 @@ const ViewNavigation: React.FC<Props> = ({
isChatVariant={!!isChatVariant}
compareMode={compareMode}
onStateChange={onStateChange}
promptRevisions={promptRevisions}
historyStatus={historyStatus}
setPromptOptParams={setPromptOptParams}
promptOptParams={promptOptParams}
isDrawerOpen={isDrawerOpen}
setIsDrawerOpen={setIsDrawerOpen}
/>
</Col>
</Row>
Expand Down
32 changes: 29 additions & 3 deletions agenta-web/src/components/Playground/Views/ParametersView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import {promptVersioning, removeVariant} from "@/lib/services/api"
import {CloudUploadOutlined, DeleteOutlined, HistoryOutlined, SaveOutlined} from "@ant-design/icons"
import {usePostHogAg} from "@/hooks/usePostHogAg"
import {isDemo} from "@/lib/helpers/utils"
import {useQueryParam} from "@/hooks/useQuery"
import {dynamicComponent} from "@/lib/helpers/dynamic"


const PromptVersioningDrawer: any = dynamicComponent(
`PromptVersioningDrawer/PromptVersioningDrawer`,
)

interface Props {
variant: Variant
Expand All @@ -30,8 +37,12 @@ interface Props {
onStateChange: (isDirty: boolean) => void
compareMode: boolean
tabID: React.MutableRefObject<string>
isDrawerOpen: boolean
setIsDrawerOpen: React.Dispatch<React.SetStateAction<boolean>>
setPromptRevisions: React.Dispatch<React.SetStateAction<IPromptVersioning | undefined>>
historyStatus: {
loading: boolean
error: boolean
}
setHistoryStatus: React.Dispatch<
React.SetStateAction<{
loading: boolean
Expand Down Expand Up @@ -76,15 +87,18 @@ const ParametersView: React.FC<Props> = ({
onStateChange,
compareMode,
tabID,
setHistoryStatus,
isDrawerOpen,
setIsDrawerOpen,
setPromptRevisions,
historyStatus,
setHistoryStatus,
}) => {
const classes = useStyles()
const posthog = usePostHogAg()
const [messageApi, contextHolder] = message.useMessage()
const [isPublishModalOpen, setPublishModalOpen] = useState(false)
const isVariantExisting = !!variant.variantId
const [revisionNum, setRevisionNum] = useQueryParam("revision")
const [promptRevisions, setPromptRevisions] = useState<IPromptRevisions>()

useEffect(() => {
onStateChange(variant.persistent === false)
Expand Down Expand Up @@ -153,6 +167,10 @@ const ParametersView: React.FC<Props> = ({
}
}

let filteredRevisions
if (promptRevisions !== undefined) {
filteredRevisions = promptRevisions.filter((item) => item.revision !== 0)
}
const items: CollapseProps["items"] = [
{
key: "1",
Expand Down Expand Up @@ -271,6 +289,14 @@ const ParametersView: React.FC<Props> = ({
setIsModalOpen={setPublishModalOpen}
environments={environments}
/>
<PromptVersioningDrawer
setIsDrawerOpen={setIsDrawerOpen}
setRevisionNum={setRevisionNum}
isDrawerOpen={isDrawerOpen}
historyStatus={historyStatus}
promptRevisions={filteredRevisions}
onStateChange={onStateChange}
/>
</div>
)
}
Expand Down
93 changes: 37 additions & 56 deletions agenta-web/src/components/Playground/Views/TestView.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {useContext, useEffect, useRef, useState} from "react"
import {Button, Input, Card, Row, Col, Space, Form, Modal} from "antd"
import {CaretRightOutlined, CloseCircleOutlined, PlusOutlined} from "@ant-design/icons"
import {callVariant} from "@/lib/services/api"
import {callVariant, promptRevision} from "@/lib/services/api"
import {
ChatMessage,
ChatRole,
Expand Down Expand Up @@ -31,9 +31,6 @@ import duration from "dayjs/plugin/duration"
import {useQueryParam} from "@/hooks/useQuery"
import {dynamicComponent} from "@/lib/helpers/dynamic"

const PromptVersioningDrawer: any = dynamicComponent(
`PromptVersioningDrawer/PromptVersioningDrawer`,
)

dayjs.extend(relativeTime)
dayjs.extend(duration)
Expand Down Expand Up @@ -143,15 +140,8 @@ interface TestViewProps {
isChatVariant?: boolean
compareMode: boolean
onStateChange: (isDirty: boolean) => void
promptRevisions: IPromptVersioning | undefined
historyStatus: {
loading: boolean
error: boolean
}
setPromptOptParams: React.Dispatch<React.SetStateAction<Parameter[] | null>>
promptOptParams: Parameter[] | null
setIsDrawerOpen: React.Dispatch<React.SetStateAction<boolean>>
isDrawerOpen: boolean
}

interface BoxComponentProps {
Expand Down Expand Up @@ -330,10 +320,6 @@ const App: React.FC<TestViewProps> = ({
compareMode,
onStateChange,
setPromptOptParams,
promptRevisions,
historyStatus,
isDrawerOpen,
setIsDrawerOpen,
}) => {
const router = useRouter()
const appId = router.query.app_id as unknown as string
Expand All @@ -348,7 +334,6 @@ const App: React.FC<TestViewProps> = ({
const [resultsList, setResultsList] = useState<string[]>(testList.map(() => ""))
const [params, setParams] = useState<Record<string, string> | null>(null)
const classes = useStylesApp({themeMode: appTheme} as StyleProps)
const filteredRevisions = promptRevisions?.revisions.filter((item) => item.revision !== 0)

const rootRef = React.useRef<HTMLDivElement>(null)
const [isLLMProviderMissingModalOpen, setIsLLMProviderMissingModalOpen] = useState(false)
Expand All @@ -364,41 +349,45 @@ const App: React.FC<TestViewProps> = ({

useEffect(() => {
if (!revisionNum) return

const revision = filteredRevisions?.find((rev) => rev.revision === parseInt(revisionNum))

if (!revision) return

setPromptOptParams((prevState: Parameter[] | null) => {
if (!prevState) {
return prevState
}

const parameterNames = [
"temperature",
"model",
"max_tokens",
"prompt_system",
"prompt_user",
"top_p",
"frequence_penalty",
"presence_penalty",
"inputs",
]

return prevState.map((param: Parameter) => {
if (parameterNames.includes(param.name)) {
const newValue = (revision?.config.parameters as Record<string, any>)[
param.name
]
if (newValue !== undefined) {
param.default = newValue
}

const fetchData = async () => {
const revision = await promptRevision(variant.variantId, parseInt(revisionNum))
if (!revision) return

setPromptOptParams((prevState: Parameter[] | null) => {
if (!prevState) {
return prevState
}
return param

const parameterNames = [
"temperature",
"model",
"max_tokens",
"prompt_system",
"prompt_user",
"top_p",
"frequence_penalty",
"presence_penalty",
"inputs",
]

return prevState.map((param: Parameter) => {
if (parameterNames.includes(param.name)) {
const newValue = (revision?.config.parameters as Record<string, any>)[
param.name
]
if (newValue !== undefined) {
param.default = newValue
}
}
return param
})
})
})
}

fetchData()
}, [revisionNum])


const abortControllersRef = useRef<AbortController[]>([])
const [isRunningAll, setIsRunningAll] = useState(false)
Expand Down Expand Up @@ -684,14 +673,6 @@ const App: React.FC<TestViewProps> = ({
have a valid API key for the model you are using.
</p>
</Modal>
<PromptVersioningDrawer
setIsDrawerOpen={setIsDrawerOpen}
setRevisionNum={setRevisionNum}
isDrawerOpen={isDrawerOpen}
historyStatus={historyStatus}
promptRevisions={filteredRevisions}
onStateChange={onStateChange}
/>
</div>
)
}
Expand Down
9 changes: 1 addition & 8 deletions agenta-web/src/lib/hooks/useVariant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {useState, useEffect} from "react"
import {promptVersioning, saveNewVariant, updateVariantParams} from "@/lib/services/api"
import {Variant, Parameter, IPromptVersioning} from "@/lib/Types"
import {Variant, Parameter, IPromptRevisions} from "@/lib/Types"
import {getAllVariantParameters, updateInputParams} from "@/lib/helpers/variantHelper"
import {isDemo} from "../helpers/utils"
import {PERMISSION_ERR_MSG} from "../helpers/axiosConfig"
Expand All @@ -13,7 +13,6 @@ import {PERMISSION_ERR_MSG} from "../helpers/axiosConfig"
* @returns
*/
export function useVariant(appId: string, variant: Variant) {
const [promptRevisions, setPromptRevisions] = useState<IPromptVersioning>()
const [historyStatus, setHistoryStatus] = useState({loading: false, error: false})
const [promptOptParams, setPromptOptParams] = useState<Parameter[] | null>(null)
const [inputParams, setInputParams] = useState<Parameter[] | null>(null)
Expand All @@ -34,10 +33,6 @@ export function useVariant(appId: string, variant: Variant) {
variant,
)
setPromptOptParams(parameters)
if (variant.variantId && isDemo()) {
const revisions = await promptVersioning(variant.variantId)
setPromptRevisions(revisions)
}
setInputParams(inputs)
setURIPath(URIPath)
setIsChatVariant(isChatVariant)
Expand Down Expand Up @@ -115,10 +110,8 @@ export function useVariant(appId: string, variant: Variant) {
saveOptParams,
refetch: fetchParameters,
isChatVariant,
promptRevisions,
historyStatus,
setPromptOptParams,
setPromptRevisions,
setHistoryStatus,
}
}
Expand Down

0 comments on commit f46153e

Please sign in to comment.