-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update PromptService, ConfigViewModels, and SemanticFunctionViewModel…
… for DashScope integration and configuration improvements (#25) * stage: more llm config * feat: add openai llm * add DashScope
- Loading branch information
Showing
14 changed files
with
270 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Avalonia.Data.Converters; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace PromptPlayground.Converters | ||
{ | ||
public class StringToBooleanConverter : IValueConverter | ||
{ | ||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
if (value == null || parameter == null || !(value is string) || !(parameter is string)) | ||
{ | ||
return false; | ||
} | ||
|
||
var strValue = (string)value; | ||
var strParameter = (string)parameter; | ||
|
||
return strValue.Equals(strParameter, StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
PromptPlayground/ViewModels/ConfigViewModels/ConfigAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using DashScope; | ||
using Humanizer; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace PromptPlayground.ViewModels.ConfigViewModels | ||
{ | ||
public class ConfigAttribute : ObservableObject | ||
{ | ||
static ConfigAttribute() | ||
{ | ||
var consts = typeof(ConfigAttribute).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy).Where(fi => fi.IsLiteral && !fi.IsInitOnly).ToList(); | ||
var ct = typeof(ConfigTypeAttribute); | ||
_fields = consts.Select(c => | ||
{ | ||
var value = c.GetValue(null) as string; | ||
|
||
if (c.IsDefined(ct)) | ||
{ | ||
return (value, c.GetCustomAttribute(ct) as ConfigTypeAttribute); | ||
} | ||
else | ||
{ | ||
return (value, new ConfigTypeAttribute()); | ||
} | ||
}).ToDictionary(_ => _.Item1!, _ => _.Item2!); | ||
} | ||
static readonly Dictionary<string, ConfigTypeAttribute> _fields; | ||
public ConfigAttribute(string name) | ||
{ | ||
Name = name; | ||
this.Type = _fields[name].Type; | ||
this.SelectValues = _fields[name].SelectValues?.ToList() ?? new List<string>(); | ||
} | ||
private string _value = string.Empty; | ||
[JsonIgnore] | ||
public string HumanizeName => Name.Humanize(); | ||
public string Type { get; set; } = "string"; | ||
public List<string> SelectValues { get; set; } | ||
public string Name { get; set; } = string.Empty; | ||
public string Value { get => _value; set => SetProperty(ref _value, value, nameof(Value)); } | ||
|
||
#region Constants | ||
public const string AzureDeployment = nameof(AzureDeployment); | ||
public const string AzureEndpoint = nameof(AzureEndpoint); | ||
public const string AzureSecret = nameof(AzureSecret); | ||
public const string AzureEmbeddingDeployment = nameof(AzureEmbeddingDeployment); | ||
|
||
public const string BaiduClientId = nameof(BaiduClientId); | ||
public const string BaiduSecret = nameof(BaiduSecret); | ||
[ConfigType("select", "Ernie-Bot", "Ernie-Bot-turbo", "BLOOMZ_7B")] | ||
public const string BaiduModel = nameof(BaiduModel); | ||
|
||
public const string OpenAIApiKey = nameof(OpenAIApiKey); | ||
public const string OpenAIModel = nameof(OpenAIModel); | ||
|
||
public const string DashScopeApiKey = nameof(DashScopeApiKey); | ||
[ConfigType("select", DashScopeModels.QWenV1, DashScopeModels.QWenPlusV1)] | ||
public const string DashScopeModel = nameof(DashScopeModel); | ||
|
||
public const string QdrantEndpoint = nameof(QdrantEndpoint); | ||
public const string QdrantApiKey = nameof(QdrantApiKey); | ||
|
||
public const string VectorSize = nameof(VectorSize); | ||
#endregion | ||
} | ||
|
||
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] | ||
public class ConfigTypeAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// `string` or `select`, use select must set SelectValues | ||
/// </summary> | ||
/// <param name="type"></param> | ||
public ConfigTypeAttribute(string type = "string", params string[] selectValues) | ||
{ | ||
Type = type; | ||
if (type == "select") | ||
{ | ||
if (selectValues == null || selectValues.Length == 0) | ||
{ | ||
throw new ArgumentException("selectValues must not be null or empty when type is select"); | ||
} | ||
|
||
SelectValues = selectValues; | ||
} | ||
} | ||
|
||
public string Type { get; } | ||
public string[]? SelectValues { get; } | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
PromptPlayground/ViewModels/ConfigViewModels/LLM/DashScopeConfigViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Azure.AI.OpenAI; | ||
using DashScope; | ||
using DashScope.Models; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Orchestration; | ||
using PromptPlayground.Services; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace PromptPlayground.ViewModels.ConfigViewModels.LLM | ||
{ | ||
internal class DashScopeConfigViewModel : ConfigViewModelBase, ILLMConfigViewModel | ||
{ | ||
public override string Name => "DashScope"; | ||
|
||
public DashScopeConfigViewModel(IConfigAttributesProvider provider) : base(provider) | ||
{ | ||
RequireAttribute(ConfigAttribute.DashScopeApiKey); | ||
RequireAttribute(ConfigAttribute.DashScopeModel); | ||
} | ||
|
||
public KernelBuilder CreateKernelBuilder() | ||
{ | ||
var apiKey = GetAttribute(ConfigAttribute.DashScopeApiKey); | ||
var model = GetAttribute(ConfigAttribute.DashScopeModel); | ||
|
||
return Kernel.Builder.WithDashScopeCompletionService(apiKey, model); | ||
} | ||
|
||
public ResultTokenUsage? GetUsage(ModelResult resultModel) | ||
{ | ||
var usage = resultModel.GetResult<CompletionResponse>().Usage; | ||
return new ResultTokenUsage(usage.InputTokens + usage.OutputTokens, usage.InputTokens, usage.OutputTokens); | ||
} | ||
} | ||
} |
Oops, something went wrong.