Using the IMemoryStore Interface and DevExpress XPO ORM to Implement a Custom Memory Store for Semantic Kernel

Using the IMemoryStore Interface and DevExpress XPO ORM to Implement a Custom Memory Store for Semantic Kernel

In today’s AI-driven world, the ability to quickly and efficiently store, retrieve, and manage data is crucial for developing sophisticated applications. One tool that helps facilitate this is the Semantic Kernel, a lightweight, open-source development kit designed for integrating AI models into C#, Python, or Java applications. It enables rapid enterprise-grade solutions by serving as an effective middleware.

One of the key concepts in Semantic Kernel is memory—a collection of records, each containing a timestamp, metadata, embeddings, and a key. These memory records can be stored in various ways, depending on how you implement the interfaces. This flexibility allows you to define the storage mechanism, which means you can choose any database solution that suits your needs.

In this blog post, we’ll walk through how to use the IMemoryStore interface in Semantic Kernel and implement a custom memory store using DevExpress XPO, an ORM (Object-Relational Mapping) tool that can interact with over 14 database engines with a single codebase.

Why Use DevExpress XPO ORM?

DevExpress XPO is a powerful, free-to-use ORM created by DevExpress that abstracts the complexities of database interactions. It supports a wide range of database engines such as SQL Server, MySQL, SQLite, Oracle, and many others, allowing you to write database-independent code. This is particularly helpful when dealing with a distributed or multi-environment system where different databases might be used.

By using XPO, we can seamlessly create, update, and manage memory records in various databases, making our application more flexible and scalable.

Implementing a Custom Memory Store with DevExpress XPO

To integrate XPO with Semantic Kernel’s memory management, we’ll implement a custom memory store by defining a database entry class and a database interaction class. Then, we’ll complete the process by implementing the IMemoryStore interface.

Step 1: Define a Database Entry Class

Our first step is to create a class that represents the memory record. In this case, we’ll define an XpoDatabaseEntry class that maps to a database table where memory records are stored.


public class XpoDatabaseEntry : XPLiteObject {
    private string _oid;
    private string _collection;
    private string _timestamp;
    private string _embeddingString;
    private string _metadataString;
    private string _key;

    [Key(false)]
    public string Oid { get; set; }
    public string Key { get; set; }
    public string MetadataString { get; set; }
    public string EmbeddingString { get; set; }
    public string Timestamp { get; set; }
    public string Collection { get; set; }

    protected override void OnSaving() {
        if (this.Session.IsNewObject(this)) {
            this.Oid = Guid.NewGuid().ToString();
        }
        base.OnSaving();
    }
}

This class extends XPLiteObject from the XPO library, which provides methods to manage the record lifecycle within the database.

Step 2: Create a Database Interaction Class

Next, we’ll define an XpoDatabase class to abstract the interaction with the data store. This class provides methods for creating tables, inserting, updating, and querying records.


internal sealed class XpoDatabase {
    public Task CreateTableAsync(IDataLayer conn) {
        using (Session session = new(conn)) {
            session.UpdateSchema(new[] { typeof(XpoDatabaseEntry).Assembly });
            session.CreateObjectTypeRecords(new[] { typeof(XpoDatabaseEntry).Assembly });
        }
        return Task.CompletedTask;
    }

    // Other database operations such as CreateCollectionAsync, InsertOrIgnoreAsync, etc.
}

This class acts as a bridge between Semantic Kernel and the database, allowing us to manage memory entries without having to write complex SQL queries.

Step 3: Implement the IMemoryStore Interface

Finally, we implement the IMemoryStore interface, which is responsible for defining how the memory store behaves. This includes methods like UpsertAsync, GetAsync, and DeleteCollectionAsync.


public class XpoMemoryStore : IMemoryStore, IDisposable {
    public static async Task ConnectAsync(string connectionString) {
        var memoryStore = new XpoMemoryStore(connectionString);
        await memoryStore._dbConnector.CreateTableAsync(memoryStore._dataLayer).ConfigureAwait(false);
        return memoryStore;
    }

    public async Task CreateCollectionAsync(string collectionName) {
        await this._dbConnector.CreateCollectionAsync(this._dataLayer, collectionName).ConfigureAwait(false);
    }

    // Other methods for interacting with memory records
}

