11import { ChatCompletionCreateParamsNonStreaming } from 'openai/resources'
22
3- import { getMissingPromptVariables , renderPromptWithVariables , validatePromptVariables } from './helpers'
3+ import { addAppendedMessages , addOverrideMessages , getMissingPromptVariables , renderPromptWithVariables , validatePromptVariables } from './helpers'
44import { mapPromptToOpenAIConfig } from './helpers/openAi'
55import { api , createApiClient } from './openapi/client'
66import {
@@ -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 > {
0 commit comments