You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-signalr/signalr-tutorial-group-chat-with-open-ai.md
+19-16Lines changed: 19 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,40 +3,43 @@ title: Build an AI-Powered Group Chat with SignalR and OpenAI
3
3
author: kevinguo-ed
4
4
description: A tutorial explaining how SignalR and OpenAI are used together to build an AI-powered group chat
5
5
ms.author: kevinguo
6
+
ms.topic: tutorial
6
7
ms.date: 09/09/2024
7
8
uid: tutorials/ai-powered-group-chat
8
9
---
9
10
11
+
# Build an AI-powered group chat with SignalR and OpenAI
12
+
10
13
## Overview
11
14
12
-
The integration of AI into applications is rapidly becoming a must-have for developers looking to help their users be more creative, productive and achieve their health goals. AI-powered features, such as intelligent chatbots, personalized recommendations, and contextual responses, add significant value to modern apps. The AI-powered apps that came out since ChatGPT captured our imagination are primarily between one user and one AI assistant. As developers get more comfortable with the capabilities of AI, they are exploring AI-powered apps in a team's context. They ask "what value can AI add to a team of collaborators"?
15
+
The integration of AI into applications is rapidly becoming a must-have for developers looking to help their users be more creative, productive, and achieve their health goals. AI-powered features, such as intelligent chatbots, personalized recommendations, and contextual responses, add significant value to modern apps. The AI-powered apps that came out since ChatGPT captured our imagination are primarily between one user and one AI assistant. As developers get more comfortable with the capabilities of AI, they're exploring AI-powered apps in a team's context. They ask "what value can AI add to a team of collaborators?"
13
16
14
-
This tutorial guides you through building a real-time group chat application. Among a group of human collaborators in a chat, there's an AI assistant which has access to the chat history and can be invited to help out by any collaborator when they start the message with `@gpt`. The finished app looks like this.
17
+
This tutorial guides you through building a real-time group chat application. Among a group of human collaborators in a chat, there's an AI assistant, which has access to the chat history and can be invited to help out by any collaborator when they start the message with `@gpt`. The finished app looks like this.
15
18
16
19
:::image type="content" source="./media/signalr-tutorial-group-chat-with-open-ai/ai-powered-group-chat.jpg" alt-text="user interface for the AI-powered group chat":::
17
20
18
21
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/aspnet/AzureSignalR-samples/tree/main/samples/AIStreaming).
19
22
20
23
## Dependencies
21
-
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.
24
+
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're required to authenticate and use either service.
22
25
23
26
# [OpenAI](#tab/open-ai)
24
-
To build this application, you will need the following:
25
-
* ASP.NET Core: To create the web application and host the SignalR hub.
26
-
*[SignalR](https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Client): For real-time communication between clients and the server.
27
-
*[OpenAI Client](https://www.nuget.org/packages/OpenAI/2.0.0-beta.10): To interact with OpenAI's API for generating AI responses.
27
+
To build this application, you need the following:
28
+
* ASP.NET Core: To create the web application and host the SignalR hub
29
+
*[SignalR](https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Client): For real-time communication between clients and the server
30
+
*[OpenAI Client](https://www.nuget.org/packages/OpenAI/2.0.0-beta.10): To interact with OpenAI's API for generating AI responses
28
31
29
32
# [Azure OpenAI](#tab/azure-open-ai)
30
-
To build this application, you will need the following:
33
+
To build this application, you need the following:
31
34
* ASP.NET Core: To create the web application and host the SignalR hub.
32
-
*[SignalR](https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Client): For real-time communication between clients and the server.
33
-
*[Azure SignalR](https://learn.microsoft.com/azure/azure-signalr/signalr-overview): For managing SignalR connections at scale
35
+
*[SignalR](https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Client): For real-time communication between clients and the server
36
+
*[Azure SignalR](./signalr-overview.md): For managing SignalR connections at scale
In this section, we'll walk through the key parts of the code that integrate SignalR with OpenAI to create an AI-enhanced group chat experience.
42
+
In this section, we walk through the key parts of the code that integrate SignalR with OpenAI to create an AI-enhanced group chat experience.
40
43
41
44
### Data flow
42
45
@@ -58,7 +61,7 @@ await foreach (var completion in chatClient.CompleteChatStreamingAsync(messagesI
58
61
59
62
### Maintain context with history
60
63
61
-
Every request to [Open AI's Chat Completions API](https://platform.openai.com/docs/guides/chat-completions) is stateless - Open AI doesn't store past interactions. In a chat application, what a user or an assistant has said is important for generating a response that's contextually relevant. We can achieve this by including chat history in every request to the Completions API.
64
+
Every request to [OpenAI's Chat Completions API](https://platform.openai.com/docs/guides/chat-completions) is stateless - OpenAI doesn't store past interactions. In a chat application, what a user or an assistant has said is important for generating a response that's contextually relevant. We can achieve this by including chat history in every request to the Completions API.
62
65
63
66
The `GroupHistoryStore` class manages chat history for each group. It stores messages posted by both the users and AI assistants, ensuring that the conversation context is preserved across interactions. This context is crucial for generating coherent AI responses.
The `CompleteChatStreamingAsync()` method streams responses from OpenAI incrementally, which allows the application to send partial responses to the client as they are generated.
84
+
The `CompleteChatStreamingAsync()` method streams responses from OpenAI incrementally, which allows the application to send partial responses to the client as they're generated.
82
85
83
-
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.
86
+
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 (for example, 20 characters). This approach ensures that users see the AI’s response as it forms, mimicking a human-like typing effect.
84
87
```csharp
85
88
totalCompletion.Append(content);
86
89
if (totalCompletion.Length-lastSentTokenLength>20)
@@ -93,6 +96,6 @@ if (totalCompletion.Length - lastSentTokenLength > 20)
93
96
## Explore further
94
97
95
98
This project opens up exciting possibilities for further enhancement:
96
-
1.**Advanced AI features**: Leverage other OpenAI capabilities like sentiment analysis, translation, or summarization.
97
-
2.**Incorporating multiple AI agents**: You can introduce multiple AI agents with distinct roles or expertise areas within the same chat. For example, one agent might focus on text generation, the other provides image or audio generation. This can create a richer and more dynamic user experience where different AI agents interact seamlessly with users and each other.
99
+
1.**Advanced AI features**: Use other OpenAI capabilities like sentiment analysis, translation, or summarization.
100
+
2.**Incorporating multiple AI agents**: You can introduce multiple AI agents with distinct roles or expertise areas within the same chat. For example, one agent might focus on text generation and the other provides image or audio generation. This interaction can create a richer and more dynamic user experience where different AI agents interact seamlessly with users and each other.
98
101
3.**Share chat history between server instances**: Implement a database layer to persist chat history across sessions, allowing conversations to resume even after a disconnect. Beyond SQL or NO SQL based solutions, you can also explore using a caching service like Redis. It can significantly improve performance by storing frequently accessed data, such as chat history or AI responses, in memory. This reduces latency and offloads database operations, leading to faster response times, particularly in high-traffic scenarios.
0 commit comments