-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
feat: Microsoft.Extensions.AI support #64
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,90 @@ | ||||||
using Microsoft.Extensions.AI; | ||||||
|
||||||
namespace Ollama; | ||||||
|
||||||
public partial class OllamaApiClient : Microsoft.Extensions.AI.IChatClient | ||||||
{ | ||||||
/// <inheritdoc /> | ||||||
public async Task<ChatCompletion> CompleteAsync( | ||||||
Check warning on line 8 in src/libs/Ollama/OllamaApiClient.IChatClient.cs GitHub Actions / Test / Build, test and publish
|
||||||
IList<ChatMessage> chatMessages, | ||||||
Check warning on line 9 in src/libs/Ollama/OllamaApiClient.IChatClient.cs GitHub Actions / Test / Build, test and publish
|
||||||
ChatOptions? options = null, | ||||||
Check warning on line 10 in src/libs/Ollama/OllamaApiClient.IChatClient.cs GitHub Actions / Test / Build, test and publish
|
||||||
CancellationToken cancellationToken = default) | ||||||
{ | ||||||
var response = await Chat.GenerateChatCompletionAsync( | ||||||
model: options?.ModelId ?? "ollama", | ||||||
messages: chatMessages.Select(x => new Message | ||||||
{ | ||||||
Content = x.Text ?? string.Empty, | ||||||
Role = x.Role.Value switch | ||||||
{ | ||||||
"assistant" => MessageRole.Assistant, | ||||||
"user" => MessageRole.User, | ||||||
"system" => MessageRole.System, | ||||||
"tool" => MessageRole.Tool, | ||||||
_ => MessageRole.User, | ||||||
}, | ||||||
}).ToArray(), | ||||||
format: options?.ResponseFormat switch | ||||||
{ | ||||||
ChatResponseFormatJson => ResponseFormat.Json, | ||||||
_ => null, | ||||||
}, | ||||||
options: new RequestOptions | ||||||
{ | ||||||
Temperature = options?.Temperature, | ||||||
}, | ||||||
stream: false, | ||||||
keepAlive: default, | ||||||
tools: options?.Tools?.Select(x => new Tool | ||||||
{ | ||||||
Function = new ToolFunction | ||||||
{ | ||||||
Name = string.Empty, | ||||||
Description = string.Empty, | ||||||
Parameters = x.AsJson(), | ||||||
}, | ||||||
}).ToList(), | ||||||
Comment on lines
+38
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Provide meaningful Currently, the Apply this diff to assign appropriate values: Function = new ToolFunction
{
- Name = string.Empty,
- Description = string.Empty,
+ Name = x.Name,
+ Description = x.Description,
Parameters = x.AsJson(),
},
|
||||||
cancellationToken: cancellationToken).WaitAsync().ConfigureAwait(false); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Review the use of Using Apply this diff to streamline the await statement: - cancellationToken: cancellationToken).WaitAsync().ConfigureAwait(false);
+ cancellationToken: cancellationToken).ConfigureAwait(false); 📝 Committable suggestion
Suggested change
Comment on lines
+13
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure Currently, the code checks if Apply this diff to add the null check: + if (response == null)
+ {
+ throw new InvalidOperationException("Response was null.");
+ }
if (response.Message == null)
{
throw new InvalidOperationException("Response message was null.");
}
|
||||||
if (response.Message == null) | ||||||
{ | ||||||
throw new InvalidOperationException("Response message was null."); | ||||||
} | ||||||
|
||||||
return new ChatCompletion(new ChatMessage( | ||||||
role: response.Message.Role switch | ||||||
{ | ||||||
MessageRole.Assistant => ChatRole.Assistant, | ||||||
MessageRole.User => ChatRole.User, | ||||||
MessageRole.System => ChatRole.System, | ||||||
MessageRole.Tool => ChatRole.Tool, | ||||||
_ => ChatRole.User, | ||||||
}, | ||||||
content: response.Message.Content) | ||||||
{ | ||||||
RawRepresentation = response.Message, | ||||||
}) | ||||||
{ | ||||||
RawRepresentation = response, | ||||||
}; | ||||||
} | ||||||
|
||||||
/// <inheritdoc /> | ||||||
public IAsyncEnumerable<StreamingChatCompletionUpdate> CompleteStreamingAsync( | ||||||
IList<ChatMessage> chatMessages, | ||||||
ChatOptions? options = null, | ||||||
CancellationToken cancellationToken = default) | ||||||
{ | ||||||
throw new NotImplementedException(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implement The Would you like assistance in implementing the |
||||||
} | ||||||
|
||||||
/// <inheritdoc /> | ||||||
public TService? GetService<TService>(object? key = null) where TService : class | ||||||
{ | ||||||
return this as TService; | ||||||
} | ||||||
|
||||||
/// <inheritdoc /> | ||||||
public ChatClientMetadata Metadata => new( | ||||||
providerName: "Ollama", | ||||||
providerUri: HttpClient.BaseAddress); | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle potential null values in
x.Role
The code uses
x.Role.Value
without verifying ifx.Role
orx.Role.Value
is null. This could lead to aNullReferenceException
ifx.Role
is null. To prevent this, consider adding a null-conditional operator or a null check.Apply this diff to safely access
x.Role.Value
:📝 Committable suggestion