Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Senparc.AI.Agents/Senparc.AI.Agents.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>0.1.3-beta1</Version>
<Version>0.1.4</Version>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<AssemblyName>Senparc.AI.Agents</AssemblyName>
Expand Down
1 change: 1 addition & 0 deletions src/Senparc.AI.Kernel.Tests/BaseSupport/KernelTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class KernelTestBase : BaseTest
{
services.AddScoped<IAiHandler, SemanticAiHandler>();
};

public KernelTestBase() : base(RegisterAction, getSenparcAiSettingFunc, serviceAction)
{

Expand Down
34 changes: 32 additions & 2 deletions src/Senparc.AI.Kernel/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.TextGeneration;
using Microsoft.SemanticKernel;
using OllamaSharp;

namespace Senparc.AI.Kernel
{
Expand All @@ -29,12 +30,41 @@ public static IKernelBuilder AddFastAPIChatCompletion(this IKernelBuilder builde
string apiKey2 = apiKey;
string orgId2 = orgId;

Func<IServiceProvider, object, OpenAIChatCompletionService> implementationFactory =
(IServiceProvider serviceProvider, object? _) =>
Func<IServiceProvider, object, OpenAIChatCompletionService> implementationFactory =
(IServiceProvider serviceProvider, object? _) =>
new OpenAIChatCompletionService(modelId, new OpenAIClient(new Uri(endpoint), new Azure.AzureKeyCredential(apiKey)), serviceProvider.GetService<ILoggerFactory>());
builder.Services.AddKeyedSingleton((object?)serviceId, (Func<IServiceProvider, object?, IChatCompletionService>)implementationFactory);
builder.Services.AddKeyedSingleton((object?)serviceId, (Func<IServiceProvider, object?, ITextGenerationService>)implementationFactory);
return builder;
}

#region Ollama

public static IKernelBuilder AddFOllamaChatCompletion(this IKernelBuilder builder, string modelId, string endpoint, string serviceId = null)
{
string modelId2 = modelId;

Func<IServiceProvider, object, OpenAIChatCompletionService> implementationFactory =
(IServiceProvider serviceProvider, object? _) =>
new OpenAIChatCompletionService(modelId: modelId, endpoint: new Uri(endpoint), apiKey: "none", loggerFactory: serviceProvider.GetService<ILoggerFactory>());
builder.Services.AddKeyedSingleton((object?)serviceId, (Func<IServiceProvider, object?, IChatCompletionService>)implementationFactory);

return builder;
}

public static IKernelBuilder AddFOllamaTextCompletion(this IKernelBuilder builder, string modelId, string endpoint, string serviceId = null)
{
string modelId2 = modelId;

Func<IServiceProvider, object, OpenAITextGenerationService> implementationFactory =
(IServiceProvider serviceProvider, object? _) =>
new OpenAITextGenerationService(modelId: modelId, new OpenAIClient(endpoint: new Uri(endpoint), new Azure.AzureKeyCredential("none")), loggerFactory: serviceProvider.GetService<ILoggerFactory>());

builder.Services.AddKeyedSingleton((object?)serviceId, (Func<IServiceProvider, object?, ITextGenerationService>)implementationFactory);

return builder;
}

#endregion
}
}
10 changes: 10 additions & 0 deletions src/Senparc.AI.Kernel/Helpers/SemanticKernelHelper.Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public IKernelBuilder ConfigChat(string userId, string modelName = null, ISenpar
endpoint: senparcAiSetting.FastAPIEndpoint,
serviceId: null
),
AiPlatform.Ollama => kernelBuilder.AddFOllamaChatCompletion(
modelId: modelName,
endpoint: senparcAiSetting.OllamaEndpoint,
serviceId: null
),
_ => throw new SenparcAiException($"没有处理当前 {nameof(AiPlatform)} 类型:{aiPlatForm}")
};

Expand Down Expand Up @@ -153,6 +158,11 @@ public IKernelBuilder ConfigTextCompletion(string userId, string modelName = nul
endpoint: senparcAiSetting.FastAPIEndpoint,
serviceId: null
),
AiPlatform.Ollama => kernelBuilder.AddFOllamaTextCompletion(
modelId: modelName,
endpoint: senparcAiSetting.OllamaEndpoint,
serviceId: null
),
_ => throw new SenparcAiException($"没有处理当前 {nameof(AiPlatform)} 类型:{aiPlatForm}")
};

