Skip to content

added AsChatClient to C# package + some clean up #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 20, 2024
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
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-14, windows-latest]
os: [ubuntu-latest, macos-15, windows-latest]

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -75,6 +75,9 @@ jobs:

steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '9.0.x'
- name: Download Linux artifacts
uses: actions/download-artifact@v3
with:
Expand All @@ -83,7 +86,7 @@ jobs:
- name: Download macOS artifacts
uses: actions/download-artifact@v3
with:
name: native-library-macos-14
name: native-library-macos-15
path: artifacts/macos
- name: Download Windows artifacts
uses: actions/download-artifact@v3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,9 @@ private ConversationContext GetConversationContext(IList<ChatMessage> messages)
var phiEngineMessages = head.Where(m => m.Role != ChatRole.System && m.Role != ChatRole.Tool)
.Select(m => m.ToConversationMessage()).ToList();
var systemInstruction = head.LastOrDefault(m => m.Role == ChatRole.System)?.Text ?? _systemInstruction;

Console.WriteLine($"Using system instruction: {systemInstruction}");
return new ConversationContext(phiEngineMessages, systemInstruction);
}

Console.WriteLine($"Using default system instruction: {_systemInstruction}");
return new ConversationContext([], _systemInstruction);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Strathweb.Phi.Engine.Microsoft.Extensions.AI;

public static class PhiEngineExtensions
{
public static PhiEngineChatClient AsChatClient(this PhiEngine engine, string id, StreamingEventHandler handler, string systemInstruction = null, InferenceOptions inferenceOptions = null) =>
new(id, engine, handler, systemInstruction, inferenceOptions);
}
5 changes: 4 additions & 1 deletion samples/csharp/chatclient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
modelBuilder.WithEventHandler(new BoxedPhiEventHandler(handler));
var model = modelBuilder.Build(cacheDir);

var chatClient = new PhiEngineChatClient("Local Phi-3 Demo", model, handler, systemInstruction: "You convert what user said to all uppercase.");
var chatClient = model.AsChatClient("Local Phi-3 Demo", handler,
systemInstruction: "You convert what user said to all uppercase.");
// or alternatively
//var chatClient = new PhiEngineChatClient("Local Phi-3 Demo", model, handler, systemInstruction: "You convert what user said to all uppercase.");

var message = new ChatMessage(ChatRole.User, "hello world");
var response = await chatClient.CompleteAsync([message]);
Expand Down
2 changes: 1 addition & 1 deletion samples/csharp/chatclient/chatclient.console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<!-- note: this is intentionally referencing the library via the nuget package: to test it, and to take advantage of the native assets there -->
<PackageReference Include="Strathweb.Phi.Engine.Microsoft.Extensions.AI" Version="0.1.3" />
<PackageReference Include="Strathweb.Phi.Engine.Microsoft.Extensions.AI" Version="0.1.2" />
</ItemGroup>

<!-- only use the below if you know what you are doing (e.g. the native assets have to provided by hand or by extra copy task) -->
Expand Down
Loading