LogoLogo
HomeNewsSupportVideos
  • Welcome
  • Wisej.NET
  • Concepts
    • Getting Started
    • General Concepts
    • Architecture
    • Extensibility
    • AI Providers
    • Vector Databases
    • Usage Metrics
    • Logging & Error Handling
  • Markup
  • Components
    • API
      • SmartAdapter
        • SmartAudioTTSAdapter
        • SmartAudioWhisperAdapter
        • SmartCalendarAdapter
        • SmartChartAdapter
        • SmartChartJS3Adapter
        • SmartChatBoxAdapter
        • SmartComboBoxAdapter
        • SmartCopilotAdapter
        • SmartDataEntryAdapter
        • SmartDocumentAdapter
        • SmartFullCalendarAdapter
        • SmartObjectAdapter
        • SmartPictureBoxAdapter
        • SmartQueryAdapter
        • SmartRealtimeAdapter
        • SmartReportAdapter
        • SmartTextBoxAdapter
        • SmartAdapter.ExtendsAttribute
        • SmartAdapter.FieldNameAttribute
        • SmartAdapter.FieldPromptAttribute
        • SmartAdapter.FieldRectangleAttribute
        • SmartAdapter.WorksWithAttribute
      • SmartEndpoint
        • AmazonBedrockEndpoint
        • AnthropicEndpoint
        • AzureAIEndpoint
        • CerebrasEndpoint
        • DeepSeekEndpoint
        • GoogleAIEndpoint
        • GroqCloudEndpoint
        • GroqCloudEndpointWhisper
        • HuggingFaceEndpoint
        • HuggingFaceJavaScriptEndpoint
        • LocalAIEndpoint
        • LocalAIEndpointImageGen
        • LocalAIEndpointTTS
        • LocalAIEndpointWhisper
        • NvidiaAIEndpoint
        • OllamaEndpoint
        • OpenAIEndpoint
        • OpenAIEndpointDallE
        • OpenAIEndpointRealtime
        • OpenAIEndpointTTS
        • OpenAIEndpointWhisper
        • SambaNovaEndpoint
        • SmartHttpEndpoint
        • TogetherAIEndpoint
        • XAIEndpoint
        • SmartEndpoint.Metrics
        • SmartEndpoint.Response
      • SmartExtensions
      • SmartHub
        • SmartSession.ConvertParameterEventArgs
        • SmartSession.ConvertParameterEventHandler
        • SmartSession.ErrorEventArgs
        • SmartSession.ErrorEventHandler
        • SmartSession.InvokeToolEventArgs
        • SmartSession.InvokeToolEventHandler
        • SmartSession.MessagesEventArgs
        • SmartSession.MessagesEventHandler
      • SmartObject
      • SmartPrompt
        • SmartAgentPrompt
        • SmartParallelPrompt
        • SmartPrompt.Parameter
        • SmartSession.ConvertParameterEventArgs
        • SmartSession.ConvertParameterEventHandler
        • SmartSession.ErrorEventArgs
        • SmartSession.ErrorEventHandler
        • SmartSession.InvokeToolEventArgs
        • SmartSession.InvokeToolEventHandler
        • SmartSession.MessagesEventArgs
        • SmartSession.MessagesEventHandler
      • SmartRealtimeSession
      • SmartSession
        • SmartSession.ConvertParameterEventArgs
        • SmartSession.ConvertParameterEventHandler
        • SmartSession.ErrorEventArgs
        • SmartSession.ErrorEventHandler
        • SmartSession.InvokeToolEventArgs
        • SmartSession.InvokeToolEventHandler
        • SmartSession.Message
        • SmartSession.MessageCollection
        • SmartSession.MessageRole
        • SmartSession.MessagesEventArgs
        • SmartSession.MessagesEventHandler
        • SmartSession.TrimmingStrategy
      • SmartTool
        • SmartTool.IToolProvider
        • SmartTool.ToolAttribute
        • SmartTool.ToolContext
      • Markup
        • MarkupExtensions
      • Controls
        • UVLightOverlay
      • Embeddings
        • EmbeddedDocument
        • Embedding
        • Matches
        • Metadata
      • Helpers
        • ApiKeys
        • Markdown
        • TextTokenizer
      • Services
        • DefaultSessionTrimmingService
        • IDocumentConversionService
          • DefaultDocumentConversionService
        • IEmbeddingGenerationService
          • DefaultEmbeddingGenerationService
          • HuggingFaceEmbeddingGenerationService
        • IEmbeddingStorageService
          • AzureAISearchEmbeddingStorageService
          • ChromaEmbeddingStorageService
          • FileSystemEmbeddingStorageService
          • MemoryEmbeddingStorageService
          • PineconeEmbeddingStorageService
          • QdrantEmbeddingStorageService
        • IHttpClientService
          • DefaultHttpClientService
        • ILoggerService
          • DefaultLoggerService
        • IOCRService
          • DefaultOCRService
        • IRerankingService
          • DefaultRerankingService
          • LocalAIRerankingService
          • PineconeRerankingService
        • ISessionTrimmingService
          • DefaultSessionTrimmingService
        • ITextSplitterService
          • RecursiveCharacterTextSplitterService
          • TextSplitterServiceBase
        • ITokenizerService
          • DefaultTokenizerService
        • IWebSearchService
          • BingWebSearchService
          • BraveWebSearchService
          • GoogleWebSearchService
      • Tools
        • ArxivTools
        • ChartJS3Tools
        • DatabaseTools
        • DataTableFilterTools
        • DocumentSearchTools
        • DocumentTools
        • FullCalendarTools
        • IToolsContainer
        • MathTools
        • ToolsContainer
        • UtilityTools
        • WebSearchTools
    • Built-in Services
      • IOCRService
      • ILoggerService
      • ITextSplitterService
      • ITokenizerService
      • IHttpClientService
      • IWebSearchService
      • IRerankingService
      • ISessionTrimmingService
      • IDocumentConversionService
      • IEmbeddingStorageService
      • IEmbeddingGenerationService
    • Built-in SmartTools
      • ToolsContainer
      • MathTools
      • UtilityTools
      • DatabaseTools
      • DocumentTools
      • DocumentSearchTools
      • WebSearchTools
      • ChartJS3Tools
      • FullCalendarTools
    • Built-in SmartAdapters
      • SmartAdapter
      • SmartAudioTTSAdapter
      • SmartAudioWhisperAdapter
      • SmartCalendarAdapter
      • SmartChartAdapter
      • SmartChartJS3Adapter
      • SmartChatBoxAdapter
      • SmartComboBoxAdapter
      • SmartCopilotAdapter
      • SmartDataEntryAdapter
      • SmartDocumentAdapter
      • SmartFullCalendarAdapter
      • SmartObjectAdapter
      • SmartPictureBoxAdapter
      • SmartQueryAdapter
      • SmartRealtimeAdapter
      • SmartReportAdapter
      • SmartTextBoxAdapter
    • Configure Services
    • Using SmartHub
    • Using SmartTools
    • Using SmartPrompt
    • Using SmartSession
    • Using SmartRealTimeAdapter
    • UVLightOverlay Control
