-
Notifications
You must be signed in to change notification settings - Fork 14
/
FilePrompt.cs
70 lines (57 loc) · 2.46 KB
/
FilePrompt.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Extensions.OpenAI.Embeddings;
using Microsoft.Azure.Functions.Worker.Extensions.OpenAI.Search;
using Microsoft.Azure.Functions.Worker.Http;
namespace SemanticAISearchEmbeddings;
public static class FilePrompt
{
public class EmbeddingsRequest
{
[JsonPropertyName("Url")]
public string? Url { get; set; }
}
public class SemanticSearchRequest
{
[JsonPropertyName("Prompt")]
public string? Prompt { get; set; }
}
[Function("IngestFile")]
public static async Task<EmbeddingsStoreOutputResponse> IngestFile(
[HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req)
{
using StreamReader reader = new(req.Body);
string request = await reader.ReadToEndAsync();
EmbeddingsRequest? requestBody = JsonSerializer.Deserialize<EmbeddingsRequest>(request);
if (requestBody == null || requestBody.Url == null)
{
throw new ArgumentException("Invalid request body. Make sure that you pass in {\"Url\": value } as the request body.");
}
Uri uri = new(requestBody.Url);
string filename = Path.GetFileName(uri.AbsolutePath);
HttpResponseData response = req.CreateResponse(HttpStatusCode.Created);
return new EmbeddingsStoreOutputResponse
{
HttpResponse = response,
SearchableDocument = new SearchableDocument(filename)
};
}
public class EmbeddingsStoreOutputResponse
{
[EmbeddingsStoreOutput("{Url}", InputType.Url, "AISearchEndpoint", "openai-index", Model = "%EMBEDDING_MODEL_DEPLOYMENT_NAME%")]
public required SearchableDocument SearchableDocument { get; init; }
public HttpResponseData? HttpResponse { get; set; }
}
[Function("PromptFile")]
public static IActionResult PromptFile(
[HttpTrigger(AuthorizationLevel.Function, "post")] SemanticSearchRequest unused,
[SemanticSearchInput("AISearchEndpoint", "openai-index", Query = "{Prompt}", ChatModel = "%CHAT_MODEL_DEPLOYMENT_NAME%", EmbeddingsModel = "%EMBEDDING_MODEL_DEPLOYMENT_NAME%")] SemanticSearchContext result)
{
return new ContentResult { Content = result.Response, ContentType = "text/plain" };
}
}