Skip to content

Commit

Permalink
feat: Added ability to expand JsonSerializerContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Sep 15, 2024
1 parent 2aedf95 commit 2137fa9
Show file tree
Hide file tree
Showing 594 changed files with 7,479 additions and 7,166 deletions.
117 changes: 79 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Discord](https://img.shields.io/discord/1115206893015662663?label=Discord&logo=discord&logoColor=white&color=d82679)](https://discord.gg/Ca2xhfBf3v)

## Features 🔥
- Fully generated C# SDK based on [official OpenAPI specification](https://api.smith.langchain.com/openapi.json) using [OpenApiGenerator](https://github.com/HavenDV/OpenApiGenerator)
- Fully generated C# SDK based on [official OpenAPI specification](https://api.smith.langchain.com/openapi.json) using [AutoSDK](https://github.com/tryAGI/AutoSDK)
- Automatic releases of new preview versions if there was an update to the OpenAPI specification
- Source generator to define tools natively through C# interfaces
- All modern .NET features - nullability, trimming, NativeAOT, etc.
Expand All @@ -17,51 +17,92 @@
### Initializing

```csharp
using var ollama = new OllamaApiClient();
using var api = new LangSmithApi();
using var openAiApi = new OpenAiApi();

var models = await ollama.Models.ListModelsAsync();
api.JsonSerializerContext = new SpecialJsonSerializerContext(OpenAI.SourceGenerationContext.Default);

// Pulling a model and reporting progress
await foreach (var response in ollama.PullModelAsync("all-minilm", stream: true))
{
Console.WriteLine($"{response.Status}. Progress: {response.Completed}/{response.Total}");
}
// or just pull the model and wait for it to finish
await ollama.Models.PullModelAndEnsureSuccessAsync("all-minilm");

// Generating an embedding
var embedding = await ollama.Embeddings.GenerateEmbeddingAsync(
model: "all-minilm",
prompt: "hello");

// Streaming a completion directly into the console
// keep reusing the context to keep the chat topic going
IList<long>? context = null;
var enumerable = ollama.Completions.GenerateCompletionAsync("llama3", "answer 5 random words", stream: true);
await foreach (var response in enumerable)
// This can be a user input to your app
var question = "Can you summarize this morning's meetings?";

// This can be retrieved in a retrieval step
const string context = "During this morning's meeting, we solved all world conflict.";
var messages = new[]
{
Console.WriteLine($"> {response.Response}");

context = response.Context;
}
"You are a helpful assistant. Please respond to the user's request only based on the given context."
.AsSystemMessage(),
$"Question: {question}\\nContext: {context}",
};

var lastResponse = await ollama.Completions.GenerateCompletionAsync("llama3", "answer 123", stream: false, context: context).WaitAsync();
Console.WriteLine(lastResponse.Response);
// Create parent run
var parentRunId = Guid.NewGuid();
await api.Run.CreateRunAsync(
name: "Chat Pipeline",
runType: CreateRunRequestRunType.Chain,
id: parentRunId,
inputs: new CreateRunRequestInputs
{
AdditionalProperties = new Dictionary<string, object>
{
["question"] = question,
},
});

var chat = ollama.Chat("mistral");
while (true)
{
var message = await chat.SendAsync("answer 123");

Console.WriteLine(message.Content);

var newMessage = Console.ReadLine();
await chat.Send(newMessage);
}
// Create child run
var childRunId = Guid.NewGuid();
await api.Run.CreateRunAsync(
name: "OpenAI Call",
runType: CreateRunRequestRunType.Llm,
id: childRunId,
parentRunId: parentRunId,
inputs: new CreateRunRequestInputs
{
AdditionalProperties = new Dictionary<string, object>
{
["messages"] = messages,
},
});

// Generate a completion
var chatCompletion = await openAiApi.Chat.CreateChatCompletionAsync(
model: CreateChatCompletionRequestModel.Gpt35Turbo,
messages: messages);

// End runs
await api.Run.UpdateRunAsync(
runId: childRunId,
outputs: new UpdateRunRequestOutputs
{
AdditionalProperties = new Dictionary<string, object>
{
["chatCompletion"] = chatCompletion,
},
},
endTime: DateTime.UtcNow.ToString("O"));
await api.Run.UpdateRunAsync(
runId: parentRunId,
outputs: new UpdateRunRequestOutputs
{
AdditionalProperties = new Dictionary<string, object>
{
["answer"] = chatCompletion.Choices[0].Message.Content ?? string.Empty,
},
},
endTime: DateTime.UtcNow.ToString("O"));
```

## Support

Priority place for bugs: https://github.com/tryAGI/LangSmith/issues
Priority place for ideas and general questions: https://github.com/tryAGI/LangSmith/discussions
Discord: https://discord.gg/Ca2xhfBf3v
Discord: https://discord.gg/Ca2xhfBf3v

## Acknowledgments

![JetBrains logo](https://resources.jetbrains.com/storage/products/company/brand/logos/jetbrains.png)

This project is supported by JetBrains through the [Open Source Support Program](https://jb.gg/OpenSourceSupport).

![CodeRabbit logo](https://opengraph.githubassets.com/1c51002d7d0bbe0c4fd72ff8f2e58192702f73a7037102f77e4dbb98ac00ea8f/marketplace/coderabbitai)

This project is supported by CodeRabbit through the [Open Source Support Program](https://github.com/marketplace/coderabbitai).
2 changes: 1 addition & 1 deletion src/libs/LangSmith/Generated/AllOf.1.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#nullable enable

namespace System
namespace LangSmith
{
/// <summary>
///
Expand Down
2 changes: 1 addition & 1 deletion src/libs/LangSmith/Generated/AnyOf.2.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#nullable enable

namespace System
namespace LangSmith
{
/// <summary>
///
Expand Down
2 changes: 1 addition & 1 deletion src/libs/LangSmith/Generated/AnyOf.3.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#nullable enable

namespace System
namespace LangSmith
{
/// <summary>
///
Expand Down
2 changes: 1 addition & 1 deletion src/libs/LangSmith/Generated/AnyOf.4.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#nullable enable

namespace System
namespace LangSmith
{
/// <summary>
///
Expand Down
2 changes: 1 addition & 1 deletion src/libs/LangSmith/Generated/AnyOf.5.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#nullable enable

namespace System
namespace LangSmith
{
/// <summary>
///
Expand Down
2 changes: 1 addition & 1 deletion src/libs/LangSmith/Generated/AnyOf.6.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#nullable enable

namespace System
namespace LangSmith
{
/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#nullable enable

namespace OpenApiGenerator.JsonConverters
namespace LangSmith.JsonConverters
{
/// <inheritdoc />
public sealed class AccessScopeJsonConverter : global::System.Text.Json.Serialization.JsonConverter<global::LangSmith.AccessScope>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#nullable enable

namespace OpenApiGenerator.JsonConverters
namespace LangSmith.JsonConverters
{
/// <inheritdoc />
public sealed class AccessScopeNullableJsonConverter : global::System.Text.Json.Serialization.JsonConverter<global::LangSmith.AccessScope?>
Expand Down
10 changes: 5 additions & 5 deletions src/libs/LangSmith/Generated/JsonConverters.AllOf1.g.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#nullable enable

namespace OpenApiGenerator.JsonConverters
namespace LangSmith.JsonConverters
{
/// <inheritdoc />
public class AllOfJsonConverter<T1> : global::System.Text.Json.Serialization.JsonConverter<global::System.AllOf<T1>>
public class AllOfJsonConverter<T1> : global::System.Text.Json.Serialization.JsonConverter<global::LangSmith.AllOf<T1>>
{
/// <inheritdoc />
public override global::System.AllOf<T1> Read(
public override global::LangSmith.AllOf<T1> Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
Expand All @@ -27,7 +27,7 @@ public class AllOfJsonConverter<T1> : global::System.Text.Json.Serialization.Jso
{
}

var result = new global::System.AllOf<T1>(
var result = new global::LangSmith.AllOf<T1>(
value1
);

Expand All @@ -44,7 +44,7 @@ public class AllOfJsonConverter<T1> : global::System.Text.Json.Serialization.Jso
/// <inheritdoc />
public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
global::System.AllOf<T1> value,
global::LangSmith.AllOf<T1> value,
global::System.Text.Json.JsonSerializerOptions options)
{
options = options ?? throw new global::System.ArgumentNullException(nameof(options));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#nullable enable

namespace OpenApiGenerator.JsonConverters
namespace LangSmith.JsonConverters
{
/// <inheritdoc />
public sealed class AllOfJsonConverterFactory1 : global::System.Text.Json.Serialization.JsonConverterFactory
{
/// <inheritdoc />
public override bool CanConvert(global::System.Type? typeToConvert)
{
return typeToConvert is { IsGenericType: true } && typeToConvert.GetGenericTypeDefinition() == typeof(global::System.AllOf<>);
return typeToConvert is { IsGenericType: true } && typeToConvert.GetGenericTypeDefinition() == typeof(global::LangSmith.AllOf<>);
}

/// <inheritdoc />
Expand Down
10 changes: 5 additions & 5 deletions src/libs/LangSmith/Generated/JsonConverters.AnyOf2.g.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#nullable enable

namespace OpenApiGenerator.JsonConverters
namespace LangSmith.JsonConverters
{
/// <inheritdoc />
public class AnyOfJsonConverter<T1, T2> : global::System.Text.Json.Serialization.JsonConverter<global::System.AnyOf<T1, T2>>
public class AnyOfJsonConverter<T1, T2> : global::System.Text.Json.Serialization.JsonConverter<global::LangSmith.AnyOf<T1, T2>>
{
/// <inheritdoc />
public override global::System.AnyOf<T1, T2> Read(
public override global::LangSmith.AnyOf<T1, T2> Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
Expand Down Expand Up @@ -39,7 +39,7 @@ public class AnyOfJsonConverter<T1, T2> : global::System.Text.Json.Serialization
{
}

var result = new global::System.AnyOf<T1, T2>(
var result = new global::LangSmith.AnyOf<T1, T2>(
value1,
value2
);
Expand All @@ -63,7 +63,7 @@ public class AnyOfJsonConverter<T1, T2> : global::System.Text.Json.Serialization
/// <inheritdoc />
public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
global::System.AnyOf<T1, T2> value,
global::LangSmith.AnyOf<T1, T2> value,
global::System.Text.Json.JsonSerializerOptions options)
{
options = options ?? throw new global::System.ArgumentNullException(nameof(options));
Expand Down
10 changes: 5 additions & 5 deletions src/libs/LangSmith/Generated/JsonConverters.AnyOf3.g.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#nullable enable

namespace OpenApiGenerator.JsonConverters
namespace LangSmith.JsonConverters
{
/// <inheritdoc />
public class AnyOfJsonConverter<T1, T2, T3> : global::System.Text.Json.Serialization.JsonConverter<global::System.AnyOf<T1, T2, T3>>
public class AnyOfJsonConverter<T1, T2, T3> : global::System.Text.Json.Serialization.JsonConverter<global::LangSmith.AnyOf<T1, T2, T3>>
{
/// <inheritdoc />
public override global::System.AnyOf<T1, T2, T3> Read(
public override global::LangSmith.AnyOf<T1, T2, T3> Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
Expand Down Expand Up @@ -51,7 +51,7 @@ public class AnyOfJsonConverter<T1, T2, T3> : global::System.Text.Json.Serializa
{
}

var result = new global::System.AnyOf<T1, T2, T3>(
var result = new global::LangSmith.AnyOf<T1, T2, T3>(
value1,
value2,
value3
Expand Down Expand Up @@ -82,7 +82,7 @@ public class AnyOfJsonConverter<T1, T2, T3> : global::System.Text.Json.Serializa
/// <inheritdoc />
public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
global::System.AnyOf<T1, T2, T3> value,
global::LangSmith.AnyOf<T1, T2, T3> value,
global::System.Text.Json.JsonSerializerOptions options)
{
options = options ?? throw new global::System.ArgumentNullException(nameof(options));
Expand Down
10 changes: 5 additions & 5 deletions src/libs/LangSmith/Generated/JsonConverters.AnyOf4.g.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#nullable enable

namespace OpenApiGenerator.JsonConverters
namespace LangSmith.JsonConverters
{
/// <inheritdoc />
public class AnyOfJsonConverter<T1, T2, T3, T4> : global::System.Text.Json.Serialization.JsonConverter<global::System.AnyOf<T1, T2, T3, T4>>
public class AnyOfJsonConverter<T1, T2, T3, T4> : global::System.Text.Json.Serialization.JsonConverter<global::LangSmith.AnyOf<T1, T2, T3, T4>>
{
/// <inheritdoc />
public override global::System.AnyOf<T1, T2, T3, T4> Read(
public override global::LangSmith.AnyOf<T1, T2, T3, T4> Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
Expand Down Expand Up @@ -63,7 +63,7 @@ public class AnyOfJsonConverter<T1, T2, T3, T4> : global::System.Text.Json.Seria
{
}

var result = new global::System.AnyOf<T1, T2, T3, T4>(
var result = new global::LangSmith.AnyOf<T1, T2, T3, T4>(
value1,
value2,
value3,
Expand Down Expand Up @@ -101,7 +101,7 @@ public class AnyOfJsonConverter<T1, T2, T3, T4> : global::System.Text.Json.Seria
/// <inheritdoc />
public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
global::System.AnyOf<T1, T2, T3, T4> value,
global::LangSmith.AnyOf<T1, T2, T3, T4> value,
global::System.Text.Json.JsonSerializerOptions options)
{
options = options ?? throw new global::System.ArgumentNullException(nameof(options));
Expand Down
10 changes: 5 additions & 5 deletions src/libs/LangSmith/Generated/JsonConverters.AnyOf5.g.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#nullable enable

namespace OpenApiGenerator.JsonConverters
namespace LangSmith.JsonConverters
{
/// <inheritdoc />
public class AnyOfJsonConverter<T1, T2, T3, T4, T5> : global::System.Text.Json.Serialization.JsonConverter<global::System.AnyOf<T1, T2, T3, T4, T5>>
public class AnyOfJsonConverter<T1, T2, T3, T4, T5> : global::System.Text.Json.Serialization.JsonConverter<global::LangSmith.AnyOf<T1, T2, T3, T4, T5>>
{
/// <inheritdoc />
public override global::System.AnyOf<T1, T2, T3, T4, T5> Read(
public override global::LangSmith.AnyOf<T1, T2, T3, T4, T5> Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
Expand Down Expand Up @@ -75,7 +75,7 @@ public class AnyOfJsonConverter<T1, T2, T3, T4, T5> : global::System.Text.Json.S
{
}

var result = new global::System.AnyOf<T1, T2, T3, T4, T5>(
var result = new global::LangSmith.AnyOf<T1, T2, T3, T4, T5>(
value1,
value2,
value3,
Expand Down Expand Up @@ -120,7 +120,7 @@ public class AnyOfJsonConverter<T1, T2, T3, T4, T5> : global::System.Text.Json.S
/// <inheritdoc />
public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
global::System.AnyOf<T1, T2, T3, T4, T5> value,
global::LangSmith.AnyOf<T1, T2, T3, T4, T5> value,
global::System.Text.Json.JsonSerializerOptions options)
{
options = options ?? throw new global::System.ArgumentNullException(nameof(options));
Expand Down
Loading

0 comments on commit 2137fa9

Please sign in to comment.