openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/customize-openairesponsescontext-attribute

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

OpenAI/src/Custom/Conversations/ConversationClient.cs

97lines · modecode

1using Microsoft.TypeSpec.Generator.Customizations;
2using System;
3using System.ClientModel;
4using System.ClientModel.Primitives;
5using System.Diagnostics.CodeAnalysis;
6
7namespace OpenAI.Conversations;
8
9// CUSTOM:
10// - Renamed.
11// - Suppressed constructor that takes endpoint parameter; endpoint is now a property in the options class.
12/// <summary> The service client for OpenAI conversation operations. </summary>
13[CodeGenType("Conversations")]
14[CodeGenSuppress("ConversationClient", typeof(ClientPipeline), typeof(Uri))]
15public partial class ConversationClient
16{
17 // CUSTOM: Added as a convenience.
18 /// <summary> Initializes a new instance of <see cref="ConversationClient"/>. </summary>
19 /// <param name="apiKey"> The API key to authenticate with the service. </param>
20 /// <exception cref="ArgumentNullException"> <paramref name="apiKey"/> is null. </exception>
21 public ConversationClient(string apiKey) : this(new ApiKeyCredential(apiKey), new OpenAIClientOptions())
22 {
23 }
24
25 // CUSTOM:
26 // - Used a custom pipeline.
27 // - Demoted the endpoint parameter to be a property in the options class.
28 /// <summary> Initializes a new instance of <see cref="ConversationClient"/>. </summary>
29 /// <param name="credential"> The <see cref="ApiKeyCredential"/> to authenticate with the service. </param>
30 /// <exception cref="ArgumentNullException"> <paramref name="credential"/> is null. </exception>
31 public ConversationClient(ApiKeyCredential credential) : this(credential, new OpenAIClientOptions())
32 {
33 }
34
35 // CUSTOM:
36 // - Used a custom pipeline.
37 // - Demoted the endpoint parameter to be a property in the options class.
38 /// <summary> Initializes a new instance of <see cref="ConversationClient"/>. </summary>
39 /// <param name="credential"> The <see cref="ApiKeyCredential"/> to authenticate with the service. </param>
40 /// <param name="options"> The options to configure the client. </param>
41 /// <exception cref="ArgumentNullException"> <paramref name="credential"/> is null. </exception>
42 public ConversationClient(ApiKeyCredential credential, OpenAIClientOptions options) : this(OpenAIClient.CreateApiKeyAuthenticationPolicy(credential), options)
43 {
44 }
45
46 // CUSTOM: Added as a convenience.
47 /// <summary> Initializes a new instance of <see cref="ConversationClient"/>. </summary>
48 /// <param name="authenticationPolicy"> The authentication policy used to authenticate with the service. </param>
49 /// <exception cref="ArgumentNullException"> <paramref name="authenticationPolicy"/> is null. </exception>
50 public ConversationClient(AuthenticationPolicy authenticationPolicy) : this(authenticationPolicy, new OpenAIClientOptions())
51 {
52 }
53
54 // CUSTOM: Added as a convenience.
55 /// <summary> Initializes a new instance of <see cref="ConversationClient"/>. </summary>
56 /// <param name="authenticationPolicy"> The authentication policy used to authenticate with the service. </param>
57 /// <param name="options"> The options to configure the client. </param>
58 /// <exception cref="ArgumentNullException"> <paramref name="authenticationPolicy"/> is null. </exception>
59 public ConversationClient(AuthenticationPolicy authenticationPolicy, OpenAIClientOptions options)
60 {
61 Argument.AssertNotNull(authenticationPolicy, nameof(authenticationPolicy));
62 options ??= new OpenAIClientOptions();
63
64 Pipeline = OpenAIClient.CreatePipeline(authenticationPolicy, options);
65 _endpoint = OpenAIClient.GetEndpoint(options);
66 }
67
68 // CUSTOM:
69 // - Used a custom pipeline.
70 // - Demoted the endpoint parameter to be a property in the options class.
71 // - Made protected.
72 /// <summary> Initializes a new instance of <see cref="ConversationClient"/>. </summary>
73 /// <param name="pipeline"> The HTTP pipeline to send and receive REST requests and responses. </param>
74 /// <param name="options"> The options to configure the client. </param>
75 /// <exception cref="ArgumentNullException"> <paramref name="pipeline"/> is null. </exception>
76 protected internal ConversationClient(ClientPipeline pipeline, OpenAIClientOptions options)
77 {
78 Argument.AssertNotNull(pipeline, nameof(pipeline));
79 options ??= new OpenAIClientOptions();
80
81 Pipeline = pipeline;
82 _endpoint = OpenAIClient.GetEndpoint(options);
83 }
84
85 [Experimental("SCME0002")]
86 public ConversationClient(ConversationClientSettings settings)
87 : this(AuthenticationPolicy.Create(settings), settings?.Options)
88 {
89 }
90
91 /// <summary>
92 /// Gets the endpoint URI for the service.
93 /// </summary>
94 [Experimental("OPENAI001")]
95 public Uri Endpoint => _endpoint;
96}
97
98