DEV Community

Shamso Osman
Shamso Osman

Posted on

Building a Chatbot with Next.js, Cerebras API and Llama 3.1

Abstract

An interactive AI-powered Japanese language learning platform built with Next.js and the Cerebras AI API. The project creates a personalized JLPT N5 study experience by combining modern AI conversation capabilities with structured language learning principles. The system provides real-time Japanese text formatting with furigana support and category-based learning modules.

Project Demo

Problem Statement

With my JLPT N5 exam looming just weeks away, I found myself needing a study buddy that could help me practice Japanese at any time. Sure, there are plenty of flashcard apps and textbooks out there, but I wanted something more interactive - something that could give me immediate feedback and adapt to my needs. That's when I decided to combine my programming skills with my Japanese studies, leveraging the new Cerebras AI API to create a personalized AI language tutor.

If you've ever studied Japanese, you know the struggle. Textbooks are static, apps can be rigid, and finding a study partner who's available at 2 AM when you're cramming particles and kanji? Good luck! Plus, most resources either show only romaji (roman letters) or throw you into the deep end with kanji without reading aids.

The Solution

  • Create an interactive, AI-powered Japanese tutor focused on JLPT N5 preparation.
  • Implement consistent Japanese text formatting with automatic furigana display.
  • Provide structured learning paths across vocabulary, grammar, reading, and kanji.
  • Deliver immediate, contextual feedback to learners.

Technologies Used

To bring this idea to life, I combined a few technologies that allowed me to quickly prototype and build the platform:

  • Frontend Framework: For the frontend, I used Next.js. Its server-side rendering capabilities and API routes make it easy to build dynamic, responsive apps. Next.js 15.0.3, React 18.2.0
  • AI Integration: The core of the chatbot is the Llama 3.1-70B model, accessed via Cerebras Cloud SDK.
  • UI Components:
    • FontAwesome icons for visual elements
    • Custom CSS modules for styling
  • State Management: React useState hooks
  • API Integration: Custom Next.js API routes

System Architecture

Frontend

  • Single-page application with component-based architecture
  • Message handling system for chat interface
  • Quick response component for guided learning
  • Responsive design with mobile support

Backend

  • RESTful API endpoint (/api/chat)
  • Integration with Cerebras AI model
  • Custom message formatting middleware
  • Environment-based configuration

Data Flow

  • User input captured through chat interface
  • Messages processed through Next.js API route
  • AI response generated via Cerebras API
  • Response formatted and displayed with proper Japanese text styling

Implementation

Since Iā€™m preparing for JLPT N5, I focused primarily on kana (Hiragana & Katakana). The chatbot is designed to return answers primarily in kana, with kanji and romaji as needed.

Key Features

Chat Interface

const [messages, setMessages] = useState([]);
const [input, setInput] = useState("");
Enter fullscreen mode Exit fullscreen mode

AI Integration

const completionParams = {
  messages: [
    { role: "system", content: systemPrompt },
    { role: "user", content: userMessage }
  ],
  model: "llama3.1-8b"
};
Enter fullscreen mode Exit fullscreen mode

Japanese Text Formatting

response = response.replace(
  /([\u4E00-\u9FFF]+)\((.*?)\)/g,
  (match, kanji, furigana) => `${kanji}(${furigana})`
);
Enter fullscreen mode Exit fullscreen mode

Challenges Overcome

  • Implemented a regex-based system for formatting Japanese text with furigana.
  • Developed dynamic, category-based quick responses to enhance learning engagement.
  • Designed a fully responsive layout compatible with various screen sizes.
  • Optimized the AI response handling for improved performance.

Testing and Validation

  • Conducted manual testing to ensure the chat functionality works as intended.
  • Tested for cross-browser compatibility to ensure consistent behavior across different browsers.
  • Validated mobile responsiveness to ensure a seamless experience on smartphones and tablets.
  • Verified the accuracy of Japanese text formatting and its proper display.
  • Ensured proper handling of API responses under various conditions.

Results and Evaluation

The system successfully:

  • Delivers real-time Japanese language instruction for learners.
  • Maintains consistent and accurate formatting of Japanese text with furigana.
  • Provides structured learning paths across various language components.
  • Ensures responsive performance across all devices.

Performance Metrics

  • Average API response time: ~2-3 seconds
  • Japanese text formatting accuracy: 100%
  • Mobile compatibility: Fully responsive design
  • Browser support: Compatible with all modern browsers

Conclusion

The project successfully creates an interactive learning environment for Japanese language learners, integrating AI capabilities with structured language instruction. Future improvements will include:

  • Integration of speech synthesis for pronunciation assistance.
  • Implementation of a progress tracking system for learners.
  • Expansion of content coverage, particularly in vocabulary and kanji.
  • Incorporation of a spaced repetition learning system to reinforce knowledge retention.

Code and Documentation

  • The project repository follows Next.js conventions for structure and organization.
  • API documentation includes detailed information about integrating with the Cerebras AI model.
  • Custom CSS modules are used for styling specific components of the UI.
  • An environment configuration guide is provided for deployment and setup.

Try It Yourself!

Want to check it out or contribute?

Remember to star the repo if you found this helpful! ā­

  • Live Demo - coming soon!

References

Top comments (0)