Expand Down
49 changes: 49 additions & 0 deletions src/Senparc.AI.Kernel/Ollama/OllamaChatCompletionService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Services;
using Microsoft.SemanticKernel.TextGeneration;

namespace Senparc.AI.Kernel.Ollama
{
public class OllamaChatCompletionService : IChatCompletionService, IAIService, ITextGenerationService
{
private readonly OllamaSharp.OllamaApiClient _core;

Dictionary<string, object?> _attributes = new Dictionary<string, object?>();

public IReadOnlyDictionary<string, object?> Attributes => _attributes;

public OllamaChatCompletionService(string url, string modelId)
{
_core = new OllamaSharp.OllamaApiClient(url, modelId);

_attributes["model"] = modelId;
}


public Task<IReadOnlyList<ChatMessageContent>> GetChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Microsoft.SemanticKernel.Kernel? kernel = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Microsoft.SemanticKernel.Kernel? kernel = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public IAsyncEnumerable<StreamingTextContent> GetStreamingTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Microsoft.SemanticKernel.Kernel? kernel = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public Task<IReadOnlyList<TextContent>> GetTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Microsoft.SemanticKernel.Kernel? kernel = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}
}
5 changes: 4 additions & 1 deletion src/Senparc.AI.Kernel/Senparc.AI.Kernel.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>0.16.3-beta1</Version>
<Version>0.17.1</Version>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<AssemblyName>Senparc.AI.Kernel</AssemblyName>
Expand Down Expand Up @@ -46,6 +46,7 @@
v0.15.3 添加 IWantToRun.SetPromptConfigParameter() 方法
v0.15.4 升级到 SK 1.10.0,为 ImportPluginFromPromptDirectory() 方法添加已有对象的检测
v0.16.1 添加 RunAsync(kernelFunction) 方法
v0.17.0 支持 Ollama
</PackageReleaseNotes>
<RepositoryUrl>https://github.com/Senparc/Senparc.AI.Kernel</RepositoryUrl>
<Configurations>Debug;Release;Test</Configurations>
Expand All @@ -68,6 +69,8 @@
<PackageReference Include="Microsoft.SemanticKernel.Connectors.HuggingFace" Version="1.10.0-preview" />
<!--<PackageReference Include="Microsoft.SemanticKernel.Functions.Semantic" Version="1.0.0-beta2" />-->
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.10.0-alpha" />
<!--<PackageReference Include="Ollama" Version="1.6.1" />-->
<PackageReference Include="OllamaSharp" Version="2.0.13" />

<PackageReference Include="Senparc.CO2NET" Version="2.4.0.1" />
</ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions src/Senparc.AI/Entities/Keys/OllamaKeys.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using Senparc.AI.Entities.Keys;

namespace Senparc.AI
{
public class OllamaKeys : BaseKeys
{
public string Endpoint { get; set; }
}
}
25 changes: 24 additions & 1 deletion src/Senparc.AI/Entities/SenparcAiSettingBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public class SenparcAiSettingBase : ISenparcAiSetting
/// </summary>
public virtual bool UseFastAPI => AiPlatform == AiPlatform.FastAPI;

/// <summary>
/// 是否使用 Ollama
/// </summary>
public virtual bool Ollama => AiPlatform == AiPlatform.Ollama;

/// <summary>
/// AI 平台类型
Expand All @@ -81,6 +85,7 @@ public class SenparcAiSettingBase : ISenparcAiSetting
public virtual HuggingFaceKeys HuggingFaceKeys { get; set; }

public virtual FastAPIKeys FastAPIKeys { get; set; }
public virtual OllamaKeys OllamaKeys { get; set; }

/// <summary>
/// Azure OpenAI 或 OpenAI API Key
Expand All @@ -91,7 +96,8 @@ public class SenparcAiSettingBase : ISenparcAiSetting
AiPlatform.NeuCharAI => NeuCharAIKeys?.ApiKey,
AiPlatform.AzureOpenAI => AzureOpenAIKeys?.ApiKey,
AiPlatform.HuggingFace => "",
AiPlatform.FastAPI => FastAPIKeys?.ApiKey,
AiPlatform.FastAPI => FastAPIKeys.ApiKey,
AiPlatform.Ollama => "",
_ => ""
};

