Skip to content

Commit bae5087

Browse files
committed
Add Azure OpenAi.
1 parent 4d5ac21 commit bae5087

12 files changed

Lines changed: 164 additions & 23 deletions

File tree

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
using BotSharp.Abstraction.Models;
2+
13
namespace BotSharp.Abstraction;
24

35
public interface IChatCompletionHandler
46
{
5-
Task GetChatCompletionsAsync(string text, Func<string, bool, Task> onChunkReceived);
7+
Task GetChatCompletionsAsync(string text,
8+
Func<string> GetInstruction,
9+
Func<List<RoleDialogModel>> GetChatHistory,
10+
Func<string, Task> onChunkReceived,
11+
Func<Task> onChunkCompleted);
612
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
using BotSharp.Abstraction.Models;
2+
13
namespace BotSharp.Abstraction;
24

35
public interface IPlatformMidware
46
{
5-
Task GetChatCompletionsAsync(string text, Func<string, bool, Task> onChunkReceived);
7+
Task GetChatCompletionsAsync(string text,
8+
Func<string> GetInstruction,
9+
Func<List<RoleDialogModel>> GetChatHistory,
10+
Func<string, Task> onChunkReceived,
11+
Func<Task> onChunkCompleted);
612
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace BotSharp.Abstraction.Models;
2+
3+
public class RoleDialogModel
4+
{
5+
public string Role { get; set; }
6+
public string Content { get; set; }
7+
}

‎src/Infrastructure/BotSharp.Core/Services/PlatformMidware.cs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Models;
12
using Microsoft.Extensions.DependencyInjection;
23

34
namespace BotSharp.Core.Services;
@@ -10,13 +11,21 @@ public PlatformMidware(IServiceProvider services)
1011
_services = services;
1112
}
1213

13-
public async Task GetChatCompletionsAsync(string text, Func<string, bool, Task> onChunkReceived)
14+
public async Task GetChatCompletionsAsync(string text,
15+
Func<string> GetInstruction,
16+
Func<List<RoleDialogModel>> GetChatHistory,
17+
Func<string, Task> onChunkReceived,
18+
Func<Task> onChunkCompleted)
1419
{
1520
var handlers = _services.GetServices<IChatCompletionHandler>().ToList();
1621
for (int i = 0; i < handlers.Count(); i++)
1722
{
1823
var handler = handlers[i];
19-
await handler.GetChatCompletionsAsync(text, onChunkReceived);
24+
await handler.GetChatCompletionsAsync(text,
25+
GetInstruction,
26+
GetChatHistory,
27+
onChunkReceived,
28+
onChunkCompleted);
2029
}
2130
}
2231
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using BotSharp.Abstraction;
2+
using BotSharp.Platform.AzureAi;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.DependencyInjection;
5+
6+
namespace BotSharp.Core;
7+
8+
public static class AzureAiServiceCollectionExtensions
9+
{
10+
public static IServiceCollection AddAzureOpenAi(this IServiceCollection services, IConfiguration config)
11+
{
12+
services.AddSingleton(x =>
13+
{
14+
var settings = new AzureAiSettings();
15+
config.Bind("AzureAi", settings);
16+
return settings;
17+
});
18+
services.AddScoped<IChatCompletionHandler, ChatCompletionHandler>();
19+
return services;
20+
}
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace BotSharp.Platform.AzureAi;
2+
3+
public class AzureAiSettings
4+
{
5+
public string ApiKey { get; set; }
6+
public string Endpoint { get; set; }
7+
public string DeploymentModel { get; set; }
8+
public string InstructionFile { get; set; }
9+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using Azure;
2+
using Azure.AI.OpenAI;
3+
using BotSharp.Abstraction;
4+
using BotSharp.Abstraction.Models;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.IO;
8+
using System.Threading.Tasks;
9+
10+
namespace BotSharp.Platform.AzureAi;
11+
12+
public class ChatCompletionHandler : IChatCompletionHandler
13+
{
14+
private readonly AzureAiSettings _settings;
15+
16+
public ChatCompletionHandler(AzureAiSettings settings)
17+
{
18+
_settings = settings;
19+
}
20+
21+
public async Task GetChatCompletionsAsync(string text,
22+
Func<string> GetInstruction,
23+
Func<List<RoleDialogModel>> GetChatHistory,
24+
Func<string, Task> onChunkReceived,
25+
Func<Task> onChunkCompleted)
26+
{
27+
var client = new OpenAIClient(new Uri(_settings.Endpoint), new AzureKeyCredential(_settings.ApiKey));
28+
var chatCompletionsOptions = PrepareOptions(text, GetInstruction, GetChatHistory);
29+
30+
var response = await client.GetChatCompletionsStreamingAsync(_settings.DeploymentModel, chatCompletionsOptions);
31+
using StreamingChatCompletions streaming = response.Value;
32+
33+
string content = "";
34+
await foreach (var choice in streaming.GetChoicesStreaming())
35+
{
36+
await foreach (var message in choice.GetMessageStreaming())
37+
{
38+
if (message.Content == null)
39+
continue;
40+
Console.Write(message.Content);
41+
content += message.Content;
42+
await onChunkReceived(message.Content);
43+
}
44+
}
45+
46+
Console.WriteLine();
47+
await onChunkCompleted();
48+
}
49+
50+
private ChatCompletionsOptions PrepareOptions(string text,
51+
Func<string> GetInstruction,
52+
Func<List<RoleDialogModel>> GetChatHistory)
53+
{
54+
var prompt = File.ReadAllText(_settings.InstructionFile);
55+
var chatCompletionsOptions = new ChatCompletionsOptions()
56+
{
57+
Messages =
58+
{
59+
new ChatMessage(ChatRole.System, prompt)
60+
}
61+
};
62+
63+
return chatCompletionsOptions;
64+
}
65+
}

‎src/Platforms/BotSharp.Platform.LlamaSharp/Handlers/ChatCompletionHandler.cs‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using BotSharp.Abstraction;
2+
using BotSharp.Abstraction.Models;
23
using BotSharp.Platform.LlamaSharp;
34
using LLama;
45
using System;
6+
using System.Collections.Generic;
57
using System.IO;
68
using System.Text;
79
using System.Threading.Tasks;
@@ -31,16 +33,21 @@ public ChatCompletionHandler(LlamaSharpSettings settings)
3133
_model.InitChatAntiprompt(new string[] { "User:" });
3234
}
3335

34-
public Task GetChatCompletionsAsync(string text, Func<string, bool, Task> onChunkReceived)
36+
public async Task GetChatCompletionsAsync(string text,
37+
Func<string> GetInstruction,
38+
Func<List<RoleDialogModel>> GetChatHistory,
39+
Func<string, Task> onChunkReceived,
40+
Func<Task> onChunkCompleted)
3541
{
3642
string totalResponse = "";
3743
foreach (var response in _model.Chat(text, "", "UTF-8"))
3844
{
39-
Console.WriteLine(response);
45+
Console.Write(response);
4046
totalResponse += response;
41-
onChunkReceived(response, false);
47+
await onChunkReceived(response);
4248
}
43-
onChunkReceived("", true);
44-
return Task.CompletedTask;
49+
50+
Console.WriteLine();
51+
await onChunkCompleted();
4552
}
4653
}

‎src/UiAdapters/ChatbotUI/ChatbotUiController.cs‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,23 @@ public async Task SendMessage([FromBody] OpenAiMessageInput input, [FromQuery(Na
5959
Response.Headers.Add(HeaderNames.Connection, "keep-alive");
6060
var outputStream = Response.Body;
6161

62-
await _platform.GetChatCompletionsAsync(input.Messages.Last().Content, async (content, end) =>
63-
{
64-
if (end)
62+
await _platform.GetChatCompletionsAsync(input.Messages.Last().Content,
63+
delegate
6564
{
66-
if (content.Length > 0)
67-
{
68-
await OnChunkReceived(outputStream, content);
69-
}
70-
71-
await OnEventCompleted(outputStream);
72-
}
73-
else
65+
return "";
66+
},
67+
delegate
68+
{
69+
return new List<RoleDialogModel>();
70+
},
71+
async content =>
7472
{
7573
await OnChunkReceived(outputStream, content);
76-
}
77-
});
74+
},
75+
async () =>
76+
{
77+
await OnEventCompleted(outputStream);
78+
});
7879
}
7980

8081
private async Task OnChunkReceived(Stream outputStream, string content)

‎src/WebStarter/Program.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
// Add BotSharp
1414
builder.Services.AddBotSharp();
15-
builder.Services.AddLlamaSharp(builder.Configuration);
15+
// builder.Services.AddLlamaSharp(builder.Configuration);
16+
builder.Services.AddAzureOpenAi(builder.Configuration);
1617

1718
var app = builder.Build();
1819

0 commit comments

Comments
 (0)