Skip to content

Commit

Permalink
chore: resolve merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
bsdayo committed May 21, 2023
2 parents 916ce5d + 4d7e5f1 commit d4443cd
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ $ dotnet run --project src/BingChat.Cli/BingChat.Cli.csproj
## Roadmap

- [x] Implement a command line tool to interact with Bing Chat.
- [ ] Provide a way to get the full result, like adaptive cards.
- [x] Provide a way to get the full result, like adaptive cards.
- [ ] Add ability to set timeout.

## Q&A
Expand Down
Binary file modified assets/cli-screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/BingChat.Cli/Commands/ConversationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ await AnsiConsole.Status()
.Spinner(Spinner.Known.BouncingBar)
.StartAsync("Bing is thinking...", async _ => { answer = await conversation.AskAsync(text); });

if (answer.EndsWith("\n<Disengaged>"))
{
conversation = null;
answer = answer.Replace("\n<Disengaged>", "\nIt might be time to move onto a new topic. Let's start over.");
}

Utils.WriteAnswer(answer, settings);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/BingChat.Cli/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void WriteMessage(string message, ChatCommandSettings settings)

public static void WriteAnswer(string answer, ChatCommandSettings settings)
{
var text = Markup.Escape(answer.Trim().ReorderFootnotes());
var text = Markup.Escape(answer.Trim());
switch (settings.Theme)
{
case ChatTheme.Bubble:
Expand Down
8 changes: 7 additions & 1 deletion src/BingChat/BingChatConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ internal static class BingChatConstants
"InternalSearchQuery",
"InternalSearchResult",
"InternalLoaderMessage",
"Disengaged",
"ActionRequest",
"Context",
"Progress",
"RenderCardRequest",
"AdsQuery",
"SemanticSerp"
"SemanticSerp",
"GenerateContentQuery",
"SearchQuery",
};
}
51 changes: 45 additions & 6 deletions src/BingChat/BingChatConversation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,61 @@ void Cleanup()

string? GetAnswer(BingChatConversationResponse response)
{
//Check status
if (!response.Item.Result.Value.Equals("Success", StringComparison.OrdinalIgnoreCase))
{
throw new BingChatException($"{response.Item.Result.Value}: {response.Item.Result.Message}");
}

for (var index = response.Item.Messages.Length - 1; index >= 0; index--)
//Collect messages, including of types: Chat, SearchQuery, LoaderMessage, Disengaged
var messages = new List<string>();
foreach (var itemMessage in response.Item.Messages)
{
var itemMessage = response.Item.Messages[index];
if (itemMessage.MessageType != null) continue;
//Not needed
if (itemMessage.Author != "bot") continue;
if (itemMessage.MessageType == "InternalSearchResult" ||
itemMessage.MessageType == "RenderCardRequest")
continue;

// maybe is possible to use itemMessage.Text directly, but some extra information will be lost
return itemMessage.AdaptiveCards[0].Body[0].Text;
//Not supported
if (itemMessage.MessageType == "GenerateContentQuery")
continue;

//From Text
var text = itemMessage.Text;

//From AdaptiveCards
var adaptiveCards = itemMessage.AdaptiveCards;
if (text is null && adaptiveCards is not null && adaptiveCards.Length > 0)
{
var bodies = new List<string>();
foreach (var body in adaptiveCards[0].Body)
{
if (body.Type != "TextBlock" || body.Text is null) continue;
bodies.Add(body.Text);
}
text = bodies.Count > 0 ? string.Join("\n", bodies) : null;
}

//From MessageType
text ??= $"<{itemMessage.MessageType}>";

//From SourceAttributions
var sourceAttributions = itemMessage.SourceAttributions;
if (sourceAttributions is not null && sourceAttributions.Length > 0)
{
text += "\n";
for (var nIndex = 0; nIndex < sourceAttributions.Length; nIndex++)
{
var sourceAttribution = sourceAttributions[nIndex];
text += $"\n[{nIndex + 1}]: {sourceAttribution.SeeMoreUrl} \"{sourceAttribution.ProviderDisplayName}\"";
}
}

messages.Add(text);
}

return null;
return messages.Count > 0 ? string.Join("\n\n", messages) : null;
}

void OnMessageReceived(string text)
Expand Down
21 changes: 15 additions & 6 deletions src/BingChat/BingChatConversationResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal sealed class ResponseItem
internal sealed class ResponseMessage
{
[JsonPropertyName("text")]
public string Text { get; set; }
public string? Text { get; set; }

[JsonPropertyName("author")]
public string Author { get; set; }
Expand All @@ -38,7 +38,10 @@ internal sealed class ResponseMessage
public string? MessageType { get; set; }

[JsonPropertyName("adaptiveCards")]
public AdaptiveCard[] AdaptiveCards { get; set; }
public AdaptiveCard[]? AdaptiveCards { get; set; }

[JsonPropertyName("sourceAttributions")]
public SourceAttribution[]? SourceAttributions { get; set; }
}

internal sealed class AdaptiveCard
Expand All @@ -53,10 +56,16 @@ internal sealed class ResponseBody
public string Type { get; set; }

[JsonPropertyName("text")]
public string Text { get; set; }
public string? Text { get; set; }
}

internal sealed class SourceAttribution
{
[JsonPropertyName("providerDisplayName")]
public string ProviderDisplayName { get; set; }

[JsonPropertyName("wrap")]
public bool Wrap { get; set; }
[JsonPropertyName("seeMoreUrl")]
public string SeeMoreUrl { get; set; }
}

internal sealed class ResponseResult
Expand All @@ -65,5 +74,5 @@ internal sealed class ResponseResult
public string Value { get; set; }

[JsonPropertyName("message")]
public string Message { get; set; }
public string? Message { get; set; }
}

0 comments on commit d4443cd

Please # to comment.