-
Notifications
You must be signed in to change notification settings - Fork 7
/
EmbeddingsGenerator.cs
54 lines (47 loc) · 2.04 KB
/
EmbeddingsGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using OpenAI.ObjectModels.ResponseModels;
using WebJobs.Extensions.OpenAI;
namespace CSharpInProcSamples;
/// <summary>
/// Examples of working with OpenAI embeddings.
/// </summary>
public class EmbeddingsGenerator
{
public record EmbeddingsRequest(string RawText, string FilePath);
/// <summary>
/// Example showing how to use the <see cref="EmbeddingsAttribute"/> input binding to generate embeddings
/// for a raw text string.
/// </summary>
[FunctionName(nameof(GenerateEmbeddings_Http_Request))]
public static void GenerateEmbeddings_Http_Request(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "embeddings")] EmbeddingsRequest req,
[Embeddings("{RawText}", InputType.RawText)] EmbeddingsContext embeddings,
ILogger logger)
{
logger.LogInformation(
"Received {count} embedding(s) for input text containing {length} characters.",
embeddings.Response.Data.Count,
req.RawText.Length);
// TODO: Store the embeddings into a database or other storage.
}
/// <summary>
/// Example showing how to use the <see cref="EmbeddingsAttribute"/> input binding to generate embeddings
/// for text contained in a file on the file system.
/// </summary>
[FunctionName(nameof(GetEmbeddings_Http_FilePath))]
public static void GetEmbeddings_Http_FilePath(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "embeddings-from-file")] EmbeddingsRequest req,
[Embeddings("{FilePath}", InputType.FilePath, MaxChunkLength = 512)] EmbeddingsContext embeddings,
ILogger logger)
{
logger.LogInformation(
"Received {count} embedding(s) for input file '{path}'.",
embeddings.Response.Data.Count,
req.FilePath);
// TODO: Store the embeddings into a database or other storage.
}
}