11// Licensed under the MIT License. See LICENSE in the project root for license information.
22
33using OpenAI . Extensions ;
4- using System . Net . Http ;
4+ using System . Collections . Generic ;
55using System . Text . Json ;
66using System . Threading ;
77using System . Threading . Tasks ;
@@ -14,6 +14,11 @@ internal AssistantsEndpoint(OpenAIClient client) : base(client) { }
1414
1515 protected override string Root => "assistants" ;
1616
17+ internal override IReadOnlyDictionary < string , IEnumerable < string > > Headers { get ; } = new Dictionary < string , IEnumerable < string > >
18+ {
19+ { "OpenAI-Beta" , [ "assistants=v2" ] }
20+ } ;
21+
1722 /// <summary>
1823 /// Get list of assistants.
1924 /// </summary>
@@ -22,9 +27,7 @@ internal AssistantsEndpoint(OpenAIClient client) : base(client) { }
2227 /// <returns><see cref="ListResponse{AssistantResponse}"/>.</returns>
2328 public async Task < ListResponse < AssistantResponse > > ListAssistantsAsync ( ListQuery query = null , CancellationToken cancellationToken = default )
2429 {
25- using var message = new HttpRequestMessage ( HttpMethod . Get , GetUrl ( queryParameters : query ) ) ;
26- message . Headers . Add ( "OpenAI-Beta" , "assistants=v2" ) ;
27- using var response = await HttpClient . SendAsync ( message , cancellationToken ) . ConfigureAwait ( false ) ;
30+ using var response = await GetAsync ( GetUrl ( queryParameters : query ) , cancellationToken ) . ConfigureAwait ( false ) ;
2831 return await response . DeserializeAsync < ListResponse < AssistantResponse > > ( EnableDebug , client , cancellationToken ) . ConfigureAwait ( false ) ;
2932 }
3033
@@ -59,10 +62,7 @@ public async Task<AssistantResponse> CreateAssistantAsync(CreateAssistantRequest
5962 {
6063 request ??= new CreateAssistantRequest ( ) ;
6164 using var payload = JsonSerializer . Serialize ( request , OpenAIClient . JsonSerializationOptions ) . ToJsonStringContent ( ) ;
62- using var message = new HttpRequestMessage ( HttpMethod . Post , GetUrl ( ) ) ;
63- message . Headers . Add ( "OpenAI-Beta" , "assistants=v2" ) ;
64- message . Content = payload ;
65- using var response = await HttpClient . SendAsync ( message , cancellationToken ) . ConfigureAwait ( false ) ;
65+ using var response = await PostAsync ( GetUrl ( ) , payload , cancellationToken ) . ConfigureAwait ( false ) ;
6666 return await response . DeserializeAsync < AssistantResponse > ( EnableDebug , payload , client , cancellationToken ) . ConfigureAwait ( false ) ;
6767 }
6868
@@ -74,9 +74,7 @@ public async Task<AssistantResponse> CreateAssistantAsync(CreateAssistantRequest
7474 /// <returns><see cref="AssistantResponse"/>.</returns>
7575 public async Task < AssistantResponse > RetrieveAssistantAsync ( string assistantId , CancellationToken cancellationToken = default )
7676 {
77- using var message = new HttpRequestMessage ( HttpMethod . Get , GetUrl ( $ "/{ assistantId } ") ) ;
78- message . Headers . Add ( "OpenAI-Beta" , "assistants=v2" ) ;
79- using var response = await HttpClient . SendAsync ( message , cancellationToken ) . ConfigureAwait ( false ) ;
77+ using var response = await GetAsync ( GetUrl ( $ "/{ assistantId } ") , cancellationToken ) . ConfigureAwait ( false ) ;
8078 return await response . DeserializeAsync < AssistantResponse > ( EnableDebug , client , cancellationToken ) . ConfigureAwait ( false ) ;
8179 }
8280
@@ -90,10 +88,7 @@ public async Task<AssistantResponse> RetrieveAssistantAsync(string assistantId,
9088 public async Task < AssistantResponse > ModifyAssistantAsync ( string assistantId , CreateAssistantRequest request , CancellationToken cancellationToken = default )
9189 {
9290 using var payload = JsonSerializer . Serialize ( request , OpenAIClient . JsonSerializationOptions ) . ToJsonStringContent ( ) ;
93- using var message = new HttpRequestMessage ( HttpMethod . Post , GetUrl ( $ "/{ assistantId } ") ) ;
94- message . Headers . Add ( "OpenAI-Beta" , "assistants=v2" ) ;
95- message . Content = payload ;
96- using var response = await HttpClient . SendAsync ( message , cancellationToken ) . ConfigureAwait ( false ) ;
91+ using var response = await PostAsync ( GetUrl ( $ "/{ assistantId } ") , payload , cancellationToken ) . ConfigureAwait ( false ) ;
9792 return await response . DeserializeAsync < AssistantResponse > ( EnableDebug , payload , client , cancellationToken ) . ConfigureAwait ( false ) ;
9893 }
9994
@@ -105,9 +100,7 @@ public async Task<AssistantResponse> ModifyAssistantAsync(string assistantId, Cr
105100 /// <returns>True, if the assistant was deleted.</returns>
106101 public async Task < bool > DeleteAssistantAsync ( string assistantId , CancellationToken cancellationToken = default )
107102 {
108- using var message = new HttpRequestMessage ( HttpMethod . Delete , GetUrl ( $ "/{ assistantId } ") ) ;
109- message . Headers . Add ( "OpenAI-Beta" , "assistants=v2" ) ;
110- using var response = await HttpClient . SendAsync ( message , cancellationToken ) . ConfigureAwait ( false ) ;
103+ using var response = await DeleteAsync ( GetUrl ( $ "/{ assistantId } ") , cancellationToken ) . ConfigureAwait ( false ) ;
111104 var result = await response . DeserializeAsync < DeletedResponse > ( EnableDebug , client , cancellationToken ) . ConfigureAwait ( false ) ;
112105 return result ? . Deleted ?? false ;
113106 }
0 commit comments