@@ -15,6 +15,7 @@ public class GptClient
1515 private readonly OpenAIClient _api ;
1616 private readonly GptCustomCommands _customCommands ;
1717 private readonly ILogger _log ;
18+ private readonly GptDefaults _gptDefaults ;
1819
1920 /// <summary>
2021 /// Initializes static members of the <see cref="GptClient" /> class.
@@ -34,7 +35,7 @@ static GptClient()
3435 /// <param name="customCommands">Custom commands handler</param>
3536 /// <param name="log">The logger instance.</param>
3637 /// <param name="settings">The API settings.</param>
37- public GptClient ( GptCustomCommands customCommands , ILogger < GptClient > log , IOptions < ApiSettings > settings )
38+ public GptClient ( GptCustomCommands customCommands , ILogger < GptClient > log , IOptions < GptDefaults > gptDefaults , IOptions < ApiSettings > settings )
3839 {
3940 var httpClient = new HttpClient ( )
4041 {
@@ -43,6 +44,7 @@ public GptClient(GptCustomCommands customCommands, ILogger<GptClient> log, IOpti
4344 _api = new OpenAIClient ( settings . Value . OpenAIKey , OpenAIClientSettings . Default , httpClient ) ;
4445 _customCommands = customCommands ;
4546 _log = log ;
47+ _gptDefaults = gptDefaults . Value ;
4648 }
4749
4850 /// <summary>
@@ -55,18 +57,16 @@ public async Task<GptResponse> GeneratePrompt(List<WritableChatPrompt> chatPromp
5557 {
5658 // get the last prompt
5759 var userPrompt = chatPrompts . Last ( chatPrompt => chatPrompt . Role == "user" ) ;
58- var prompt = new GptRequest
59- {
60- UserId = userId ,
61- Prompt = userPrompt . Content
62- } ;
60+ var prompt = GptRequest . Default ( _gptDefaults ) ;
61+ prompt . UserId = userId ;
62+ prompt . Prompt = userPrompt . Content ;
6363
6464 var chatRequest = ParseRequest ( chatPrompts , prompt ) ;
6565
6666 try
6767 {
6868 var result = await _api . ChatEndpoint . GetCompletionAsync ( chatRequest ) ;
69- _log . LogInformation ( "GPT-3 response: {Response}" , result . FirstChoice ) ;
69+ _log . LogInformation ( "GPT response: {Response}" , result . FirstChoice ) ;
7070
7171 return new GptResponse
7272 {
@@ -96,7 +96,8 @@ private ChatRequest ParseRequest(List<WritableChatPrompt> chatPrompts, GptReques
9696 {
9797 foreach ( var chatPrompt in chatPrompts )
9898 {
99- var content = new GptRequest { Prompt = chatPrompt . Content } ;
99+ var content = GptRequest . Default ( _gptDefaults ) ;
100+ content . Prompt = chatPrompt . Content ;
100101 ResolveModel ( ref content ) ;
101102 ResolveParameters ( ref content ) ;
102103 chatPrompt . Content = content . Prompt ;
@@ -106,12 +107,12 @@ private ChatRequest ParseRequest(List<WritableChatPrompt> chatPrompts, GptReques
106107 ResolveParameters ( ref request ) ;
107108
108109 WritableChatPrompt system ;
109- if ( request . System != null )
110- system = new WritableChatPrompt ( "system" , request . System ) ;
110+ if ( request . System . ShoudReplace )
111+ system = new WritableChatPrompt ( "system" , request . System . Build ( ) ) ;
111112 else
112113 {
113114 system = new WritableChatPrompt ( "system" ,
114- $ "You are a helpful assistant. Today is { DateTime . Now : yyyy-MM-ddTHH:mm:ssZ} " ) ;
115+ $ "You are a helpful assistant. Today is { DateTime . Now : yyyy-MM-ddTHH:mm:ssZ} " + request . System . Build ( ) ) ;
115116 }
116117
117118 var requestPrompts = new List < WritableChatPrompt > ( ) ;
@@ -162,9 +163,13 @@ public static void ResolveModel(ref GptRequest input)
162163 }
163164 }
164165
165- if ( ! modelFound )
166+ if ( modelFound ) return ;
167+
168+ var inputModel = input . Model ;
169+ // check if current model is valid
170+ if ( Models . All ( modelInfo => modelInfo . Model != inputModel ) )
166171 {
167- // if no match is found , set model property of input to the model of the first item in the models array
172+ // if not , set model property of input to the model of the first item in the models array
168173 input . Model = Models [ 0 ] . Model ;
169174 }
170175 }
@@ -187,14 +192,7 @@ public void ResolveParameters(ref GptRequest input)
187192 var paramValueTrim = match . Groups [ 2 ] ? . Value . Trim ( ) ?? string . Empty ;
188193 var paramValue = paramValueTrim . Trim ( '"' ) ;
189194
190- var paramNameIndex = inputPrompt . IndexOf ( paramName , StringComparison . InvariantCultureIgnoreCase ) ;
191- var paramEndIndex = paramNameIndex + paramName . Length + paramValueTrim . Length + 2 ;
192-
193- if ( lastIndex + 5 < paramNameIndex ) break ;
194-
195- lastIndex = paramEndIndex ;
196- input . Prompt = input . Prompt . Replace ( paramName + " " + paramValueTrim , "" ) . Trim ( ) ;
197-
195+ bool hasValue = true ;
198196 try
199197 {
200198 switch ( paramName )
@@ -218,19 +216,49 @@ public void ResolveParameters(ref GptRequest input)
218216 input . Model = paramValue . ToLowerInvariant ( ) ;
219217 break ;
220218 case "-system" :
221- input . System = paramValue ;
219+ input . System . Replace ( paramValue ) ;
222220 break ;
223221 default :
224222 if ( _customCommands . TryResolveCommand ( paramName , out var prompt ) )
225223 {
226- input . Prompt = prompt + "\n " + input . Prompt ;
224+ if ( prompt . AsSystem )
225+ {
226+ input . System . Append ( prompt ! . Prompt ) ;
227+ }
228+ else
229+ {
230+ input . Prompt = prompt ! . Prompt + "\n " + input . Prompt ;
231+ }
232+ hasValue = false ;
227233 }
228234 else
229235 {
230236 Console . WriteLine ( $ "Unrecognized parameter: { paramName } ") ;
231237 }
232238 break ;
233239 }
240+
241+ // Trim the input Prompt to remove the parameter,
242+ // update last index to check if we've reached the end of the parameters
243+ int paramNameIndex = inputPrompt . IndexOf ( paramName , StringComparison . InvariantCultureIgnoreCase ) ;
244+
245+ int paramEndIndex ;
246+ string searchString ;
247+ if ( hasValue )
248+ {
249+ paramEndIndex = paramNameIndex + paramName . Length + paramValueTrim . Length + 2 ;
250+ searchString = paramName + " " + paramValueTrim ;
251+ }
252+ else
253+ {
254+ paramEndIndex = paramNameIndex + paramName . Length + 2 ;
255+ searchString = paramName + " " ;
256+ }
257+
258+ if ( lastIndex + 5 < paramNameIndex ) break ;
259+
260+ lastIndex = paramEndIndex ;
261+ input . Prompt = input . Prompt . Replace ( searchString , "" ) . Trim ( ) ;
234262 }
235263 catch ( Exception e )
236264 {
0 commit comments