Expand Down Expand Up @@ -156,6 +162,11 @@ public class SenparcAiSettingBase : ISenparcAiSetting

#endregion

#region Ollama
public string OllamaEndpoint => OllamaKeys?.Endpoint;

#endregion

public virtual bool IsOpenAiKeysSetted => OpenAIKeys != null && !OpenAIKeys.ApiKey.IsNullOrEmpty();


Expand Down Expand Up @@ -219,6 +230,18 @@ public ISenparcAiSetting SetFastAPI(FastAPIKeys fastAPIKeys)
return this;
}

/// <summary>
/// 设置 Ollama
/// </summary>
/// <param name="ollamaAPIKeys"></param>
/// <returns></returns>
public ISenparcAiSetting SetOllama(OllamaKeys ollamaAPIKeys)
{
this.AiPlatform = AiPlatform.Ollama;
this.OllamaKeys = ollamaAPIKeys;
return this;
}

/// <summary>
/// 设置其他平台
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Senparc.AI/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public enum AiPlatform
AzureOpenAI = 16,
HuggingFace = 32,
//Oobabooga = 64,//未实现
FastAPI = 128
FastAPI = 128,
Ollama = 256,
}

/// <summary>
Expand Down
15 changes: 12 additions & 3 deletions src/Senparc.AI/Interfaces/ISenparcAiSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public interface ISenparcAiSetting
AiPlatform.NeuCharAI => NeuCharEndpoint,
AiPlatform.HuggingFace => HuggingFaceEndpoint,
AiPlatform.FastAPI => FastAPIEndpoint,
AiPlatform.Ollama => OllamaEndpoint,
_ => throw new SenparcAiException($"未配置 {AiPlatform} 的 Endpoint 输出")
};

Expand All @@ -64,6 +65,7 @@ public interface ISenparcAiSetting
OpenAIKeys OpenAIKeys { get; set; }
HuggingFaceKeys HuggingFaceKeys { get; set; }
FastAPIKeys FastAPIKeys { get; set; }
OllamaKeys OllamaKeys { get; set; }

/// <summary>
/// Neuchar OpenAI 或 Azure OpenAI 或 OpenAI API Key
Expand Down Expand Up @@ -122,13 +124,18 @@ public interface ISenparcAiSetting

#endregion


#region FastAPI

string FastAPIEndpoint { get; }

#endregion

#region Ollama

string OllamaEndpoint { get; }

#endregion

/// <summary>
/// OpenAIKeys 是否已经设置
/// </summary>
Expand All @@ -142,7 +149,8 @@ public interface ISenparcAiSetting
AiPlatform.NeuCharAI => NeuCharAIKeys.ModelName,
AiPlatform.HuggingFace => HuggingFaceKeys.ModelName,
AiPlatform.FastAPI => FastAPIKeys.ModelName,
_ => throw new SenparcAiException($"100-未配置 {AiPlatform} 的 ModelName")
AiPlatform.Ollama => OllamaKeys.ModelName,
_ => throw new SenparcAiException($"100-未配置 {AiPlatform} 的 Endpoint 输出")
};

#pragma warning disable CS8603 // 可能返回 null 引用。
Expand All @@ -153,7 +161,8 @@ public interface ISenparcAiSetting
AiPlatform.NeuCharAI => null,
AiPlatform.HuggingFace => null,
AiPlatform.FastAPI => null,
_ => throw new SenparcAiException($"未配置 {AiPlatform} 的 DeploymentName")
AiPlatform.Ollama => null,
_ => throw new SenparcAiException($"未配置 {AiPlatform} 的 DeploymentName 输出")
};
#pragma warning restore CS8603 // 可能返回 null 引用。

Expand Down
2 changes: 1 addition & 1 deletion src/Senparc.AI/Senparc.AI.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>0.16.3-beta1</Version>
<Version>0.16.4</Version>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<AssemblyName>Senparc.AI</AssemblyName>
Expand Down