Skip to content

Commit

Permalink
references and API usage updated for v24.2
Browse files Browse the repository at this point in the history
  • Loading branch information
e1em3ntoDX committed Nov 12, 2024
1 parent 5924666 commit 623c0fc
Show file tree
Hide file tree
Showing 11 changed files with 450 additions and 421 deletions.
46 changes: 20 additions & 26 deletions CS/console-ai-extension/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using DevExpress.AIIntegration.Extensions;
using DevExpress.AIIntegration.Localization;
using DevExpress.Data;
using Microsoft.Extensions.AI;
using System;
using System.Globalization;


Expand All @@ -14,9 +16,6 @@ static void Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;

//Enable sending large texts to Ollama
//AsyncDownloadPolicy.ConfigureHttpClient += AsyncDownloadPolicy_ConfigureHttpClient;

//Enable localization
//AIIntegrationLocalizer.Active = new CustomAILocalizer();

Expand All @@ -33,15 +32,6 @@ static void Main(string[] args)
Console.ReadKey();
}

private static void AsyncDownloadPolicy_ConfigureHttpClient(object sender, AsyncDownloadPolicy.ConfigureHttpClientEventArgs e)
{
string? fullTypeName = e?.ValueType.FullName;
if (fullTypeName.Contains("Ollama"))
{
e.Client.Timeout = TimeSpan.FromMinutes(15);
}
}

public class SampleAITextModifier
{
//Modify the following lines to obtain and pass your personal Azure OpenAI credentails to the Register* method.
Expand All @@ -50,15 +40,18 @@ public class SampleAITextModifier
string DeploymentName { get { return Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENTNAME"); } }

AIExtensionsContainerDefault defaultAIContainer;

public SampleAITextModifier()
{
defaultAIContainer = new AIExtensionsContainerDefault();
//defaultAIContainer.RegisterChatClientOllamaAIService("http://localhost:11434/api/chat", "llama3.1");
defaultAIContainer.RegisterChatClientOpenAIService(
new AzureOpenAIClient(new Uri(AzureOpenAIEndpoint),
new System.ClientModel.ApiKeyCredential(AzureOpenAIKey)),
DeploymentName
);

///To register Ollama
//OllamaChatClient ollamaChatClient = new OllamaChatClient("http://localhost:11434/api/chat", "llama3.1");
//defaultAIContainer = AIExtensionsContainerConsole.CreateDefaultAIExtensionContainer(ollamaChatClient);

///To register Azure OpenAI
AzureOpenAIClient azureOpenAIClient = new AzureOpenAIClient(new Uri(AzureOpenAIEndpoint),
new System.ClientModel.ApiKeyCredential(AzureOpenAIKey));
defaultAIContainer = AIExtensionsContainerConsole.CreateDefaultAIExtensionContainer(azureOpenAIClient.AsChatClient(DeploymentName));
}

public void ChangeDefaults()
Expand All @@ -81,7 +74,7 @@ public async Task<string> GetTranslation(string textToTranslate, string language
}
return translatedText;
}
// Something unexpected happens
// When something unexpected has happened
switch (result.Status)
{
case ResponseStatus.MaxTokenLimitExceeded:
Expand All @@ -98,13 +91,14 @@ public async Task<string> GetTranslation(string textToTranslate, string language
// How to replace the default extension
public async Task<string> GetShakespeareText(string textToModify)
{
defaultAIContainer.Register<RewriteStyleRequest, WilliamShakespeareStyleExtension>();
string res = await defaultAIContainer.RewriteStyleAsync(new RewriteStyleRequest(textToModify, WritingStyle.Formal));
return res;
defaultAIContainer.Register<ChangeStyleRequest, WilliamShakespeareStyleExtension>();
string res = await defaultAIContainer.ChangeStyleAsync(new ChangeStyleRequest(textToModify, WritingStyle.Formal));
return res;
}

//How to register and call your own extension
public async Task<string> GetMarkTwainText(string textToModify) {
public async Task<string> GetMarkTwainText(string textToModify)
{
var localContainer = new AIExtensionsContainerLocal(defaultAIContainer);
localContainer.Register<AuthoredStyleRequest, AuthoredStyleExtension>();

Expand All @@ -119,10 +113,10 @@ public async Task<string> GetMarkTwainText(string textToModify) {
}
}
#region How to modify the default extension
public class WilliamShakespeareStyleExtension : RewriteStyleExtension
public class WilliamShakespeareStyleExtension : ChangeStyleExtension
{
public WilliamShakespeareStyleExtension(IServiceProvider serviceProvider) : base(serviceProvider) { }
protected override string GetSystemPrompt(RewriteStyleRequest request)
protected override string GetSystemPrompt(ChangeStyleRequest request)
{
return "Rewrite this text in the William Shakespeare style.";
}
Expand Down
8 changes: 4 additions & 4 deletions CS/console-ai-extension/Runtime-AI-Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DevExpress.AIIntegration" Version="24.2.1-alpha-24241" />
<PackageReference Include="DevExpress.AIIntegration.Azure.OpenAI" Version="24.2.1-alpha-24241" />
<PackageReference Include="DevExpress.AIIntegration.Ollama" Version="24.2.1-alpha-24241" />
<PackageReference Include="DevExpress.AIIntegration.OpenAI" Version="24.2.1-alpha-24241" />
<PackageReference Include="Azure.AI.OpenAI" Version="2.0.0" />
<PackageReference Include="DevExpress.AIIntegration" Version="24.2.1-alpha-24317" />
<PackageReference Include="Microsoft.Extensions.AI.Ollama" Version="9.0.0-preview.9.24525.1" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.0.0-preview.9.24525.1" />
</ItemGroup>

</Project>
18 changes: 9 additions & 9 deletions CS/winforms-ai-extensions/MemoEditForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions CS/winforms-ai-extensions/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Azure.AI.OpenAI;
using DevExpress.AIIntegration;
using Microsoft.Extensions.AI;

namespace WinForms_AI_Extensions
{
internal static class Program {
internal static class Program
{
//Modify the following lines to obtain and pass your personal Azure OpenAI credentails to the Register* method.
static string AzureOpenAIEndpoint { get { return Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"); } }
static string AzureOpenAIKey { get { return Environment.GetEnvironmentVariable("AZURE_OPENAI_APIKEY"); } }
Expand All @@ -27,11 +29,12 @@ static void Main()

private static void RegisterDevExpressAI()
{
AIExtensionsContainerDesktop.Default.RegisterChatClientOpenAIService(
new AzureOpenAIClient(
new Uri(AzureOpenAIEndpoint),
new System.ClientModel.ApiKeyCredential(AzureOpenAIKey)),
DeploymentName);
///To register Ollama
//OllamaChatClient ollamaChatClient = new OllamaChatClient("http://localhost:11434/api/chat", "llama3.1");

AzureOpenAIClient azureOpenAIClient = new AzureOpenAIClient(new Uri(AzureOpenAIEndpoint),
new System.ClientModel.ApiKeyCredential(AzureOpenAIKey));
AIExtensionsContainerDesktop.Default.RegisterChatClient(azureOpenAIClient.AsChatClient(DeploymentName));
}
}
}
Loading

0 comments on commit 623c0fc

Please sign in to comment.