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
  • Parallel Prompt
  • Agent Prompt
  • Using INI Files for Prompts
  • How to Replace Built-In Prompts
  • Examples
Export as PDF
  1. Components

Using SmartPrompt

PreviousUsing SmartToolsNextUsing SmartSession

Last updated 3 days ago

Overview

The is a specialized object that facilitates communication with the LLM (Language Model) endpoint. Unlike a basic message sent to the LLM, the SmartPrompt offers advanced functionality by incorporating events, tools, parameters, and images. This enhanced capability allows developers to create more interactive and context-aware prompts, enabling a richer and more versatile interaction with the LLM.

Utilize the SmartPrompt when you need to send a query to the AI for quick, one-off interactions without the need to manage an ongoing session. It is particularly effective for brief engagements where simplicity and efficiency are paramount. Additionally, the SmartPrompt can be seamlessly integrated within tools and can be invoked during other AI interactions, providing a streamlined approach to accessing AI capabilities in various contexts.

How to Use

To execute AI queries using the SmartPrompt, the primary requirement is an endpoint. Once the endpoint is established, you can invoke the AskAsync method on your prompt. Although the SmartPrompt instance is reusable, it does not maintain a history of previous requests—each invocation of AskAsync initiates a new request.

SmartPrompts are versatile, capable of utilizing any tool, and can function as AI agents without necessitating any additional coding. Moreover, they can be effectively employed in service and console applications that operate without a user interface.

var prompt = new SmartPrompt(
	"You are an expert car mechanic." + 
	"Always answer the user's questions with technical details using your expertise.");

C.WriteLine((await prompt.AskAsync(endpoint, "How often shall I change the transmission fluid on my yellow 1977 Chevy Camaro?")).Text);

C.WriteLine((await prompt.AskAsync(endpoint, "What are the steps to replace the spark plugs?")).Text);

// Output
/*
For a 1977 Chevy Camaro, it is generally recommended to change the transmission fluid every 30,000 to 40,000 miles. However, this can vary based on driving conditions and maintenance history. If the vehicle is used for heavy-duty driving, such as towing or frequent stop-and-go traffic, it may be beneficial to change the fluid more frequently. Regularly checking the fluid level and condition can also help in determining the appropriate interval. If the fluid appears dark or has a burnt smell, it is a good indication that it needs to be changed. Always refer to the owner's manual for specific recommendations for your vehicle.
Replacing spark plugs is a crucial maintenance task to ensure your engine runs smoothly. Here are the steps to replace them:

1. **Gather Tools and Materials**:
   - Spark plug socket
   - Ratchet wrench
   - Extension bar
   - Torque wrench
   - New spark plugs
   - Dielectric grease
   - Spark plug gap tool
   - Compressed air (optional)

2. **Prepare the Vehicle**:
   - Ensure the engine is cool to prevent burns.
   - Disconnect the negative battery terminal to avoid electrical shorts.

3. **Access the Spark Plugs**:
   - Remove any engine covers or components obstructing access to the spark plugs.
   - Identify the spark plug wires or ignition coils.

4. **Remove the Old Spark Plugs**:
   - If equipped with ignition coils, disconnect the electrical connectors and remove the coils.
   - Use the compressed air to clean around the spark plug area to prevent debris from entering the combustion chamber.
   - Use the spark plug socket and ratchet to carefully unscrew and remove the old spark plugs.

5. **Inspect and Prepare New Spark Plugs**:
   - Check the gap of the new spark plugs using the gap tool and adjust if necessary according to the manufacturer's specifications.
   - Apply a small amount of dielectric grease to the inside of the spark plug boot.

6. **Install the New Spark Plugs**:
   - Carefully thread the new spark plugs into the cylinder head by hand to avoid cross-threading.
   - Use the torque wrench to tighten the spark plugs to the manufacturer's specified torque setting.

7. **Reassemble Components**:
   - Reinstall the ignition coils or spark plug wires, ensuring they are securely connected.
   - Reattach any engine covers or components that were removed.

8. **Reconnect the Battery**:
   - Reconnect the negative battery terminal.

9. **Test the Engine**:
   - Start the engine to ensure it runs smoothly and check for any unusual noises or misfires.

By following these steps, you can effectively replace the spark plugs and maintain optimal engine performance.
*/
Dim prompt As New SmartPrompt(
    "You are an expert car mechanic." & 
    "Always answer the user's questions with technical details using your expertise.")

