Browse Source

Merge pull request #169 from drasticactions/sk-api-update

SemanticKernel API Update
tags/v0.6.0
Martin Evans GitHub 2 years ago
parent
commit
6a04f05e06
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 23 deletions
  1. +23
    -22
      LLama.SemanticKernel/ChatCompletion/LLamaSharpChatCompletion.cs
  2. +6
    -0
      LLama.SemanticKernel/ChatCompletion/LLamaSharpChatResult.cs
  3. +1
    -1
      LLama.SemanticKernel/LLamaSharp.SemanticKernel.csproj

+ 23
- 22
LLama.SemanticKernel/ChatCompletion/LLamaSharpChatCompletion.cs View File

@@ -1,13 +1,6 @@
using LLama; using LLama;
using Microsoft.SemanticKernel.AI.ChatCompletion; using Microsoft.SemanticKernel.AI.ChatCompletion;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


namespace LLamaSharp.SemanticKernel.ChatCompletion; namespace LLamaSharp.SemanticKernel.ChatCompletion;


@@ -19,12 +12,32 @@ public sealed class LLamaSharpChatCompletion : IChatCompletion
private const string UserRole = "user:"; private const string UserRole = "user:";
private const string AssistantRole = "assistant:"; private const string AssistantRole = "assistant:";
private ChatSession session; private ChatSession session;
private ChatRequestSettings defaultRequestSettings;


public LLamaSharpChatCompletion(InteractiveExecutor model)
public LLamaSharpChatCompletion(InteractiveExecutor model, ChatRequestSettings? defaultRequestSettings = default)
{ {
this.session = new ChatSession(model) this.session = new ChatSession(model)
.WithHistoryTransform(new HistoryTransform()) .WithHistoryTransform(new HistoryTransform())
.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { UserRole, AssistantRole })); .WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { UserRole, AssistantRole }));
this.defaultRequestSettings = defaultRequestSettings ??= new ChatRequestSettings()
{
MaxTokens = 256,
Temperature = 0,
TopP = 0,
StopSequences = new List<string> { }
};
}

public LLamaSharpChatCompletion(ChatSession session, ChatRequestSettings? defaultRequestSettings = default)
{
this.session = session;
this.defaultRequestSettings = defaultRequestSettings ??= new ChatRequestSettings()
{
MaxTokens = 256,
Temperature = 0,
TopP = 0,
StopSequences = new List<string> { }
};
} }


/// <inheritdoc/> /// <inheritdoc/>
@@ -43,13 +56,7 @@ public sealed class LLamaSharpChatCompletion : IChatCompletion
/// <inheritdoc/> /// <inheritdoc/>
public async Task<IReadOnlyList<IChatResult>> GetChatCompletionsAsync(ChatHistory chat, ChatRequestSettings? requestSettings = null, CancellationToken cancellationToken = default) public async Task<IReadOnlyList<IChatResult>> GetChatCompletionsAsync(ChatHistory chat, ChatRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
{ {
requestSettings ??= new ChatRequestSettings()
{
MaxTokens = 256,
Temperature = 0,
TopP = 0,
StopSequences = new List<string> { }
};
requestSettings = requestSettings ?? this.defaultRequestSettings;


var result = this.session.ChatAsync(chat.ToLLamaSharpChatHistory(), requestSettings.ToLLamaSharpInferenceParams(), cancellationToken); var result = this.session.ChatAsync(chat.ToLLamaSharpChatHistory(), requestSettings.ToLLamaSharpInferenceParams(), cancellationToken);


@@ -59,13 +66,7 @@ public sealed class LLamaSharpChatCompletion : IChatCompletion
/// <inheritdoc/> /// <inheritdoc/>
public async IAsyncEnumerable<IChatStreamingResult> GetStreamingChatCompletionsAsync(ChatHistory chat, ChatRequestSettings? requestSettings = null, [EnumeratorCancellation] CancellationToken cancellationToken = default) public async IAsyncEnumerable<IChatStreamingResult> GetStreamingChatCompletionsAsync(ChatHistory chat, ChatRequestSettings? requestSettings = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{ {
requestSettings ??= new ChatRequestSettings()
{
MaxTokens = 256,
Temperature = 0,
TopP = 0,
StopSequences = new List<string> { }
};
requestSettings = requestSettings ?? this.defaultRequestSettings;


var result = this.session.ChatAsync(chat.ToLLamaSharpChatHistory(), requestSettings.ToLLamaSharpInferenceParams(), cancellationToken); var result = this.session.ChatAsync(chat.ToLLamaSharpChatHistory(), requestSettings.ToLLamaSharpInferenceParams(), cancellationToken);




+ 6
- 0
LLama.SemanticKernel/ChatCompletion/LLamaSharpChatResult.cs View File

@@ -1,4 +1,5 @@
using Microsoft.SemanticKernel.AI.ChatCompletion; using Microsoft.SemanticKernel.AI.ChatCompletion;
using Microsoft.SemanticKernel.Orchestration;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text; using System.Text;


@@ -6,6 +7,7 @@ namespace LLamaSharp.SemanticKernel.ChatCompletion;


internal sealed class LLamaSharpChatResult : IChatStreamingResult internal sealed class LLamaSharpChatResult : IChatStreamingResult
{ {
private readonly ModelResult _modelResult;
private readonly IAsyncEnumerable<string> _stream; private readonly IAsyncEnumerable<string> _stream;


/// <summary> /// <summary>
@@ -15,7 +17,11 @@ internal sealed class LLamaSharpChatResult : IChatStreamingResult
public LLamaSharpChatResult(IAsyncEnumerable<string> stream) public LLamaSharpChatResult(IAsyncEnumerable<string> stream)
{ {
_stream = stream; _stream = stream;
this._modelResult = new ModelResult(stream);
} }

public ModelResult ModelResult => this._modelResult;

/// <inheritdoc/> /// <inheritdoc/>
public async Task<ChatMessageBase> GetChatMessageAsync(CancellationToken cancellationToken = default) public async Task<ChatMessageBase> GetChatMessageAsync(CancellationToken cancellationToken = default)
{ {


+ 1
- 1
LLama.SemanticKernel/LLamaSharp.SemanticKernel.csproj View File

@@ -33,7 +33,7 @@
</PropertyGroup> </PropertyGroup>


<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.SemanticKernel.Abstractions" Version="0.21.230828.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Abstractions" Version="0.24.230911.2-preview" />
</ItemGroup> </ItemGroup>


<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'"> <ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">


Loading…
Cancel
Save