> ## Content Index
> Fetch the complete content index at: https://serpapi.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# Connect DeepSeek API with real-time data from the Internet
- URL: https://serpapi.com/blog/connect-deepseek-api-with-the-internet-google-search-and-more/
- Published: 2025-02-11T06:19:49.000Z
- Updated: 2025-12-19T00:24:03.000Z
- Description: Learn how to connect DeepSeek API with the internet to add more up-to-date information to the AI model.
- Author: Hilman Ramadhan
- Tags: deepseek, AI ML

I can't find the knowledge cutoff for DeepSeek, so I asked DeepSeek directly

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2025/02/CleanShot-2025-02-10-at-13.59.50.png)

DeepSeek response on the knowledge cutoff

As you can see, its latest knowledge is from July 2024, and it suggests using internet access to get up-to-date information. 

If you're using the platform directly, you can activate the "search" feature to search the web:

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2025/02/CleanShot-2025-02-10-at-14.02.52.png)

Search web feature on DeepSeek

But if you're using an API, we need to find a workaround, which is exactly what we'll discuss in this thread!

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2025/02/connect-DeepSeek-API-with-the-internet-1.webp)

Connect DeepSeek API with the internet

## Understand JSON Output

How do we understand a human language and call a function with the correct parameters from it? We need these two steps:

- Extract important information from raw user's inquiry
- Use the extracted information as a parameter to call any function we need. In this sample, to connect the API to the internet, we'll call another API (not DeepSeek API), depending on what we're going to do.
- (Optional) We can provide the information from the function we call to an AI model, to turn this information to a human language response.

Here is the simulation for the first step:

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2025/02/human-language-to-structured-JSON.png)

Human language to structured JSON

This is exactly what the **JSON Output** feature from DeepSeek does. It returns a nice structured JSON from human language. We need this structured data to be able to call a custom function with the correct parameters.

For the second step, it'll really depend on what we're going to do and what functions/API we want to call. For the above example, we may want to call a function that searches for items like this:

```python
def findProducts(item, amount, brand, price):
  # A method that call API to search for items with those information
```

## Connect DeepSeek with Google Search result

Let's jump to the example!

In this sample, we will add Google Search capability to the AI Model so they can do a Google search before answering our questions. It enables the Model to access organic search results, knowledge graphs, or Google Answer Box. 

Google Answer Box itself is handy for handling different types of questions that need real-time data like the weather:

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2025/02/CleanShot-2025-02-10-at-14.17.53.png)

Google answer box example.

It's not limited to just weather, any questions that Google can answer, the DeepSeek AI Model will be able to provide it as well!