Console.WriteLine((Await prompt.AskAsync(endpoint, "How often shall I change the transmission fluid on my yellow 1977 Chevy Camaro?")).Text)

Console.WriteLine((Await prompt.AskAsync(endpoint, "What are the steps to replace the spark plugs?")).Text)

' Output
' For a 1977 Chevy Camaro, it is generally recommended to change the transmission fluid every 30,000 to 40,000 miles. 
' However, this can vary based on driving conditions and maintenance history. 
' If the vehicle is used for heavy-duty driving, such as towing or frequent stop-and-go traffic, 
' it may be beneficial to change the fluid more frequently. 
' Regularly checking the fluid level and condition can also help in determining the appropriate interval. 
' If the fluid appears dark or has a burnt smell, it is a good indication that it needs to be changed. 
' Always refer to the owner's manual for specific recommendations for your vehicle.

' Replacing spark plugs is a crucial maintenance task to ensure your engine runs smoothly. Here are the steps to replace them:

' 1. **Gather Tools and Materials**:
'    - Spark plug socket
'    - Ratchet wrench
'    - Extension bar
'    - Torque wrench
'    - New spark plugs
'    - Dielectric grease
'    - Spark plug gap tool
'    - Compressed air (optional)

' 2. **Prepare the Vehicle**:
'    - Ensure the engine is cool to prevent burns.
'    - Disconnect the negative battery terminal to avoid electrical shorts.

' 3. **Access the Spark Plugs**:
'    - Remove any engine covers or components obstructing access to the spark plugs.
'    - Identify the spark plug wires or ignition coils.

' 4. **Remove the Old Spark Plugs**:
'    - If equipped with ignition coils, disconnect the electrical connectors and remove the coils.
'    - Use the compressed air to clean around the spark plug area to prevent debris from entering the combustion chamber.
'    - Use the spark plug socket and ratchet to carefully unscrew and remove the old spark plugs.

' 5. **Inspect and Prepare New Spark Plugs**:
'    - Check the gap of the new spark plugs using the gap tool and adjust if necessary according to the manufacturer's specifications.
'    - Apply a small amount of dielectric grease to the inside of the spark plug boot.

' 6. **Install the New Spark Plugs**:
'    - Carefully thread the new spark plugs into the cylinder head by hand to avoid cross-threading.
'    - Use the torque wrench to tighten the spark plugs to the manufacturer's specified torque setting.

' 7. **Reassemble Components**:
'    - Reinstall the ignition coils or spark plug wires, ensuring they are securely connected.
'    - Reattach any engine covers or components that were removed.

' 8. **Reconnect the Battery**:
'    - Reconnect the negative battery terminal.

' 9. **Test the Engine**:
'    - Start the engine to ensure it runs smoothly and check for any unusual noises or misfires.

' By following these steps, you can effectively replace the spark plugs and maintain optimal engine performance.

The code snippet above demonstrates how to effectively reuse a SmartPrompt instance that has been initialized with a system prompt.

SmartPrompts can also incorporate parameters, similar to how SmartHub and SmartSession objects function. For example, the initialization shown earlier can be modified to:

prompt.Parameters.Add("year", 1977);
C.WriteLine((await prompt.AskAsync(endpoint, "How often shall I change the transmission fluid on my yellow {{year}} Chevy Camaro?")).Text);

Parallel Prompt

Agent Prompt

In Wisej.AI, there are three distinct types of SmartAgentPrompt: BeforePrompt, AfterPrompt and WhenDone.

The BeforePrompt agents are activated prior to the adapter invoking the AI model, allowing for any necessary pre-processing or modifications before the AI model generates a response.

The AfterPrompt agents are triggered after the adapter receives a response from the AI model but before any further processing of that response takes place. This allows for post-response handling, such as additional processing or validation.

