DEV Community

Apollo
Apollo

Posted on

How to integrate DeepSeek R1 into your React app

Integrating DeepSeek R1 into Your React App: A Comprehensive Guide

DeepSeek R1 is a powerful AI-powered search engine designed to deliver highly accurate and context-aware search results. Integrating it into your React application can significantly enhance your app's search capabilities, providing users with a seamless and intelligent search experience. In this tutorial, we'll walk you through the process of integrating DeepSeek R1 into your React app, complete with real code snippets.

Prerequisites

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

  1. Node.js installed (version 14 or higher).
  2. A React project set up. If you don’t have one, create it using npx create-react-app deepseek-r1-demo.
  3. An API key from DeepSeek R1. You can obtain this by signing up on their platform.

Step 1: Install Required Dependencies

First, install the necessary dependencies. We'll use axios for making HTTP requests to the DeepSeek R1 API.

npm install axios
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure Environment Variables

Store your DeepSeek R1 API key in an environment variable to keep it secure. Create a .env file in the root of your project and add the following:

REACT_APP_DEEPSEEK_API_KEY=your_api_key_here
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a DeepSeek Service

Create a service module to interact with the DeepSeek R1 API. This module will handle the API requests and responses.

Create a new file deepseekService.js in the src directory:

import axios from 'axios';

const DEEPSEEK_API_URL = 'https://api.deepseek.com/v1/search';
const API_KEY = process.env.REACT_APP_DEEPSEEK_API_KEY;

const deepseekService = {
  search: async (query) => {
    try {
      const response = await axios.post(
        DEEPSEEK_API_URL,
        { query },
        {
          headers: {
            'Authorization': `Bearer ${API_KEY}`,
            'Content-Type': 'application/json',
          },
        }
      );
      return response.data;
    } catch (error) {
      console.error('Error fetching search results:', error);
      throw error;
    }
  },
};

export default deepseekService;
Enter fullscreen mode Exit fullscreen mode

Step 4: Build the Search Component

Now, let's create a SearchComponent that will utilize the DeepSeek service to fetch and display search results.

Create a new file SearchComponent.js in the src directory:

import React, { useState } from 'react';
import deepseekService from './deepseekService';

const SearchComponent = () => {
  const [query, setQuery] = useState('');
  const [results, setResults] = useState([]);
  const [loading, setLoading] = useState(false);

  const handleSearch = async () => {
    if (!query) return;

    setLoading(true);

    try {
      const data = await deepseekService.search(query);
      setResults(data.results);
    } catch (error) {
      console.error('Search failed:', error);
    } finally {
      setLoading(false);
    }
  };

  return (
    <div>
      <input
        type="text"
        value={query}
        onChange={(e) => setQuery(e.target.value)}
        placeholder="Search..."
      />
      <button onClick={handleSearch} disabled={loading}>
        {loading ? 'Searching...' : 'Search'}
      </button>

      <ul>
        {results.map((result, index) => (
          <li key={index}>
            <h3>{result.title}</h3>
            <p>{result.snippet}</p>
            <a href={result.url} target="_blank" rel="noopener noreferrer">Read more</a>
          </li>
        ))}
      </ul>
    </div>
  );
};

export default SearchComponent;
Enter fullscreen mode Exit fullscreen mode

Step 5: Integrate the Search Component

Finally, integrate the SearchComponent into your main application. Open src/App.js and modify it as follows:

import React from 'react';
import SearchComponent from './SearchComponent';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <h1>DeepSeek R1 Integration</h1>
        <SearchComponent />
      </header>
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

Step 6: Styling (Optional)

Add some basic styles to make your search component more visually appealing. Update src/App.css:

.App {
  text-align: center;
  padding: 20px;
}

input[type="text"] {
  padding: 10px;
  width: 300px;
  margin-right: 10px;
}

button {
  padding: 10px 20px;
  background-color: #007BFF;
  color: white;
  border: none;
  cursor: pointer;
}

button:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  background-color: #f9f9f9;
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ddd;
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Congratulations! You've successfully integrated DeepSeek R1 into your React application. With this setup, your users can now enjoy a highly intelligent and context-aware search experience directly within your app. This integration can be further extended with advanced features like autocomplete, filters, and analytics, depending on your application's needs.

By leveraging DeepSeek R1, you not only enhance your app's search functionality but also provide a more engaging and efficient user experience. Happy coding!


πŸš€ 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)