1+ using ServiceStack . DataAnnotations ;
2+ using ServiceStack . Web ;
3+
4+ namespace ServiceStack . AI ;
5+
6+ public class ChatCompletionLog : IMeta
7+ {
8+ [ AutoIncrement ]
9+ public long Id { get ; set ; }
10+
11+ /// <summary>
12+ /// Unique user-specified or system generated GUID for Job
13+ /// </summary>
14+ [ Index ( Unique = true ) ] public virtual string ? RefId { get ; set ; }
15+
16+ /// <summary>
17+ /// Associate Job with a tag group
18+ /// </summary>
19+ public virtual string ? Tag { get ; set ; }
20+
21+ /// <summary>
22+ /// The ASP .NET Identity Auth User Id to populate the IRequest Context ClaimsPrincipal and User Session
23+ /// </summary>
24+ public virtual string ? UserId { get ; set ; }
25+
26+ /// <summary>
27+ /// The API Key, if one was used to access the Chat Service
28+ /// </summary>
29+ public virtual string ? ApiKey { get ; set ; }
30+
31+ public string Model { get ; set ; }
32+
33+ public string ? UserPrompt { get ; set ; }
34+
35+ public string ? Answer { get ; set ; }
36+
37+ /// <summary>
38+ /// JSON Body of Request
39+ /// </summary>
40+ [ StringLength ( StringLengthAttribute . MaxText ) ]
41+ public virtual string RequestBody { get ; set ; }
42+
43+ /// <summary>
44+ /// The Response DTO JSON Body
45+ /// </summary>
46+ [ StringLength ( StringLengthAttribute . MaxText ) ]
47+ public virtual string ? ResponseBody { get ; set ; }
48+
49+ public virtual string ? ErrorCode { get ; set ; }
50+
51+ public virtual ResponseStatus ? Error { get ; set ; }
52+
53+ [ Index ] public virtual DateTime CreatedDate { get ; set ; }
54+
55+ public virtual int ? DurationMs { get ; set ; }
56+
57+ public int ? PromptTokens { get ; set ; }
58+
59+ public int ? CompletionTokens { get ; set ; }
60+
61+ public virtual Dictionary < string , string > ? Meta { get ; set ; }
62+ }
63+
64+ public static class ChatCompletionLogUtils
65+ {
66+ public static ChatCompletionLog ToChatCompletionLog ( this IRequest req , ChatCompletion request , ChatResponse response , string ? refId = null )
67+ {
68+ ArgumentNullException . ThrowIfNull ( request ) ;
69+ ArgumentNullException . ThrowIfNull ( response ) ;
70+
71+ var userId = req . GetClaimsPrincipal ( ) ? . GetUserId ( ) ;
72+ var apiKey = req . GetApiKey ( ) ;
73+ userId ??= apiKey ? . UserAuthId ;
74+ userId ??= req . GetSession ( ) ? . UserAuthId ;
75+ var duration = req . GetElapsed ( ) ;
76+
77+ return new ChatCompletionLog
78+ {
79+ RefId = refId ?? req . GetTraceId ( ) ?? Guid . NewGuid ( ) . ToString ( "N" ) ,
80+ UserId = userId ,
81+ ApiKey = apiKey ? . Key ,
82+ Model = request . Model ,
83+ UserPrompt = request . GetUserPrompt ( ) ,
84+ Answer = response . GetAnswer ( ) ,
85+ RequestBody = request . ToJson ( ) ,
86+ ResponseBody = response . ToJson ( ) ,
87+ CreatedDate = DateTime . UtcNow ,
88+ DurationMs = duration != TimeSpan . Zero ? ( int ) duration . TotalMilliseconds : null ,
89+ PromptTokens = response . Usage ? . PromptTokens ,
90+ CompletionTokens = response . Usage ? . CompletionTokens ,
91+ } ;
92+ }
93+
94+ public static ChatCompletionLog ToChatCompletionLog ( this IRequest req , ChatCompletion request , Exception ex , string ? refId = null )
95+ {
96+ ArgumentNullException . ThrowIfNull ( request ) ;
97+ ArgumentNullException . ThrowIfNull ( ex ) ;
98+
99+ var userId = req . GetClaimsPrincipal ( ) ? . GetUserId ( ) ;
100+ var apiKey = req . GetApiKey ( ) ;
101+ userId ??= apiKey ? . UserAuthId ;
102+ userId ??= req . GetSession ( ) ? . UserAuthId ;
103+ var duration = req . GetElapsed ( ) ;
104+
105+ var status = ex . ToResponseStatus ( ) ;
106+
107+ return new ChatCompletionLog
108+ {
109+ RefId = refId ?? req . GetTraceId ( ) ?? Guid . NewGuid ( ) . ToString ( "N" ) ,
110+ UserId = userId ,
111+ ApiKey = apiKey ? . Key ,
112+ Model = request . Model ,
113+ UserPrompt = request . GetUserPrompt ( ) ,
114+ RequestBody = request . ToJson ( ) ,
115+ ErrorCode = status . ErrorCode ,
116+ Error = status ,
117+ CreatedDate = DateTime . UtcNow ,
118+ DurationMs = duration != TimeSpan . Zero ? ( int ) duration . TotalMilliseconds : null ,
119+ } ;
120+ }
121+ }
0 commit comments