As you can see, using a [Web Search API like SerpApi](https://serpapi.com/use-cases/web-search-api) will enable your AI model to access real-time data from the internet.

[The Web Search API for AI Applications in 2026Build AI apps without scraping headaches. Learn how a Web Search API extends model knowledge, delivers real-time data, and gives you full control and flexibility.![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/icon/serpapi-favicon-211.png)SerpApiHilman Ramadhan![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/thumbnail/web-search-API-for-AI-apps.webp)](https://serpapi.com/blog/the-web-search-api-for-ai-applications/)

### Scraping Google search results

You may think that we need to scrape the Google search for this. Luckily, SerpApi provides an easy way to access Google Search information easily using [Google Search API by SerpApi](https://serpapi.com/search-api).

You can play around on the playground for free to see what the structure looks like when using this API: <https://serpapi.com/playground>

### Code tutorial

Let's build the program now. The final source code is available on GitHub:

[GitHub - hilmanski/deepseek-with-realtime-data: Connect DeepSeek with a real-time data information from Google search results.Connect DeepSeek with a real-time data information from Google search results. - hilmanski/deepseek-with-realtime-data![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/icon/pinned-octocat-093da3e6fa40-5.svg)GitHubhilmanski![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/thumbnail/deepseek-with-realtime-data)](https://github.com/hilmanski/deepseek-with-realtime-data)

**Video version**  
If you prefer to watch a video, here is the video on our YouTube channel

**Get your API Keys**  
Register at serpapi.com and deepseek.com to get their API keys. 

**Test SerpApi Google Search API**  
Let's try out our Google Search API by SerpApi. Create a new Python file, name it whatever you want:

```python
import requests

SERPAPI_API_KEY = "YOUR_SERPAPI_API_KEY"

def googleSearch(keyword):
    url = f"https://serpapi.com/search.json?q={keyword}&api_key={SERPAPI_API_KEY}"
    response = requests.get(url)
    return response.json()

print(googleSearch("How to make a cake"))
```

Now, try to run this file. Ensure you're able to see the results from SerpApi.

**Prepare DeepSeek API**  
Let's prepare the client to call DeepSeek API. We'll be using OpenAI SDK. Run

```
pip3 install openai
```

Then add this code to the previous file

```python
import json
import requests
from openai import OpenAI

DEEPSEEK_API_KEY = "DEEPSEEK_API_KEY"
SERPAPI_API_KEY = "SERPAPI_API_KEY"

def googleSearch(keyword):
    url = f"https://serpapi.com/search.json?q={keyword}&api_key={SERPAPI_API_KEY}"
    response = requests.get(url)
    return response.json()

# Add client for DeepSeek
client = OpenAI(
    api_key=DEEPSEEK_API_KEY,
    base_url="https://api.deepseek.com",
)
```

**JSON Output function**  
Now, we'll test the JSON Output feature from DeepSeek that able to extract important information from human language. We won't call the `googleSearch` method yet in this step.

```python
client = OpenAI(
    api_key=DEEPSEEK_API_KEY,
    base_url="https://api.deepseek.com",
)

# Basic sample for DeepSeek JSON Output
system_prompt = """
Please parse the "keyword" from user's message to be used in a Google search and output them in JSON format. 

EXAMPLE INPUT: 
What's the weather like in New York today?

EXAMPLE JSON OUTPUT:
{
    "keyword": "weather in New York"
}
"""

user_prompt = "What's the weather like in London today?"

messages = [{"role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt}]

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=messages,
    response_format={
        'type': 'json_object'
    }
)

print(json.loads(response.choices[0].message.content))

```

- Provide a system\_prompt that tells what the AI model should do. It must includes "JSON" keyword
- Add the user\_prompt. It could be anything.
- Call the chat completions method with the `response_format` above.

Here is the output:

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2025/02/CleanShot-2025-02-11-at-11.22.08.png)

weather in London

Let's try something else. I'm going to replace the `user_prompt` with this:

```python
user_prompt = "Can you please check Tesla's stock price today?"
```

It returns:

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2025/02/CleanShot-2025-02-11-at-11.24.23.png)

stock query

As you can see, the AI model is smart enough to extract information from the user prompt into a keyword that we can search for.

**Connect DeepSeek with external function**  
Now is the fun part, let's connect the `googleSearch` method we've prepared before with the keyword we extract.

```python
response = client.chat.completions.create(
    model="deepseek-chat",
    messages=messages,
    response_format={
        'type': 'json_object'
    }
)

keyword = json.loads(response.choices[0].message.content)["keyword"]
googleResponse = googleSearch(keyword)
print(json.dumps(googleResponse, indent=4)) #I wrap it on json.dumps to make it easier to see
```

There is a lot of information returned from Google Search API, but we are only interested in the `answer_box` part. As you can see, it includes the current stock price information, which is exactly what we need.

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2025/02/CleanShot-2025-02-11-at-11.29.49.png)

Answer box from Google Search response

**Response with human natural language**  
We can stop the program by just returning the price information, but we can increase the user experience by returning the information with human natural language.

To do this, we'll need to send this information to AI and let it answer the question. 

**Warning:** To avoid using too many tokens, we should only pass the needed information to the AI Model, not the whole response.

Here's what the code looks like:

```python
response = client.chat.completions.create(
    model="deepseek-chat",
    messages=messages,
    response_format={
        'type': 'json_object'
    }
)

keyword = json.loads(response.choices[0].message.content)["keyword"]
googleResponse = googleSearch(keyword)
answer_box = googleResponse["answer_box"] 
answer_box_to_string = json.dumps(answer_box, indent=2)

if answer_box:
    response = client.chat.completions.create(
        model="deepseek-chat",
        messages=[
            {"role": "system", "content": "Answer the question from user with provided information: " + answer_box_to_string},
            {"role": "user", "content": user_prompt},
        ],
    )

    print(response.choices[0].message.content)
else:
    print("No answer box found")
```

We call another chat completion method to answer the question in human language. Here is the response: 

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2025/02/CleanShot-2025-02-11-at-14.15.34.png)

real time information from DeepSeek

We can verify this information is real by comparing it with the actual Google search result:

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2025/02/CleanShot-2025-02-11-at-14.15.59.png)

