3
3
using DevExpress . AIIntegration . Extensions ;
4
4
using DevExpress . AIIntegration . Localization ;
5
5
using DevExpress . Data ;
6
+ using Microsoft . Extensions . AI ;
7
+ using System ;
6
8
using System . Globalization ;
7
9
8
10
@@ -14,9 +16,6 @@ static void Main(string[] args)
14
16
{
15
17
Console . OutputEncoding = System . Text . Encoding . UTF8 ;
16
18
17
- //Enable sending large texts to Ollama
18
- //AsyncDownloadPolicy.ConfigureHttpClient += AsyncDownloadPolicy_ConfigureHttpClient;
19
-
20
19
//Enable localization
21
20
//AIIntegrationLocalizer.Active = new CustomAILocalizer();
22
21
@@ -33,15 +32,6 @@ static void Main(string[] args)
33
32
Console . ReadKey ( ) ;
34
33
}
35
34
36
- private static void AsyncDownloadPolicy_ConfigureHttpClient ( object sender , AsyncDownloadPolicy . ConfigureHttpClientEventArgs e )
37
- {
38
- string ? fullTypeName = e ? . ValueType . FullName ;
39
- if ( fullTypeName . Contains ( "Ollama" ) )
40
- {
41
- e . Client . Timeout = TimeSpan . FromMinutes ( 15 ) ;
42
- }
43
- }
44
-
45
35
public class SampleAITextModifier
46
36
{
47
37
//Modify the following lines to obtain and pass your personal Azure OpenAI credentails to the Register* method.
@@ -50,15 +40,18 @@ public class SampleAITextModifier
50
40
string DeploymentName { get { return Environment . GetEnvironmentVariable ( "AZURE_OPENAI_DEPLOYMENTNAME" ) ; } }
51
41
52
42
AIExtensionsContainerDefault defaultAIContainer ;
43
+
53
44
public SampleAITextModifier ( )
54
45
{
55
- defaultAIContainer = new AIExtensionsContainerDefault ( ) ;
56
- //defaultAIContainer.RegisterChatClientOllamaAIService("http://localhost:11434/api/chat", "llama3.1");
57
- defaultAIContainer . RegisterChatClientOpenAIService (
58
- new AzureOpenAIClient ( new Uri ( AzureOpenAIEndpoint ) ,
59
- new System . ClientModel . ApiKeyCredential ( AzureOpenAIKey ) ) ,
60
- DeploymentName
61
- ) ;
46
+
47
+ ///To register Ollama
48
+ //OllamaChatClient ollamaChatClient = new OllamaChatClient("http://localhost:11434/api/chat", "llama3.1");
49
+ //defaultAIContainer = AIExtensionsContainerConsole.CreateDefaultAIExtensionContainer(ollamaChatClient);
50
+
51
+ ///To register Azure OpenAI
52
+ AzureOpenAIClient azureOpenAIClient = new AzureOpenAIClient ( new Uri ( AzureOpenAIEndpoint ) ,
53
+ new System . ClientModel . ApiKeyCredential ( AzureOpenAIKey ) ) ;
54
+ defaultAIContainer = AIExtensionsContainerConsole . CreateDefaultAIExtensionContainer ( azureOpenAIClient . AsChatClient ( DeploymentName ) ) ;
62
55
}
63
56
64
57
public void ChangeDefaults ( )
@@ -81,7 +74,7 @@ public async Task<string> GetTranslation(string textToTranslate, string language
81
74
}
82
75
return translatedText ;
83
76
}
84
- // Something unexpected happens
77
+ // When something unexpected has happened
85
78
switch ( result . Status )
86
79
{
87
80
case ResponseStatus . MaxTokenLimitExceeded :
@@ -98,13 +91,14 @@ public async Task<string> GetTranslation(string textToTranslate, string language
98
91
// How to replace the default extension
99
92
public async Task < string > GetShakespeareText ( string textToModify )
100
93
{
101
- defaultAIContainer . Register < RewriteStyleRequest , WilliamShakespeareStyleExtension > ( ) ;
102
- string res = await defaultAIContainer . RewriteStyleAsync ( new RewriteStyleRequest ( textToModify , WritingStyle . Formal ) ) ;
103
- return res ;
94
+ defaultAIContainer . Register < ChangeStyleRequest , WilliamShakespeareStyleExtension > ( ) ;
95
+ string res = await defaultAIContainer . ChangeStyleAsync ( new ChangeStyleRequest ( textToModify , WritingStyle . Formal ) ) ;
96
+ return res ;
104
97
}
105
98
106
99
//How to register and call your own extension
107
- public async Task < string > GetMarkTwainText ( string textToModify ) {
100
+ public async Task < string > GetMarkTwainText ( string textToModify )
101
+ {
108
102
var localContainer = new AIExtensionsContainerLocal ( defaultAIContainer ) ;
109
103
localContainer . Register < AuthoredStyleRequest , AuthoredStyleExtension > ( ) ;
110
104
@@ -119,10 +113,10 @@ public async Task<string> GetMarkTwainText(string textToModify) {
119
113
}
120
114
}
121
115
#region How to modify the default extension
122
- public class WilliamShakespeareStyleExtension : RewriteStyleExtension
116
+ public class WilliamShakespeareStyleExtension : ChangeStyleExtension
123
117
{
124
118
public WilliamShakespeareStyleExtension ( IServiceProvider serviceProvider ) : base ( serviceProvider ) { }
125
- protected override string GetSystemPrompt ( RewriteStyleRequest request )
119
+ protected override string GetSystemPrompt ( ChangeStyleRequest request )
126
120
{
127
121
return "Rewrite this text in the William Shakespeare style." ;
128
122
}
0 commit comments