Skip to content

Commit 062f3f3

Browse files
committed
feat: Added initial implementation.
1 parent 0c31a0d commit 062f3f3

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- All modern .NET features - nullability, trimming, NativeAOT, etc.
1313
- Support .Net Framework/.Net Standard 2.0
1414
- Support for all Ollama API endpoints including chats, embeddings, listing models, pulling and creating new models, and more.
15+
- Supporting [Microsoft.Extensions.AI](https://devblogs.microsoft.com/dotnet/introducing-microsoft-extensions-ai-preview/)
1516

1617
## Usage
1718

src/libs/Ollama/Ollama.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212

1313
<PropertyGroup Label="Nuget">
1414
<Description>Generated C# SDK based on Ollama OpenAPI specification.</Description>
15-
<PackageTags>api;client;sdk;dotnet;swagger;openapi;specification;ollama;generated</PackageTags>
15+
<PackageTags>api;client;sdk;dotnet;swagger;openapi;specification;ollama;generated;ai;abstractions;llama;ichatclient</PackageTags>
1616
</PropertyGroup>
1717

1818
<ItemGroup>
1919
<PackageReference Include="CSharpToJsonSchema" Version="3.9.1" />
20+
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.0.0-preview.9.24507.7" />
2021
<PackageReference Include="PolySharp" Version="1.14.1">
2122
<PrivateAssets>all</PrivateAssets>
2223
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using Microsoft.Extensions.AI;
2+
3+
namespace Ollama;
4+
5+
public partial class OllamaApiClient : Microsoft.Extensions.AI.IChatClient
6+
{
7+
/// <inheritdoc />
8+
public async Task<ChatCompletion> CompleteAsync(
9+
IList<ChatMessage> chatMessages,
10+
ChatOptions? options = null,
11+
CancellationToken cancellationToken = default)
12+
{
13+
var response = await Chat.GenerateChatCompletionAsync(
14+
model: options?.ModelId ?? "ollama",
15+
messages: chatMessages.Select(x => new Message
16+
{
17+
Content = x.Text ?? string.Empty,
18+
Role = x.Role.Value switch
19+
{
20+
"assistant" => MessageRole.Assistant,
21+
"user" => MessageRole.User,
22+
"system" => MessageRole.System,
23+
"tool" => MessageRole.Tool,
24+
_ => MessageRole.User,
25+
},
26+
}).ToArray(),
27+
format: options?.ResponseFormat switch
28+
{
29+
ChatResponseFormatJson => ResponseFormat.Json,
30+
_ => null,
31+
},
32+
options: new RequestOptions
33+
{
34+
Temperature = options?.Temperature,
35+
},
36+
stream: false,
37+
keepAlive: default,
38+
tools: options?.Tools?.Select(x => new Tool
39+
{
40+
Function = new ToolFunction
41+
{
42+
Name = string.Empty,
43+
Description = string.Empty,
44+
Parameters = x.AsJson(),
45+
},
46+
}).ToList(),
47+
cancellationToken: cancellationToken).WaitAsync().ConfigureAwait(false);
48+
if (response.Message == null)
49+
{
50+
throw new InvalidOperationException("Response message was null.");
51+
}
52+
53+
return new ChatCompletion(new ChatMessage(
54+
role: response.Message.Role switch
55+
{
56+
MessageRole.Assistant => ChatRole.Assistant,
57+
MessageRole.User => ChatRole.User,
58+
MessageRole.System => ChatRole.System,
59+
MessageRole.Tool => ChatRole.Tool,
60+
_ => ChatRole.User,
61+
},
62+
content: response.Message.Content)
63+
{
64+
RawRepresentation = response.Message,
65+
})
66+
{
67+
RawRepresentation = response,
68+
};
69+
}
70+
71+
/// <inheritdoc />
72+
public IAsyncEnumerable<StreamingChatCompletionUpdate> CompleteStreamingAsync(
73+
IList<ChatMessage> chatMessages,
74+
ChatOptions? options = null,
75+
CancellationToken cancellationToken = default)
76+
{
77+
throw new NotImplementedException();
78+
}
79+
80+
/// <inheritdoc />
81+
public TService? GetService<TService>(object? key = null) where TService : class
82+
{
83+
return this as TService;
84+
}
85+
86+
/// <inheritdoc />
87+
public ChatClientMetadata Metadata => new(
88+
providerName: "Ollama",
89+
providerUri: HttpClient.BaseAddress);
90+
}

0 commit comments

Comments
 (0)