-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeaviateClientExtensions.cs
More file actions
64 lines (61 loc) · 2.6 KB
/
WeaviateClientExtensions.cs
File metadata and controls
64 lines (61 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using Weaviate.Client.Managed.Schema;
using Weaviate.Client.Models;
namespace Weaviate.Client.Managed.Extensions;
/// <summary>
/// Extension methods for WeaviateClient to support ORM operations.
/// </summary>
public static class WeaviateClientExtensions
{
/// <summary>
/// Returns a new <see cref="ClientConfiguration"/> with the
/// <c>X-Weaviate-Client-Integration</c> header set for the managed client.
/// Use this when constructing <see cref="WeaviateContext"/> without dependency injection.
/// </summary>
/// <param name="config">The base configuration.</param>
public static ClientConfiguration WithManagedIntegrationHeader(
this ClientConfiguration config
) =>
config.WithIntegration(
WeaviateDefaults.IntegrationAgent(
DependencyInjection.WeaviateManagedServiceCollectionExtensions.IntegrationName
)
);
/// <summary>
/// Creates a Weaviate collection from a C# class decorated with ORM attributes.
/// The class must have a [WeaviateCollection] attribute or the class name will be used as the collection name.
/// </summary>
/// <typeparam name="T">The class type representing the collection schema.</typeparam>
/// <param name="collections">The collections interface.</param>
/// <param name="configure">Optional callback to modify the collection configuration before creation.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A CollectionClient for the newly created collection.</returns>
/// <example>
/// <code>
/// [WeaviateCollection("Articles")]
/// public class Article
/// {
/// [Property(DataType.Text)]
/// public string Title { get; set; }
///
/// [Vector<Vectorizer.Text2VecOpenAI>(Model = "ada-002")]
/// public float[]? Embedding { get; set; }
/// }
///
/// var collection = await client.Collections.CreateFromClass<Article>();
/// // Or with configuration override:
/// var collection = await client.Collections.CreateFromClass<Article>(
/// configure: config => config.Name = "CustomName");
/// </code>
/// </example>
public static async Task<CollectionClient> CreateFromClass<T>(
this CollectionsClient collections,
Action<CollectionCreateParams>? configure = null,
CancellationToken cancellationToken = default
)
where T : class
{
var config = CollectionSchemaBuilder.FromClass<T>();
configure?.Invoke(config);
return await collections.Create(config, cancellationToken);
}
}