AI Bot

Translated from the Spanish original. Read in Spanish

In this tutorial we’ll learn how to build a simple AI bot that answers questions using Python and Azure OpenAI. We’ll put together a basic system that can answer questions in an interactive conversation from the command line.

iStock AI Generator

Prerequisites

Before you start, install the following libraries in your Python environment:

pip install langchain
pip install -U langchain-openai
pip install azure-identity

Authentication

First, we need to load the credentials required to authenticate against Azure and use the Azure OpenAI services.

import os
from azure.identity import ChainedTokenCredential, EnvironmentCredential

We load the environment variables AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET with their corresponding values.

os.environ['AZURE_TENANT_ID'] = "00000000-0000-0000-0000-000000000000"
os.environ['AZURE_CLIENT_ID'] = "00000000-0000-0000-0000-000000000000"
os.environ['AZURE_CLIENT_SECRET'] = "xxxxx"

To get the access token we use Azure Identity. You can find more information in the official Azure Identity documentation.

credential = ChainedTokenCredential(EnvironmentCredential())
access_token = credential.get_token("https://cognitiveservices.azure.com/.default")

Implementing the Question-and-Answer Bot

Next, we set the deployment to the name of the deployment and set the required environment variables.

deployment = "zerogap-gpt4-turbo"

os.environ["AZURE_OPENAI_ENDPOINT"] = "https://zerogap.openai.azure.com/"
os.environ["OPENAI_API_VERSION"] = "2024-02-15-preview"
os.environ["OPENAI_API_TYPE"] = "azure_ad"
os.environ["AZURE_OPENAI_API_KEY"] = access_token.token
os.environ["OPENAI_DEPLOYMENT"] = deployment

We configure the LLM and the message template for questions and answers. You can find more information about these settings in the LangChain documentation and the message templates quick start guide.

from langchain_openai import AzureChatOpenAI
from langchain.prompts import ChatPromptTemplate

llm = AzureChatOpenAI(openai_api_version="2024-02-15-preview", azure_deployment=deployment,  temperature=0.5)

qa_system_prompt = """Tú eres un asistente para tareas de respuesta a preguntas. \
Utiliza un máximo de tres frases y mantén la respuesta concisa.\
"""
qa_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", qa_system_prompt),
        ("human", "{input}"),
    ]
)

We define the processing chain for the bot and set up the interactive chat from the command line.

from langchain.prompts import ChatPromptTemplate

chain = qa_prompt | llm

print("\033[92m" + "¡Bienvenido al Bot de IA!" + "\033[0m")
print("\033[92m" + "###############################" + "\033[0m")
print("\033[92m" + "###############################" + "\033[0m")
print("\n")

while True:
    question = input("\033[93m" + "Ingresa tu pregunta: " + "\033[0m")
    if question == "quit":
        break
    result = chain.invoke({"input": question})
    print("\033[92m" + "Respuesta: " + "\033[0m", result)

With this code you can interact with the chatbot by typing questions in your terminal.

Complete Code

Here is the complete code to run the question-and-answer AI bot using Python and Azure OpenAI:

import os
from azure.identity import ChainedTokenCredential, EnvironmentCredential
from langchain_openai import AzureChatOpenAI
from langchain.prompts import ChatPromptTemplate

os.environ['AZURE_TENANT_ID'] = "00000000-0000-0000-0000-000000000000"
os.environ['AZURE_CLIENT_ID'] = "00000000-0000-0000-0000-000000000000"
os.environ['AZURE_CLIENT_SECRET'] = "xxxxx"

credential = ChainedTokenCredential(EnvironmentCredential())
access_token = credential.get_token("https://cognitiveservices.azure.com/.default")

deployment = "zerogap-gpt4-turbo"

os.environ["AZURE_OPENAI_ENDPOINT"] = "https://zerogap.openai.azure.com/"
os.environ["OPENAI_API_VERSION"] = "2024-02-15-preview"
os.environ["OPENAI_API_TYPE"] = "azure_ad"
os.environ["AZURE_OPENAI_API_KEY"] = access_token.token
os.environ["OPENAI_DEPLOYMENT"] = deployment

llm = AzureChatOpenAI(openai_api_version="2024-02-15-preview", azure_deployment=deployment,  temperature=0.5)

qa_system_prompt = """Tú eres un asistente para tareas de respuesta a preguntas. \
Utiliza un máximo de tres frases y mantén la respuesta concisa.\
"""
qa_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", qa_system_prompt),
        ("human", "{input}"),
    ]
)

chain = qa_prompt | llm

print("\033[92m" + "¡Bienvenido al Bot de IA!" + "\033[0m")
print("\033[92m" + "###############################" + "\033[0m")
print("\033[92m" + "###############################" + "\033[0m")
print("\n")

while True:
    question = input("\033[93m" + "Ingresa tu pregunta: " + "\033[0m")
    if question == "quit":
        break
    result = chain.invoke({"input": question})
    print("\033[92m" + "Respuesta: " + "\033[0m", result)

With this simple example, you’ve built an AI bot that can be the foundation for more complex systems.

Maximiliano Díaz Doglia

AI Platform Engineer & Full-Stack Developer
Building Enterprise Integrations & Automations

Published in: AI