Skip to content

Commit

Permalink
Move to .Net Standard2
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeaugrand committed Jan 4, 2024
1 parent 8a6dec4 commit de77b79
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task<IReadOnlyList<ChatMessageContent>> GetChatMessageContentsAsync
{
var system = string.Join("\n", chatHistory.Where(x => x.Role == AuthorRole.System).Select(x => x.Content));
var user = chatHistory.Last(x => x.Role == AuthorRole.User);

var data = new
{
model = Attributes["model_id"] as string,
Expand All @@ -34,7 +34,7 @@ public async Task<IReadOnlyList<ChatMessageContent>> GetChatMessageContentsAsync

ValidateOllamaResponse(response);

var json = JsonSerializer.Deserialize<JsonNode>(await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false));
var json = JsonSerializer.Deserialize<JsonNode>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));

return new List<ChatMessageContent> { new(AuthorRole.Assistant, json!["response"]!.GetValue<string>(), modelId: Attributes["model_id"] as string) };
}
Expand All @@ -45,7 +45,7 @@ public async IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessa
{
var system = string.Join("\n", chatHistory.Where(x => x.Role == AuthorRole.System).Select(x => x.Content));
var user = chatHistory.Last(x => x.Role == AuthorRole.User);

var data = new
{
model = Attributes["model_id"] as string,
Expand All @@ -58,25 +58,22 @@ public async IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessa
var response = await Http.PostAsJsonAsync($"{Attributes["base_url"]}/api/generate", data, cancellationToken).ConfigureAwait(false);

ValidateOllamaResponse(response);

var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);

await using (stream)
{
using var reader = new StreamReader(stream);
using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

var done = false;
using var reader = new StreamReader(stream);

while (!done)
{
var json = JsonSerializer.Deserialize<JsonNode>(
await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false)
);
var done = false;

while (!done)
{
var json = JsonSerializer.Deserialize<JsonNode>(
await response.Content.ReadAsStringAsync().ConfigureAwait(false)
);

done = json!["done"]!.GetValue<bool>();
done = json!["done"]!.GetValue<bool>();

yield return new StreamingChatMessageContent(AuthorRole.Assistant, json["response"]!.GetValue<string>(), modelId: Attributes["model_id"] as string);
}
yield return new StreamingChatMessageContent(AuthorRole.Assistant, json["response"]!.GetValue<string>(), modelId: Attributes["model_id"] as string);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>Latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand All @@ -15,6 +16,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
</ItemGroup>

<!-- Nuget Stuff -->
Expand Down Expand Up @@ -43,8 +45,8 @@
</PropertyGroup>

<ItemGroup>
<None Include="./README.md" Pack="true" PackagePath="/"/>
<None Include="../LICENSE.md" Pack="true" PackagePath="$(PackageLicenseFile)"/>
<None Include="./README.md" Pack="true" PackagePath="/" />
<None Include="../LICENSE.md" Pack="true" PackagePath="$(PackageLicenseFile)" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<IList<ReadOnlyMemory<float>>> GenerateEmbeddingsAsync(IList<st

ValidateOllamaResponse(response);

var json = JsonSerializer.Deserialize<JsonNode>(await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false));
var json = JsonSerializer.Deserialize<JsonNode>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));

var embedding = new ReadOnlyMemory<float>(json!["embedding"]?.AsArray().GetValues<float>().ToArray());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task<IReadOnlyList<TextContent>> GetTextContentsAsync(string prompt

ValidateOllamaResponse(response);

var json = JsonSerializer.Deserialize<JsonNode>(await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false));
var json = JsonSerializer.Deserialize<JsonNode>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));

return new List<TextContent> { new(json!["response"]!.GetValue<string>()) };
}
Expand All @@ -47,25 +47,22 @@ public async IAsyncEnumerable<StreamingTextContent> GetStreamingTextContentsAsyn
var response = await Http.PostAsJsonAsync($"{Attributes["base_url"]}/api/generate", data, cancellationToken).ConfigureAwait(false);

ValidateOllamaResponse(response);

var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);

await using (stream)
{
using var reader = new StreamReader(stream);
using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

using var reader = new StreamReader(stream);

var done = false;
var done = false;

while (!done)
{
var json = JsonSerializer.Deserialize<JsonNode>(
await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false)
);
while (!done)
{
var json = JsonSerializer.Deserialize<JsonNode>(
await response.Content.ReadAsStringAsync().ConfigureAwait(false)
);

done = json!["done"]!.GetValue<bool>();
done = json!["done"]!.GetValue<bool>();

yield return new StreamingTextContent(json["response"]!.GetValue<string>());
}
yield return new StreamingTextContent(json["response"]!.GetValue<string>());
}
}
}

0 comments on commit de77b79

Please sign in to comment.