Skip to content

Commit

Permalink
add function calling
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-b-iodigital committed Mar 6, 2024
1 parent 2fc188e commit 49d6a13
Show file tree
Hide file tree
Showing 14 changed files with 373 additions and 93 deletions.
5 changes: 4 additions & 1 deletion src/ConversationalSearchPlatform.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ Microsoft Visual Studio Solution File, Format Version 12.00
VisualStudioVersion = 17.8.34525.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{33CCB6E3-745D-42A7-A75B-C9B30AD359E4}"
ProjectSection(SolutionItems) = preProject
nuget.config = nuget.config
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConversationalSearchPlatform.BackOffice", "backoffice\ConversationalSearchPlatform.BackOffice\ConversationalSearchPlatform.BackOffice.csproj", "{72303287-C899-41CE-95A2-09534F6B3066}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConversationalSearchPlatform.Scraper", "scraper\ConversationalSearchPlatform.Scraper\ConversationalSearchPlatform.Scraper.csproj", "{583B2FA8-EC60-46AC-88B7-34C5D855C764}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConversationalSearchPlatform.Widget", "widget\ConversationalSearchPlatform.Widget\ConversationalSearchPlatform.Widget.csproj", "{687A9754-5AAB-47F8-AA76-9A959C1D613E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jint.Fetch", "Jint.Fetch\Jint.Fetch.csproj", "{88306706-982B-44EB-8678-96E709FB17C6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Jint.Fetch", "Jint.Fetch\Jint.Fetch.csproj", "{88306706-982B-44EB-8678-96E709FB17C6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Finbuckle.MultiTenant;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Identity.Client;
using Swashbuckle.AspNetCore.Filters;

namespace ConversationalSearchPlatform.BackOffice.Api.Conversation;
Expand Down Expand Up @@ -142,7 +143,7 @@ CancellationToken cancellationToken
var tenantId = httpContext.GetTenantHeader();
var holdConversation = new HoldConversation(conversationId, tenantId, request.Prompt, request.Context, request.Debug, (Language)request.Language);
var holdConversation = new HoldConversation(conversationId, tenantId, new Rystem.OpenAi.Chat.ChatMessage() { Content = request.Prompt, Role = Rystem.OpenAi.Chat.ChatRole.User }, request.Context, request.Debug, (Language)request.Language);
await foreach (var crr in conversationService
.ConverseStreamingAsync(
Expand Down Expand Up @@ -187,7 +188,7 @@ private static async Task<ConversationReferencedResponse> HandleHoldConversation
IConversationService conversationService,
CancellationToken cancellationToken)
{
var holdConversation = new HoldConversation(conversationId, tenantId, request.Prompt, request.Context, request.Debug, (Language)request.Language);
var holdConversation = new HoldConversation(conversationId, tenantId, new Rystem.OpenAi.Chat.ChatMessage() { Content = request.Prompt, Role = Rystem.OpenAi.Chat.ChatRole.User }, request.Context, request.Debug, (Language)request.Language);
var response = await conversationService.ConverseAsync(holdConversation, cancellationToken);

return MapToApiResponse(response);
Expand Down Expand Up @@ -307,7 +308,7 @@ private static async Task HandleHoldConversationWebSocketMessage(
}

var tenantId = tenant.Id!;
var holdConversation = new HoldConversation(request.ConversationId.Value, tenantId, request.Prompt, request.Context, request.Debug, (Language)request.Language);
var holdConversation = new HoldConversation(request.ConversationId.Value, tenantId, new Rystem.OpenAi.Chat.ChatMessage() { Content = request.Prompt, Role = Rystem.OpenAi.Chat.ChatRole.User }, request.Context, request.Debug, (Language)request.Language);

await foreach (var crr in conversationService
.ConverseStreamingAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
try
{
Loading = true;
conversationResult = await ConversationService.ConverseAsync(new HoldConversation(CurrentConversationId!.Value, TenantInfo.Id!, prompt, cleanedContextVariables, Debug, Language));
conversationResult = await ConversationService.ConverseAsync(new HoldConversation(CurrentConversationId!.Value, TenantInfo.Id!, new Rystem.OpenAi.Chat.ChatMessage() { Content = prompt, Role = Rystem.OpenAi.Chat.ChatRole.User }, cleanedContextVariables, Debug, Language));
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@
<_ContentIncludedByDefault Remove="Components\TryItOut\TryItOut.razor" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Jint.Fetch\Jint.Fetch.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public static ChatRequestBuilder AddPreviousMessages(this ChatRequestBuilder cha
{
foreach (var conversation in previousMessages)
{
chatRequestBuilder.AddUserMessage(conversation.Prompt);
chatRequestBuilder.AddAssistantMessage(conversation.Response);
chatRequestBuilder.AddMessage(conversation.Prompt);
chatRequestBuilder.AddMessage(conversation.Response);
}

return chatRequestBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public static bool IsAnswerCompleted(this ChatChoice chunk, ILogger logger)
completed = true;
break;

case { FinishReason: "function_call" }:
completed = true;
break;

case { FinishReason: "length" }:
completed = true;
logger.LogDebug("Stopped due to length");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ namespace ConversationalSearchPlatform.BackOffice.Extensions;

public static class ChatResultExtensions
{
public static string CombineAnswers(this ChatResult chatResult)
public static ChatMessage GetFirstAnswer(this ChatResult chatResult)
{
var answers = chatResult
var answer = chatResult
.Choices?
.Select(choice => choice.Message)
.Where(message => message != null)
.Select(message => message!)
.Where(msg => msg.Role == ChatRole.Assistant)
.Select(message => message.Content)
.Where(content => content != null) ??
Enumerable.Empty<string>();
.FirstOrDefault()?
.Message;

return string.Join(Environment.NewLine, answers)
.ReplaceLineEndings();
if (answer == null)
{
answer = new ChatMessage()
{
Role = ChatRole.Assistant,
Content = string.Empty,
};
}

return answer;
}

public static string CombineStreamAnswer(this StreamingChatResult chatResult)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ private async Task CreateEntry(ApplicationDbContext db, string tenantId, Website
{
foreach (var node in nodes)
{
var cleanText = Regex.Replace(node.InnerText, @"\s+", " ").Trim();
cleanText = WebUtility.HtmlDecode(cleanText);
//var cleanText = Regex.Replace(node.InnerText, @"\s+", " ").Trim();
var cleanText = WebUtility.HtmlDecode(node.InnerText);

if (!string.IsNullOrEmpty(cleanText))
{
Expand Down
Loading

0 comments on commit 49d6a13

Please sign in to comment.