|
1 | | -using Newtonsoft.Json; |
2 | | -using System; |
3 | | -using System.Collections.Generic; |
4 | | -using System.Net.Http; |
5 | | -using System.Security.Authentication; |
6 | | -using System.Text; |
7 | | -using System.Threading.Tasks; |
| 1 | +using System.Threading.Tasks; |
8 | 2 |
|
9 | 3 | namespace OpenAI_API.Embedding |
10 | 4 | { |
11 | | - /// <summary> |
12 | | - /// OpenAI’s text embeddings measure the relatedness of text strings by generating an embedding, which is a vector (list) of floating point numbers. The distance between two vectors measures their relatedness. Small distances suggest high relatedness and large distances suggest low relatedness. |
13 | | - /// </summary> |
14 | | - public class EmbeddingEndpoint |
15 | | - { |
16 | | - OpenAIAPI Api; |
17 | | - /// <summary> |
18 | | - /// This allows you to send request to the recommended model without needing to specify. Every request uses the <see cref="Model.AdaTextEmbedding"/> model |
19 | | - /// </summary> |
20 | | - public EmbeddingRequest DefaultEmbeddingRequestArgs { get; set; } = new EmbeddingRequest() { Model = Model.AdaTextEmbedding }; |
21 | | - |
22 | | - /// <summary> |
23 | | - /// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of <see cref="OpenAIAPI"/> as <see cref="OpenAIAPI.Embeddings"/>. |
24 | | - /// </summary> |
25 | | - /// <param name="api"></param> |
26 | | - internal EmbeddingEndpoint(OpenAIAPI api) |
27 | | - { |
28 | | - this.Api = api; |
29 | | - } |
30 | | - |
31 | | - /// <summary> |
32 | | - /// Ask the API to embedd text using the default embedding model <see cref="Model.AdaTextEmbedding"/> |
33 | | - /// </summary> |
34 | | - /// <param name="input">Text to be embedded</param> |
35 | | - /// <returns>Asynchronously returns the embedding result. Look in its <see cref="Data.Embedding"/> property of <see cref="EmbeddingResult.Data"/> to find the vector of floating point numbers</returns> |
36 | | - public async Task<EmbeddingResult> CreateEmbeddingAsync(string input) |
37 | | - { |
38 | | - DefaultEmbeddingRequestArgs.Input = input; |
39 | | - return await CreateEmbeddingAsync(DefaultEmbeddingRequestArgs); |
40 | | - } |
41 | | - |
42 | | - /// <summary> |
43 | | - /// Ask the API to embedd text using a custom request |
44 | | - /// </summary> |
45 | | - /// <param name="request">Request to be send</param> |
46 | | - /// <returns>Asynchronously returns the embedding result. Look in its <see cref="Data.Embedding"/> property of <see cref="EmbeddingResult.Data"/> to find the vector of floating point numbers</returns> |
47 | | - public async Task<EmbeddingResult> CreateEmbeddingAsync(EmbeddingRequest request) |
48 | | - { |
49 | | - if (Api.Auth?.ApiKey is null) |
50 | | - { |
51 | | - throw new AuthenticationException("You must provide API authentication. Please refer to https://github.com/OkGoDoIt/OpenAI-API-dotnet#authentication for details."); |
52 | | - } |
53 | | - |
54 | | - HttpClient client = new HttpClient(); |
55 | | - client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Api.Auth.ApiKey); |
56 | | - client.DefaultRequestHeaders.Add("User-Agent", "okgodoit/dotnet_openai_api"); |
57 | | - |
58 | | - string jsonContent = JsonConvert.SerializeObject(request, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); |
59 | | - var stringContent = new StringContent(jsonContent, Encoding.UTF8, "application/json"); |
60 | | - |
61 | | - var response = await client.PostAsync($"https://api.openai.com/v1/embeddings", stringContent); |
62 | | - if (response.IsSuccessStatusCode) |
63 | | - { |
64 | | - string resultAsString = await response.Content.ReadAsStringAsync(); |
65 | | - |
66 | | - var res = JsonConvert.DeserializeObject<EmbeddingResult>(resultAsString); |
67 | | - |
68 | | - return res; |
69 | | - } |
70 | | - else |
71 | | - { |
72 | | - throw new HttpRequestException("Error calling OpenAi API to get completion. HTTP status code: " + response.StatusCode.ToString() + ". Request body: " + jsonContent); |
73 | | - } |
74 | | - } |
75 | | - |
76 | | - } |
| 5 | + /// <summary> |
| 6 | + /// OpenAI’s text embeddings measure the relatedness of text strings by generating an embedding, which is a vector (list) of floating point numbers. The distance between two vectors measures their relatedness. Small distances suggest high relatedness and large distances suggest low relatedness. |
| 7 | + /// </summary> |
| 8 | + public class EmbeddingEndpoint : EndpointBase |
| 9 | + { |
| 10 | + /// <summary> |
| 11 | + /// This allows you to send request to the recommended model without needing to specify. Every request uses the <see cref="Model.AdaTextEmbedding"/> model |
| 12 | + /// </summary> |
| 13 | + public EmbeddingRequest DefaultEmbeddingRequestArgs { get; set; } = new EmbeddingRequest() { Model = Model.AdaTextEmbedding }; |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// The name of the endpoint, which is the final path segment in the API URL. For example, "embeddings". |
| 17 | + /// </summary> |
| 18 | + protected override string Endpoint { get { return "embeddings"; } } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of <see cref="OpenAIAPI"/> as <see cref="OpenAIAPI.Embeddings"/>. |
| 22 | + /// </summary> |
| 23 | + /// <param name="api"></param> |
| 24 | + internal EmbeddingEndpoint(OpenAIAPI api) : base(api) { } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Ask the API to embedd text using the default embedding model <see cref="Model.AdaTextEmbedding"/> |
| 28 | + /// </summary> |
| 29 | + /// <param name="input">Text to be embedded</param> |
| 30 | + /// <returns>Asynchronously returns the embedding result. Look in its <see cref="Data.Embedding"/> property of <see cref="EmbeddingResult.Data"/> to find the vector of floating point numbers</returns> |
| 31 | + public async Task<EmbeddingResult> CreateEmbeddingAsync(string input) |
| 32 | + { |
| 33 | + EmbeddingRequest req = new EmbeddingRequest(DefaultEmbeddingRequestArgs.Model, input); |
| 34 | + return await CreateEmbeddingAsync(req); |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Ask the API to embedd text using a custom request |
| 39 | + /// </summary> |
| 40 | + /// <param name="request">Request to be send</param> |
| 41 | + /// <returns>Asynchronously returns the embedding result. Look in its <see cref="Data.Embedding"/> property of <see cref="EmbeddingResult.Data"/> to find the vector of floating point numbers</returns> |
| 42 | + public async Task<EmbeddingResult> CreateEmbeddingAsync(EmbeddingRequest request) |
| 43 | + { |
| 44 | + return await HttpPost<EmbeddingResult>(postData: request); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Ask the API to embedd text using the default embedding model <see cref="Model.AdaTextEmbedding"/> |
| 49 | + /// </summary> |
| 50 | + /// <param name="input">Text to be embedded</param> |
| 51 | + /// <returns>Asynchronously returns the first embedding result as an array of floats.</returns> |
| 52 | + public async Task<float[]> GetEmbeddingsAsync(string input) |
| 53 | + { |
| 54 | + EmbeddingRequest req = new EmbeddingRequest(DefaultEmbeddingRequestArgs.Model, input); |
| 55 | + var embeddingResult = await CreateEmbeddingAsync(req); |
| 56 | + return embeddingResult?.Data?[0]?.Embedding; |
| 57 | + } |
| 58 | + } |
77 | 59 | } |
0 commit comments