Skip to content

Commit f0664d0

Browse files
ferantiverockittel
andauthored
feat (code-style): [website] libs, formatting, and conventions (#40)
Co-authored-by: Chad Kittel <chad.kittel@gmail.com>
1 parent cd284d7 commit f0664d0

File tree

16 files changed

+403
-433
lines changed

16 files changed

+403
-433
lines changed

.devcontainer/devcontainer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "Azure OpenAI end-to-end basic reference implementation",
3+
"image": "mcr.microsoft.com/devcontainers/dotnet:dev-8.0-jammy",
4+
"runArgs": ["--network=host"],
5+
"remoteUser": "vscode",
6+
"features": {
7+
},
8+
"customizations": {
9+
"vscode": {
10+
"extensions": [
11+
"ms-dotnettools.csdevkit",
12+
"ms-azuretools.vscode-bicep"
13+
],
14+
"settings": {}
15+
}
16+
},
17+
"forwardPorts": []
18+
}

infra-as-code/bicep/webapp.bicep

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ resource appsettings 'Microsoft.Web/sites/config@2022-09-01' = {
117117
APPINSIGHTS_INSTRUMENTATIONKEY: appInsights.properties.InstrumentationKey
118118
APPLICATIONINSIGHTS_CONNECTION_STRING: appInsights.properties.ConnectionString
119119
ApplicationInsightsAgent_EXTENSION_VERSION: '~2'
120-
chatApiKey: '@Microsoft.KeyVault(SecretUri=${keyVault::chatApiKey.properties.secretUri})'
121-
chatApiEndpoint: chatProject::scoreEndpoint.properties.scoringUri
122-
chatInputName: 'question'
123-
chatOutputName: 'answer'
120+
ChatApiKey: '@Microsoft.KeyVault(SecretUri=${keyVault::chatApiKey.properties.secretUri})'
121+
ChatApiEndpoint: chatProject::scoreEndpoint.properties.scoringUri
122+
ChatInputName: 'question'
123+
ChatOutputName: 'answer'
124124
keyVaultReferenceIdentity: appServiceManagedIdentity.id
125125
}
126126
}

website/chatui.sln

Lines changed: 0 additions & 24 deletions
This file was deleted.

website/chatui.zip

-297 KB
Binary file not shown.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace chatui.Configuration;
4+
5+
public class ChatApiOptions
6+
{
7+
[Url]
8+
public string ChatApiEndpoint { get; init; } = default!;
9+
10+
[Required]
11+
public string ChatApiKey { get; init; } = default!;
12+
13+
public string ChatInputName { get; init; } = "chat_input";
14+
15+
public string ChatOutputName { get; init; } = "chat_output";
16+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System.Net.Http.Headers;
2+
using System.Text.Json;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.Extensions.Options;
5+
using chatui.Configuration;
6+
using chatui.Models;
7+
8+
namespace chatui.Controllers;
9+
10+
[ApiController]
11+
[Route("[controller]/[action]")]
12+
13+
public class ChatController(
14+
IHttpClientFactory httpClientFactory,
15+
IOptionsMonitor<ChatApiOptions> options,
16+
ILogger<ChatController> logger) : ControllerBase
17+
{
18+
private readonly HttpClient _client = httpClientFactory.CreateClient("ChatClient");
19+
private readonly IOptionsMonitor<ChatApiOptions> _options = options;
20+
private readonly ILogger<ChatController> _logger = logger;
21+
22+
[HttpPost]
23+
public async Task<IActionResult> Completions([FromBody] string prompt)
24+
{
25+
if (string.IsNullOrWhiteSpace(prompt))
26+
throw new ArgumentException("Prompt cannot be null, empty, or whitespace.", nameof(prompt));
27+
28+
_logger.LogDebug("Prompt received {Prompt}", prompt);
29+
30+
var _config = _options.CurrentValue;
31+
32+
var requestBody = JsonSerializer.Serialize(new Dictionary<string, string>
33+
{
34+
[_config.ChatInputName] = prompt
35+
});
36+
37+
using var request = new HttpRequestMessage(HttpMethod.Post, _config.ChatApiEndpoint)
38+
{
39+
Content = new StringContent(requestBody, System.Text.Encoding.UTF8, "application/json"),
40+
};
41+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _config.ChatApiKey);
42+
43+
var response = await _client.SendAsync(request);
44+
var responseContent = await response.Content.ReadAsStringAsync();
45+
46+
_logger.LogInformation("HTTP status code: {StatusCode}", response.StatusCode);
47+
48+
if (!response.IsSuccessStatusCode)
49+
{
50+
_logger.LogError("Error response: {Content}", responseContent);
51+
52+
foreach (var (key, value) in response.Headers)
53+
_logger.LogDebug("Header {Key}: {Value}", key, string.Join(", ", value));
54+
55+
foreach (var (key, value) in response.Content.Headers)
56+
_logger.LogDebug("Content-Header {Key}: {Value}", key, string.Join(", ", value));
57+
58+
return BadRequest(responseContent);
59+
}
60+
61+
_logger.LogInformation("Successful response: {Content}", responseContent);
62+
63+
var result = JsonSerializer.Deserialize<Dictionary<string, string>>(responseContent);
64+
var output = result?.GetValueOrDefault(_config.ChatOutputName) ?? string.Empty;
65+
66+
return Ok(new HttpChatResponse(true, output));
67+
}
68+
}

website/chatui/Controllers/ChatGPTController.cs

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using Microsoft.AspNetCore.Mvc;
22

3-
namespace chatui.Controllers
3+
namespace chatui.Controllers;
4+
5+
public class HomeController : Controller
46
{
5-
public class HomeController : Controller
7+
public IActionResult Index()
68
{
7-
public IActionResult Index()
8-
{
9-
return View();
10-
}
9+
return View();
1110
}
1211
}

website/chatui/Models/ChatRequest.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,3 @@
1-
namespace chatui.Models
2-
{
3-
public class HttpChatGPTResponse
4-
{
5-
public bool Success { get; set; }
6-
public string Data { get; set; }
7-
public string Error { get; set; }
8-
}
1+
namespace chatui.Models;
92

10-
public class ChatResponse
11-
{
12-
public string id { get; set; }
13-
public string _object { get; set; }
14-
public int created { get; set; }
15-
public Choice[] choices { get; set; }
16-
public Usage usage { get; set; }
17-
}
18-
19-
public class Usage
20-
{
21-
public int prompt_tokens { get; set; }
22-
public int completion_tokens { get; set; }
23-
public int total_tokens { get; set; }
24-
}
25-
26-
public class Choice
27-
{
28-
public int index { get; set; }
29-
public Message message { get; set; }
30-
public string finish_reason { get; set; }
31-
}
32-
33-
}
3+
public record HttpChatResponse(bool Success, string Data);

0 commit comments

Comments
 (0)