Tesla stock today

Imagine the possibilities now that we can add real-time data to our DeepSeek AI model!

## Places recommendation with DeepSeek 

Let's take a look at another case. Let's say we want to find places on Google Maps based on what the user says.

The steps are similar to the previous example, but this time, we'll be using [Google Maps API](https://serpapi.com/google-maps-api) for the additional function.

The final source code is available on GitHub:

[GitHub - hilmanski/deepseek-with-realtime-data: Connect DeepSeek with a real-time data information from Google search results.Connect DeepSeek with a real-time data information from Google search results. - hilmanski/deepseek-with-realtime-data![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/icon/pinned-octocat-093da3e6fa40-6.svg)GitHubhilmanski![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/thumbnail/deepseek-with-realtime-data-1)](https://github.com/hilmanski/deepseek-with-realtime-data)

**Google Maps API method**  
Here is the method for accessing Google Maps API by SerpApi:

```python
def mapsSearch(keyword):
    url = f"https://serpapi.com/search.json?engine=google_maps&q={keyword}&api_key={SERPAPI_API_KEY}"
    response = requests.get(url)
    return response.json()
```

**System Prompt**  
Adjust the system prompt based on what we need

```python
system_prompt = """
Please parse the "keyword" from user's message to be used in a Google Maps and output them in JSON format. 

EXAMPLE INPUT: 
I'm hungry, not sure what's to eat?

EXAMPLE JSON OUTPUT:
{
    "keyword": "restaurant"
}

OTHER EXAMPLE INPUT:
I'm craving some sushi right now

OTHER EXAMPLE JSON OUTPUT:
{
    "keyword": "sushi restaurant"
}
"""
```

**Test DeepSeek JSON Output**  
Ensure the JSON Output works correctly, try to run this script:

```python
user_prompt = "Really craving some Korean BBQ right now"

messages = [{"role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt}]

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=messages,
    response_format={
        'type': 'json_object'
    }
)

keyword = json.loads(response.choices[0].message.content)["keyword"]
print(keyword)
```

I got "Korean BBQ restaurant". 

**Connect with Google Maps API**  
Now that we're able to retrieve the correct keyword from the user, let's call the `mapsSearch` function we prepared

```python
user_prompt = "Really craving some Korean BBQ right now"

messages = [{"role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt}]

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=messages,
    response_format={
        'type': 'json_object'
    }
)

keyword = json.loads(response.choices[0].message.content)["keyword"]
mapsResult = mapsSearch(keyword)

if mapsResult["local_results"]:
    top_places = mapsResult["local_results"][:3]
    response = client.chat.completions.create(
        model="deepseek-chat",
        messages=[
            {"role": "system", "content": "Answer the user with top 3 places informations: " + json.dumps(top_places, indent=2)},
            {"role": "user", "content": user_prompt},
        ],
    )

    print(response.choices[0].message.content)
else:
    print("No local results found")
```

Explanation:  
\- We store the map results in `mapsResult` variable  
\- I cut the results to only grab the top 3 results, feel free to adjust based on your use case  
\- Call AI model to respond in human language while providing the top place information

Here's the result

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2025/02/CleanShot-2025-02-12-at-07.36.30.png)

AI places recommendation

## Function Calling VS JSON Output

We have two main ways to call another function using the DeepkSeek API: Function Calling and JSON Output. While these methods do not facilitate direct internet calls, they are useful for extracting important information from a raw string, which can then be used to call an external function.

**What are the differences?**  
Function Calling is focused on executing specific functionalities provided by an API, while JSON Output emphasizes structured data exchange in a readable format. **It's also important to note that currently, the function calling method is not stable.** That's why we're going to use the JSON Output method in this tutorial.

Reference:  
\- Function Calling: <https://api-docs.deepseek.com/guides/function%5Fcalling>  
\- JSON Output: <https://api-docs.deepseek.com/guides/json%5Fmode>

If you're interested in the basic DeekSeek API, please take a look at this post:

[Explore DeepSeek API - Chat Completion and moreThe new OpenAI competitor is here. Explore DeepSeek API: Chat completion, JSON Output, Function Calling, multi-round conversation, and more!![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/icon/serpapi-favicon-44.png)SerpApiHilman Ramadhan![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/thumbnail/DeepSeek-JSON-Output-API.webp)](https://serpapi.com/blog/explore-deepseek-api/)

##