11using Microsoft . AspNetCore . Mvc ;
22using Microsoft . Extensions . Options ;
3- using Azure ;
4- using Azure . AI . Agents . Persistent ;
3+ using Azure . AI . Projects ;
4+ using Azure . AI . Projects . OpenAI ;
5+ using OpenAI . Responses ;
56using chatui . Configuration ;
67
78namespace chatui . Controllers ;
@@ -10,56 +11,51 @@ namespace chatui.Controllers;
1011[ Route ( "[controller]/[action]" ) ]
1112
1213public class ChatController (
13- PersistentAgentsClient client ,
14+ AIProjectClient projectClient ,
1415 IOptionsMonitor < ChatApiOptions > options ,
1516 ILogger < ChatController > logger ) : ControllerBase
1617{
17- private readonly PersistentAgentsClient _client = client ;
18+ private readonly AIProjectClient _projectClient = projectClient ;
1819 private readonly IOptionsMonitor < ChatApiOptions > _options = options ;
1920 private readonly ILogger < ChatController > _logger = logger ;
2021
21- // TODO: [security] Do not trust client to provide threadId . Instead map current user to their active threadid in your application's own state store.
22- // Without this security control in place, a user can inject messages into another user's thread .
23- [ HttpPost ( "{threadId }" ) ]
24- public async Task < IActionResult > Completions ( [ FromRoute ] string threadId , [ FromBody ] string prompt )
22+ // TODO: [security] Do not trust client to provide conversationId . Instead map current user to their active converstaionid in your application's own state store.
23+ // Without this security control in place, a user can inject messages into another user's conversation .
24+ [ HttpPost ( "{conversationId }" ) ]
25+ public async Task < IActionResult > Completions ( [ FromRoute ] string conversationId , [ FromBody ] string message )
2526 {
26- if ( string . IsNullOrWhiteSpace ( prompt ) )
27- throw new ArgumentException ( "Prompt cannot be null, empty, or whitespace." , nameof ( prompt ) ) ;
27+ if ( string . IsNullOrWhiteSpace ( message ) )
28+ throw new ArgumentException ( "Message cannot be null, empty, or whitespace." , nameof ( message ) ) ;
29+ _logger . LogDebug ( "Prompt received {Prompt}" , message ) ;
2830
29- _logger . LogDebug ( "Prompt received {Prompt}" , prompt ) ;
30- var _config = _options . CurrentValue ;
31-
32- PersistentThreadMessage message = await _client . Messages . CreateMessageAsync (
33- threadId ,
34- MessageRole . User ,
35- prompt ) ;
31+ #pragma warning disable OPENAI001
32+ MessageResponseItem userMessageResponseItem = ResponseItem . CreateUserMessageItem (
33+ [ ResponseContentPart . CreateInputTextPart ( message ) ] ) ;
3634
37- ThreadRun run = await _client . Runs . CreateRunAsync ( threadId , _config . AIAgentId ) ;
35+ var _config = _options . CurrentValue ;
36+ AgentRecord agentRecord = await _projectClient . Agents . GetAgentAsync ( _config . AIAgentId ) ;
37+ var agent = agentRecord . Versions . Latest ;
3838
39- while ( run . Status == RunStatus . Queued || run . Status == RunStatus . InProgress || run . Status == RunStatus . RequiresAction )
40- {
41- await Task . Delay ( TimeSpan . FromMilliseconds ( 500 ) ) ;
42- run = ( await _client . Runs . GetRunAsync ( threadId , run . Id ) ) . Value ;
43- }
39+ ProjectResponsesClient responsesClient
40+ = _projectClient . OpenAI . GetProjectResponsesClientForAgent ( agent , conversationId ) ;
4441
45- Pageable < PersistentThreadMessage > messages = _client . Messages . GetMessages (
46- threadId : threadId , order : ListSortOrder . Ascending ) ;
42+ var agentResponseItem = await responsesClient . CreateResponseAsync ( [ userMessageResponseItem ] ) ;
4743
48- var fullText =
49- messages
50- . Where ( m => m . Role == MessageRole . Agent )
51- . SelectMany ( m => m . ContentItems . OfType < MessageTextContent > ( ) )
52- . Last ( ) . Text ;
44+ var fullText = agentResponseItem . Value . GetOutputText ( ) ;
5345
5446 return Ok ( new { data = fullText } ) ;
5547 }
5648
5749 [ HttpPost ]
58- public async Task < IActionResult > Threads ( )
50+ public async Task < IActionResult > Conversations ( )
5951 {
60- // TODO [performance efficiency] Delay creating a thread until the first user message arrives.
61- PersistentAgentThread thread = await _client . Threads . CreateThreadAsync ( ) ;
52+ // TODO [performance efficiency] Delay creating a conversation until the first user message arrives.
53+ ProjectConversationCreationOptions conversationOptions = new ( ) ;
54+
55+ ProjectConversation conversation
56+ = await _projectClient . OpenAI . Conversations . CreateProjectConversationAsync (
57+ conversationOptions ) ;
6258
63- return Ok ( new { id = thread . Id } ) ;
59+ return Ok ( new { id = conversation . Id } ) ;
6460 }
6561}
0 commit comments