Skip to content

Commit 18c1ea7

Browse files
committed
support override and append messages, user field
1 parent c692f18 commit 18c1ea7

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

src/Client.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ChatCompletionCreateParamsNonStreaming } from 'openai/resources'
22

3-
import { getMissingPromptVariables, renderPromptWithVariables, validatePromptVariables } from './helpers'
3+
import { addAppendedMessages, addOverrideMessages, getMissingPromptVariables, renderPromptWithVariables, validatePromptVariables } from './helpers'
44
import { mapPromptToOpenAIConfig } from './helpers/openAi'
55
import { api, createApiClient } from './openapi/client'
66
import {
@@ -10,6 +10,7 @@ import {
1010
EvaluationGroupBody,
1111
PromptConfiguration,
1212
PromptConfigurationBody,
13+
PromptMessage,
1314
PromptTool,
1415
PromptToolBody
1516
} from './types'
@@ -34,26 +35,56 @@ export default class PromptFoundry {
3435
return this.client.getPrompt({ params: { promptId: id }, headers: { 'X-API-KEY': this.apiKey } })
3536
}
3637

37-
public async getPrompt({ id, variables }: { id: string; variables: Record<string, string> }): Promise<PromptConfiguration> {
38+
public async getPrompt({
39+
id,
40+
variables,
41+
appendMessages,
42+
overrideMessages
43+
}: {
44+
id: string
45+
variables: Record<string, string>
46+
appendMessages?: PromptMessage[]
47+
overrideMessages?: PromptMessage[]
48+
userId?: string
49+
}): Promise<PromptConfiguration> {
3850
const result = await this.getRawPrompt({ id })
3951

4052
if (!validatePromptVariables(result, variables)) {
4153
const missingVariables = getMissingPromptVariables(result, variables)
4254
throw new Error(`Missing variables in prompt: ${missingVariables.join(', ')}`)
4355
}
44-
return renderPromptWithVariables(result, variables)
56+
57+
const updatedWithVariables = renderPromptWithVariables(result, variables)
58+
59+
if (overrideMessages) {
60+
return addOverrideMessages(updatedWithVariables, overrideMessages)
61+
}
62+
63+
if (appendMessages) {
64+
return addAppendedMessages(updatedWithVariables, appendMessages)
65+
}
66+
67+
return updatedWithVariables
4568
}
4669

4770
public async getOpenAiPrompt({
4871
id,
49-
variables
72+
variables,
73+
user,
74+
appendMessages,
75+
overrideMessages
5076
}: {
5177
id: string
5278
variables: Record<string, string>
79+
appendMessages?: PromptMessage[]
80+
overrideMessages?: PromptMessage[]
81+
user?: string
5382
}): Promise<ChatCompletionCreateParamsNonStreaming> {
54-
const updatedWithVariables = await this.getPrompt({ id, variables })
83+
const updatedWithVariables = await this.getPrompt({ id, variables, appendMessages, overrideMessages })
5584

56-
return mapPromptToOpenAIConfig(updatedWithVariables)
85+
return mapPromptToOpenAIConfig(updatedWithVariables, {
86+
user
87+
})
5788
}
5889

5990
public createPrompt(data: PromptConfigurationBody): Promise<PromptConfiguration> {

src/helpers/openAi.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ const getTools = (tools: PromptTool[]): ChatCompletionTool[] | undefined => {
114114
return tools.map((tool) => mapToolToOpenAi(tool))
115115
}
116116

117-
export const mapPromptToOpenAIConfig = (promptConfig: PromptConfiguration): ChatCompletionCreateParamsNonStreaming => {
117+
export const mapPromptToOpenAIConfig = (
118+
promptConfig: PromptConfiguration,
119+
{ user }: { user?: string } = {}
120+
): ChatCompletionCreateParamsNonStreaming => {
118121
const { messages: promptMessages, parameters, tools } = promptConfig
119122

120123
const messages = mapMessagesToOpenAI(promptMessages)
@@ -132,6 +135,7 @@ export const mapPromptToOpenAIConfig = (promptConfig: PromptConfiguration): Chat
132135
response_format: {
133136
type: parameters.responseFormat === 'JSON' ? 'json_object' : 'text'
134137
},
135-
tools: getTools(tools)
138+
tools: getTools(tools),
139+
user
136140
}
137141
}

src/helpers/template.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ export function renderPromptWithVariables(prompt: PromptConfiguration, variables
4949
}
5050
}
5151

52+
export function addAppendedMessages(prompt: PromptConfiguration, messages: PromptMessage[]): PromptConfiguration {
53+
return {
54+
...prompt,
55+
messages: [...prompt.messages, ...messages]
56+
}
57+
}
58+
59+
export function addOverrideMessages(prompt: PromptConfiguration, messages: PromptMessage[]): PromptConfiguration {
60+
return {
61+
...prompt,
62+
messages
63+
}
64+
}
65+
5266
export function getMissingPromptVariables(prompt: PromptConfiguration, variables: Record<string, string>): string[] {
5367
const variablesInPrompt = extractVariablesFromMessages(prompt.messages)
5468

0 commit comments

Comments
 (0)