Skip to content

Latest commit

 

History

History
214 lines (168 loc) · 11.9 KB

File metadata and controls

214 lines (168 loc) · 11.9 KB

Optimizing Chatbot Efficiency

Costa Rica

GitHub GitHub Cloud2BR OSS - Learning Hub

Last updated: 2026-01-29


Table of Contents (Click to expand)

Find below strategies to enhance process efficiency, these approaches can significantly reduce computational demands and enhance model efficiency in chatbot applications.

  • Contextual Memory: Implement a server-side system that retains conversation context. This allows for sending only new user requests to the model, with the server adding necessary context before the API call.
  • Prompt Compression: Apply techniques to shorten the prompt by removing non-essential words, simplifying sentences, and concentrating on key context and instructions.
  • Batch Processing: For multiple queries, group them into a single API call to decrease request numbers and boost efficiency.
  • Parameterization: Use a parameterized format for prompts to allow dynamic input substitution, reducing prompt size while keeping it flexible.
  • Preprocessing: Clean and preprocess input data to eliminate unnecessary whitespace, punctuation, and formatting issues, optimizing prompt processing efficiency.

General Overview

Set Up Your Azure Environment

Task Steps
Create an Azure OpenAI Resource 1. Sign in to the Azure Portal:
- Go to the Azure Portal.
- Sign in with your Azure account.
2. Create a Resource:
- Click on Create a resource.
- Search for Azure OpenAI.
- Select Azure OpenAI and click Create.
3. Configure the Resource:
- Fill in the required details such as Subscription, Resource Group, Region, and Name.
- Choose the appropriate pricing tier.
- Click Review + create and then Create.
Deploy a Model 1. Navigate to Azure OpenAI Studio:
- Go to Azure OpenAI Studio.
- Sign in with your Azure credentials.
2. Create a Deployment:
- Select your Azure OpenAI resource.
- Click on Deployments and then Create new deployment.
- Choose the model you want to deploy (e.g., GPT-4).
- Configure the deployment settings and click Create.

Set Up a Database for Context Storage

Using Azure SQL Database:

  1. Create an Azure SQL Database:

    • In the Azure Portal, click on Create a resource.
    • Search for SQL Database and select it.
    • Fill in the required details such as Subscription, Resource Group, Database Name, and Server.
    • Configure the database settings and click Review + create and then Create.
  2. Configure the Database:

    • Set up the firewall rules to allow your application to connect to the database.
    • Create a table to store conversation context.
    CREATE TABLE context (
        user_id NVARCHAR(50) PRIMARY KEY,
        conversation NVARCHAR(MAX)
    );

Implement Contextual Memory in the Application

To implement contextual memory, you'll need to store and manage the conversation context on the server. This way, you only have to send the user's most recent request with each request. Here's a step-by-step guide to getting started:

  • Set Up a Server: To store the conversation context. This can be done using a web server with a database.
  • Store Conversation Context: When a user sends a message, store the conversation context in a database. This context can include the user’s previous messages, the bot’s responses, and any other relevant information.
  • Retrieve Context: When a new message is received, retrieve the stored context from the database. Combine this context with the new user message to form the prompt for the model.
  • Update Context: After generating a response, update the stored context with the new user message and the bot’s response.

Example

Here’s a simplified example using Python and a database like Azure SQL. For more information on how to connect to the SQL database click here. For more about Azure OpenAI On Your Data API click here

import pyodbc

# Define the connection string for Azure SQL database
# Please replace the placeholders with your actual Azure SQL database credentials
connection_string = (
    "Driver={ODBC Driver 17 for SQL Server};"
    "Server=your_server.database.windows.net;"
    "Database=your_database;"
    "Uid=your_username;"
    "Pwd=your_password;"
    "Encrypt=yes;"
    "TrustServerCertificate=no;"
    "Connection Timeout=30;"
)

def get_db_connection():
    ## Establish a connection to the Azure SQL database.## 
    try:
        conn = pyodbc.connect(connection_string)
        return conn
    except pyodbc.Error as e:
        print(f"Error connecting to database: {e}")
        return None

def create_table():
    ## Create the context table if it doesn't exist.## 
    with get_db_connection() as conn:
        if conn:
            with conn.cursor() as cursor:
                cursor.execute('''
                IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'context')
                BEGIN
                    CREATE TABLE context (
                        user_id NVARCHAR(255) PRIMARY KEY,
                        conversation NVARCHAR(MAX)
                    )
                END
                ''')
                conn.commit()

def store_context(user_id, conversation):
    ## Store the conversation context in the database.## 
    with get_db_connection() as conn:
        if conn:
            with conn.cursor() as cursor:
                cursor.execute('''
                MERGE INTO context AS target
                USING (SELECT ? AS user_id, ? AS conversation) AS source
                ON (target.user_id = source.user_id)
                WHEN MATCHED THEN 
                    UPDATE SET conversation = source.conversation
                WHEN NOT MATCHED THEN 
                    INSERT (user_id, conversation) VALUES (source.user_id, source.conversation);
                ''', (user_id, conversation))
                conn.commit()

def retrieve_context(user_id):
    ## Retrieve the conversation context from the database.## 
    with get_db_connection() as conn:
        if conn:
            with conn.cursor() as cursor:
                cursor.execute('SELECT conversation FROM context WHERE user_id = ?', (user_id,))
                result = cursor.fetchone()
                return result[0] if result else ""

Integrating with Azure OpenAI:

Tip

Use the Azure OpenAI API to send the prompt and receive the response. Click here if want to see more.

import os
from openai import AzureOpenAI
    
client = AzureOpenAI(
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),  
    api_version="2024-02-01",
    azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
    )

#This will correspond to the custom name you chose for your deployment when you deployed a model. 
deployment_name='REPLACE_WITH_YOUR_DEPLOYMENT_NAME'

# Function to send prompt to the model
# Send a completion call to generate an answer
def send_to_model(prompt):
    print('Sending a test completion job')
    start_phrase = 'Write a tagline for an ice cream shop. '
    response = client.completions.create(model=deployment_name, prompt=start_phrase, max_tokens=10)
    print(start_phrase+response.choices[0].text)
    return response.choices[0].text.strip()

# Example usage
user_id = "user123"
new_message = "Hello, how are you?"

# Retrieve existing context
context = retrieve_context(user_id)

# Combine context with new message
full_prompt = context + "\nUser: " + new_message

# Send full_prompt to the model and get response
response = send_to_model(full_prompt)

# Update context
new_context = full_prompt + "\nBot: " + response
store_context(user_id, new_context)

Other considerations:

  • To enhance context management, consider truncating older parts of the conversation, summarizing previous discussions, and using tokens effectively to manage prompt size.
  • For security and privacy, ensure the stored context is secure and adheres to privacy laws by encrypting sensitive data, implementing access controls, and ensuring compliance with data protection regulations like GDPR and CCPA.

Wiki

Total views

Refresh Date: 2026-04-07