Powered by GitBook
On this page
  • Overview
  • How to Use
  • Properties
  • Examples
Export as PDF
  1. Components

Using SmartHub

PreviousConfigure ServicesNextUsing SmartTools

Last updated 6 days ago

Overview

The primary function of the SmartHub is to connect multiple SmartAdapters and offer a centralized location for managing the events from these associated adapters. It provides intuitive design-time UI editors that enable developers to easily select and incorporate endpoints and adapters into the container currently being designed.

Furthermore, a SmartHub automatically scans its container for methods that are marked with the [SmartTool.Tool] attribute. These methods are seamlessly integrated as tools into the SmartAdapters associated with the SmartHub. The tools that are imported from the container operate within the context of the container itself. Consequently, there is no need to invoke the UseTools or UseTool methods in this scenario.

How to Use

To utilize a SmartHub instance, you have two options: you can either drag and drop it from the toolbox onto a design surface (refer to the page for more details) or you can instantiate the component programmatically. When creating the SmartHub instance through code, you have the option to specify a container for the component, although this is not mandatory. This flexibility allows developers to integrate the SmartHub instance into their applications in a manner that best fits their workflow and project structure.

var smartHub = new SmartHub();
// or var smartHub = new SmartHub(container);
smartHub.Endpoint = new OpenAIEndpoint { ApiKey = "..." };
Dim smartHub As New SmartHub()
' or Dim smartHub As New SmartHub(container)
smartHub.Endpoint = New OpenAIEndpoint With {.ApiKey = "..."}

Using a SmartHub is required only when using one of the adapters. Otherwise, you can access all Wisej.AI features without setting up a SmartHub. It is entirely possible to instantiate and utilize any the SmartPrompt or SmartSession classes independently of a SmartHub.

However, if your project requires centralized management of certain events, monitoring the utilization of AI providers from a unified location, or the automatic importation of tools defined in a container, the SmartHub class becomes particularly useful.

In addition to offering a centralized hub for multiple adapters, the SmartHub class provides advanced functionality designed to streamline the processes of document ingestion and embedding. This includes the capability to perform similarity queries and calculate similarity scores between vectors with ease.

