Skip to content

Commit b6c9dfd

Browse files
Added the content and code snippets for AzureBaseService class.
1 parent 1c2b530 commit b6c9dfd

File tree

1 file changed

+103
-35
lines changed

1 file changed

+103
-35
lines changed

MAUI/DataForm/AI-Powered-Smart-Paste-DataForm.md

Lines changed: 103 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,52 +26,120 @@ To enable AI functionality in your .NET MAUI Scheduler, first ensure that you ha
2626

2727
### Step 3: Connect to the Azure OpenAI
2828

29-
To connect your .NET MAUI app to Azure OpenAI, create a service class that handles communication with the AI model. Start by initializing the OpenAIClient using your Azure endpoint and API key.
29+
To connect your .NET MAUI app to Azure OpenAI, create a service class that handles communication with the AI model.
3030

31-
In this service, define a method called GetAnswerFromGPT. This method takes a user prompt as input, sends it to the deployed model, and returns the AI-generated response.
31+
```
32+
/// <summary>
33+
/// Helper class to interact with Azure AI.
34+
/// </summary>
35+
internal class AzureOpenAIServiceConnector : AzureBaseService
36+
{
37+
38+
}
39+
```
40+
41+
In this service, define a method called `GetAnswerFromGPT`. This method takes a user prompt from the SfAIAssistView control as input, sends it to the deployed model (e.g., GPT35Turbo), and returns the AI-generated response.
42+
43+
```
44+
/// <summary>
45+
/// Helper class to interact with Azure AI.
46+
/// </summary>
47+
internal class AzureOpenAIServiceConnector : AzureBaseService
48+
{
49+
50+
/// <summary>
51+
/// Retrieves an answer from the deployment name model using the provided user prompt.
52+
/// </summary>
53+
/// <param name="userPrompt">The user prompt.</param>
54+
/// <returns>The AI response.</returns>
55+
internal async Task<string> GetAnswerFromGPT(string userPrompt)
56+
{
57+
ChatHistory = string.Empty;
58+
if (IsCredentialValid && Client != null && ChatHistory != null)
59+
{
60+
// Add the user's prompt as a user message to the conversation.
61+
ChatHistory = ChatHistory + "You are a predictive analytics assistant.";
62+
// Add the user's prompt as a user message to the conversation.
63+
ChatHistory = ChatHistory + userPrompt;
64+
try
65+
{
66+
//// Send the chat completion request to the OpenAI API and await the response.
67+
var response = await Client.CompleteAsync(ChatHistory);
68+
return response.ToString();
69+
}
70+
catch
71+
{
72+
// If an exception occurs (e.g., network issues, API errors), return an empty string.
73+
return "";
74+
}
75+
}
3276
77+
return "";
78+
}
79+
}
3380
```
34-
/// <summary>
35-
/// Helper class to interact with Azure AI.
36-
/// </summary>
37-
internal class AzureOpenAIServiceConnector : AzureBaseService
81+
82+
Within the base service class (AzureBaseService), initialize the OpenAIClient with your Azure endpoint, deployment name, and API key.
83+
84+
```
85+
public abstract class AzureBaseService
3886
{
87+
#region Fields
88+
/// <summary>
89+
/// The EndPoint
90+
/// </summary>
91+
internal const string Endpoint = "YOUR_END_POINT_NAME";
3992
4093
/// <summary>
41-
/// Retrieves an answer from the deployment name model using the provided user prompt.
94+
/// The Deployment name
4295
/// </summary>
43-
/// <param name="userPrompt">The user prompt.</param>
44-
/// <returns>The AI response.</returns>
45-
internal async Task<string> GetAnswerFromGPT(string userPrompt)
46-
{
47-
ChatHistory = string.Empty;
48-
if (IsCredentialValid && Client != null && ChatHistory != null)
49-
{
50-
// Add the user's prompt as a user message to the conversation.
51-
ChatHistory = ChatHistory + "You are a predictive analytics assistant.";
52-
// Add the user's prompt as a user message to the conversation.
53-
ChatHistory = ChatHistory + userPrompt;
54-
try
55-
{
56-
//// Send the chat completion request to the OpenAI API and await the response.
57-
var response = await Client.CompleteAsync(ChatHistory);
58-
return response.ToString();
59-
}
60-
catch
61-
{
62-
// If an exception occurs (e.g., network issues, API errors), return an empty string.
63-
return "";
64-
}
65-
}
96+
internal const string DeploymentName = "DEPLOYMENT_NAME";
97+
98+
/// <summary>
99+
/// The Image Deployment name
100+
/// </summary>
101+
internal const string ImageDeploymentName = "IMAGE_DEPOLYMENT_NAME";
102+
103+
/// <summary>
104+
/// The API key
105+
/// </summary>
106+
internal const string Key = "API_KEY";
107+
108+
/// <summary>
109+
/// The already credential validated field
110+
/// </summary>
111+
private static bool isAlreadyValidated = false;
112+
113+
/// <summary>
114+
/// Indicating whether an credentials are valid or not
115+
/// </summary>
116+
private static bool _isCredentialValid;
66117
67-
return "";
118+
#endregion
119+
120+
public AzureBaseService()
121+
{
122+
ValidateCredential();
68123
}
69-
}
70-
```
71124
125+
internal IChatClient? Client { get; set; }
126+
127+
/// <summary>
128+
/// To get the Azure open ai method
129+
/// </summary>
130+
private void GetAzureOpenAI()
131+
{
132+
try
133+
{
134+
var client = new AzureOpenAIClient(new Uri(Endpoint), new AzureKeyCredential(Key)).AsChatClient(modelId: DeploymentName);
135+
this.Client = client;
136+
}
137+
catch (Exception)
138+
{
139+
}
140+
}
141+
}
72142
```
73-
this.client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
74-
```
75143

76144
## Integrating AI-powered Smart Paste in .NET MAUI DataForm
77145

0 commit comments

Comments
 (0)