示例#1
0
        /// <summary>
        /// Get details about a particular Engine from the API, specifically properties such as <see cref="Engine.Owner"/> and <see cref="Engine.Ready"/>
        /// </summary>
        /// <param name="id">The id/name of the engine to get more details about</param>
        /// <param name="auth">API authentication in order to call the API endpoint.  If not specified, attempts to use a default.</param>
        /// <returns>Asynchronously returns the <see cref="Engine"/> with all available properties</returns>
        public static async Task <Engine> RetrieveEngineDetailsAsync(string id, APIAuthentication auth = null)
        {
            if (auth.ThisOrDefault()?.ApiKey is null)
            {
                throw new AuthenticationException("You must provide API authentication.  Please refer to https://github.com/OkGoDoIt/OpenAI-API-dotnet#authentication for details.");
            }

            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", auth.ThisOrDefault().ApiKey);
            client.DefaultRequestHeaders.Add("User-Agent", "okgodoit/dotnet_openai_api");

            var response = await client.GetAsync(@"https://api.openai.com/v1/engines/" + id);

            if (response.IsSuccessStatusCode)
            {
                string resultAsString = await response.Content.ReadAsStringAsync();

                var engine = JsonConvert.DeserializeObject <Engine>(resultAsString);
                return(engine);
            }
            else
            {
                throw new HttpRequestException("Error calling OpenAi API to get engine details.  HTTP status code: " + response.StatusCode.ToString());
            }
        }
示例#2
0
 /// <summary>
 /// Creates a new entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints
 /// </summary>
 /// <param name="apiKeys">The API authentication information to use for API calls, or <see langword="null"/> to attempt to use the <see cref="APIAuthentication.Default"/>, potentially loading from environment vars or from a config file.</param>
 /// <param name="engine">The <see cref="Engine"/>/model to use for API calls, defaulting to <see cref="Engine.Davinci"/> if not specified.</param>
 public OpenAIAPI(APIAuthentication apiKeys = null, Engine engine = null)
 {
     this.Auth        = apiKeys.ThisOrDefault();
     this.UsingEngine = engine ?? Engine.Default;
     Completions      = new CompletionEndpoint(this);
     Engines          = new EnginesEndpoint(this);
     Search           = new SearchEndpoint(this);
 }
示例#3
0
        /// <summary>
        /// List all engines via the API
        /// </summary>
        /// <param name="auth">API authentication in order to call the API endpoint.  If not specified, attempts to use a default.</param>
        /// <returns>Asynchronously returns the list of all <see cref="Engine"/>s</returns>
        public static async Task <List <Engine> > GetEnginesAsync(APIAuthentication auth = null)
        {
            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", auth.ThisOrDefault().ApiKey);
            client.DefaultRequestHeaders.Add("User-Agent", "okgodoit/dotnet_openai_api");

            var response = await client.GetAsync(@"https://api.openai.com/v1/engines");

            string resultAsString = await response.Content.ReadAsStringAsync();

            if (response.IsSuccessStatusCode)
            {
                var engines = JsonConvert.DeserializeObject <JsonHelperRoot>(resultAsString).data;
                return(engines);
            }
            else
            {
                throw new HttpRequestException("Error calling OpenAi API to get list of engines.  HTTP status code: " + response.StatusCode.ToString() + ". Content: " + resultAsString);
            }
        }