The XpoMemoryStore class takes advantage of XPO’s ORM features, making it easy to create collections, store and retrieve memory records, and perform batch operations. Since Semantic Kernel doesn’t care where memory records are stored as long as the interfaces are correctly implemented, you can now store your memory records in any of the databases supported by XPO.

Advantages of Using XPO with Semantic Kernel

  • Database Independence: You can switch between multiple databases without changing your codebase.
  • Scalability: XPO’s ability to manage complex relationships and large datasets makes it ideal for enterprise-grade solutions.
  • ORM Abstraction: With XPO, you avoid writing SQL queries and focus on high-level operations like creating and updating objects.

Conclusion

In this blog post, we’ve demonstrated how to integrate DevExpress XPO ORM with the Semantic Kernel using the IMemoryStore interface. This approach allows you to store AI-driven memory records in a wide variety of databases while maintaining a flexible, scalable architecture.

In future posts, we’ll explore specific use cases and how you can leverage this memory store in real-world applications. For the complete implementation, you can check out my GitHub fork.

Stay tuned for more insights and examples!

Unlocking the Power of Augmented Data Models: Enhance Analytics and AI Integration for Better Insights

Unlocking the Power of Augmented Data Models: Enhance Analytics and AI Integration for Better Insights

In today’s data-driven world, the need for more sophisticated and insightful data models has never been greater. Traditional database models, while powerful, often fall short of delivering the depth and breadth of insights required by modern organizations. Enter the augmented data model, a revolutionary approach that extends beyond the limitations of traditional models by integrating additional data sources, enhanced data features, advanced analytical capabilities, and AI-driven techniques. This blog post explores the key components, applications, and benefits of augmented data models.

Key Components of an Augmented Data Model

1. Integration of Diverse Data Sources

An augmented data model combines structured, semi-structured, and unstructured data from various sources such as databases, data lakes, social media, IoT devices, and external data feeds. This integration enables a holistic view of data across the organization, breaking down silos and fostering a more interconnected understanding of the data landscape.

2. Enhanced Data Features

Beyond raw data, augmented data models include derived attributes, calculated fields, and metadata to enrich the data. Machine learning and artificial intelligence are employed to create predictive and prescriptive data features, transforming raw data into actionable insights.

3. Advanced Analytics

Augmented data models incorporate advanced analytical models, including machine learning, statistical models, and data mining techniques. These models support real-time analytics and streaming data processing, enabling organizations to make faster, data-driven decisions.

4. AI-Driven Embeddings

One of the standout features of augmented data models is the creation of embeddings. These are dense vector representations of data (such as words, images, or user behaviors) that capture their semantic meaning. Embeddings enhance machine learning models, making them more effective at tasks such as recommendation, natural language processing, and image recognition.

5. Data Visualization and Reporting

To make complex data insights accessible, augmented data models facilitate advanced data visualization tools and dashboards. These tools allow users to interact with data dynamically through self-service analytics platforms, turning data into easily digestible visual stories.

6. Improved Data Quality and Governance

Ensuring data quality is paramount in augmented data models. Automated data cleansing, validation, and enrichment processes maintain high standards of data quality. Robust data governance policies manage data lineage, security, and compliance, ensuring that data is trustworthy and reliable.

7. Scalability and Performance

Designed to handle large volumes of data, augmented data models scale horizontally across distributed systems. They are optimized for high performance in data processing and querying, ensuring that insights are delivered swiftly and efficiently.

Applications and Benefits

Enhanced Decision Making

With deeper insights and predictive capabilities, augmented data models significantly improve decision-making processes. Organizations can move from reactive to proactive strategies, leveraging data to anticipate trends and identify opportunities.

Operational Efficiency

By streamlining data processing and integration, augmented data models reduce manual efforts and errors. This leads to more efficient operations and a greater focus on strategic initiatives.

Customer Insights

Augmented data models enable a 360-degree view of customers by integrating various touchpoints and interactions. This comprehensive view allows for more personalized and effective customer engagement strategies.

Innovation

Supporting advanced analytics and machine learning initiatives, augmented data models foster innovation within the organization. They provide the tools and insights needed to develop new products, services, and business models.

Real-World Examples

Customer 360 Platforms

By combining CRM data, social media interactions, and transactional data, augmented data models create a comprehensive view of customer behavior. This holistic approach enables personalized marketing and improved customer service.

IoT Analytics

