Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,15 @@ function buildPythonSnippet(
host: string,
projectApiKey: string,
variables: string[],
labelName?: string
labelName?: string,
showConfig?: boolean
): string {
const compileLines = variables.length
? `\nsystem_prompt = prompts.compile(result.prompt, {${variables.map((v) => `${JSON.stringify(v)}: '...'`).join(', ')}})`
: ''
const configLines = showConfig
? `\nmodel = (result.config or {}).get('model', 'your-default-model') # config stored with this prompt version`
: ''
const labelArg = labelName ? `label=${JSON.stringify(labelName)}, ` : ''
return `from posthog import Posthog
from posthog.ai.prompts import Prompts
Expand All @@ -546,7 +550,7 @@ posthog = Posthog(
)
prompts = Prompts(posthog)

result = prompts.get(${JSON.stringify(promptName)}, ${labelArg}with_metadata=True, fallback='You are a helpful assistant.')${compileLines}
result = prompts.get(${JSON.stringify(promptName)}, ${labelArg}with_metadata=True, fallback='You are a helpful assistant.')${compileLines}${configLines}
# result.name / result.version -> send as $ai_prompt_name / $ai_prompt_version on your LLM events`
}

Expand All @@ -555,11 +559,15 @@ function buildNodeSnippet(
host: string,
projectApiKey: string,
variables: string[],
labelName?: string
labelName?: string,
showConfig?: boolean
): string {
const compileLines = variables.length
? `\nconst systemPrompt = prompts.compile(result.prompt, {${variables.map((v) => ` ${JSON.stringify(v)}: '...'`).join(',')} })`
: ''
const configLines = showConfig
? `\nconst model = result.config?.model ?? 'your-default-model' // config stored with this prompt version`
: ''
const labelArg = labelName ? `label: '${labelName}', ` : ''
return `import { Prompts } from '@posthog/ai'
import { PostHog } from 'posthog-node'
Expand All @@ -570,7 +578,7 @@ const posthog = new PostHog('${projectApiKey}', {
})
const prompts = new Prompts({ posthog })

const result = await prompts.get(${JSON.stringify(promptName)}, { ${labelArg}fallback: 'You are a helpful assistant.' })${compileLines}
const result = await prompts.get(${JSON.stringify(promptName)}, { ${labelArg}fallback: 'You are a helpful assistant.' })${compileLines}${configLines}
// result.name / result.version -> send as $ai_prompt_name / $ai_prompt_version on your LLM events`
}

Expand All @@ -588,6 +596,8 @@ export function PromptCodeSnippets({ prompt }: { prompt: LLMPrompt }): JSX.Eleme
const snippetLabel = labelsEnabled
? (promptLabels.find((label) => label.name === 'production') ?? promptLabels[0])?.name
: undefined
// Same principle for config: only show the usage line once this prompt has one.
const showConfig = !!featureFlags[FEATURE_FLAGS.LLM_PROMPT_CONFIG] && prompt.config != null

return (
<div className="mb-6" data-attr="llma-prompt-code-snippets">
Expand Down Expand Up @@ -620,7 +630,14 @@ export function PromptCodeSnippets({ prompt }: { prompt: LLMPrompt }): JSX.Eleme
label: 'Python',
content: (
<CodeSnippet language={Language.Python}>
{buildPythonSnippet(prompt.name, host, projectApiKey, variables, snippetLabel)}
{buildPythonSnippet(
prompt.name,
host,
projectApiKey,
variables,
snippetLabel,
showConfig
)}
</CodeSnippet>
),
},
Expand All @@ -629,7 +646,14 @@ export function PromptCodeSnippets({ prompt }: { prompt: LLMPrompt }): JSX.Eleme
label: 'Node.js',
content: (
<CodeSnippet language={Language.JavaScript}>
{buildNodeSnippet(prompt.name, host, projectApiKey, variables, snippetLabel)}
{buildNodeSnippet(
prompt.name,
host,
projectApiKey,
variables,
snippetLabel,
showConfig
)}
</CodeSnippet>
),
},
Expand Down
Loading