Ejemplo n.º 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());
            }
        }
Ejemplo n.º 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);
 }
        /// <summary>
        /// A helper method to swap out <see langword="null"/> <see cref="APIAuthentication"/> objects with the <see cref="APIAuthentication.Default"/> authentication, possibly loaded from ENV or a config file.
        /// </summary>
        /// <param name="auth">The specific authentication to use if not <see langword="null"/></param>
        /// <returns>Either the provided <paramref name="auth"/> or the <see cref="APIAuthentication.Default"/></returns>
        public static APIAuthentication ThisOrDefault(this APIAuthentication auth)
        {
            if (auth == null)
            {
                auth = APIAuthentication.Default;
            }

            return(auth);
        }
Ejemplo n.º 4
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);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Gets more details about this Engine from the API, specifically properties such as <see cref="Owner"/> and <see cref="Ready"/>
 /// </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 an Engine with all relevant properties filled in</returns>
 public async Task <Engine> RetrieveEngineDetailsAsync(APIAuthentication auth = null)
 {
     return(await EnginesEndpoint.RetrieveEngineDetailsAsync(this.EngineName, auth));
 }