The following code snippet illustrates the simplest method to convert, vectorize, and store a document into a vector database. This streamlined process is achieved with just a single line of code, powered by the Wisej.AI services.

var embeddedDoc = await smartHub.IngestDocumentAsync(
    Application.MapPath("Files\\Q4-FinancialReport.pdf"),
    "Financials\\Q4\\Report.pdf");
Dim embeddedDoc = Await smartHub.IngestDocumentAsync(
    Application.MapPath("Files\Q4-FinancialReport.pdf"),
    "Financials\Q4\Report.pdf")

This line of code involves several services that work together in a sequence to convert and store a PDF document as a an . First, the IDocumentConversionService is used to convert the PDF document into a text format, allowing for easier manipulation and processing of its contents. Next, the ITextSplitterService takes over to divide the converted text into overlapping chunks. These chunks assist in maximizing context and relevance when the text is later analyzed or queried. Following this, the IEmbeddingGenerationService processes these text chunks, transforming them into vectors via embedding, which is a numerical representation that captures the semantic meaning of the text. Finally, the IEmbeddingStorageService saves these generated vectors, accompanying text chunks, and associated metadata into a vector database. This storage format facilitates efficient retrieval and utilization of the document's content in future operations.

Once documents are ingested, they become accessible to all components of Wisej.AI through the DocumentSearchTools object. This integration ensures that any part of the Wisej.AI framework that requires access to the processed documents can retrieve and utilize them as needed.

Properties

Name
Description

ContainerControl

Control associated with the SmartHub. Can be a Page, a Form or any ContainerControl in Wisej.NET.

Culture

Overrides the default language of the application. If you want your AI to speak a particular language without changing the application's culture, use this property.

DataSource

Optional data source object that can be used to resolve parameters in prompts. Parametere are delimited by {{}}. I.e. {{UserName}}

Endpoint

Endpoint used by the adapters associated to the SmartHub.

Examples

Below are some examples demonstrating some of the functionalities offered by the SmartHub component:

  1. Simple Logging Example: This example illustrates a straightforward approach, which involves logging all usage reported by the AI provider.

  2. Advanced Tool Processing Example: This example is more complex, enabling the SmartHub to either pre-process or post-process tool invocations.

  3. Quick Similarity Score: Calculates similarity between a query and an an array of text strings using smartHub’s async embedding-based similarity function.

Simple Logging Example
this.smartHub1.AfterResponseReceive += (s, e) => {
    // the last message is the assistant response and contains the reported usage
    var usage = e.Messages.Last().Usage;
    LogUsage(usage.InputTokens, usage.OutputTokens);
}

// Or, if you like to use markup code

this.smartHub1.OnAfterResponseReceive(e => {
    // the last message is the assistant response and contains the reported usage
    var usage = e.Messages.Last().Usage;
    LogUsage(usage.InputTokens, usage.OutputTokens);
});
Advanced Tool Processing Example
this.smartHub1.OnBeforeInvokeTool(e => {

    var context = e.ToolContext;
    var tool = e.ToolContext.Tool;
    if (tool.Name == "read_sales")
    {
        // if the AI is trying to read the sales from
        // a table named "Secret", block it.
        if (tool.Parameters[0].Name == "TableName" &&
            Object.Equals(context.Arguments["TableName"], "Secret"))
        {
            // set our return value.
            // it can be a concrete value or you may also return
            // a Task for asynchronous operations.
            context.ReturnValue = "You are not allowed to read this data.";

            // prevent Wisej.AI from calling the tool.
            e.Handled = true;
        }
    }
});

this.smartHub1.OnAfterInvokeTool(e => {

    var context = e.ToolContext;
    var tool = e.ToolContext.Tool;
    if (tool.Name == "run_scada_command")
    {
        Log($"Executed SCACA command: {context.Arguments[tool.Parameters[0].Name]}");
        Log($"  >> Result: {context.ReturnValue}");
    }
});
var smartHub = new SmartHub {
    Endpoint = new OpenAIEndpoint { ApiKey = "..."}
};

var score = await smartHub.SimilarityScoreAsync(
    "Tell me about machine learning and computer science.",
    [
        "The quick brown fox jumps over the lazy dog.",
        "Artificial Intelligence is transforming the world.",
        "I love to play piano in my free time.",
        "Space exploration is a fascinating subject.",
        "Python is a popular programming language."
    ]);
    
Console.WriteLine(String.Join("\n", score.Select(s => s.ToString())));

// Output
/*
0.08265121
0.3526109
0.1460542
0.2151319
0.1906273
*/

When generating embeddings with the default IEmbeddingGenerationService, ensure the correct ApiKey is available to the service’s endpoint—either set as an environment variable or specified in the file.

Getting Started
EmbeddedDocument
ApiKeys.json