WhenDone agents are triggered after the adapter has completed processing the response from the AI model and has fully updated the connected controls, if any.

SmartAgentPrompts have the capability to utilize various tools, which significantly enhances their functionality when used in conjunction with adapters. This combination allows developers to perform complex tasks, such as dynamic data manipulation or custom response handling.

By default, agents do not alter or inject any messages; they operate in a sequential manner. This behavior is crucial because when multiple agents are subscribed, they are invoked in the order they were added. For example, an agent might leverage a tool to send an email upon detecting a specific event in the response being handled by the adapter. Moreover, developers can create custom agent classes to further enhance the functionality of adapters, enabling more tailored and sophisticated interactions.

var endpoint = new OpenAIEndpoint { ApiKey = "..." };

// Injects CoT steps
class MyAgent : SmartAgentPrompt
{
    public MyAgent() 
        : base(
            AgentRunPosition.BeforePrompt, 
        "    Output the steps required to answer the question.")
    {
    }

    protected override async Task<Message> RunAgentAsync(
        SmartAdapter adapter, MessageCollection messages)
    {
        var response = await base.RunAgentAsync(adapter, messages);
        messages.Add(MessageRole.Assistant, response.Text);
        return response;
    }
}

// Use the SmartDocumentAdapter to ask a question in console mode, without a UI
var adapter = new SmartDocumentAdapter
{
	Hub = new SmartHub { 
		Endpoint = endpoint
	}
};

new MyAgent().Subscribe(adapter);

var response = await adapter.AskAsync("When I was 3 years old, my partner was 3 times my age. Now, I am 20 years old. How old is my partner? Do not show me your reasoning.");

Console.WriteLine(response);

// Output
// Your partner is 26 years old.

Using INI Files for Prompts

Wisej.AI already comes equipped with numerous complex prompts integrated into the system, including prompts for adapters and tools. However, as you continue to work with Wisej.AI, you may need to create additional, more specific prompts. These could involve tasks such as defining how fields are extracted, describing custom tools, configuring agents, and more.

All prompts in Wisej.AI can be saved to a .ini file located in the /AI folder under a designated key name. You have the flexibility to use multiple .ini files and can override any of the pre-existing prompts as needed.

For example, if you are configuring the FieldPrompt of a TextBox that is inside a container associated with the SmartDataEntryAdapter you can either type the prompt directly in the property, or you can use a key from the .ini files.

Instead of "Combine supplier name and address..." type "[MyFields.SupplierPrompt]". Then create a "Prompts.ini" file under the /AI folder and set the content to:

#
# This is a comment
#
[MyFields.SupplierPrompt]
Combine supplier name and address in a new line

This feature enables you to extract all prompts into .ini files, offering enhanced flexibility. You can modify prompts without needing to recompile the application and tailor different prompts for various clients.

You can enable the auto-refresh feature by setting this Application Switch in your code:

AppContext.SetSwitch("SmartPromptWatchPromptFiles", true);

How to Replace Built-In Prompts

If you want to see and potentially replace any of the built-in prompts, use

SmartPrompt.SavePrompts(filePath)

It will export all the prompts into a single .ini file that you can copy into your /AI folder. It will override the internal prompts.

Examples

The SmartPrompt provides the quickest means of sending requests to an AI endpoint. It harnesses the full capabilities of the Wisej.AI agentic approach, allowing developers to utilize it with just a single line of code. Internally, SmartPrompt employs a private instance of the SmartSession class, which is discarded after each request. This instance is solely used to manage the internal agentic loop, ensuring efficient handling of operations.

The following examples demonstrate how to effortlessly use the SmartPrompt in various scenarios:

  1. The Smallest Chat Console in the World: Implement a minimal chat interface to interact with AI seamlessly.

  2. Execute a Smart Web Search: Perform intelligent web searches with enhanced efficiency using AI capabilities.

  3. Describe an Image: Leverage AI to analyze and image and write a detailed description.

The Smallest Chat Console in the World
var prompt = new SmartPrompt();
var endpoint = new OpenAIEndpoint { ApiKey = "..." };

while (true)
  Console.WriteLine((await prompt.AskAsync(endpoint, Console.ReadLine())).Text);
