Connect DeepSeek API with real-time data from the Internet
I can't find the knowledge cutoff for DeepSeek, so I asked DeepSeek directly
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:
But if you're using an API, we need to find a workaround, which is exactly what we'll discuss in this thread!
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:
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:
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:
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!
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.
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:
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:
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
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.
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:
Let's try something else. I'm going to replace the user_prompt
with this:
user_prompt = "Can you please check Tesla's stock price today?"
It returns:
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.
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.
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:
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:
We can verify this information is real by comparing it with the actual Google search result:
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 for the additional function.
The final source code is available on GitHub:
Google Maps API method
Here is the method for accessing Google Maps API by SerpApi:
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
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:
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
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
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_calling
- JSON Output: https://api-docs.deepseek.com/guides/json_mode
If you're interested in the basic DeekSeek API, please take a look at this post: