Skip to content

Conversation

@kevinguo-ed
Copy link
Contributor

@kevinguo-ed kevinguo-ed commented Aug 27, 2024

Copy link
Contributor

@mikekistler mikekistler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this content really works as a tutorial. This is more an explanation of the sample project. Can you move this down to the Samples section?

Copy link
Member

@IEvangelist IEvangelist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits

Copy link
Contributor

@Rick-Anderson Rick-Anderson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this looks like a really great article. It just needs a little work and we can help you make the changes.

We use OpenAI for generating intelligent, context-aware responses and SignalR for delivering the response to users in a group. You can find the complete code [in this repo](https://github.com/microsoft/SignalR-Samples-AI/tree/main/AIStreaming).

## Dependencies
You can use either Azure OpenAI or OpenAI for this project. Make sure to update the `endpoint` and `key` in `appsetting.json`. `OpenAIExtensions` reads the configuration when the app starts and they are required to authenticate and use either service.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be

Update the endpoint and key in appsetting.json file as shown in the following code:

After this PR merges, we can move the code to a sample folder and display the appsetting.json with the changes highlighted.

@kevinguo-ed
Copy link
Contributor Author

@kevinguo-ed please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@dotnet-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@dotnet-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@dotnet-policy-service agree company="Microsoft"

Contributor License Agreement

@dotnet-policy-service agree company="Microsoft"

@wadepickett
Copy link
Contributor

wadepickett commented Oct 7, 2024

I don't think this content really works as a tutorial. This is more an explanation of the sample project. Can you move this down to the Samples section?

I agree with both of Mike's points above.

@BrennanConroy
This looks like the sample itself from https://github.com/microsoft/SignalR-Samples-AI/tree/main/AIStreaming would be a candidate to go here with the existing SignalR samples:
https://github.com/aspnet/SignalR-samples

Then per Mike's point above, the content for that sample in this PR here (after another edit round which I will help with) would go in a README for the sample. It is more of a description of the sample, rather than a tutorial.

Thoughts?

@wadepickett
Copy link
Contributor

@BrennanConroy, thoughts? See suggestion above.

@wadepickett
Copy link
Contributor

I don't think this content really works as a tutorial. This is more an explanation of the sample project. Can you move this down to the Samples section?

(I've been assigned to finish this up.)

Thanks @mikekistler , you mentioned moving this down to the "Samples section"

I assume you meant moving it down in the TOC menu to "Real-time apps > Samples "

image

...that points directly to the sample repo here.
https://github.com/aspnet/SignalR-samples

Were you thinking the content in this PR should move to the SignalR-samples repo, or were you envisioning sub links under the Real-time apps > Samples TOC? If the latter, then I will create two sub items to "Samples" in the TOC, one for the samples repo (and make it more clear that is where it points to) and another for this topic.

Also,
The code sample for the doc in the PR is located here:
https://github.com/microsoft/SignalR-Samples-AI/tree/main/AIStreaming

It is not clear to me if the intent was that the samples code for this topic stays in that repo. I'm unaware of the history on that. I'll assume it does rather than moving it to the sample to the SignalR-samples repo unless I hear otherwise.

Thanks for any clarification.

@mikekistler
Copy link
Contributor

Moving this to the SignalR-samples repo was what I had in mind, but sadly that repo is archived now, so we probably need some other solution. I don't have any strong opinion on this -- only that we should differentiate tutorials and samples for the benefit of users.

Copy link
Member

@IEvangelist IEvangelist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! A few nits and FYIs, otherwise :shipit:

Comment on lines +23 to +34
# [OpenAI](#tab/open-ai)
To build this application, you will need the following:
* ASP.NET Core: To create the web application and host the SignalR hub.
* [SignalR](https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Client): For real-time communication between clients and the server.
* [OpenAI Client](https://www.nuget.org/packages/OpenAI/2.0.0-beta.10): To interact with OpenAI's API for generating AI responses.

# [Azure OpenAI](#tab/azure-open-ai)
To build this application, you will need the following:
* ASP.NET Core: To create the web application and host the SignalR hub.
* [SignalR](https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Client): For real-time communication between clients and the server.
* [Azure OpenAI](https://www.nuget.org/packages/Azure.AI.OpenAI/2.0.0-beta.3): Azure.AI.OpenAI
---
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the added space helps with readability...and not that it really matters, but I've found myself trying to avoid tabs with H1's, you're free to use any heading value so long as the #tab bookmark/syntax is in place. Also, removed versions from the packages as stable releases are now available. See microsoft/SignalR-Samples-AI#5, where I just did a PR to upgrade these in the source.

Suggested change
# [OpenAI](#tab/open-ai)
To build this application, you will need the following:
* ASP.NET Core: To create the web application and host the SignalR hub.
* [SignalR](https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Client): For real-time communication between clients and the server.
* [OpenAI Client](https://www.nuget.org/packages/OpenAI/2.0.0-beta.10): To interact with OpenAI's API for generating AI responses.
# [Azure OpenAI](#tab/azure-open-ai)
To build this application, you will need the following:
* ASP.NET Core: To create the web application and host the SignalR hub.
* [SignalR](https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Client): For real-time communication between clients and the server.
* [Azure OpenAI](https://www.nuget.org/packages/Azure.AI.OpenAI/2.0.0-beta.3): Azure.AI.OpenAI
---
### [OpenAI](#tab/open-ai)
To build this application, you will need the following:
* ASP.NET Core: To create the web application and host the SignalR hub.
* [SignalR](https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Client): For real-time communication between clients and the server.
* [OpenAI Client](https://www.nuget.org/packages/OpenAI): To interact with OpenAI's API for generating AI responses.
### [Azure OpenAI](#tab/azure-open-ai)
To build this application, you will need the following:
* ASP.NET Core: To create the web application and host the SignalR hub.
* [SignalR](https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Client): For real-time communication between clients and the server.
* [Azure OpenAI](https://www.nuget.org/packages/Azure.AI.OpenAI): `Azure.AI.OpenAI`
---


### Data flow

:::image type="content" source="./sequence-diagram-ai-powered-group-chat.png" alt-text="sequence diagram for the AI-powered group chat":::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an idea, not something that you need to do, but since we still do not have native support from the Learn platform for mermaid diagrams-I've been using mermaid.live and its share feature, that lets you encode/share diagrams. You can build this out, create the share, and embed the share as a comment in the Markdown (which hides it from the end user) but the content developer can use it as a source of truth for future edits, re-exporting it as an image to update the image itself.

For example:

<!--

https://mermaid.live/edit#pako:eNp1kk1P4zAQhv_KaK6botLQFnxAyhbt7mFRUQIXlMsQD6lFYmf9wVfV_752UyQQ4NPY7-N3xjPeYmMko0DH_wLrhi8UtZb6WkNcFLzRob9jO-4Hsl41aiDtoQJyUDUblqH7Si-S_lv5P-EOisYroz8zZWKuybYcYx7MZ2KdiLWNaZy35I2FX0F_47ZKbPEaLMPKuN44uPhZH7gKJufnP6AQcG1V27J143kxnpcCVhtuHhyY4EelM2aAS7IP0jwdTPYXJpPnUlQNabiP1dyUfw9OrOUHy7WAK3KO3TtmnaRJqqJkH6x2wI_UBUrvAcsudP5jXatoEmtVzjuQ5GkUJVNswSN5hgIz7Nn2pGQc4jbpNfoN91yjiKGMD6ix1rvIpWlWL7pB4W3gDK0J7QbFPXUu7sIQE7yN_w1hqWLPL8cvsv8pGcZeo9jiM4rl8uh0tjidneXzfDmbH88zfEGRnxxN48rzs3yaLxYn08Uuw1djounx_vbtPk4Jdv8Bo5TJ1A

-->

Check out this link.


The `CompleteChatStreamingAsync()` method streams responses from OpenAI incrementally, which allows the app to send partial responses to the client as they are generated.

The code uses a `StringBuilder` to accumulate the AI's response. It checks the length of the buffered content and sends it to the clients when it exceeds a certain threshold (e.g., 20 characters). This approach ensures that users see the AI’s response as it forms, mimicking a human-like typing effect.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The code uses a `StringBuilder` to accumulate the AI's response. It checks the length of the buffered content and sends it to the clients when it exceeds a certain threshold (e.g., 20 characters). This approach ensures that users see the AI’s response as it forms, mimicking a human-like typing effect.
The code uses a <xref:System.Text.StringBuilder> to accumulate the AI's response. It checks the length of the buffered content and sends it to the clients when it exceeds a certain threshold (e.g., 20 characters). This approach ensures that users see the AI’s response as it forms, mimicking a human-like typing effect.

id,
totalCompletion.ToString());

lastSentTokenLength = totalCompletion.Length;
Copy link
Member

@BrennanConroy BrennanConroy Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This is something I don't like about bing chat, it sends the entire message every update. Really it should just append the new content on the client side, saves bandwidth and memory usage on the server.

@wadepickett
Copy link
Contributor

wadepickett commented Mar 23, 2025

Closing this PR since commits were merged through a new one.

The commits here were moved into a new PR by request, which included all changes asked for at the time, plus some updates per review at #34848 and that merged after approval.

However, I didn't realize that two more suggestions were made on this PR just before I merged to main.

  1. @IEvangelist, I added your suggested StringBuilder link just now with Minor, xref link fix. #35029
  2. @BrennanConroy, for: "nit: This is something I don't like about Bing chat, it sends the entire message every update. Really it should just append the new content on the client side". It wasn't clear to me if there was an design change to be made here in the example to accommodate the problem, in which case I will create a new issue for it, or if you were just noting a Bing chat design problem, no sample change needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants