Integrating Search Functionality into Your Chatbot or LLM App with Brutus AI
Enhancing user experience in chatbot or large language model (LLM) applications is crucial for engagement and utility. One effective way to achieve this is by integrating search functionality directly into your app, allowing users to query information seamlessly. This tutorial focuses on using BrutusAI API for adding advanced search capabilities to your chatbot or LLM app, including providing search results and suggesting intelligent related questions.
In today's rapidly evolving digital landscape, users expect instant and accurate information from chatbots and LLM apps. By integrating search functionality into your app, you can significantly enhance user interaction and satisfaction. BrutusAI API emerges as a powerful tool to seamlessly add search capabilities to your application.
Why Add Search to Your Chatbot?
Before diving into the integration process, let's understand the importance of embedding search functionalities in chatbots:
- Upto-Date Information: Users can access the latest information and updates directly from the search results.
- Enhanced User Experience: Users can receive instant answers to their inquiries directly within the chat interface, eliminating the need to navigate away from the conversation.
- Contextual Awareness: Integrating search helps your chatbot understand and respond to queries with a higher degree of relevance and personalization.
- Continuous Learning: The feedback loop from search queries can be used to refine and improve your bot's responses over time.
What to Expect from this Tutorial
This tutorial will guide you through the process of integrating search functionality into your chatbot or LLM app using the BrutusAI API. The tutorial will cover the following:
- Setting Up BrutusAI: You will learn how to create an account and set up your project on BrutusAI.
- Integrating the BrutusAI API: You will learn how to integrate the BrutusAI API into your chatbot or LLM app.
- Using the API: You will learn how to use the API to provide search results and suggest intelligent, related questions to users.
Setting Up BrutusAI
First, login to BrutusAI's platform page and sign up for an account. You'll need to obtain an API key to access their services, including the search and related questions features.
Integrating the BrutusAI API
When trying to integrate the BrutusAI API into your chatbot or LLM app, it's imprtant to understand the different endpoints and functionalities available. The following are the key endpoints you'll be working with:
-
/v1/chat/completions
: This endpoint allows you to query the BrutusAI search engine and retrieve relevant search results based on the user's query.It accepts a
history
parameter, which is a list of of previous messages in the conversation, and other parameters such assearch_mode
,image_result
, andsearch_result
are also available.Parameters such as
search_mode
,image_result
, andsearch_result
are optional and can be used to customize the search results.By default, the
search_mode
is set toauto
,image_result
andsearch_result
are set to4
. That means the API would automatically determine based on conversation if it's required to generate search results or not, and it would return 4 search results and 4 image results. Many times, if conversation includes general meesages like "Hi there", "How are you?", "I am good", etc, then it's not required to generate search results. -
So the example request to the
/v1/chat/completions
endpoint would look like:import http.client import json conn = http.client.HTTPSConnection("brutusai.com") payload = json.dumps({ "history": [ { "content": "When is America's next general election?", "role": "user" } ], "search_mode": "search", "image_result": 2, "search_result": 2 }) headers = { 'x-api-key': 'your_api_key', 'Content-Type': 'application/json' } conn.request("POST", "/api/v1/chat/completions", payload, headers) res = conn.getresponse() data = res.read() print(data.decode("utf-8"))
-
So the example request to the
/v1/chat/completions
without the optional parameters would look like:import http.client import json conn = http.client.HTTPSConnection("brutusai.com") payload = json.dumps({ "history": [ { "content": "When is America's next general election?", "role": "user" } ] }) headers = { 'x-api-key': 'your_api_key', 'Content-Type': 'application/json' } conn.request("POST", "/api/v1/chat/completions", payload, headers) res = conn.getresponse() data = res.read() print(data.decode("utf-8"))
-
Example response from the
/v1/chat/completions
endpoint be something like this:{ "data": { "history": [ { "content": "When is America's next general election?", "role": "user" }, { "content": "The next general election in America is the 2024 United States presidential election, where incumbent President Joe Biden, a member of the Democratic Party, is running for re-election. His predecessor Donald Trump, a member of the Republican Party, is also expected to participate in the election. The election is scheduled for a date in 2024, and more information can be found on the Wikipedia page dedicated to the 2024 United States presidential election. Additionally, a PDF document highlighting key dates related to the 2024 elections, including the presidential qualifying date, is available for further reference.", "image_result": "[{\"title\": \"General Election\", \"imageUrl\": \"https://www.votecalhounfl.gov/portals/calhoun/Images/2024revElectionDates.jpg?ver=xwPVS3zdnC5VFXsU9QoTgw%3D%3D\", \"link\": \"https://www.votecalhounfl.gov/portals/calhoun/Images/2024revElectionDates.jpg?ver=xwPVS3zdnC5VFXsU9QoTgw%3D%3D\", \"snippet\": \"General Election\"}, {\"title\": \"Chart: 2024: The Super Election Year | Statista\", \"imageUrl\": \"http://cdn.statcdn.com/Infographic/images/normal/31604.jpeg\", \"link\": \"http://cdn.statcdn.com/Infographic/images/normal/31604.jpeg\", \"snippet\": \"Chart: 2024: The Super Election Year | Statista\"}, {\"title\": \"US 2024 presidential election: Dates, candidates and latest issues ...\", \"imageUrl\": \"https://cloudfront-us-east-2.images.arcpublishing.com/reuters/5ARS5ZBCEVK5DP6JSVWL6XGPR4.jpg\", \"link\": \"https://cloudfront-us-east-2.images.arcpublishing.com/reuters/5ARS5ZBCEVK5DP6JSVWL6XGPR4.jpg\", \"snippet\": \"US 2024 presidential election: Dates, candidates and latest issues ...\"}, {\"title\": \"US Presidential Elections 2024: Key dates and events, what lies ...\", \"imageUrl\": \"https://img.etimg.com/thumb/width-1200,height-900,imgsize-41570,resizemode-75,msid-106923480/news/international/world-news/us-presidential-elections-2024-key-dates-and-events-what-lies-ahead.jpg\", \"link\": \"https://img.etimg.com/thumb/width-1200,height-900,imgsize-41570,resizemode-75,msid-106923480/news/international/world-news/us-presidential-elections-2024-key-dates-and-events-what-lies-ahead.jpg\", \"snippet\": \"US Presidential Elections 2024: Key dates and events, what lies ...\"}]", "peopleAlsoAsk": "[]", "role": "assistant", "search_result": "[{\"title\": \"2024 United States presidential election - Wikipedia\", \"link\": \"https://en.wikipedia.org/wiki/2024_United_States_presidential_election\", \"snippet\": \"Incumbent President Joe Biden, a member of the Democratic Party, is running for re-election. His predecessor Donald Trump, a member of the Republican Party, is ...\"}, {\"title\": \"[PDF] 2024 ELECTIONS\", \"link\": \"https://www.sos.la.gov/ElectionsAndVoting/PublishedDocuments/ElectionsCalendar2024.pdf\", \"snippet\": \"***Early voting advanced one day due to the state observed holiday for Thanksgiving and Acadian Day. PRESIDENTIAL QUALIFYING. 3/23/2024 Presidential Preference ...\"}]" } ] }, "message": "Success" }
-
Understanding the response:
The
history
field contains the response to the user's query, it's the part of history. So it will include conversation history.- The
history
is list of objects, each object contains thecontent
field which contains the response to the user's query, therole
field which contains the role of the message, it can be eitheruser
orassistant
. - The other fields such as
image_result
,search_result
, andpeopleAlsoAsk
are optional and contain the search results and related questions. - The
image_result
field contains a list of image results related to the user's query. - The
search_result
field contains a list of search results related to the user's query. - The
peopleAlsoAsk
field contains a list of related questions that the user might be interested in. This is only generated when ai detects that user might be interested in related questions.
- The
Using the API
Now with api key and understanding of the endpoints, you can start integrating the BrutusAI API into your chatbot or LLM app. You have to make a POST request to the /v1/chat/completions
endpoint with the user's conversation history. The API will then return the search results and related questions based on the conversation history.
New users get $10
free credit to use the API. You can use this credit to test the API and see how it works. Once you've used up your free credit, you can purchase more credits from the BrutusAI platform.