File tree Expand file tree Collapse file tree 3 files changed +31
-4
lines changed
Expand file tree Collapse file tree 3 files changed +31
-4
lines changed Original file line number Diff line number Diff line change @@ -95,6 +95,7 @@ public async Task<ChatResponse> GetResponseAsync(IEnumerable<ChatMessage> messag
9595
9696 return new ChatResponse ( message )
9797 {
98+ ConversationId = options is GrokChatOptions { StoreMessages : true } ? response . Id : null ,
9899 ResponseId = response . Id ,
99100 ModelId = response . Model ,
100101 CreatedAt = response . Created . ToDateTimeOffset ( ) ,
@@ -298,6 +299,11 @@ codeResult.RawRepresentation is ToolCall codeToolCall &&
298299 IList < IncludeOption > includes = [ IncludeOption . InlineCitations ] ;
299300 if ( options is GrokChatOptions grokOptions )
300301 {
302+ request . StoreMessages = grokOptions . StoreMessages == true ;
303+ // TODO: should we throw if we find a conversation id but not StoreMessages=true as it might be a user error?
304+ if ( request . StoreMessages && options . ConversationId is not null )
305+ request . PreviousResponseId = options . ConversationId ;
306+
301307 // NOTE: overrides our default include for inline citations, potentially.
302308 request . Include . Clear ( ) ;
303309 request . Include . AddRange ( grokOptions . Include ) ;
Original file line number Diff line number Diff line change @@ -38,4 +38,7 @@ public class GrokChatOptions : ChatOptions
3838 /// <summary>Additional outputs to include in responses.</summary>
3939 /// <remarks>Defaults to including <see cref="IncludeOption.InlineCitations"/>.</remarks>
4040 public IList < IncludeOption > Include { get ; set ; } = [ IncludeOption . InlineCitations ] ;
41+
42+ /// <summary>Whether to store request and responses.</summary>
43+ public bool ? StoreMessages { get ; set ; }
4144}
Original file line number Diff line number Diff line change 11using System . Text . Json ;
2- using System . Text . Json . Nodes ;
3- using Azure ;
42using Devlooped . Extensions . AI . Grok ;
53using Devlooped . Grok ;
64using Microsoft . Extensions . AI ;
7- using OpenAI . Realtime ;
85using static ConfigurationExtensions ;
9- using OpenAIClientOptions = OpenAI . OpenAIClientOptions ;
106
117namespace Devlooped . Extensions . AI ;
128
139public class GrokTests ( ITestOutputHelper output )
1410{
11+ [ SecretsFact ( "XAI_API_KEY" ) ]
12+ public async Task GrokRemembersConversation ( )
13+ {
14+ var chat = new GrokClient ( Configuration [ "XAI_API_KEY" ] ! ) . AsIChatClient ( "grok-4-1-fast-non-reasoning" )
15+ . AsBuilder ( )
16+ . UseLogging ( output . AsLoggerFactory ( ) )
17+ . Build ( ) ;
18+
19+ var options = new GrokChatOptions { StoreMessages = true } ;
20+ var response = await chat . GetResponseAsync ( "Hey, my alias is kzu. Keep that in mind." , options ) ;
21+
22+ Assert . NotNull ( response . ConversationId ) ;
23+
24+ var id = response . ConversationId ;
25+ options . ConversationId = id ;
26+
27+ response = await chat . GetResponseAsync ( "What is my alias?" , options ) ;
28+
29+ Assert . Contains ( "kzu" , response . Text ) ;
30+ }
31+
32+
1533 [ SecretsFact ( "XAI_API_KEY" ) ]
1634 public async Task GrokInvokesTools ( )
1735 {
You can’t perform that action at this time.
0 commit comments