Repository navigation
feat(prompts): insert, display, and preview prompt references in the editor #102368
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
18e48f0
feat(prompts): insert, display, and preview prompt references in the …
jurajmajerik 551cbe7
fix(prompts): search the reference picker server-side and harden prev…
jurajmajerik 18339f4
fix(prompts): resolve preview against the url version and clear it on…
jurajmajerik d19d87f
fix(prompts): drop the empty-render story the smoke test cannot scree…
jurajmajerik 648a782
chore(visual): update storybook baselines
posthog[bot] 5e46a9f
fix(prompts): extract variables from the displayed text
jurajmajerik 7ddacf8
fix(prompts): clear resolved preview when any prompt load starts
jurajmajerik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
products/ai_observability/frontend/prompts/InsertPromptReferenceButton.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { useActions, useValues } from 'kea' | ||
| import { useState } from 'react' | ||
|
|
||
| import { IconPlusSmall } from '@posthog/icons' | ||
| import { LemonButton, LemonModal } from '@posthog/lemon-ui' | ||
|
|
||
| import { insertPromptReferenceLogic } from './insertPromptReferenceLogic' | ||
| import { PromptReferencePicker } from './PromptReferencePicker' | ||
|
|
||
| export function InsertPromptReferenceButton({ | ||
| currentPromptName, | ||
| onInsert, | ||
| }: { | ||
| currentPromptName: string | ||
| onInsert: (tag: string) => void | ||
| }): JSX.Element { | ||
| const { promptOptionsLoading } = useValues(insertPromptReferenceLogic) | ||
| const { loadPromptOptions } = useActions(insertPromptReferenceLogic) | ||
| const [isOpen, setIsOpen] = useState(false) | ||
|
|
||
| return ( | ||
| <> | ||
| <LemonButton | ||
| size="small" | ||
| icon={<IconPlusSmall />} | ||
| loading={!isOpen && promptOptionsLoading} | ||
| tooltip="Insert a reference to another prompt. Its content replaces the tag when the prompt is fetched." | ||
| data-attr="llma-prompt-insert-reference-button" | ||
| onClick={(e) => { | ||
| e.preventDefault() | ||
| if (promptOptionsLoading) { | ||
| return | ||
| } | ||
| setIsOpen(true) | ||
| loadPromptOptions('') | ||
| }} | ||
| > | ||
| Insert reference | ||
| </LemonButton> | ||
| <LemonModal | ||
| isOpen={isOpen} | ||
| onClose={() => setIsOpen(false)} | ||
| title="Insert prompt reference" | ||
| description="Reference a label to pick up its future moves, or pin a version to freeze the content." | ||
| width={480} | ||
| > | ||
| <PromptReferencePicker | ||
| currentPromptName={currentPromptName} | ||
| onSelect={(tag) => { | ||
| onInsert(tag) | ||
| setIsOpen(false) | ||
| }} | ||
| /> | ||
| </LemonModal> | ||
| </> | ||
| ) | ||
| } | ||
89 changes: 89 additions & 0 deletions
89
products/ai_observability/frontend/prompts/PromptReferencePicker.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { useActions, useValues } from 'kea' | ||
| import { useState } from 'react' | ||
|
|
||
| import { Combobox, ComboboxContent, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList } from 'lib/ui/quill' | ||
|
|
||
| import { insertPromptReferenceLogic } from './insertPromptReferenceLogic' | ||
| import { buildPromptReferenceTag } from './promptReferences' | ||
|
|
||
| interface ReferenceOption { | ||
| tag: string | ||
| promptName: string | ||
| selectorLabel: string | ||
| } | ||
|
|
||
| export function PromptReferencePicker({ | ||
| currentPromptName, | ||
| onSelect, | ||
| }: { | ||
| currentPromptName: string | ||
| onSelect: (tag: string) => void | ||
| }): JSX.Element { | ||
| const { promptOptions, promptOptionsLoading } = useValues(insertPromptReferenceLogic) | ||
| const { loadPromptOptions } = useActions(insertPromptReferenceLogic) | ||
| const [inputValue, setInputValue] = useState('') | ||
|
|
||
| // Keys double as display strings so the combobox's text filtering matches | ||
| // what the item shows. The "(pinned)" suffix cannot collide with a label | ||
| // row: label names allow no spaces or parentheses. | ||
| const optionsByKey = new Map<string, ReferenceOption>() | ||
| for (const prompt of promptOptions) { | ||
| if (prompt.name === currentPromptName) { | ||
| continue | ||
| } | ||
| for (const label of prompt.all_labels) { | ||
| optionsByKey.set(`${prompt.name} @ ${label.name}`, { | ||
| tag: buildPromptReferenceTag(prompt.name, { label: label.name }), | ||
| promptName: prompt.name, | ||
| selectorLabel: `@ ${label.name} (follows the label)`, | ||
| }) | ||
| } | ||
| optionsByKey.set(`${prompt.name} @ v${prompt.latest_version} (pinned)`, { | ||
| tag: buildPromptReferenceTag(prompt.name, { version: prompt.latest_version }), | ||
| promptName: prompt.name, | ||
| selectorLabel: `@ v${prompt.latest_version} (pinned)`, | ||
| }) | ||
| } | ||
| const items = [...optionsByKey.keys()] | ||
|
|
||
| return ( | ||
| <Combobox | ||
| autoHighlight | ||
| items={items} | ||
| value={null} | ||
| onValueChange={(next: string | null) => { | ||
| const option = next ? optionsByKey.get(next) : undefined | ||
| if (option) { | ||
| onSelect(option.tag) | ||
| } | ||
| }} | ||
| inputValue={inputValue} | ||
| onInputValueChange={(value: string) => { | ||
| setInputValue(value) | ||
| loadPromptOptions(value) | ||
| }} | ||
| defaultOpen | ||
| > | ||
| <ComboboxInput | ||
| placeholder="Search prompts" | ||
| autoFocus | ||
| showTrigger={false} | ||
| data-attr="llma-prompt-insert-reference-input" | ||
| /> | ||
| <ComboboxContent> | ||
| <ComboboxEmpty>{promptOptionsLoading ? 'Loading prompts...' : 'No matching prompts'}</ComboboxEmpty> | ||
| <ComboboxList> | ||
| {(item: string) => { | ||
| const option = optionsByKey.get(item) | ||
| return ( | ||
| <ComboboxItem key={item} value={item} className="ps-2"> | ||
| <span className="font-medium">{option?.promptName}</span> | ||
| <span className="text-xs text-secondary">{option?.selectorLabel}</span> | ||
| </ComboboxItem> | ||
| ) | ||
| }} | ||
| </ComboboxList> | ||
| </ComboboxContent> | ||
| </Combobox> | ||
| ) | ||
| } |
17 changes: 17 additions & 0 deletions
17
products/ai_observability/frontend/prompts/PromptReferenceTags.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react' | ||
|
|
||
| import { PromptReferenceTags } from './PromptReferenceTags' | ||
|
|
||
| const meta: Meta<typeof PromptReferenceTags> = { | ||
| title: 'Scenes-App/AI Observability/PromptReferenceTags', | ||
| component: PromptReferenceTags, | ||
| } | ||
| export default meta | ||
|
|
||
| type Story = StoryObj<typeof PromptReferenceTags> | ||
|
|
||
| export const LabelAndVersionReferences: Story = { | ||
| args: { | ||
| text: 'Intro.\n@@@prompt:name=guardrails|label=production@@@\nMore text.\n@@@prompt:name=tone|version=3@@@', | ||
| }, | ||
| } |
28 changes: 28 additions & 0 deletions
28
products/ai_observability/frontend/prompts/PromptReferenceTags.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { LemonTag, Link } from '@posthog/lemon-ui' | ||
|
|
||
| import { urls } from 'scenes/urls' | ||
|
|
||
| import { extractPromptReferences } from './promptReferences' | ||
|
|
||
| export function PromptReferenceTags({ text }: { text: string }): JSX.Element | null { | ||
| const references = extractPromptReferences(text) | ||
| if (references.length === 0) { | ||
| return null | ||
| } | ||
| return ( | ||
| <div className="flex flex-wrap items-center gap-1"> | ||
| <span className="text-xs text-secondary">References:</span> | ||
| {references.map((reference) => ( | ||
| <Link | ||
| key={reference.raw} | ||
| to={urls.aiObservabilityPrompt(reference.name)} | ||
| data-attr="llma-prompt-reference-tag" | ||
| > | ||
| <LemonTag type="completion" size="small"> | ||
| {reference.name} @ {reference.label ?? `v${reference.version}`} | ||
| </LemonTag> | ||
| </Link> | ||
| ))} | ||
| </div> | ||
| ) | ||
| } |
61 changes: 61 additions & 0 deletions
61
products/ai_observability/frontend/prompts/insertPromptReferenceLogic.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { MakeLogicType, kea, path } from 'kea' | ||
| import { loaders } from 'kea-loaders' | ||
|
|
||
| import { ApiConfig } from '~/lib/api' | ||
|
|
||
| import { llmPromptsList } from '../generated/api' | ||
| import type { LLMPromptListApi } from '../generated/api.schemas' | ||
|
|
||
| // Generated by kea-typegen. Update if you're an agent, ignore if you're human. | ||
| export interface insertPromptReferenceLogicValues { | ||
| promptOptions: LLMPromptListApi[] | ||
| promptOptionsLoading: boolean | ||
| } | ||
|
|
||
| // Generated by kea-typegen. Update if you're an agent, ignore if you're human. | ||
| export interface insertPromptReferenceLogicActions { | ||
| loadPromptOptions: (search: string) => string | ||
| loadPromptOptionsFailure: ( | ||
| error: string, | ||
| errorObject?: any | ||
| ) => { | ||
| error: string | ||
| errorObject?: any | ||
| } | ||
| loadPromptOptionsSuccess: ( | ||
| promptOptions: LLMPromptListApi[], | ||
| payload?: string | ||
| ) => { | ||
| promptOptions: LLMPromptListApi[] | ||
| payload?: string | ||
| } | ||
| } | ||
|
|
||
| export type insertPromptReferenceLogicType = MakeLogicType< | ||
| insertPromptReferenceLogicValues, | ||
| insertPromptReferenceLogicActions | ||
| > | ||
|
|
||
| export const insertPromptReferenceLogic = kea<insertPromptReferenceLogicType>([ | ||
| path(['scenes', 'ai-observability', 'insertPromptReferenceLogic']), | ||
| loaders({ | ||
| promptOptions: [ | ||
| [] as LLMPromptListApi[], | ||
| { | ||
| loadPromptOptions: async (search: string, breakpoint) => { | ||
| if (search) { | ||
| await breakpoint(250) | ||
| } | ||
| const response = await llmPromptsList(String(ApiConfig.getCurrentTeamId()), { | ||
| content: 'none', | ||
| order_by: 'name', | ||
| limit: 100, | ||
| ...(search ? { search } : {}), | ||
| }) | ||
| breakpoint() | ||
| return response.results | ||
|
jurajmajerik marked this conversation as resolved.
Resolved
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| }, | ||
| }, | ||
| ], | ||
| }), | ||
| ]) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.