Integrating sensor data, machine logs, and external environmental data, augmented data models optimize operations in manufacturing or smart cities. They enable real-time monitoring and predictive maintenance, reducing downtime and increasing efficiency.

Fraud Detection Systems

Using transactional data, user behavior analytics, and external threat intelligence, augmented data models detect and prevent fraudulent activities. Advanced machine learning models identify patterns and anomalies indicative of fraud, providing a proactive defense mechanism.

AI-Powered Recommendations

Embeddings created from user interactions, product descriptions, and historical purchase data power personalized recommendations in e-commerce. These AI-driven insights enhance customer experience and drive sales.

Conclusion

Augmented data models represent a significant advancement in the way organizations handle and analyze data. By leveraging modern technologies and methodologies, including the creation of embeddings for AI, these models provide a more comprehensive and actionable view of the data. The result is enhanced decision-making, improved operational efficiency, deeper customer insights, and a platform for innovation. As organizations continue to navigate the complexities of the data landscape, augmented data models will undoubtedly play a pivotal role in shaping the future of data analytics.

 

Comparing OpenAI’s ChatGPT and Microsoft’s Copilot mobile apps

Comparing OpenAI’s ChatGPT and Microsoft’s Copilot mobile apps

OpenAI’s ChatGPT and Microsoft’s Copilot are two powerful AI tools that have revolutionized the way we interact with technology. While both are designed to assist users in various tasks, they each have unique features that set them apart.

OpenAI’s ChatGPT

ChatGPT, developed by OpenAI, is a large language model chatbot capable of communicating with users in a human-like way¹⁷. It can answer questions, create recipes, write code, and offer advice¹⁷. It uses a powerful generative AI model and has access to several tools which it can use to complete tasks²⁶.

Key Features of ChatGPT

  • Chat with Images: You can show ChatGPT images and start a chat.
  • Image Generation: Create images simply by describing them in ChatGPT.
  • Voice Chat: You can now use voice to engage in a back-and-forth conversation with ChatGPT.
  • Web Browsing: Gives ChatGPT the ability to search the internet for additional information.
  • Advanced Data Analysis: Interact with data documents (Excel, CSV, JSON).

Microsoft’s Copilot

Microsoft’s Copilot is an AI companion that works everywhere you do and intelligently adapts to your needs. It can chat with text, voice, and image capabilities, summarize documents and web pages, create images, and use plugins and Copilot GPTs

Key Features of Copilot

  • Chat with Text, Voice, and Image Capabilities: Copilot includes chat with text, voice, and image capabilities/
  • Summarization of Documents and Web Pages: It can summarize documents and web pages.
  • Image Creation: Copilot can create images.
  • Web Grounding: It can ground information from the web.
  • Use of Plugins and Copilot GPTs: Copilot can use plugins and Copilot GPTs.

Comparison of Mobile App Features

Feature OpenAI’s ChatGPT Microsoft’s Copilot
Chat with Text Yes Yes
Voice Input Yes Yes
Image Capabilities Yes Yes
Summarization No Yes
Image Creation Yes Yes
Web Grounding No Yes

What makes the difference, the action button for the iPhone

The action button on iPhones, available on the iPhone 15 Pro and later models, is a customizable button for quick tasks. By default, it opens the camera or activates the flashlight. However, users can customize it to perform various actions, including launching a specific app. When set to launch an app, pressing the action button will instantly open the chosen app, such as the ChatGPT voice interface. This integration is further enhanced by the new ChatGPT-4.0 capabilities, which offer more accurate responses, better understanding of context, and faster processing times. This makes voice interactions with ChatGPT smoother and more efficient, allowing users to quickly and effectively communicate with the AI.

 

 

 

 

The ChatGPT voice interface is one of my favorite features, but there’s one thing missing for it to be perfect. Currently, you can’t send pictures or videos during a voice conversation. The workaround is to leave the voice interface, open the chat interface, find the voice conversation in the chat list, and upload the picture there. However, this brings another problem: you can’t return to the voice interface and continue the previous voice conversation.

Microsoft Copilot, if you are reading this, when will you add a voice interface? And when you finally do it, don’t forget to add the picture and video feature I want. That is all for my wishlist.

 

The Steps to Create, Train, Save, and Load a Spam Detection AI Model Using ML.NET

The Steps to Create, Train, Save, and Load a Spam Detection AI Model Using ML.NET

