Web assistant with function calling using Next JS
A chat app for playing with TuneStudio APIs
This tutorial focuses on demonstrating the interaction between a chat application and TuneStudio APIs to provide functionalities beyond simple text messaging. By enabling functions such as web searching, fetching news, online shopping information retrieval, URL summarization, and image generation, developers can create more robust and interactive user experiences.
Tools and Libraries
For this implementation, we’ll be using several tools and libraries:
- NextJs: To manage the frontend and backend of the application.
- TuneStudio API: To stream the chat operations and incorporate various functionalities.
- Serper API: To fetch search, news articles and other information.
- Stability API: To generate images based on the user’s requests.
Setting Up the Environment
To integrate TuneStudio APIs, you will need several keys which can be obtained by registering on the respective platforms:
- TUNE_KEY from TuneStudio
- SERPER_KEY from Serper
- STABILITY_API_KEY from Stability
Store these keys in your .env
file as shown:
Backend Implementation Using TuneStudio APIs
Start by creating a file named route.ts inside app/prompt directory. This file will handle the API requests and responses.
We will be defining some tools that our model can use, for that create a file named constants.ts inside the app directory.
The backbone of our application is the function that streams responses based on various operations, defined in the TuneAIStream
module.
The TuneAIStream
function handles the response from the TuneStudio API based on the messages it receives:
The API is used to initiate different AI functionalities like web searching (search_web
), fetching news (get_news
), or summarizing content (summarize_given_url
), mostly dependent on the user’s requests parsed from the chat messages.
Frontend Interaction
Create a file named page.tsx inside the app directory to handle the frontend interaction.
You can find complete code for the frontend interaction here
Frontend interaction involves sending messages to the backend API and displaying the responses in the chat interface. The user can input text messages, and the application will process them through the TuneStudio APIs to provide the desired functionalities. We have a handleSearch function that sends the user’s message to the backend API and receives the response. The response is then displayed in the chat interface.
- The
handleSearch
function exemplifies the interaction with the backend. It prepares a POST request with user messages and sends it to our endpoint which then communicates with TuneStudio API. - The function parses and handles streaming responses effectively by updating the UI in real-time as chunks of data are received from the TuneStudio API.
Conclusion
Innovative use of TuneStudio APIs in a chat application setup allows the creation of a vibrant, interactive user experience that goes beyond traditional chat mechanisms. By carefully harnessing AI capabilities, developers can deliver a dynamic environment where information retrieval becomes part of the conversation, enriching user engagement and providing instant value from within the chat interface. This tutorial has outlined setting up such functionalities using TuneStudio’s powerful tools, paving the way for more comprehensive and interactive applications.
You can find the complete code for this tutorial here
Was this page helpful?