diff --git a/OpenAI_API/Embedding/EmbeddingEndpoint.cs b/OpenAI_API/Embedding/EmbeddingEndpoint.cs new file mode 100644 index 0000000..5c7ff83 --- /dev/null +++ b/OpenAI_API/Embedding/EmbeddingEndpoint.cs @@ -0,0 +1,65 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Security.Authentication; +using System.Text; +using System.Threading.Tasks; + +namespace OpenAI_API.Embedding +{ + public class EmbeddingEndpoint + { + OpenAIAPI Api; + /// + /// This allows you to set default parameters for every request, for example to set a default temperature or max tokens. For every request, if you do not have a parameter set on the request but do have it set here as a default, the request will automatically pick up the default value. + /// + public EmbeddingRequest DefaultCompletionRequestArgs { get; set; } = new EmbeddingRequest(); + + /// + /// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of as . + /// + /// + internal EmbeddingEndpoint(OpenAIAPI api) + { + this.Api = api; + } + + public async Task CreateEmbeddingAsync(string input) + { + return await CreateEmbeddingAsync(new EmbeddingRequest() { input = input }); + } + + public async Task CreateEmbeddingAsync(EmbeddingRequest request) + { + if (Api.Auth?.ApiKey is null) + { + throw new AuthenticationException("You must provide API authentication. Please refer to https://github.com/OkGoDoIt/OpenAI-API-dotnet#authentication for details."); + } + + request.Model = Api.UsingEngine; + + HttpClient client = new HttpClient(); + client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Api.Auth.ApiKey); + client.DefaultRequestHeaders.Add("User-Agent", "okgodoit/dotnet_openai_api"); + + string jsonContent = JsonConvert.SerializeObject(request, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); + var stringContent = new StringContent(jsonContent, UnicodeEncoding.UTF8, "application/json"); + + var response = await client.PostAsync($"https://api.openai.com/v1/embeddings", stringContent); + if (response.IsSuccessStatusCode) + { + string resultAsString = await response.Content.ReadAsStringAsync(); + + var res = JsonConvert.DeserializeObject(resultAsString); + + return res; + } + else + { + throw new HttpRequestException("Error calling OpenAi API to get completion. HTTP status code: " + response.StatusCode.ToString() + ". Request body: " + jsonContent); + } + } + + } +} diff --git a/OpenAI_API/Embedding/EmbeddingRequest.cs b/OpenAI_API/Embedding/EmbeddingRequest.cs new file mode 100644 index 0000000..fe86fcd --- /dev/null +++ b/OpenAI_API/Embedding/EmbeddingRequest.cs @@ -0,0 +1,25 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenAI_API.Embedding +{ + public class EmbeddingRequest + { + /// + /// ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them. + /// + [JsonIgnore] + public Engine Model { get; set; } + + [JsonProperty("model")] + public string ModelName { get => Model.EngineName; } + + /// + /// The suffix that comes after a completion of inserted text. + /// + [JsonProperty("input")] + public string input { get; set; } + } +} diff --git a/OpenAI_API/Embedding/EmbeddingResult.cs b/OpenAI_API/Embedding/EmbeddingResult.cs new file mode 100644 index 0000000..4d30693 --- /dev/null +++ b/OpenAI_API/Embedding/EmbeddingResult.cs @@ -0,0 +1,43 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenAI_API.Embedding +{ + public class EmbeddingResult + { + [JsonProperty("object")] + + public string Object { get; set; } + + [JsonProperty("data")] + public Data[] Data { get; set; } + + } + + public class Data + { + [JsonProperty("object")] + + public string Object { get; set; } + + [JsonProperty("embedding")] + public float[] Embedding { get; set; } + + [JsonProperty("index")] + public int Index { get; set; } + + } + + public class Usage + { + [JsonProperty("prompt_tokens")] + public int PromptTokens { get; set; } + + [JsonProperty("total_tokens")] + public int TotalTokens { get; set; } + + } + +} diff --git a/OpenAI_API/Engine/Engine.cs b/OpenAI_API/Engine/Engine.cs index 2c85aa4..c679bb6 100644 --- a/OpenAI_API/Engine/Engine.cs +++ b/OpenAI_API/Engine/Engine.cs @@ -103,7 +103,7 @@ public Engine() /// /// Most capable GPT-3 model. Can do any task the other models can do, often with less context. In addition to responding to prompts, also supports inserting completions within text. /// - public static Engine Davinci => new Engine("text-davinci-002") { Owner = "openai", Ready = true }; + public static Engine Davinci => new Engine("text-davinci-003") { Owner = "openai", Ready = true }; /// /// The default Engine to use in the case no other is specified. Defaults to diff --git a/OpenAI_API/OpenAIAPI.cs b/OpenAI_API/OpenAIAPI.cs index 2baedea..39f96ff 100644 --- a/OpenAI_API/OpenAIAPI.cs +++ b/OpenAI_API/OpenAIAPI.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using OpenAI_API.Embedding; using System; using System.Collections.Generic; using System.IO; @@ -9,53 +10,55 @@ namespace OpenAI_API { - /// - /// Entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints - /// - public class OpenAIAPI - { - /// - /// The API authentication information to use for API calls - /// - public APIAuthentication Auth { get; set; } - - /// - /// Specifies which /model to use for API calls - /// - public Engine UsingEngine { get; set; } = Engine.Default; - - /// - /// Creates a new entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints - /// - /// The API authentication information to use for API calls, or to attempt to use the , potentially loading from environment vars or from a config file. - /// The /model to use for API calls, defaulting to if not specified. - 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); - } - - /// - /// Text generation is the core function of the API. You give the API a prompt, and it generates a completion. The way you “program” the API to do a task is by simply describing the task in plain english or providing a few written examples. This simple approach works for a wide range of use cases, including summarization, translation, grammar correction, question answering, chatbots, composing emails, and much more (see the prompt library for inspiration). - /// - public CompletionEndpoint Completions { get; } - - /// - /// The API endpoint for querying available Engines/models - /// - public EnginesEndpoint Engines { get; } - - /// - /// The API lets you do semantic search over documents. This means that you can provide a query, such as a natural language question or a statement, and find documents that answer the question or are semantically related to the statement. The “documents” can be words, sentences, paragraphs or even longer documents. For example, if you provide documents "White House", "hospital", "school" and query "the president", you’ll get a different similarity score for each document. The higher the similarity score, the more semantically similar the document is to the query (in this example, “White House” will be most similar to “the president”). - /// - public SearchEndpoint Search { get; } - - - - - - } + /// + /// Entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints + /// + public class OpenAIAPI + { + /// + /// The API authentication information to use for API calls + /// + public APIAuthentication Auth { get; set; } + + /// + /// Specifies which /model to use for API calls + /// + public Engine UsingEngine { get; set; } = Engine.Default; + + /// + /// Creates a new entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints + /// + /// The API authentication information to use for API calls, or to attempt to use the , potentially loading from environment vars or from a config file. + /// The /model to use for API calls, defaulting to if not specified. + 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); + Embeddings = new EmbeddingEndpoint(this); + + } + + /// + /// Text generation is the core function of the API. You give the API a prompt, and it generates a completion. The way you “program” the API to do a task is by simply describing the task in plain english or providing a few written examples. This simple approach works for a wide range of use cases, including summarization, translation, grammar correction, question answering, chatbots, composing emails, and much more (see the prompt library for inspiration). + /// + public CompletionEndpoint Completions { get; } + + /// + /// The API endpoint for querying available Engines/models + /// + public EnginesEndpoint Engines { get; } + + /// + /// The API lets you do semantic search over documents. This means that you can provide a query, such as a natural language question or a statement, and find documents that answer the question or are semantically related to the statement. The “documents” can be words, sentences, paragraphs or even longer documents. For example, if you provide documents "White House", "hospital", "school" and query "the president", you’ll get a different similarity score for each document. The higher the similarity score, the more semantically similar the document is to the query (in this example, “White House” will be most similar to “the president”). + /// + public SearchEndpoint Search { get; } + + public EmbeddingEndpoint Embeddings { get; } + + + + } } diff --git a/OpenAI_API/OpenAI_API.csproj b/OpenAI_API/OpenAI_API.csproj index 70a4064..ee14ca7 100644 --- a/OpenAI_API/OpenAI_API.csproj +++ b/OpenAI_API/OpenAI_API.csproj @@ -1,4 +1,4 @@ - + netstandard2.0 @@ -18,9 +18,9 @@ okgodoit-signing.pfx false OpenAI - 1.2.0 - 1.2.0.0 - 1.2.0.0 + 1.2.2 + 1.2.2.0 + 1.2.2.0