This article demonstrates the process of creating, training, saving, and loading a spam detection AI model using ML.NET, but also emphasizes the reusability of the trained model. By following the steps in the article, you will be able to create a model that can be easily reused and integrated into your .NET applications, allowing you to effectively identify and filter out spam emails.

Prerequisites

  • Basic understanding of C#
  • Familiarity with ML.NET and machine learning concepts

Code Overview

    1. Import necessary namespaces:

      using System;
      using System.IO;
      using System.Linq;
      using Microsoft.ML;
      using Microsoft.ML.Data;
    
    1. Define the Email class and its properties:

      public class Email
      {
        public string Content { get; set; }
        public bool IsSpam { get; set; }
      }
    
    1. Create a sample dataset for training the model:

      var sampleData = new List<Email>
      {
        new Email { Content = "Buy cheap products now", IsSpam = true },
        new Email { Content = "Meeting at 3 PM", IsSpam = false },
      };
    
    1. Initialize a new MLContext, which is the main entry point to ML.NET:

      var mlContext = new MLContext();
    
    1. Load the sample data into an IDataView:

      var trainData = mlContext.Data.LoadFromEnumerable(sampleData);
    
    1. Define the data processing pipeline and the training algorithm (SdcaLogisticRegression):

      var pipeline = mlContext.Transforms.Text.FeaturizeText("Features", nameof(Email.Content))
        .Append(mlContext.BinaryClassification.Trainers.SdcaLogisticRegression());
    
    1. Train the model:

      var model = pipeline.Fit(trainData);
    
    1. Save the trained model as a .NET binary:

      mlContext.Model.Save(model, trainData.Schema, "model.zip");
    
    1. Load the saved model:

      var newMlContext = new MLContext();
      DataViewSchema modelSchema;
      ITransformer trainedModel = newMlContext.Model.Load("model.zip", out modelSchema);
    
    1. Create a prediction engine:

      var predictionEngine = mlContext.Model.CreatePredictionEngine<Email, SpamPrediction>(trainedModel);
    
    1. Test the model with a sample email:

      var sampleEmail = new Email { Content = "Special discount, buy now!" };
      var prediction = predictionEngine.Predict(sampleEmail);
    
    1. Output the prediction:

      Debug.WriteLine($"Email: '{sampleEmail.Content}' is {(prediction.IsSpam ? "spam" : "not spam")}");
    
    1. Assert that the prediction is correct:

      Assert.IsTrue(prediction.IsSpam);
    
    1. Verify that the model was saved:

      if(File.Exists("model.zip"))
        Assert.Pass();
      else
        Assert.Fail();
    

Conclusion

In this article, we explained a simple spam detection model in ML.NET and demonstrated how to train and test the model. This code can be extended to build more complex models, and can be used as a starting point for exploring machine learning in .NET.

Github Repo

The Meme: A Cultural A.I Embedding

The Meme: A Cultural A.I Embedding

Unpacking Memes and AI Embeddings: An Intriguing Intersection

The Essence of Embeddings in AI

In the realm of artificial intelligence, the concept of an embedding is pivotal. It’s a method of converting complex, high-dimensional data like text, images, or sounds into a lower-dimensional space. This transformation captures the essence of the data’s most relevant features.

Imagine a vast library of books. An embedding is like a skilled librarian who can distill each book into a single, insightful summary. This process enables machines to process and understand vast swathes of data more efficiently and meaningfully.

The Meme: A Cultural Embedding

A meme is a cultural artifact, often an image with text, that encapsulates a collective experience, emotion, or idea in a highly condensed format. It’s a snippet of culture, distilled down to its most essential and relatable elements.

The Intersection: AI Embeddings and Memes

The connection between AI embeddings and memes lies in their shared essence of abstraction and distillation. Both serve as compact representations of more complex entities. An AI embedding abstracts media into a form that captures its most relevant features, just as a meme condenses an experience or idea into a simple format.

Implications and Insights

This intersection offers fascinating implications. For instance, when AI learns to understand and generate memes, it’s tapping into the cultural and emotional undercurrents that memes represent. This requires a nuanced understanding of human experiences and societal contexts – a significant challenge for AI.

Moreover, the study of memes can inform AI research, leading to more adaptable and resilient AI models.

Conclusion

In conclusion, while AI embeddings and memes operate in different domains, they share a fundamental similarity in their approach to abstraction. This intersection opens up possibilities for both AI development and our understanding of cultural phenomena.