Execute a Smart Web Search
// Register Bing for the IWebSearchService.
Application.Services
	.AddOrReplaceService<IWebSearchService>(
		new BingWebSearchService { ApiKey = "..."});

// Create SmartPrompt with web searching skills.
var prompt = new SmartPrompt().UseTools(new WebSearchTools());
var endpoint = new OpenAIEndpoint { ApiKey = "... };

// Ask
var answer = await prompt.AskAsync(
   endpoint, 
   "Find the best flight from New York to Milan that leaves in the morning." +
   "Include all details.");
Console.WriteLine(answer.Text);

// Output
/*
Here are some of the best morning flights from New York to Milan:

1. **Delta Airlines**
   - Departure: 7:20 AM from JFK
   - Arrival: 8:10 AM at MXP
   - Duration: 7 hours 50 minutes
   - Price: $450

2. **British Airways**
   - Departure: 7:20 AM from JFK
   - Arrival: 8:10 AM at MXP
   - Duration: 7 hours 50 minutes
   - Price: $463

3. **United Airlines**
   - Departure: 5:30 AM from EWR
   - Arrival: 7:30 AM at MXP
   - Duration: 8 hours
   - Price: $470

4. **Neos Air**
   - Departure: 5:00 AM from JFK
   - Arrival: 7:00 AM at MXP
   - Duration: 8 hours
   - Price: $483

These flights are direct and offer competitive pricing.
*/
Classify Data in Parallel
var endpoint = new OpenAIEndpoint { ApiKey = "..." };

var prompt = new SmartParallelPrompt(
	"Classify user sentiment using these categories: Good, Bad, Decent.\n"+
	"Output only the category and nothing else.");

var comments = new[] { 
	"I love your product!",
	"The item was broken, I returned it",
	"Good quality but the instructions are lacking"
};

var results = await prompt.AskAsync(endpoint, comments);
Console.WriteLine(String.Join("\r\n", results.Select(r => r.Text)));

// Output
/*
Good
Bad
Decent
*/
Describe an Image
var endpoint = new OpenAIEndpoint { ApiKey = "..." };

var prompt = new SmartPrompt(
	"You are an expert in intellectual property." +
	"Your job is to describe the provided trademark in sufficient details for the patent office.");

var response = await prompt.AskAsync(endpoint, "", Image.FromFile("Adidas-Logo.jpg"));

Console.WriteLine(response.Text);

// Output
/*
The trademark consists of a stylized design featuring three parallel diagonal stripes, which are slanted to the right, forming a triangular shape. Below the stripes, the word "adidas" is written in lowercase letters using a bold, sans-serif font. The design is typically presented in a monochromatic color scheme, often black on a white background. The combination of the three stripes and the brand name creates a distinctive and recognizable logo.
*/

The class is an extension of the , designed to enable the execution of multiple LLM queries concurrently. It includes the ability to trigger the event during the processing of these queries, providing real-time updates on their status. Although there is no built-in limit to the number of parallel requests that can be sent, it is advisable for developers to exercise discretion in managing the volume of concurrent requests to avoid potential performance issues or overloading the system.

The serves as an "agent" capable of subscribing to and unsubscribing from smart adapters. An agent instance can subscribe to multiple adapters.

The example above is a variation of the same problem solved using a object .

Classify Data in Parallel: Utilize AI to classify data concurrently, boosting processing speed and accuracy. Uses the to execute tasks in parallel.

SmartPrompt
SmartAgentPrompt
SmartParallelPrompt
SmartParallelPrompt
SmartPrompt
SmartSession
here
var prompt = new SmartParallelPrompt();
var responses = await prompt.AskAsync(
    openAIEndpoint1, 
    ["question1", "question2"]);
Console.WriteLine(responses[0].Text);
Console.WriteLine(responses[1].Text);
Dim prompt As New SmartParallelPrompt()
Dim responses = Await prompt.AskAsync( _ 
    openAIEndpoint1, _
    {"question1", "question2"})
Console.WriteLine(responses(0).Text)
Console.WriteLine(responses(1).Text)
Progress