-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49f24f3
commit 9e9fe47
Showing
6 changed files
with
190 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
/// <summary> | ||
/// 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. | ||
/// </summary> | ||
public EmbeddingRequest DefaultCompletionRequestArgs { get; set; } = new EmbeddingRequest(); | ||
|
||
/// <summary> | ||
/// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of <see cref="OpenAIAPI"/> as <see cref="OpenAIAPI.Completions"/>. | ||
/// </summary> | ||
/// <param name="api"></param> | ||
internal EmbeddingEndpoint(OpenAIAPI api) | ||
{ | ||
this.Api = api; | ||
} | ||
|
||
public async Task<EmbeddingResult> CreateEmbeddingAsync(string input) | ||
{ | ||
return await CreateEmbeddingAsync(new EmbeddingRequest() { input = input }); | ||
} | ||
|
||
public async Task<EmbeddingResult> 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<EmbeddingResult>(resultAsString); | ||
|
||
return res; | ||
} | ||
else | ||
{ | ||
throw new HttpRequestException("Error calling OpenAi API to get completion. HTTP status code: " + response.StatusCode.ToString() + ". Request body: " + jsonContent); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace OpenAI_API.Embedding | ||
{ | ||
public class EmbeddingRequest | ||
{ | ||
/// <summary> | ||
/// 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. | ||
/// </summary> | ||
[JsonIgnore] | ||
public Engine Model { get; set; } | ||
|
||
[JsonProperty("model")] | ||
public string ModelName { get => Model.EngineName; } | ||
|
||
/// <summary> | ||
/// The suffix that comes after a completion of inserted text. | ||
/// </summary> | ||
[JsonProperty("input")] | ||
public string input { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters