Large language models (LLMs) have changed the game of natural language processing in recent times, enabling developers to build complex chat interfaces that can talk like humans. The potential for these interfaces spans customer service, virtual assistants in general, training, and education through entertainment platforms. However, constructing useful LLM chat interfaces is not without its complications and challenges.
I have been working on integrating AI functionalities and researching how chat interfaces can be built to navigate and utilize diverse data sources using LLM and Agents. For this proof of concept, I have used Azure OpenAI and other AI functionalities within Azure. It demonstrates various use cases, design patterns, and implementation options. The goal is to provide a foundation for architects and AI enthusiasts to explore the potential of Azure AI and make informed decisions about solution approaches. These use cases leverage diverse data sources such as SQL DB, Cosmos DB, CSV files, Multiple data sources, etc. The primary goal of this project is not only to showcase different use cases but also to explore various implementation options.
Pre-requisites:
If you don’t have an Azure account set up, you can set one up here with some free credits.
Chat With CSV:
Below is an example that shows how a natural language interface can be built on any CSV file using LLM and Agents. By leveraging the sample code, Users can upload a preprocessed CSV file, ask questions about the data, and get answers from the AI model.
You can find the complete file for chat_with_CSV here.
Step 1: Define the required variables like API keys, API endpoints, loading formats, etc
I have used environment variables. You can have them in a config file or define them in the same file.
For this, we need to import AzureChatOpenAI from langchain_openai and use the below params,
11m=AzureChatOpenAI(azure_endpoint=aoai_endpoint,
openai_api_key=aoai_api_key,
temperature=0,
azure_deployment=deployment_name,
openai_api_version=aoai_api_version,
streaming=True)
azure_endpoint: Azure endpoint, including the resource.
openai_api_key: This is a unique identifier that is used to authenticate and control access to OpenAI’s APIs.
openai_api_version: The service APIs are versioned using the API-version query parameter. All versions follow the YYYY-MM-DD date structure.
streaming: Default this boolean to False, which indicates whether the stream has the results.
Temperature: Temperature is a parameter that controls the randomness of the output generated by the AI model. A lower temperature results in more predictable and conservative outputs. A higher temperature allows for more creativity and diversity in the responses. It’s a way to fine-tune the balance between randomness and determinism in the model’s output.
deployment_name: A model deployment. If given, set the base client URL to include /deployments/{azure_deployment}. Note: this means you won’t be able to use non-deployment endpoints.
You can add more parameters if required; the details are in this link.
number_of_head_rows=df.shape[0]#send all the rows to LLM
)
The pandas agent is a LangChain agent created using the create_pandas_dataframe_agent function, which takes the below inputs and params,
A language model(LLM) as input.
A pandas data frame(CSV data) containing the data as input.
Verbose: If the agent is returning Python code, it might be helpful to examine this code to understand what’s going wrong. You could do this by setting verbose=True when creating the agent, which should print out the generated Python code.
agent_Type: This shows how to initialize the agent using the OPENAI_FUNCTIONS agent type. This creates an agent that uses OpenAI function calling to communicate its decisions on what actions to take.
handle_parsing_error: Occasionally, the LLM cannot determine what step to take because its outputs are not correctly formatted to be handled by the output parser. In this case, by default, the agent errors. But you can easily control this functionality with handle_parsing_errors.
When the run method is called on the panda agent with the input message from the prompt and callback arguments, it goes through a sequence of steps to generate an answer.
Initially, the agent identifies the task and selects the appropriate action to retrieve the required information from the data frame. After that, it observes the output, combines the observations, and generates the final answer.
Chat with Database:
The sample code below shows how a natural language interface can be built on structured data like SQL DB and NoSQL, like Cosmos DB, with Azure OpenAI’s capabilities. This can be utilized as an SQL programmer Assistant. The objective is to generate SQL code (SQL Server) to retrieve an answer to a natural language query.
You can find the entire file for chat_with_DB here.
Structured Data Like SQL DB:
Step 1: Load the Azure and Database connection variables
I have used environment variables; you can have it as a config file or define it in the same file.
model=deployment_name,# The deployment name you chose when you deployed the ChatGPT or GPT-4 model.
messages=messages,
temperature=0,
max_tokens=2000
)
After getting the client, the API ChatCompletions gets the user prompt and generates the SQL query for the natural language query
model: OpenAI uses the model keyword argument to specify what model to use. Azure OpenAI has the concept of unique model deployments. When you use Azure OpenAI, a model should refer to the underlying deployment name you chose when you deployed the model. See the model endpoint compatibility table for details on which models work with the Chat API.
max_tokens The maximum number of tokens can be generated in the chat completion. The total length of input tokens and generated tokens is limited by the model’s context length.
temperature: What sampling temperature should be used? Between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or top_p but not both.
messages: A list of messages comprising the conversation so far.
If you want, you can add more params based on the requirement.
Step 3: Using Panda, read sql to get the query result
Utilizing panda read sql (pandas.read_sql(sql, con)) to read the sql query or database table into a data frame and return the pandas data frame containing the results of the query run.
defrun_sql_query(aoai_sqlquery):
”’
Function to run the
generated SQL Query on SQL server and retrieve output.
Input: AOAI completion (SQL Query)
Output: Pandas dataframe containing results of the query run
”’
conn=connect_sql_server()
df=pd.read_sql(aoai_sqlquery,conn)
returndf
No SQL like COSMOS DB:
Step 1: Create an Azure OpenAI client
To create clients, we utilize AzureOpenAI from Openai.
api-version: A valid Azure OpenAI API version, such as the API version you selected when you imported the API.
api_key: A unique identifier used to authenticate and control access to OpenAI’s APIs.
Step 2: Create a model response for the chat conversation
After getting the client, the API ChatCompletions gets the user prompt and generates the query along with the response for the natural language query. Adding params details based on the requirement.
Be sure to include the “extra_body” parameter when using Cosmos as the data source.
description=“Unique identifier for the conversation.”,
default=“”,
is_shared=True,
),
],
)
Utilizing create_openai_tools_agent to create an agent that uses OpenAItools and
Create an agent executor by passing in the agent and tools and run the agent using RunnableWithMessageHistory. RunnableWithMessageHistory must always be called with a config that contains the appropriate parameters for the chat message history factory.
Step 5: Agent Executor is invoked with prompt and config