DEV Community

Apollo
Apollo

Posted on

How to integrate DeepSeek R1 into your React app

Integrating DeepSeek R1 into Your React App

DeepSeek R1 is a powerful AI-driven search and recommendation engine that can enhance the user experience in your React applications. This tutorial will guide you through the process of integrating DeepSeek R1 into a React app, from setting up the SDK to implementing advanced search features.

Prerequisites

Before diving into the integration, ensure you have the following:

  • Node.js installed (v14 or higher)
  • A React project (create one using npx create-react-app if you don't have one)
  • An API key from DeepSeek R1 (sign up at DeepSeek's Developer Portal)

Step 1: Install the DeepSeek R1 SDK

First, install the DeepSeek R1 SDK using npm:

npm install @deepseek/r1-sdk
Enter fullscreen mode Exit fullscreen mode

Step 2: Initialize the SDK

Create a new file deepseek.js in your src directory to initialize the SDK. This ensures the SDK is set up once and can be reused across your application.

// src/deepseek.js
import { DeepSeekR1 } from '@deepseek/r1-sdk';

const API_KEY = 'your_api_key_here';
const deepSeek = new DeepSeekR1(API_KEY);

export default deepSeek;
Enter fullscreen mode Exit fullscreen mode

Replace 'your_api_key_here' with your actual DeepSeek R1 API key.

Step 3: Create a Search Component

Now, let's create a search component that leverages DeepSeek R1 to fetch and display search results.

// src/components/Search.js
import React, { useState } from 'react';
import deepSeek from '../deepseek';

const Search = () => {
  const [query, setQuery] = useState('');
  const [results, setResults] = useState([]);

  const handleSearch = async (e) => {
    e.preventDefault();
    try {
      const response = await deepSeek.search(query);
      setResults(response.data.results);
    } catch (error) {
      console.error('Error fetching search results:', error);
    }
  };

  return (
    <div>
      <form onSubmit={handleSearch}>
        <input
          type="text"
          value={query}
          onChange={(e) => setQuery(e.target.value)}
          placeholder="Search..."
        />
        <button type="submit">Search</button>
      </form>
      <ul>
        {results.map((result, index) => (
          <li key={index}>{result.title}</li>
        ))}
      </ul>
    </div>
  );
};

export default Search;
Enter fullscreen mode Exit fullscreen mode

This component includes:

  • A form with an input field for the user to enter their search query.
  • A useState hook to manage the query state.
  • A handleSearch function that fetches search results from DeepSeek R1.
  • A list to display the results.

Step 4: Integrate Search Component into Your App

Integrate the Search component into your main App component.

// src/App.js
import React from 'react';
import Search from './components/Search';

function App() {
  return (
    <div className="App">
      <h1>DeepSeek R1 Search Demo</h1>
      <Search />
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

Now, when you run your app (npm start), you should see a search bar that fetches results using DeepSeek R1.

Step 5: Implement Advanced Features (Optional)

DeepSeek R1 supports advanced features like filtering, sorting, and pagination. Here's how you can implement filtering by category:

// src/components/Search.js
import React, { useState } from 'react';
import deepSeek from '../deepseek';

const Search = () => {
  const [query, setQuery] = useState('');
  const [category, setCategory] = useState('all');
  const [results, setResults] = useState([]);

  const handleSearch = async (e) => {
    e.preventDefault();
    try {
      const response = await deepSeek.search(query, { category });
      setResults(response.data.results);
    } catch (error) {
      console.error('Error fetching search results:', error);
    }
  };

  return (
    <div>
      <form onSubmit={handleSearch}>
        <input
          type="text"
          value={query}
          onChange={(e) => setQuery(e.target.value)}
          placeholder="Search..."
        />
        <select value={category} onChange={(e) => setCategory(e.target.value)}>
          <option value="all">All Categories</option>
          <option value="books">Books</option>
          <option value="movies">Movies</option>
          <option value="music">Music</option>
        </select>
        <button type="submit">Search</button>
      </form>
      <ul>
        {results.map((result, index) => (
          <li key={index}>{result.title}</li>
        ))}
      </ul>
    </div>
  );
};

export default Search;
Enter fullscreen mode Exit fullscreen mode

In this enhanced version:

  • A dropdown menu allows users to filter results by category.
  • The handleSearch function includes the category in the search parameters.

Step 6: Handle Errors Gracefully

Add error handling to improve user experience when something goes wrong.

// src/components/Search.js
import React, { useState } from 'react';
import deepSeek from '../deepseek';

const Search = () => {
  const [query, setQuery] = useState('');
  const [category, setCategory] = useState('all');
  const [results, setResults] = useState([]);
  const [error, setError] = useState(null);

  const handleSearch = async (e) => {
    e.preventDefault();
    setError(null);
    try {
      const response = await deepSeek.search(query, { category });
      setResults(response.data.results);
    } catch (error) {
      setError('Failed to fetch search results. Please try again.');
    }
  };

  return (
    <div>
      <form onSubmit={handleSearch}>
        <input
          type="text"
          value={query}
          onChange={(e) => setQuery(e.target.value)}
          placeholder="Search..."
        />
        <select value={category} onChange={(e) => setCategory(e.target.value)}>
          <option value="all">All Categories</option>
          <option value="books">Books</option>
          <option value="movies">Movies</option>
          <option value="music">Music</option>
        </select>
        <button type="submit">Search</button>
      </form>
      {error && <p style={{ color: 'red' }}>{error}</p>}
      <ul>
        {results.map((result, index) => (
          <li key={index}>{result.title}</li>
        ))}
      </ul>
    </div>
  );
};

export default Search;
Enter fullscreen mode Exit fullscreen mode

Now, errors are displayed to the user in a user-friendly manner.

Conclusion

Integrating DeepSeek R1 into your React app opens up a world of possibilities for enhancing search functionality. From basic search implementations to advanced features like filtering and error handling, DeepSeek R1 provides a robust platform for delivering tailored search experiences to your users. Experiment with its capabilities, and elevate your app's usability to new heights.


🚀 Stop Writing Boilerplate Prompts

If you want to skip the setup and code 10x faster with complete AI architecture patterns, grab my Senior React Developer AI Cookbook ($19). It includes Server Action prompt libraries, UI component generation loops, and hydration debugging strategies.

Browse all 10+ developer products at the Apollo AI Store | Or snipe Solana tokens free via @ApolloSniper_Bot.

Top comments (0)