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